Skip to content

Commit c5595eb

Browse files
committed
Compare Mainline branch candidates without remote
- Refactor `GetMainlineBranches` into many smaller methods. - Add logging in each method. - Compare Mainline branch candidates without remote.
1 parent 780f815 commit c5595eb

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

src/GitVersion.Core.Tests/IntegrationTests/MainlineDevelopmentMode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ public void GivenNoMainThrowsWarning()
562562
fixture.Repository.Branches.Remove(fixture.Repository.Branches["main"]);
563563

564564
var exception = Assert.Throws<WarningException>(() => fixture.AssertFullSemver("1.1.0-alpha.1", config));
565-
exception.Message.ShouldMatch("No branches can be found matching the commit .* in the configured Mainline branches: main, support");
565+
exception!.Message.ShouldMatch("No branches can be found matching the commit .* in the configured Mainline branches: main, support");
566566
}
567567
}
568568

src/GitVersion.Core/Core/RepositoryStore.cs

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -295,14 +295,50 @@ static IEnumerable<IBranch> InnerGetBranchesContainingCommit(ICommit commit, IEn
295295

296296
public Dictionary<string, List<IBranch>> GetMainlineBranches(ICommit commit, IEnumerable<KeyValuePair<string, BranchConfig?>>? mainlineBranchConfigs) =>
297297
this.repository.Branches
298-
.Where(b => mainlineBranchConfigs?.Any(c => c.Value?.Regex != null && Regex.IsMatch(b.Name.Friendly, c.Value.Regex)) == true)
298+
.Where(b => BranchIsMainline(b, mainlineBranchConfigs))
299299
.Select(b => new { Origin = FindBranchOrigin(b, commit), Branch = b })
300-
.Where(a => a.Origin != null)
301-
.GroupBy(b => b.Origin?.Sha, b => b.Branch)
302-
.ToDictionary(b => b.Key, b => b.ToList());
300+
.Where(x => x.Origin is not null)
301+
.GroupBy(x => x.Origin!.Sha, a => a.Branch)
302+
.ToDictionary(x => x.Key, x => x.ToList());
303+
304+
private bool BranchIsMainline(INamedReference branch, IEnumerable<KeyValuePair<string, BranchConfig?>>? mainlineBranchConfigs) =>
305+
mainlineBranchConfigs?.Any(c => BranchMatchesMainlineConfig(branch, c)) == true;
306+
307+
private bool BranchMatchesMainlineConfig(INamedReference branch, KeyValuePair<string, BranchConfig?> mainlineBranchConfig)
308+
{
309+
if (mainlineBranchConfig.Value?.Regex == null)
310+
{
311+
return false;
312+
}
313+
314+
var mainlineRegex = mainlineBranchConfig.Value.Regex;
315+
var branchName = branch.Name.WithoutRemote;
316+
var match = Regex.IsMatch(branchName, mainlineRegex);
317+
this.log.Info($"'{mainlineRegex}' {(match ? "matches" : "does not match")} '{branchName}'.");
318+
return match;
319+
}
320+
321+
private ICommit? FindBranchOrigin(IBranch branch, ICommit commit)
322+
{
323+
var branchName = branch.Name.Friendly;
324+
var mergeBase = FindMergeBase(branch.Tip!, commit);
325+
if (mergeBase is not null)
326+
{
327+
this.log.Info($"Found merge base {mergeBase.Sha} for '{branchName}'.");
328+
return mergeBase;
329+
}
330+
331+
var branchCommit = FindCommitBranchWasBranchedFrom(branch, null);
332+
if (branchCommit != BranchCommit.Empty)
333+
{
334+
this.log.Info($"Found parent commit {branchCommit.Commit.Sha} for '{branchName}'.");
335+
return branchCommit.Commit;
336+
}
337+
338+
this.log.Info($"Found no merge base or parent commit for '{branchName}'.");
339+
return null;
340+
}
303341

304-
private ICommit FindBranchOrigin(IBranch branch, ICommit commit) =>
305-
FindMergeBase(branch.Tip!, commit) ?? FindCommitBranchWasBranchedFrom(branch, null).Commit;
306342

307343

308344
/// <summary>

src/GitVersion.Core/VersionCalculation/MainlineVersionCalculator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ private IBranch GetMainline(ICommit? baseVersionSource)
129129
if (!mainlineBranches.Any())
130130
{
131131
var mainlineBranchConfigsString = string.Join(", ", mainlineBranchConfigs.Select(b => b.Value.Name));
132-
throw new WarningException($"No branches can be found matching the commit {context.CurrentCommit.Sha} in the configured Mainline branches: {mainlineBranchConfigsString}");
132+
throw new WarningException($"No branches can be found matching the commit {context.CurrentCommit?.Sha} in the configured Mainline branches: {mainlineBranchConfigsString}");
133133
}
134134

135135
var mainlineBranchNames = mainlineBranches.Values.SelectMany(branches => branches.Select(b => b.Name.Friendly));

0 commit comments

Comments
 (0)