Skip to content

Commit

Permalink
When incrementing using commit, consider tags with valid version only…
Browse files Browse the repository at this point in the history
… (see issue GitTools#3757)
  • Loading branch information
adaskos-signal committed Nov 7, 2023
1 parent 8e1c745 commit e2365d9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
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

0 comments on commit e2365d9

Please sign in to comment.