Skip to content

Commit 94e2127

Browse files
committed
Remove FixTheBaseVersionSourceOfMergeMessageStrategyIfReleaseBranchWasMergedAndDeleted in NextVersionCalculator and move business logic to MergeMessageVersionStrategy class.
1 parent f39b551 commit 94e2127

File tree

9 files changed

+79
-58
lines changed

9 files changed

+79
-58
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ public void ShouldNotConsiderTheMergeCommitFromReleaseToMainWhenCommitHasNotBeen
679679
fixture.Repository.Branches.Remove("release/1.0.0");
680680

681681
// ❔ expected: "0.0.1+4" because until the commit is not tagged it's a hotfix
682-
fixture.AssertFullSemver("1.0.0+4", configuration);
682+
fixture.AssertFullSemver("1.0.0+0", configuration);
683683

684684
fixture.ApplyTag("1.0.0");
685685

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,12 +551,12 @@ public void PreventDecrementationOfVersionsOnTheMainBranch()
551551
// ❔ expected: "0.0.1+4"
552552
// This behavior needs to be changed for the git flow workflow using the track-merge-message or track-merge-target options.
553553
// [Bug] track-merge-changes produces unexpected result when combining hotfix and support branches #3052
554-
fixture.AssertFullSemver("1.0.0+4", configurationBuilder.Build());
554+
fixture.AssertFullSemver("1.0.0+0", configurationBuilder.Build());
555555

556556
configurationBuilder.WithNextVersion("1.0.0");
557557

558558
// ✅ succeeds as expected
559-
fixture.AssertFullSemver("1.0.0+4", configurationBuilder.Build());
559+
fixture.AssertFullSemver("1.0.0+0", configurationBuilder.Build());
560560

561561
// Mark this version as RTM
562562
fixture.ApplyTag("1.0.0");

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ public void MainVersioningContinuousCorrectlyAfterMergingReleaseBranch()
195195
fixture.Checkout(MainBranch);
196196
fixture.Repository.MergeNoFF("release-2.0.0", Generate.SignatureNow());
197197

198+
fixture.AssertFullSemver("2.0.0+0");
199+
fixture.Repository.Branches.Remove("release-2.0.0");
198200
fixture.AssertFullSemver("2.0.0+0");
199201
fixture.Repository.ApplyTag("2.0.0");
200202
fixture.Repository.MakeCommits(1);

src/GitVersion.Core/PublicAPI.Shipped.txt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,7 @@ GitVersion.SemanticVersion.BuildMetaData -> GitVersion.SemanticVersionBuildMetaD
843843
GitVersion.SemanticVersion.CompareTo(GitVersion.SemanticVersion! value) -> int
844844
GitVersion.SemanticVersion.CompareTo(GitVersion.SemanticVersion? value, bool includePrerelease) -> int
845845
GitVersion.SemanticVersion.Equals(GitVersion.SemanticVersion? obj) -> bool
846+
GitVersion.SemanticVersion.HasPreReleaseTagWithLabel.get -> bool
846847
GitVersion.SemanticVersion.IncrementVersion(GitVersion.VersionField incrementStrategy) -> GitVersion.SemanticVersion!
847848
GitVersion.SemanticVersion.IsEmpty() -> bool
848849
GitVersion.SemanticVersion.Major -> long
@@ -972,15 +973,16 @@ GitVersion.VersionCalculation.IVersionFilter.Exclude(GitVersion.VersionCalculati
972973
GitVersion.VersionCalculation.IVersionStrategy
973974
GitVersion.VersionCalculation.IVersionStrategy.GetBaseVersions(GitVersion.Model.Configuration.EffectiveBranchConfiguration! configuration) -> System.Collections.Generic.IEnumerable<GitVersion.VersionCalculation.BaseVersion!>!
974975
GitVersion.VersionCalculation.MergeMessageVersionStrategy
975-
GitVersion.VersionCalculation.MergeMessageVersionStrategy.MergeMessageVersionStrategy(GitVersion.Logging.ILog! log, System.Lazy<GitVersion.GitVersionContext!>! versionContext) -> void
976+
GitVersion.VersionCalculation.MergeMessageVersionStrategy.MergeMessageVersionStrategy(GitVersion.Logging.ILog! log, System.Lazy<GitVersion.GitVersionContext!>! versionContext, GitVersion.Common.IRepositoryStore! repositoryStore) -> void
976977
GitVersion.VersionCalculation.MinDateVersionFilter
977978
GitVersion.VersionCalculation.MinDateVersionFilter.Exclude(GitVersion.VersionCalculation.BaseVersion! version, out string? reason) -> bool
978979
GitVersion.VersionCalculation.MinDateVersionFilter.MinDateVersionFilter(System.DateTimeOffset minimum) -> void
979980
GitVersion.VersionCalculation.NextVersion
980981
GitVersion.VersionCalculation.NextVersion.BaseVersion.get -> GitVersion.VersionCalculation.BaseVersion!
981-
GitVersion.VersionCalculation.NextVersion.BaseVersion.set -> void
982982
GitVersion.VersionCalculation.NextVersion.Branch.get -> GitVersion.IBranch!
983+
GitVersion.VersionCalculation.NextVersion.CompareTo(GitVersion.VersionCalculation.NextVersion! other) -> int
983984
GitVersion.VersionCalculation.NextVersion.Configuration.get -> GitVersion.Model.Configuration.EffectiveConfiguration!
985+
GitVersion.VersionCalculation.NextVersion.Equals(GitVersion.VersionCalculation.NextVersion! other) -> bool
984986
GitVersion.VersionCalculation.NextVersion.IncrementedVersion.get -> GitVersion.SemanticVersion!
985987
GitVersion.VersionCalculation.NextVersion.NextVersion(GitVersion.SemanticVersion! incrementedVersion, GitVersion.VersionCalculation.BaseVersion! baseVersion, GitVersion.IBranch! branch, GitVersion.Model.Configuration.EffectiveConfiguration! configuration) -> void
986988
GitVersion.VersionCalculation.NextVersion.NextVersion(GitVersion.SemanticVersion! incrementedVersion, GitVersion.VersionCalculation.BaseVersion! baseVersion, GitVersion.Model.Configuration.EffectiveBranchConfiguration! configuration) -> void
@@ -1235,6 +1237,8 @@ override GitVersion.SemanticVersionPreReleaseTag.ToString() -> string!
12351237
override GitVersion.VersionCalculation.BaseVersion.ToString() -> string!
12361238
override GitVersion.VersionCalculation.ConfigNextVersionVersionStrategy.GetBaseVersions(GitVersion.Model.Configuration.EffectiveBranchConfiguration! configuration) -> System.Collections.Generic.IEnumerable<GitVersion.VersionCalculation.BaseVersion!>!
12371239
override GitVersion.VersionCalculation.MergeMessageVersionStrategy.GetBaseVersions(GitVersion.Model.Configuration.EffectiveBranchConfiguration! configuration) -> System.Collections.Generic.IEnumerable<GitVersion.VersionCalculation.BaseVersion!>!
1240+
override GitVersion.VersionCalculation.NextVersion.Equals(object! other) -> bool
1241+
override GitVersion.VersionCalculation.NextVersion.GetHashCode() -> int
12381242
override GitVersion.VersionCalculation.NextVersion.ToString() -> string!
12391243
override GitVersion.VersionCalculation.TaggedCommitVersionStrategy.GetBaseVersions(GitVersion.Model.Configuration.EffectiveBranchConfiguration! configuration) -> System.Collections.Generic.IEnumerable<GitVersion.VersionCalculation.BaseVersion!>!
12401244
override GitVersion.VersionCalculation.TaggedCommitVersionStrategy.VersionTaggedCommit.ToString() -> string!
@@ -1354,6 +1358,12 @@ static GitVersion.SemanticVersionPreReleaseTag.operator ==(GitVersion.SemanticVe
13541358
static GitVersion.SemanticVersionPreReleaseTag.operator >(GitVersion.SemanticVersionPreReleaseTag? left, GitVersion.SemanticVersionPreReleaseTag? right) -> bool
13551359
static GitVersion.SemanticVersionPreReleaseTag.operator >=(GitVersion.SemanticVersionPreReleaseTag? left, GitVersion.SemanticVersionPreReleaseTag? right) -> bool
13561360
static GitVersion.SemanticVersionPreReleaseTag.Parse(string? preReleaseTag) -> GitVersion.SemanticVersionPreReleaseTag!
1361+
static GitVersion.VersionCalculation.NextVersion.operator !=(GitVersion.VersionCalculation.NextVersion! left, GitVersion.VersionCalculation.NextVersion! right) -> bool
1362+
static GitVersion.VersionCalculation.NextVersion.operator <(GitVersion.VersionCalculation.NextVersion! left, GitVersion.VersionCalculation.NextVersion! right) -> bool
1363+
static GitVersion.VersionCalculation.NextVersion.operator <=(GitVersion.VersionCalculation.NextVersion! left, GitVersion.VersionCalculation.NextVersion! right) -> bool
1364+
static GitVersion.VersionCalculation.NextVersion.operator ==(GitVersion.VersionCalculation.NextVersion! left, GitVersion.VersionCalculation.NextVersion! right) -> bool
1365+
static GitVersion.VersionCalculation.NextVersion.operator >(GitVersion.VersionCalculation.NextVersion! left, GitVersion.VersionCalculation.NextVersion! right) -> bool
1366+
static GitVersion.VersionCalculation.NextVersion.operator >=(GitVersion.VersionCalculation.NextVersion! left, GitVersion.VersionCalculation.NextVersion! right) -> bool
13571367
static readonly GitVersion.BranchCommit.Empty -> GitVersion.BranchCommit
13581368
static readonly GitVersion.Helpers.StringComparerUtils.IgnoreCaseComparer -> System.StringComparer!
13591369
static readonly GitVersion.Helpers.StringComparerUtils.OsDependentComparer -> System.StringComparer!

src/GitVersion.Core/VersionCalculation/BaseVersionCalculators/MergeMessageVersionStrategy.cs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Diagnostics.CodeAnalysis;
22
using System.Text.RegularExpressions;
3+
using GitVersion.Common;
34
using GitVersion.Configuration;
45
using GitVersion.Extensions;
56
using GitVersion.Logging;
@@ -15,9 +16,13 @@ namespace GitVersion.VersionCalculation;
1516
public class MergeMessageVersionStrategy : VersionStrategyBase
1617
{
1718
private readonly ILog log;
19+
private readonly IRepositoryStore repositoryStore;
1820

19-
public MergeMessageVersionStrategy(ILog log, Lazy<GitVersionContext> versionContext)
20-
: base(versionContext) => this.log = log.NotNull();
21+
public MergeMessageVersionStrategy(ILog log, Lazy<GitVersionContext> versionContext, IRepositoryStore repositoryStore) : base(versionContext)
22+
{
23+
this.log = log.NotNull();
24+
this.repositoryStore = repositoryStore.NotNull();
25+
}
2126

2227
public override IEnumerable<BaseVersion> GetBaseVersions(EffectiveBranchConfiguration configuration)
2328
{
@@ -34,10 +39,28 @@ public override IEnumerable<BaseVersion> GetBaseVersions(EffectiveBranchConfigur
3439
{
3540
this.log.Info($"Found commit [{Context.CurrentCommit}] matching merge message format: {mergeMessage.FormatName}");
3641
var shouldIncrement = !configuration.Value.PreventIncrementOfMergedBranchVersion;
37-
return new[]
42+
43+
var message = c.Message.Trim();
44+
45+
var baseVersionSource = c;
46+
47+
if (shouldIncrement)
3848
{
39-
new BaseVersion($"{MergeMessageStrategyPrefix} '{c.Message.Trim()}'", shouldIncrement, mergeMessage.Version, c, null)
40-
};
49+
var parents = c.Parents.ToArray();
50+
if (parents.Length == 2 && message.Contains("Merge branch") && message.Contains("release"))
51+
{
52+
baseVersionSource = this.repositoryStore.FindMergeBase(parents[0], parents[1]);
53+
}
54+
}
55+
56+
var baseVersion = new BaseVersion(
57+
source: $"{MergeMessageStrategyPrefix} '{message}'",
58+
shouldIncrement: shouldIncrement,
59+
semanticVersion: mergeMessage.Version,
60+
baseVersionSource: baseVersionSource,
61+
branchNameOverride: null
62+
);
63+
return new[] { baseVersion };
4164
}
4265
return Enumerable.Empty<BaseVersion>();
4366
})

src/GitVersion.Core/VersionCalculation/NextVersion.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
namespace GitVersion.VersionCalculation;
55

6-
public class NextVersion
6+
public class NextVersion : IComparable<NextVersion>, IEquatable<NextVersion>
77
{
8-
public BaseVersion BaseVersion { get; set; }
8+
public BaseVersion BaseVersion { get; }
99

1010
public SemanticVersion IncrementedVersion { get; }
1111

@@ -26,5 +26,25 @@ public NextVersion(SemanticVersion incrementedVersion, BaseVersion baseVersion,
2626
Branch = branch.NotNull();
2727
}
2828

29+
public int CompareTo(NextVersion other) => IncrementedVersion.CompareTo(other.IncrementedVersion);
30+
31+
public static bool operator ==(NextVersion left, NextVersion right) => left.CompareTo(right) == 0;
32+
33+
public static bool operator !=(NextVersion left, NextVersion right) => left.CompareTo(right) != 0;
34+
35+
public static bool operator <(NextVersion left, NextVersion right) => left.CompareTo(right) < 0;
36+
37+
public static bool operator <=(NextVersion left, NextVersion right) => left.CompareTo(right) <= 0;
38+
39+
public static bool operator >(NextVersion left, NextVersion right) => left.CompareTo(right) > 0;
40+
41+
public static bool operator >=(NextVersion left, NextVersion right) => left.CompareTo(right) >= 0;
42+
43+
public bool Equals(NextVersion other) => this == other;
44+
45+
public override bool Equals(object other) => other is NextVersion nextVersion && Equals(nextVersion);
46+
2947
public override string ToString() => $"{BaseVersion} | {IncrementedVersion}";
48+
49+
public override int GetHashCode() => ToString().GetHashCode();
3050
}

src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs

Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,17 @@ public virtual NextVersion FindVersion()
4848
EnsureHeadIsNotDetached(Context);
4949
}
5050

51-
5251
// TODO: It is totally unimportant that the current commit has been tagged or not IMO. We can make a double check actually if the result
5352
// is the same or make it configurable but each run should be deterministic.Even if the development process goes on the tagged commit
54-
// should always calculating the the same result. Otherwise something is wrong with the configuration or someone messed up the branching history.
53+
// should always calculating the same result. Otherwise something is wrong with the configuration or someone messed up the branching history.
5554

5655
SemanticVersion? taggedSemanticVersion = null;
5756

5857
if (Context.IsCurrentCommitTagged)
5958
{
6059
// Will always be 0, don't bother with the +0 on tags
61-
var semanticVersionBuildMetaData = this.mainlineVersionCalculator.CreateVersionBuildMetaData(Context.CurrentCommit);
60+
var semanticVersionBuildMetaData = this.mainlineVersionCalculator.CreateVersionBuildMetaData(Context.CurrentCommit!);
6261
semanticVersionBuildMetaData.CommitsSinceTag = null;
63-
6462
var semanticVersion = new SemanticVersion(Context.CurrentCommitTaggedVersion) { BuildMetaData = semanticVersionBuildMetaData };
6563
taggedSemanticVersion = semanticVersion;
6664
}
@@ -87,7 +85,7 @@ public virtual NextVersion FindVersion()
8785
}
8886
}
8987

90-
var hasPreReleaseTag = semver.PreReleaseTag?.HasTag() == true;
88+
var hasPreReleaseTag = semver.HasPreReleaseTagWithLabel;
9189
var tag = nextVersion.Configuration.Tag;
9290
var branchConfigHasPreReleaseTagConfigured = !tag.IsNullOrEmpty();
9391
var preReleaseTagDoesNotMatchConfiguration = hasPreReleaseTag && branchConfigHasPreReleaseTagConfigured && semver.PreReleaseTag?.Name != tag;
@@ -124,9 +122,9 @@ private void UpdatePreReleaseTag(EffectiveBranchConfiguration configuration, Sem
124122
.GetVersionTagsOnBranch(Context.CurrentBranch, Context.FullConfiguration.TagPrefix)
125123
.FirstOrDefault(v => v.PreReleaseTag?.Name?.IsEquivalentTo(tagToUse) == true);
126124

127-
if (lastTag != null && MajorMinorPatchEqual(lastTag, semanticVersion) && lastTag.PreReleaseTag?.HasTag() == true)
125+
if (lastTag != null && MajorMinorPatchEqual(lastTag, semanticVersion) && lastTag.HasPreReleaseTagWithLabel)
128126
{
129-
number = lastTag.PreReleaseTag.Number + 1;
127+
number = lastTag.PreReleaseTag!.Number + 1;
130128
}
131129

132130
number ??= 1;
@@ -154,10 +152,8 @@ private NextVersion Calculate(IBranch branch, Config configuration)
154152
using (log.IndentLog("Calculating the base versions"))
155153
{
156154
var nextVersions = GetNextVersions(branch, configuration).ToArray();
155+
var maxVersion = nextVersions.Max();
157156

158-
FixTheBaseVersionSourceOfMergeMessageStrategyIfReleaseBranchWasMergedAndDeleted(nextVersions);
159-
160-
var maxVersion = nextVersions.Aggregate((v1, v2) => v1.IncrementedVersion > v2.IncrementedVersion ? v1 : v2);
161157
var matchingVersionsOnceIncremented = nextVersions
162158
.Where(v => v.BaseVersion.BaseVersionSource != null && v.IncrementedVersion == maxVersion.IncrementedVersion)
163159
.ToList();
@@ -191,11 +187,11 @@ static NextVersion CompareVersions(
191187
else
192188
{
193189
IEnumerable<NextVersion> filteredVersions = nextVersions;
194-
if (!maxVersion.IncrementedVersion.PreReleaseTag!.HasTag())
190+
if (!maxVersion.IncrementedVersion.HasPreReleaseTagWithLabel)
195191
{
196192
// If the maximal version has no pre-release tag defined than we want to determine just the latest previous
197193
// base source which are not coming from pre-release tag.
198-
filteredVersions = filteredVersions.Where(v => !v.BaseVersion.SemanticVersion.PreReleaseTag!.HasTag());
194+
filteredVersions = filteredVersions.Where(v => !v.BaseVersion.SemanticVersion.HasPreReleaseTagWithLabel);
199195
}
200196

201197
var version = filteredVersions
@@ -287,38 +283,4 @@ private bool IncludeVersion(BaseVersion baseVersion, IgnoreConfig ignoreConfigur
287283
}
288284
return true;
289285
}
290-
291-
private void FixTheBaseVersionSourceOfMergeMessageStrategyIfReleaseBranchWasMergedAndDeleted(IEnumerable<NextVersion> nextVersions)
292-
{
293-
// TODO: Please us the mechanism per convention and configuration and make the decision in the IVersionStrategy implementation.
294-
if (ReleaseBranchExistsInRepo()) return;
295-
296-
foreach (var nextVersion in nextVersions)
297-
{
298-
if (nextVersion.BaseVersion.Source.Contains(MergeMessageVersionStrategy.MergeMessageStrategyPrefix)
299-
&& nextVersion.BaseVersion.Source.Contains("Merge branch")
300-
&& nextVersion.BaseVersion.Source.Contains("release"))
301-
{
302-
if (nextVersion.BaseVersion.BaseVersionSource != null)
303-
{
304-
var parents = nextVersion.BaseVersion.BaseVersionSource.Parents.ToList();
305-
306-
// TODO: Please find the correct base version in the IVersionStrategy implementation.
307-
nextVersion.BaseVersion = new BaseVersion(
308-
nextVersion.BaseVersion.Source,
309-
nextVersion.BaseVersion.ShouldIncrement,
310-
nextVersion.BaseVersion.SemanticVersion,
311-
this.repositoryStore.FindMergeBase(parents[0], parents[1]),
312-
nextVersion.BaseVersion.BranchNameOverride);
313-
}
314-
}
315-
}
316-
}
317-
318-
private bool ReleaseBranchExistsInRepo()
319-
{
320-
var releaseBranchConfig = Context.FullConfiguration.GetReleaseBranchConfig();
321-
var releaseBranches = this.repositoryStore.GetReleaseBranches(releaseBranchConfig);
322-
return releaseBranches.Any();
323-
}
324286
}

src/GitVersion.Core/VersionCalculation/SemanticVersioning/SemanticVersion.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public class SemanticVersion : IFormattable, IComparable<SemanticVersion>, IEqua
2424
public SemanticVersionPreReleaseTag? PreReleaseTag;
2525
public SemanticVersionBuildMetaData? BuildMetaData;
2626

27+
public bool HasPreReleaseTagWithLabel => PreReleaseTag?.HasTag() == true;
28+
2729
public SemanticVersion(long major = 0, long minor = 0, long patch = 0)
2830
{
2931
this.Major = major;

src/GitVersion.Core/VersionCalculation/SemanticVersioning/SemanticVersionPreReleaseTag.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ public SemanticVersionPreReleaseTag(SemanticVersionPreReleaseTag? preReleaseTag)
3333
}
3434

3535
public string? Name { get; set; }
36+
3637
public long? Number { get; set; }
38+
3739
public bool? PromotedFromCommits { get; set; }
3840

3941
public override bool Equals(object? obj) => Equals(obj as SemanticVersionPreReleaseTag);

0 commit comments

Comments
 (0)