Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When incrementing version using commit message, we only consider tags with a valid version #3759

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,41 @@ public void CanUseCommitMessagesToBumpVersion_TagTakesPriority()
fixture.AssertFullSemver("1.1.1-1");
}

[Theory]
[TestCase("", "NotAVersion", "2.0.0-1", "1.9.0", "1.9.0", "1.9.1-1")]
[TestCase("", "1.5.0", "1.5.0", "1.9.0", "1.9.0", "1.9.1-1")]
[TestCase("prefix", "1.5.0", "2.0.0-1", "1.9.0", "2.0.0-1", "2.0.0-2")]
[TestCase("prefix", "1.5.0", "2.0.0-1", "prefix1.9.0", "1.9.0", "1.9.1-1")]
[TestCase("prefix", "prefix1.5.0", "1.5.0", "1.9.0", "1.5.0", "1.5.1-1")]
public void CanUseCommitMessagesToBumpVersion_TagsTakePriorityOnlyIfVersions(
string tagPrefix,
string firstTag,
string expectedAfterFirstTag,
string secondTag,
string expectedAfterSecondTag,
string expectedVersionAfterNewCommit)
{
var configuration = GitFlowConfigurationBuilder.New
.WithTagPrefix(tagPrefix)
.Build();

using var fixture = new EmptyRepositoryFixture();
var repo = fixture.Repository;

repo.MakeATaggedCommit($"{tagPrefix}1.0.0");
repo.MakeACommit("+semver:major");
fixture.AssertFullSemver("2.0.0-1", configuration);

repo.ApplyTag(firstTag);
fixture.AssertFullSemver(expectedAfterFirstTag, configuration);

repo.ApplyTag(secondTag);
fixture.AssertFullSemver(expectedAfterSecondTag, configuration);

repo.MakeACommit();
fixture.AssertFullSemver(expectedVersionAfterNewCommit, configuration);
}

[Theory]
[TestCase("build: Cleaned up various things", "1.0.1")]
[TestCase("build: Cleaned up various things\n\nSome descriptive text", "1.0.1")]
Expand Down
19 changes: 13 additions & 6 deletions src/GitVersion.Core/VersionCalculation/IncrementStrategyFinder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Concurrent;
using System.Text.RegularExpressions;
using GitVersion.Common;
using GitVersion.Configuration;
using GitVersion.Extensions;

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

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

private readonly IGitRepository repository;
private readonly IRepositoryStore repositoryStore;

public IncrementStrategyFinder(IGitRepository repository)
public IncrementStrategyFinder(
IGitRepository repository,
IRepositoryStore repositoryStore)
{
this.repository = repository.NotNull();
this.tagsShaCache = new Lazy<IReadOnlySet<string?>>(ReadRepositoryTagsSha);
this.repositoryStore = repositoryStore.NotNull();
}

public VersionField DetermineIncrementedField(ICommit? currentCommit, BaseVersion baseVersion, EffectiveConfiguration configuration)
Expand Down Expand Up @@ -84,10 +87,16 @@ public VersionField DetermineIncrementedField(ICommit? currentCommit, BaseVersio
}

var commits = GetIntermediateCommits(baseCommit, currentCommit);
//get tags with valid version - depends on configuration (see #3757)
var versionTags = new Lazy<IReadOnlySet<string?>>(() =>
this.repositoryStore.GetTaggedSemanticVersions(configuration.TagPrefix, configuration.SemanticVersionFormat)
.Select(x => x.Tag.TargetSha)
.ToHashSet());

// consider commit messages since latest tag only (see #3071)
commits = commits
.Reverse()
.TakeWhile(x => !this.tagsShaCache.Value.Contains(x.Sha))
.TakeWhile(x => !versionTags.Value.Contains(x.Sha))
.Reverse();

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

private IReadOnlySet<string?> ReadRepositoryTagsSha() => repository.Tags.Select(t => t.TargetSha).ToHashSet();

private static Regex TryGetRegexOrDefault(string? messageRegex, Regex defaultRegex) =>
messageRegex == null
? defaultRegex
Expand Down