From e2365d943b8fc3e0c99a359d23166f192db74dad Mon Sep 17 00:00:00 2001 From: Antonios Daskos Date: Tue, 7 Nov 2023 18:03:48 +0200 Subject: [PATCH] When incrementing using commit, consider tags with valid version only (see issue #3757) --- .../VersionBumpingScenarios.cs | 35 +++++++++++++++++++ .../IncrementStrategyFinder.cs | 19 ++++++---- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/src/GitVersion.Core.Tests/IntegrationTests/VersionBumpingScenarios.cs b/src/GitVersion.Core.Tests/IntegrationTests/VersionBumpingScenarios.cs index ec91cc6f8f..f626dfde93 100644 --- a/src/GitVersion.Core.Tests/IntegrationTests/VersionBumpingScenarios.cs +++ b/src/GitVersion.Core.Tests/IntegrationTests/VersionBumpingScenarios.cs @@ -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")] diff --git a/src/GitVersion.Core/VersionCalculation/IncrementStrategyFinder.cs b/src/GitVersion.Core/VersionCalculation/IncrementStrategyFinder.cs index dd8e02fc2b..b2f2139c03 100644 --- a/src/GitVersion.Core/VersionCalculation/IncrementStrategyFinder.cs +++ b/src/GitVersion.Core/VersionCalculation/IncrementStrategyFinder.cs @@ -1,5 +1,6 @@ using System.Collections.Concurrent; using System.Text.RegularExpressions; +using GitVersion.Common; using GitVersion.Configuration; using GitVersion.Extensions; @@ -16,7 +17,6 @@ internal class IncrementStrategyFinder : IIncrementStrategyFinder private readonly Dictionary commitIncrementCache = new(); private readonly Dictionary> headCommitsMapCache = new(); private readonly Dictionary headCommitsCache = new(); - private readonly Lazy> tagsShaCache; private static readonly Regex DefaultMajorPatternRegex = new(DefaultMajorPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase); private static readonly Regex DefaultMinorPatternRegex = new(DefaultMinorPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase); @@ -24,11 +24,14 @@ internal class IncrementStrategyFinder : IIncrementStrategyFinder 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>(ReadRepositoryTagsSha); + this.repositoryStore = repositoryStore.NotNull(); } public VersionField DetermineIncrementedField(ICommit? currentCommit, BaseVersion baseVersion, EffectiveConfiguration configuration) @@ -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>(() => + 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) @@ -104,8 +113,6 @@ public VersionField DetermineIncrementedField(ICommit? currentCommit, BaseVersio ); } - private IReadOnlySet ReadRepositoryTagsSha() => repository.Tags.Select(t => t.TargetSha).ToHashSet(); - private static Regex TryGetRegexOrDefault(string? messageRegex, Regex defaultRegex) => messageRegex == null ? defaultRegex