diff --git a/src/GitVersion.Core.Tests/IntegrationTests/OtherScenarios.cs b/src/GitVersion.Core.Tests/IntegrationTests/OtherScenarios.cs index 306ae3def6..048a8ee7dc 100644 --- a/src/GitVersion.Core.Tests/IntegrationTests/OtherScenarios.cs +++ b/src/GitVersion.Core.Tests/IntegrationTests/OtherScenarios.cs @@ -1218,4 +1218,57 @@ public void EnsurePreventIncrementWhenCurrentCommitTaggedOnReleaseBranchAndIncre fixture.AssertFullSemver(semVersion, configuration); } + + [TestCase(false, "2.0.0-alpha.3")] + [TestCase(true, "2.0.0-alpha.2")] + public void EnsureVersionAfterMainIsMergedBackToDevelopIsCorrectForTrunkBased(bool applyTag, string semanticVersion) + { + var configuration = GitFlowConfigurationBuilder.New + .WithVersionStrategy(VersionStrategies.TrunkBased) + .Build(); + + using var fixture = new EmptyRepositoryFixture("main"); + + fixture.MakeACommit("A"); + fixture.ApplyTag("1.0.0"); + fixture.BranchTo("develop"); + fixture.MakeACommit("B +semver: major"); + + // ✅ succeeds as expected + fixture.AssertFullSemver("2.0.0-alpha.1", configuration); + + fixture.Checkout("main"); + fixture.MakeACommit("C"); + if (applyTag) fixture.ApplyTag("1.0.1"); + fixture.Checkout("develop"); + fixture.MergeNoFF("main"); + + // ✅ succeeds as expected + fixture.AssertFullSemver(semanticVersion, configuration); + } + + [Test] + public void EnsureVersionAfterMainIsMergedBackToDevelopIsCorrectForGitFlow() + { + var configuration = GitFlowConfigurationBuilder.New.Build(); + + using var fixture = new EmptyRepositoryFixture("main"); + + fixture.MakeACommit("A"); + fixture.ApplyTag("1.0.0"); + fixture.BranchTo("develop"); + fixture.MakeACommit("B +semver: major"); + + // ✅ succeeds as expected + fixture.AssertFullSemver("2.0.0-alpha.1", configuration); + + fixture.Checkout("main"); + fixture.MakeACommit("C"); + fixture.ApplyTag("1.0.1"); + fixture.Checkout("develop"); + fixture.MergeNoFF("main"); + + // ✅ succeeds as expected + fixture.AssertFullSemver("2.0.0-alpha.3", configuration); + } } diff --git a/src/GitVersion.Core/VersionCalculation/IncrementStrategyFinder.cs b/src/GitVersion.Core/VersionCalculation/IncrementStrategyFinder.cs index e3224c1785..6dcb5814bc 100644 --- a/src/GitVersion.Core/VersionCalculation/IncrementStrategyFinder.cs +++ b/src/GitVersion.Core/VersionCalculation/IncrementStrategyFinder.cs @@ -40,7 +40,7 @@ public VersionField DetermineIncrementedField( configuration.NotNull(); var commitMessageIncrement = FindCommitMessageIncrement( - configuration, baseVersion.BaseVersionSource, currentCommit, label); + configuration, baseVersion, currentCommit, label); var defaultIncrement = configuration.Increment.ToVersionField(); @@ -81,31 +81,38 @@ public VersionField DetermineIncrementedField( } private VersionField? FindCommitMessageIncrement( - EffectiveConfiguration configuration, ICommit? baseCommit, ICommit? currentCommit, string? label) + EffectiveConfiguration configuration, BaseVersion baseVersion, ICommit? currentCommit, string? label) { if (configuration.CommitMessageIncrementing == CommitMessageIncrementMode.Disabled) { return null; } + var baseCommit = baseVersion.BaseVersionSource; + //get tags with valid version - depends on configuration (see #3757) var targetShas = new Lazy>(() => this.taggedSemanticVersionRepository.GetTaggedSemanticVersions(configuration.TagPrefix, configuration.SemanticVersionFormat) - .SelectMany(_ => _).Where(_ => _.Value.IsMatchForBranchSpecificLabel(label)).Select(_ => _.Tag.TargetSha).ToHashSet() + .SelectMany(_ => _).Where(_ => _.Value.IsMatchForBranchSpecificLabel(label)).Select(_ => _.Tag.TargetSha).ToHashSet() ); - var commits = GetIntermediateCommits(baseCommit, currentCommit); + var targetTags = this.taggedSemanticVersionRepository + .GetTaggedSemanticVersions(configuration.TagPrefix, configuration.SemanticVersionFormat) + .SelectMany(_ => _).Where(element => element.Value.IsMatchForBranchSpecificLabel(label) + && element.Value.CompareTo(baseVersion.GetSemanticVersion()) == 0).Select(_ => _.Tag.TargetSha).ToHashSet(); + + var intermediateCommits = GetIntermediateCommits(baseCommit, currentCommit); // consider commit messages since latest tag only (see #3071) - commits = commits + var commits = intermediateCommits .Reverse() - .TakeWhile(x => !targetShas.Value.Contains(x.Sha)) + .TakeWhile(x => baseCommit != x) + .Where(element => !targetShas.Value.Contains(element.Sha)) .Reverse(); if (configuration.CommitMessageIncrementing == CommitMessageIncrementMode.MergeMessageOnly) { commits = commits.Where(c => c.Parents.Count() > 1); } - return GetIncrementForCommits( majorVersionBumpMessage: configuration.MajorVersionBumpMessage, minorVersionBumpMessage: configuration.MinorVersionBumpMessage,