Skip to content

Commit cfc4bc2

Browse files
When incrementing using commit, consider tags with valid version only (see issue #3757)
1 parent 8e1c745 commit cfc4bc2

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,41 @@ public void CanUseCommitMessagesToBumpVersion_TagTakesPriority()
5555
fixture.AssertFullSemver("1.1.1-1");
5656
}
5757

58+
[Theory]
59+
[TestCase("", "NotAVersion", "2.0.0-1", "1.9.0", "1.9.0", "1.9.1-1")]
60+
[TestCase("", "1.5.0", "1.5.0", "1.9.0", "1.9.0", "1.9.1-1")]
61+
[TestCase("prefix", "1.5.0", "2.0.0-1", "1.9.0", "2.0.0-1", "2.0.0-2")]
62+
[TestCase("prefix", "1.5.0", "2.0.0-1", "prefix1.9.0", "1.9.0", "1.9.1-1")]
63+
[TestCase("prefix", "prefix1.5.0", "1.5.0", "1.9.0", "1.5.0", "1.5.1-1")]
64+
public void CanUseCommitMessagesToBumpVersion_TagsTakePriorityOnlyIfVersions(
65+
string tagPrefix,
66+
string firstTag,
67+
string expectedAfterFirstTag,
68+
string secondTag,
69+
string expectedAfterSecondTag,
70+
string expectedVersionAfterNewCommit)
71+
{
72+
var configuration = GitFlowConfigurationBuilder.New
73+
.WithTagPrefix(tagPrefix)
74+
.Build();
75+
76+
using var fixture = new EmptyRepositoryFixture();
77+
var repo = fixture.Repository;
78+
79+
repo.MakeATaggedCommit($"{tagPrefix}1.0.0");
80+
repo.MakeACommit("+semver:major");
81+
fixture.AssertFullSemver("2.0.0-1", configuration);
82+
83+
repo.ApplyTag(firstTag);
84+
fixture.AssertFullSemver(expectedAfterFirstTag, configuration);
85+
86+
repo.ApplyTag(secondTag);
87+
fixture.AssertFullSemver(expectedAfterSecondTag, configuration);
88+
89+
repo.MakeACommit();
90+
fixture.AssertFullSemver(expectedVersionAfterNewCommit, configuration);
91+
}
92+
5893
[Theory]
5994
[TestCase("build: Cleaned up various things", "1.0.1")]
6095
[TestCase("build: Cleaned up various things\n\nSome descriptive text", "1.0.1")]

src/GitVersion.Core/VersionCalculation/IncrementStrategyFinder.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Concurrent;
22
using System.Text.RegularExpressions;
3+
using GitVersion.Common;
34
using GitVersion.Configuration;
45
using GitVersion.Extensions;
56

@@ -16,19 +17,21 @@ internal class IncrementStrategyFinder : IIncrementStrategyFinder
1617
private readonly Dictionary<string, VersionField?> commitIncrementCache = new();
1718
private readonly Dictionary<string, Dictionary<string, int>> headCommitsMapCache = new();
1819
private readonly Dictionary<string, ICommit[]> headCommitsCache = new();
19-
private readonly Lazy<IReadOnlySet<string?>> tagsShaCache;
2020

2121
private static readonly Regex DefaultMajorPatternRegex = new(DefaultMajorPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
2222
private static readonly Regex DefaultMinorPatternRegex = new(DefaultMinorPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
2323
private static readonly Regex DefaultPatchPatternRegex = new(DefaultPatchPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
2424
private static readonly Regex DefaultNoBumpPatternRegex = new(DefaultNoBumpPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
2525

2626
private readonly IGitRepository repository;
27+
private readonly IRepositoryStore repositoryStore;
2728

28-
public IncrementStrategyFinder(IGitRepository repository)
29+
public IncrementStrategyFinder(
30+
IGitRepository repository,
31+
IRepositoryStore repositoryStore)
2932
{
3033
this.repository = repository.NotNull();
31-
this.tagsShaCache = new Lazy<IReadOnlySet<string?>>(ReadRepositoryTagsSha);
34+
this.repositoryStore = repositoryStore.NotNull();
3235
}
3336

3437
public VersionField DetermineIncrementedField(ICommit? currentCommit, BaseVersion baseVersion, EffectiveConfiguration configuration)
@@ -84,10 +87,16 @@ public VersionField DetermineIncrementedField(ICommit? currentCommit, BaseVersio
8487
}
8588

8689
var commits = GetIntermediateCommits(baseCommit, currentCommit);
90+
//get tags with valid version - depends on configuration (see #3757)
91+
var versionTags = new Lazy<IReadOnlySet<string?>>(() =>
92+
this.repositoryStore.GetTaggedSemanticVersions(configuration.TagPrefix, configuration.SemanticVersionFormat)
93+
.Select(x => x.Tag.TargetSha)
94+
.ToHashSet());
95+
8796
// consider commit messages since latest tag only (see #3071)
8897
commits = commits
8998
.Reverse()
90-
.TakeWhile(x => !this.tagsShaCache.Value.Contains(x.Sha))
99+
.TakeWhile(x => !versionTags.Value.Contains(x.Sha))
91100
.Reverse();
92101

93102
if (configuration.CommitMessageIncrementing == CommitMessageIncrementMode.MergeMessageOnly)
@@ -104,8 +113,6 @@ public VersionField DetermineIncrementedField(ICommit? currentCommit, BaseVersio
104113
);
105114
}
106115

107-
private IReadOnlySet<string?> ReadRepositoryTagsSha() => repository.Tags.Select(t => t.TargetSha).ToHashSet();
108-
109116
private static Regex TryGetRegexOrDefault(string? messageRegex, Regex defaultRegex) =>
110117
messageRegex == null
111118
? defaultRegex

0 commit comments

Comments
 (0)