From 0ce2b071c55a23a5f29670ed82210dacd206d400 Mon Sep 17 00:00:00 2001 From: HHobeck Date: Sat, 24 Feb 2024 08:58:35 +0100 Subject: [PATCH] Fix GitVersion calculates the wrong version after main is merged back to develop. --- .../BranchConfiguration.cs | 7 --- .../GitVersionConfiguration.cs | 7 +++ .../IntegrationTests/OtherScenarios.cs | 53 +++++++++++++++++++ .../Configuration/IBranchConfiguration.cs | 2 - .../Configuration/IGitVersionConfiguration.cs | 2 + .../Extensions/ConfigurationExtensions.cs | 2 +- src/GitVersion.Core/PublicAPI.Unshipped.txt | 2 +- .../IncrementStrategyFinder.cs | 21 +++++--- 8 files changed, 78 insertions(+), 18 deletions(-) diff --git a/src/GitVersion.Configuration/BranchConfiguration.cs b/src/GitVersion.Configuration/BranchConfiguration.cs index 0b1c49f203..a323c42e1b 100644 --- a/src/GitVersion.Configuration/BranchConfiguration.cs +++ b/src/GitVersion.Configuration/BranchConfiguration.cs @@ -107,11 +107,4 @@ public virtual IBranchConfiguration Inherit(IBranchConfiguration configuration) PreReleaseWeight = PreReleaseWeight ?? configuration.PreReleaseWeight }; } - - public IBranchConfiguration Empty() => new BranchConfiguration - { - RegularExpression = string.Empty, - Label = ConfigurationConstants.BranchNamePlaceholder, - Increment = IncrementStrategy.Inherit - }; } diff --git a/src/GitVersion.Configuration/GitVersionConfiguration.cs b/src/GitVersion.Configuration/GitVersionConfiguration.cs index 5700548f0f..2ab0c5f57d 100644 --- a/src/GitVersion.Configuration/GitVersionConfiguration.cs +++ b/src/GitVersion.Configuration/GitVersionConfiguration.cs @@ -157,4 +157,11 @@ public string ToJsonString() stream.Flush(); return stringBuilder.ToString(); } + + public IBranchConfiguration GetEmptyBranchConfiguration() => new BranchConfiguration + { + RegularExpression = string.Empty, + Label = ConfigurationConstants.BranchNamePlaceholder, + Increment = IncrementStrategy.Inherit + }; } 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/Configuration/IBranchConfiguration.cs b/src/GitVersion.Core/Configuration/IBranchConfiguration.cs index 0b9248ac79..0d1bb62da8 100644 --- a/src/GitVersion.Core/Configuration/IBranchConfiguration.cs +++ b/src/GitVersion.Core/Configuration/IBranchConfiguration.cs @@ -38,7 +38,5 @@ public bool IsMatch(string branchName) IReadOnlyCollection IsSourceBranchFor { get; } - IBranchConfiguration Empty(); - IBranchConfiguration Inherit(IBranchConfiguration configuration); } diff --git a/src/GitVersion.Core/Configuration/IGitVersionConfiguration.cs b/src/GitVersion.Core/Configuration/IGitVersionConfiguration.cs index 47a5db9e31..6680f5aea0 100644 --- a/src/GitVersion.Core/Configuration/IGitVersionConfiguration.cs +++ b/src/GitVersion.Core/Configuration/IGitVersionConfiguration.cs @@ -51,4 +51,6 @@ public interface IGitVersionConfiguration : IBranchConfiguration IIgnoreConfiguration Ignore { get; } string ToJsonString(); + + IBranchConfiguration GetEmptyBranchConfiguration(); } diff --git a/src/GitVersion.Core/Extensions/ConfigurationExtensions.cs b/src/GitVersion.Core/Extensions/ConfigurationExtensions.cs index 04b558fb55..8d0168961b 100644 --- a/src/GitVersion.Core/Extensions/ConfigurationExtensions.cs +++ b/src/GitVersion.Core/Extensions/ConfigurationExtensions.cs @@ -24,7 +24,7 @@ public static IBranchConfiguration GetBranchConfiguration(this IGitVersionConfig public static IBranchConfiguration GetBranchConfiguration(this IGitVersionConfiguration configuration, ReferenceName branchName) { var branchConfiguration = GetBranchConfigurations(configuration, branchName.WithoutOrigin).FirstOrDefault(); - branchConfiguration ??= configuration.Empty(); + branchConfiguration ??= configuration.GetEmptyBranchConfiguration(); return branchConfiguration; } diff --git a/src/GitVersion.Core/PublicAPI.Unshipped.txt b/src/GitVersion.Core/PublicAPI.Unshipped.txt index 2844692991..4a8a405677 100644 --- a/src/GitVersion.Core/PublicAPI.Unshipped.txt +++ b/src/GitVersion.Core/PublicAPI.Unshipped.txt @@ -147,7 +147,6 @@ GitVersion.Configuration.EffectiveConfiguration.VersionStrategy.get -> GitVersio GitVersion.Configuration.IBranchConfiguration GitVersion.Configuration.IBranchConfiguration.CommitMessageIncrementing.get -> GitVersion.VersionCalculation.CommitMessageIncrementMode? GitVersion.Configuration.IBranchConfiguration.DeploymentMode.get -> GitVersion.VersionCalculation.DeploymentMode? -GitVersion.Configuration.IBranchConfiguration.Empty() -> GitVersion.Configuration.IBranchConfiguration! GitVersion.Configuration.IBranchConfiguration.Increment.get -> GitVersion.IncrementStrategy GitVersion.Configuration.IBranchConfiguration.Inherit(GitVersion.Configuration.IBranchConfiguration! configuration) -> GitVersion.Configuration.IBranchConfiguration! GitVersion.Configuration.IBranchConfiguration.IsMainBranch.get -> bool? @@ -178,6 +177,7 @@ GitVersion.Configuration.IGitVersionConfiguration.AssemblyVersioningFormat.get - GitVersion.Configuration.IGitVersionConfiguration.AssemblyVersioningScheme.get -> GitVersion.Extensions.AssemblyVersioningScheme? GitVersion.Configuration.IGitVersionConfiguration.Branches.get -> System.Collections.Generic.IReadOnlyDictionary! GitVersion.Configuration.IGitVersionConfiguration.CommitDateFormat.get -> string? +GitVersion.Configuration.IGitVersionConfiguration.GetEmptyBranchConfiguration() -> GitVersion.Configuration.IBranchConfiguration! GitVersion.Configuration.IGitVersionConfiguration.Ignore.get -> GitVersion.Configuration.IIgnoreConfiguration! GitVersion.Configuration.IGitVersionConfiguration.TagPreReleaseWeight.get -> int? GitVersion.Configuration.IGitVersionConfiguration.TagPrefix.get -> string? 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,