Skip to content

Commit a3a3f75

Browse files
authored
Merge pull request #3406 from HHobeck/feature/1789_track-merge-target_in_branch_config_not_working
Ensure track-merge-target: Strategy which will look for tagged merge commits directly off the current branch
2 parents b0d2007 + 2cf22a4 commit a3a3f75

File tree

12 files changed

+174
-97
lines changed

12 files changed

+174
-97
lines changed

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,28 @@ public void BranchesWithIllegalCharsShouldNotBeUsedInVersionNames()
3636
[Test]
3737
public void ShouldNotGetVersionFromFeatureBranchIfNotMerged()
3838
{
39+
// * 1c08923 54 minutes ago (HEAD -> develop)
40+
// | * 03dd6d5 56 minutes ago (tag: 1.0.1-feature.1, feature)
41+
// |/
42+
// * e2ff13b 58 minutes ago (tag: 1.0.0-unstable.0, main)
43+
44+
var configuration = GitFlowConfigurationBuilder.New
45+
.WithBranch("develop", builder => builder.WithTrackMergeTarget(false))
46+
.Build();
47+
3948
using var fixture = new EmptyRepositoryFixture();
40-
fixture.Repository.MakeATaggedCommit("1.0.0-unstable.0"); // initial commit in main
4149

42-
fixture.Repository.CreateBranch("feature");
43-
Commands.Checkout(fixture.Repository, "feature");
44-
fixture.Repository.MakeATaggedCommit("1.0.1-feature.1");
50+
fixture.MakeATaggedCommit("1.0.0-unstable.0"); // initial commit in main
4551

46-
Commands.Checkout(fixture.Repository, MainBranch);
47-
fixture.Repository.CreateBranch("develop");
48-
Commands.Checkout(fixture.Repository, "develop");
52+
fixture.BranchTo("feature");
53+
fixture.MakeATaggedCommit("1.0.1-feature.1");
54+
fixture.Checkout(MainBranch);
55+
fixture.BranchTo("develop");
4956
fixture.Repository.MakeACommit();
5057

51-
var version = fixture.GetVersion();
52-
version.SemVer.ShouldBe("1.0.0-alpha.1");
58+
fixture.AssertFullSemver("1.0.0-alpha.1", configuration);
59+
60+
fixture.Repository.DumpGraph();
5361
}
5462

5563
[TestCase("alpha", "JIRA-123", "alpha")]

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Globalization;
2+
using GitVersion.Configuration;
23
using GitVersion.Core.Tests.Helpers;
34
using GitVersion.Helpers;
45
using LibGit2Sharp;
@@ -127,4 +128,38 @@ public void NoDirtyFlagInCleanRepository()
127128
const int zero = 0;
128129
version.UncommittedChanges.ShouldBe(zero.ToString(CultureInfo.InvariantCulture));
129130
}
131+
132+
[TestCase(false, "1.1.0-alpha.2")]
133+
[TestCase(true, "1.2.0-alpha.1")]
134+
public void EnusreTrackMergeTargetStrategyWhichWillLookForTaggedMergecommits(bool trackMergeTarget, string expectedVersion)
135+
{
136+
// * 9daa6ea 53 minutes ago (HEAD -> develop)
137+
// | * 85536f2 55 minutes ago (tag: 1.1.0, main)
138+
// | |\
139+
// | |/
140+
// |/|
141+
// * | 4a5ef1a 56 minutes ago
142+
// |/
143+
// * c7f68af 58 minutes ago (tag: 1.0.0)
144+
145+
var configuration = GitFlowConfigurationBuilder.New
146+
.WithBranch("main", builder => builder.WithIsMainline(false))
147+
.WithBranch("develop", builder => builder
148+
.WithTrackMergeTarget(trackMergeTarget).WithTracksReleaseBranches(false)
149+
).Build();
150+
151+
using var fixture = new EmptyRepositoryFixture();
152+
fixture.MakeATaggedCommit("1.0.0");
153+
fixture.BranchTo("develop");
154+
fixture.MakeACommit();
155+
fixture.Checkout("main");
156+
fixture.MergeNoFF("develop");
157+
fixture.ApplyTag("1.1.0");
158+
fixture.Checkout("develop");
159+
fixture.MakeACommit();
160+
161+
fixture.AssertFullSemver(expectedVersion, configuration);
162+
163+
fixture.Repository.DumpGraph();
164+
}
130165
}

src/GitVersion.Core/Core/Abstractions/IRepositoryStore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public interface IRepositoryStore
1818
IBranch GetTargetBranch(string? targetBranchName);
1919
IBranch? FindBranch(string? branchName);
2020
IBranch? FindMainBranch(GitVersionConfiguration configuration);
21+
IEnumerable<IBranch> FindMainlineBranches(GitVersionConfiguration configuration);
2122
IEnumerable<IBranch> GetReleaseBranches(IEnumerable<KeyValuePair<string, BranchConfiguration>> releaseBranchConfig);
2223
IEnumerable<IBranch> ExcludingBranches(IEnumerable<IBranch> branchesToExclude);
2324
IEnumerable<IBranch> GetBranchesContainingCommit(ICommit? commit, IEnumerable<IBranch>? branches = null, bool onlyTrackedBranches = false);
@@ -41,8 +42,7 @@ public interface IRepositoryStore
4142
SemanticVersion? GetCurrentCommitTaggedVersion(ICommit? commit, string? tagPrefix, SemanticVersionFormat versionFormat, bool handleDetachedBranch);
4243

4344
IEnumerable<SemanticVersion> GetVersionTagsOnBranch(IBranch branch, string? tagPrefixRegex, SemanticVersionFormat versionFormat);
44-
IEnumerable<(ITag Tag, SemanticVersion Semver, ICommit Commit)> GetValidVersionTags(string? tagPrefixRegex, SemanticVersionFormat versionFormat, DateTimeOffset? olderThan = null);
45-
45+
IEnumerable<SemanticVersionWithTag> GetSemanticVersionFromTags(string? tagPrefixRegex, SemanticVersionFormat semanticVersionFormat);
4646
bool IsCommitOnBranch(ICommit? baseVersionSource, IBranch branch, ICommit firstMatchingCommit);
4747

4848
int GetNumberOfUncommittedChanges();

src/GitVersion.Core/Core/MergeCommitFinder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace GitVersion;
66

77
internal class MergeCommitFinder
88
{
9-
private readonly IEnumerable<IBranch> excludedBranches;
9+
private readonly IEnumerable<IBranch> branches;
1010
private readonly ILog log;
1111
private readonly Dictionary<IBranch, List<BranchCommit>> mergeBaseCommitsCache = new();
1212
private readonly RepositoryStore repositoryStore;
@@ -16,7 +16,7 @@ public MergeCommitFinder(RepositoryStore repositoryStore, GitVersionConfiguratio
1616
{
1717
this.repositoryStore = repositoryStore.NotNull();
1818
this.configuration = configuration.NotNull();
19-
this.excludedBranches = repositoryStore.ExcludingBranches(excludedBranches.NotNull());
19+
this.branches = repositoryStore.ExcludingBranches(excludedBranches.NotNull());
2020
this.log = log.NotNull();
2121
}
2222

@@ -41,7 +41,7 @@ public IEnumerable<BranchCommit> FindMergeCommitsFor(IBranch branch)
4141

4242
private IEnumerable<BranchCommit> FindMergeBases(IBranch branch)
4343
{
44-
var sourceBranches = new SourceBranchFinder(this.excludedBranches, this.configuration)
44+
var sourceBranches = new SourceBranchFinder(this.branches, this.configuration)
4545
.FindSourceBranchesOf(branch);
4646

4747
foreach (var sourceBranch in sourceBranches)

src/GitVersion.Core/Core/RepositoryStore.cs

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public IBranch GetTargetBranch(string? targetBranchName)
107107
public IBranch? FindMainBranch(GitVersionConfiguration configuration)
108108
{
109109
var mainBranchRegex = configuration.Branches[ConfigurationConstants.MainBranchKey].Regex
110-
?? configuration.Branches[ConfigurationConstants.MasterBranchKey].Regex;
110+
?? configuration.Branches[ConfigurationConstants.MasterBranchKey].Regex;
111111

112112
if (mainBranchRegex == null)
113113
{
@@ -118,6 +118,20 @@ public IBranch GetTargetBranch(string? targetBranchName)
118118
Regex.IsMatch(b.Name.Friendly, mainBranchRegex, RegexOptions.IgnoreCase));
119119
}
120120

121+
public IEnumerable<IBranch> FindMainlineBranches(GitVersionConfiguration configuration)
122+
{
123+
configuration.NotNull();
124+
125+
foreach (var branch in this.repository.Branches)
126+
{
127+
var branchConfiguration = configuration.GetBranchConfiguration(branch);
128+
if (branchConfiguration.IsMainline == true)
129+
{
130+
yield return branch;
131+
}
132+
}
133+
}
134+
121135
public IEnumerable<IBranch> GetReleaseBranches(IEnumerable<KeyValuePair<string, BranchConfiguration>> releaseBranchConfig)
122136
=> this.repository.Branches.Where(b => IsReleaseBranch(b, releaseBranchConfig));
123137

@@ -243,37 +257,25 @@ public IEnumerable<SemanticVersion> GetVersionTagsOnBranch(IBranch branch, strin
243257

244258
using (this.log.IndentLog($"Getting version tags from branch '{branch.Name.Canonical}'."))
245259
{
246-
var tags = GetValidVersionTags(tagPrefixRegex, versionFormat);
247-
var tagsBySha = tags.Where(t => t.Tag.TargetSha != null).ToLookup(t => t.Tag.TargetSha, t => t);
260+
var semanticVersions = GetSemanticVersionFromTags(tagPrefixRegex, versionFormat);
261+
var tagsBySha = semanticVersions.Where(t => t.Tag.TargetSha != null).ToLookup(t => t.Tag.TargetSha, t => t);
248262

249-
var versionTags = (branch.Commits?.SelectMany(c => tagsBySha[c.Sha].Select(t => t.Semver)) ?? Enumerable.Empty<SemanticVersion>()).ToList();
263+
var versionTags = (branch.Commits?.SelectMany(c => tagsBySha[c.Sha].Select(t => t.Value)) ?? Enumerable.Empty<SemanticVersion>()).ToList();
250264

251265
this.semanticVersionTagsOnBranchCache.Add(branch, versionTags);
252266
return versionTags;
253267
}
254268
}
255269

256-
public IEnumerable<(ITag Tag, SemanticVersion Semver, ICommit Commit)> GetValidVersionTags(string? tagPrefixRegex, SemanticVersionFormat versionFormat, DateTimeOffset? olderThan = null)
270+
public IEnumerable<SemanticVersionWithTag> GetSemanticVersionFromTags(string? tagPrefixRegex, SemanticVersionFormat semanticVersionFormat)
257271
{
258-
var tags = new List<(ITag, SemanticVersion, ICommit)>();
259-
260272
foreach (var tag in this.repository.Tags)
261273
{
262-
if (!SemanticVersion.TryParse(tag.Name.Friendly, tagPrefixRegex, out var semver, versionFormat))
263-
continue;
264-
265-
var commit = tag.PeeledTargetCommit();
266-
267-
if (commit == null)
268-
continue;
269-
270-
if (olderThan.HasValue && commit.When > olderThan.Value)
271-
continue;
272-
273-
tags.Add((tag, semver, commit));
274+
if (SemanticVersion.TryParse(tag.Name.Friendly, tagPrefixRegex, out var semanticVersion, semanticVersionFormat))
275+
{
276+
yield return new SemanticVersionWithTag(semanticVersion, tag);
277+
}
274278
}
275-
276-
return tags;
277279
}
278280

279281
public IEnumerable<ICommit> GetCommitLog(ICommit? baseVersionSource, ICommit? currentCommit)
@@ -302,15 +304,11 @@ private IEnumerable<SemanticVersion> GetCurrentCommitSemanticVersions(ICommit? c
302304
if (commit == null)
303305
return Array.Empty<SemanticVersion>();
304306

305-
var targetCommit = tag.PeeledTargetCommit();
306-
if (targetCommit == null)
307-
return Array.Empty<SemanticVersion>();
308-
309-
var commitToCompare = handleDetachedBranch ? FindMergeBase(commit, targetCommit) : commit;
307+
var commitToCompare = handleDetachedBranch ? FindMergeBase(commit, tag.Commit) : commit;
310308

311309
var tagName = tag.Name.Friendly;
312310

313-
return Equals(targetCommit, commitToCompare) && SemanticVersion.TryParse(tagName, tagPrefix, out var version, versionFormat)
311+
return Equals(tag.Commit, commitToCompare) && SemanticVersion.TryParse(tagName, tagPrefix, out var version, versionFormat)
314312
? new[] { version }
315313
: Array.Empty<SemanticVersion>();
316314
}

src/GitVersion.Core/Git/ITag.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ namespace GitVersion;
33
public interface ITag : IEquatable<ITag?>, IComparable<ITag>, INamedReference
44
{
55
string? TargetSha { get; }
6-
ICommit? PeeledTargetCommit();
6+
7+
ICommit Commit { get; }
78
}

src/GitVersion.Core/GitVersionContext.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using GitVersion.Configuration;
2+
using GitVersion.Extensions;
23

34
namespace GitVersion;
45

@@ -25,9 +26,9 @@ public class GitVersionContext
2526
public GitVersionContext(IBranch currentBranch, ICommit? currentCommit,
2627
GitVersionConfiguration configuration, SemanticVersion? currentCommitTaggedVersion, int numberOfUncommittedChanges)
2728
{
28-
CurrentBranch = currentBranch;
29+
CurrentBranch = currentBranch.NotNull();
2930
CurrentCommit = currentCommit;
30-
Configuration = configuration;
31+
Configuration = configuration.NotNull();
3132
CurrentCommitTaggedVersion = currentCommitTaggedVersion;
3233
NumberOfUncommittedChanges = numberOfUncommittedChanges;
3334
}

0 commit comments

Comments
 (0)