Skip to content

Commit 2d1166e

Browse files
committed
Topological commit sort causes issues when master is merged into a feature branch
1 parent 7b50d63 commit 2d1166e

File tree

8 files changed

+50
-23
lines changed

8 files changed

+50
-23
lines changed

docs/input/docs/reference/modes/continuous-delivery.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Continuous delivery is good when you deploy continuously to an testing system.
2424
* 1.1.0-3
2525
* 1.1.0-2 (tag: 1.1.0-2) <-- This is the version which has been deployed on testing
2626
* 1.1.0-1
27-
* 1.1.1-0
27+
* 1.1.0-0
2828

2929
Tags are not required but optional in this mode to communicate when the release
3030
is done as it's an automated process.

docs/input/docs/reference/modes/continuous-deployment.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ to deploy on production. This means that GitVersion will build
2121
* 1.2.0
2222
* 1.1.0 (tag: 1.1.0) <-- This is the version which has been deployed on production
2323
* 1.1.0
24-
* 1.1.1
24+
* 1.1.0
2525

2626
Tags are required in this mode to communicate when the deployment happens on production.
2727

docs/input/docs/reference/modes/manual-deployment.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ deployed. For instance:
2121
* 1.1.0-2+1
2222
* 1.1.0-1+2 (tag: 1.1.0-1) <-- This is the version which has been deployed on testing
2323
* 1.1.0-1+1
24-
* 1.1.1-1+0
24+
* 1.1.0-1+0
2525

2626
Tags are required in this mode to communicate when the release is done as it's
2727
an external manual process.

src/GitVersion.Core.Tests/Extensions/GitToolsTestingExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public static void AssertFullSemver(this RepositoryFixtureBase fixture, string f
138138
/// <summary>
139139
/// Simulates running on build server
140140
/// </summary>
141-
public static void InitializeRepo(this RemoteRepositoryFixture fixture)
141+
public static void InitializeRepository(this RemoteRepositoryFixture fixture)
142142
{
143143
var gitVersionOptions = new GitVersionOptions
144144
{

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void DoNotBlowUpWhenMainAndDevelopPointAtSameCommit()
2626
Commands.Fetch(fixture.LocalRepositoryFixture.Repository, fixture.LocalRepositoryFixture.Repository.Network.Remotes.First().Name, [], new(), null);
2727
Commands.Checkout(fixture.LocalRepositoryFixture.Repository, fixture.Repository.Head.Tip);
2828
fixture.LocalRepositoryFixture.Repository.Branches.Remove(MainBranch);
29-
fixture.InitializeRepo();
29+
fixture.InitializeRepository();
3030
fixture.AssertFullSemver("1.0.1-1");
3131
}
3232

@@ -92,15 +92,15 @@ public void DoNotBlowUpWhenDevelopAndFeatureBranchPointAtSameCommit()
9292
Commands.Fetch(fixture.LocalRepositoryFixture.Repository, fixture.LocalRepositoryFixture.Repository.Network.Remotes.First().Name, [], new(), null);
9393
Commands.Checkout(fixture.LocalRepositoryFixture.Repository, fixture.Repository.Head.Tip);
9494
fixture.LocalRepositoryFixture.Repository.Branches.Remove(MainBranch);
95-
fixture.InitializeRepo();
95+
fixture.InitializeRepository();
9696
fixture.AssertFullSemver("1.1.0-alpha.1");
9797
}
9898

9999
[TestCase(true, 1)]
100100
[TestCase(false, 1)]
101101
[TestCase(true, 5)]
102102
[TestCase(false, 5)]
103-
public void HasDirtyFlagWhenUncommittedChangesAreInRepo(bool stageFile, int numberOfFiles)
103+
public void HasDirtyFlagWhenUncommittedChangesAreInRepository(bool stageFile, int numberOfFiles)
104104
{
105105
using var fixture = new EmptyRepositoryFixture();
106106
fixture.MakeACommit();
@@ -133,7 +133,7 @@ public void NoDirtyFlagInCleanRepository()
133133

134134
[TestCase(false, "1.1.0-alpha.2")]
135135
[TestCase(true, "1.2.0-alpha.1")]
136-
public void EnsureTrackMergeTargetStrategyWhichWillLookForTaggedMergecommits(bool trackMergeTarget, string expectedVersion)
136+
public void EnsureTrackMergeTargetStrategyWhichWillLookForTaggedMergeCommits(bool trackMergeTarget, string expectedVersion)
137137
{
138138
// * 9daa6ea 53 minutes ago (HEAD -> develop)
139139
// | * 85536f2 55 minutes ago (tag: 1.1.0, main)
@@ -1283,7 +1283,7 @@ public void EnsureVersionAfterMainIsMergedBackToDevelopIsCorrectForMainline(bool
12831283
}
12841284

12851285
[TestCase(false, "2.0.0-alpha.3")]
1286-
[TestCase(true, "2.0.0-alpha.3")]
1286+
[TestCase(true, "3.0.0-alpha.2")]
12871287
public void EnsureVersionAfterMainIsMergedBackToDevelopIsCorrectForGitFlow(bool applyTag, string semanticVersion)
12881288
{
12891289
var configuration = GitFlowConfigurationBuilder.New.Build();
@@ -1300,7 +1300,7 @@ public void EnsureVersionAfterMainIsMergedBackToDevelopIsCorrectForGitFlow(bool
13001300

13011301
fixture.Checkout("main");
13021302
fixture.MakeACommit("C");
1303-
if (applyTag) fixture.ApplyTag("1.0.1");
1303+
if (applyTag) fixture.ApplyTag("2.0.0");
13041304
fixture.Checkout("develop");
13051305
fixture.MergeNoFF("main");
13061306

@@ -1380,7 +1380,8 @@ public void AlternativeSemanticVersionsShouldBeConsidered()
13801380
}
13811381

13821382
[TestCase(null, "6.0.0-beta.6")]
1383-
[TestCase("beta", "6.0.0-beta.21")]
1383+
[TestCase("beta", "6.0.0-beta.6")]
1384+
[TestCase("gamma", "6.0.0-gamma.21")]
13841385
public void AlternativeSemanticVersionsShouldBeConsidered(string? labelOnMain, string version)
13851386
{
13861387
var configuration = GitFlowConfigurationBuilder.New
@@ -1391,14 +1392,16 @@ public void AlternativeSemanticVersionsShouldBeConsidered(string? labelOnMain, s
13911392
using var fixture = new EmptyRepositoryFixture();
13921393

13931394
fixture.MakeATaggedCommit("1.0.0");
1394-
fixture.MakeATaggedCommit("4.0.0-beta.14");
13951395
fixture.MakeACommit("A");
1396+
fixture.ApplyTag("4.0.0-beta.14");
1397+
fixture.ApplyTag("4.0.0-gamma.14");
1398+
fixture.MakeACommit("B");
13961399
fixture.MakeATaggedCommit("6.0.0-alpha.1");
13971400
fixture.MakeATaggedCommit("6.0.0-alpha.2");
13981401
fixture.MakeATaggedCommit("6.0.0-alpha.3");
1399-
fixture.MakeACommit("B");
1400-
fixture.MakeATaggedCommit("6.0.0-beta.5");
14011402
fixture.MakeACommit("C");
1403+
fixture.MakeATaggedCommit("6.0.0-beta.5");
1404+
fixture.MakeACommit("D");
14021405

14031406
fixture.AssertFullSemver(version, configuration);
14041407
}

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,23 @@ public void RepositoryWithALotOfTags()
1212

1313
using var fixture = new EmptyRepositoryFixture();
1414

15-
const int maxCommits = 500;
16-
for (int i = 0; i < maxCommits; i++)
15+
Random random = new(4711);
16+
SemanticVersion semanticVersion = SemanticVersion.Empty;
17+
for (int i = 0; i < 500; i++)
1718
{
18-
fixture.MakeATaggedCommit($"1.0.{i}");
19+
VersionField versionField = (VersionField)random.Next(1, 4);
20+
semanticVersion = semanticVersion.Increment(versionField, string.Empty, forceIncrement: true);
21+
fixture.MakeATaggedCommit(semanticVersion.ToString("j"));
1922
}
2023

2124
fixture.BranchTo("feature");
2225
fixture.MakeACommit();
2326

2427
var sw = Stopwatch.StartNew();
2528

26-
fixture.AssertFullSemver($"1.0.{maxCommits}-feature.1+1", configuration);
29+
fixture.AssertFullSemver("170.3.3-feature.1+1", configuration);
2730
sw.Stop();
2831

29-
sw.ElapsedMilliseconds.ShouldBeLessThan(5000);
32+
sw.ElapsedMilliseconds.ShouldBeLessThan(2500);
3033
}
3134
}

src/GitVersion.Core/VersionCalculation/IncrementStrategyFinder.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,11 @@ private IReadOnlyCollection<ICommit> GetCommitHistory(string? tagPrefix, Semanti
106106
.ToHashSet()
107107
);
108108

109-
var intermediateCommits = GetIntermediateCommits(baseVersionSource, currentCommit, ignore).ToArray();
109+
var intermediateCommits = this.repositoryStore.GetCommitLog(
110+
baseVersionSource: baseVersionSource,
111+
currentCommit: currentCommit,
112+
ignore: ignore
113+
);
110114
var commitLog = intermediateCommits.ToDictionary(element => element.Id.Sha);
111115

112116
foreach (var intermediateCommit in intermediateCommits.Reverse())
@@ -197,7 +201,6 @@ public IEnumerable<ICommit> GetMergedCommits(ICommit mergeCommit, int index, IIg
197201

198202
ICommit findMergeBase = this.repositoryStore.FindMergeBase(baseCommit, mergedCommit)
199203
?? throw new InvalidOperationException("Cannot find the base commit of merged branch.");
200-
201204
return GetIntermediateCommits(findMergeBase, mergedCommit, ignore);
202205
}
203206

src/GitVersion.Core/VersionCalculation/VersionSearchStrategies/TaggedCommitVersionStrategy.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using GitVersion.Configuration;
22
using GitVersion.Core;
33
using GitVersion.Extensions;
4+
using GitVersion.Logging;
5+
using static GitVersion.Core.RegexPatterns;
46

57
namespace GitVersion.VersionCalculation;
68

@@ -10,14 +12,15 @@ namespace GitVersion.VersionCalculation;
1012
/// Increments if the tag is not the current commit.
1113
/// </summary>
1214
internal sealed class TaggedCommitVersionStrategy(
13-
Lazy<GitVersionContext> contextLazy,
15+
Lazy<GitVersionContext> contextLazy, ILog log,
1416
ITaggedSemanticVersionService taggedSemanticVersionService,
1517
IIncrementStrategyFinder incrementStrategyFinder)
1618
: IVersionStrategy
1719
{
1820
private readonly ITaggedSemanticVersionService taggedSemanticVersionService = taggedSemanticVersionService.NotNull();
1921
private readonly Lazy<GitVersionContext> contextLazy = contextLazy.NotNull();
2022
private readonly IIncrementStrategyFinder incrementStrategyFinder = incrementStrategyFinder.NotNull();
23+
private readonly ILog log = log.NotNull();
2124

2225
private GitVersionContext Context => contextLazy.Value;
2326

@@ -41,6 +44,7 @@ private IEnumerable<BaseVersion> GetBaseVersionsInternal(EffectiveBranchConfigur
4144

4245
var label = configuration.Value.GetBranchSpecificLabel(Context.CurrentBranch.Name, null);
4346

47+
SemanticVersion semanticVersionTreshold = new(0, 0, 0);
4448
List<SemanticVersionWithTag> alternativeSemanticVersionsWithTag = [];
4549
foreach (var semanticVersionWithTag in taggedSemanticVersions)
4650
{
@@ -50,6 +54,19 @@ private IEnumerable<BaseVersion> GetBaseVersionsInternal(EffectiveBranchConfigur
5054
continue;
5155
}
5256

57+
var alternativeSemanticVersionMax = alternativeSemanticVersionsWithTag.Max()?.Value;
58+
var highestPossibleSemanticVersion = semanticVersionWithTag.Value.Increment(
59+
VersionField.Major, null, forceIncrement: true, alternativeSemanticVersionMax
60+
);
61+
if (highestPossibleSemanticVersion.IsLessThan(semanticVersionTreshold, includePreRelease: false))
62+
{
63+
this.log.Info(
64+
$"The tag '{semanticVersionWithTag.Value}' is skipped because it will never be higher than other tags."
65+
);
66+
alternativeSemanticVersionsWithTag.Clear();
67+
continue;
68+
}
69+
5370
var baseVersionSource = semanticVersionWithTag.Tag.Commit;
5471
var increment = incrementStrategyFinder.DetermineIncrementedField(
5572
currentCommit: Context.CurrentCommit,
@@ -58,6 +75,7 @@ private IEnumerable<BaseVersion> GetBaseVersionsInternal(EffectiveBranchConfigur
5875
configuration: configuration.Value,
5976
label: label
6077
);
78+
semanticVersionTreshold = semanticVersionWithTag.Value.Increment(increment, null, forceIncrement: true);
6179

6280
yield return new BaseVersion(
6381
$"Git tag '{semanticVersionWithTag.Tag.Name.Friendly}'", semanticVersionWithTag.Value, baseVersionSource)
@@ -67,7 +85,7 @@ private IEnumerable<BaseVersion> GetBaseVersionsInternal(EffectiveBranchConfigur
6785
Increment = increment,
6886
ForceIncrement = false,
6987
Label = label,
70-
AlternativeSemanticVersion = alternativeSemanticVersionsWithTag.Max()?.Value
88+
AlternativeSemanticVersion = alternativeSemanticVersionMax
7189
}
7290
};
7391
alternativeSemanticVersionsWithTag.Clear();

0 commit comments

Comments
 (0)