Skip to content

Commit 9de8a77

Browse files
authored
Merge pull request #2379 from kcaswick/issue-1
Reproduce 'Sequence contains no matching element' error
2 parents 309118b + c5595eb commit 9de8a77

File tree

3 files changed

+71
-7
lines changed

3 files changed

+71
-7
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using GitVersion.VersionCalculation;
66
using LibGit2Sharp;
77
using NUnit.Framework;
8+
using Shouldly;
89

910
namespace GitVersion.Core.Tests.IntegrationTests;
1011

@@ -537,6 +538,32 @@ public void BranchWithoutMergeBaseMainlineBranchIsFound()
537538
fixture.AssertFullSemver("0.1.3-issue-branch.1", currentConfig);
538539
}
539540

541+
[Test]
542+
public void GivenARemoteGitRepositoryWithCommitsThenClonedLocalDevelopShouldMatchRemoteVersion()
543+
{
544+
using var fixture = new RemoteRepositoryFixture();
545+
fixture.AssertFullSemver("0.1.4", config);
546+
fixture.BranchTo("develop");
547+
fixture.AssertFullSemver("0.2.0-alpha.0", config);
548+
Console.WriteLine(fixture.SequenceDiagram.GetDiagram());
549+
var local = fixture.CloneRepository();
550+
fixture.AssertFullSemver("0.2.0-alpha.0", config, repository: local.Repository);
551+
local.Repository.DumpGraph();
552+
}
553+
554+
[Test]
555+
public void GivenNoMainThrowsWarning()
556+
{
557+
using var fixture = new EmptyRepositoryFixture();
558+
fixture.Repository.MakeACommit();
559+
fixture.Repository.MakeATaggedCommit("1.0.0");
560+
fixture.Repository.MakeACommit();
561+
Commands.Checkout(fixture.Repository, fixture.Repository.CreateBranch("develop"));
562+
fixture.Repository.Branches.Remove(fixture.Repository.Branches["main"]);
563+
564+
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");
566+
}
540567
}
541568

542569
internal static class CommitExtensions

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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ private IBranch GetMainline(ICommit? baseVersionSource)
128128

129129
if (!mainlineBranches.Any())
130130
{
131-
throw new WarningException("No mainline branches found!");
131+
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}");
132133
}
133134

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

0 commit comments

Comments
 (0)