From a2344cccd9da1a81be716f90f7e7ab5e9144c16c Mon Sep 17 00:00:00 2001 From: HHobeck Date: Fri, 19 Apr 2024 13:04:29 +0200 Subject: [PATCH] Integrate code review remarks of asbjornu. --- docs/input/docs/reference/configuration.md | 3 +- .../Workflows/TrunkBased/v1.yml | 2 +- .../IntegrationTests/OtherScenarios.cs | 57 +++++++++++++++++++ .../TrunkBased/NonTrunk/CommitOnNonTrunk.cs | 5 +- .../NonTrunk/CommitOnNonTrunkBranchedBase.cs | 4 +- .../CommitOnNonTrunkBranchedToNonTrunk.cs | 3 +- .../CommitOnNonTrunkBranchedToTrunk.cs | 3 +- .../CommitOnNonTrunkWithPreReleaseTagBase.cs | 3 +- .../CommitOnNonTrunkWithStableTagBase.cs | 5 +- .../NonTrunk/FirstCommitOnRelease.cs | 10 ++-- .../NonTrunk/MergeCommitOnNonTrunkBase.cs | 5 +- .../Trunk/CommitOnTrunkBranchedBase.cs | 2 +- .../Trunk/CommitOnTrunkBranchedToNonTrunk.cs | 3 +- .../Trunk/CommitOnTrunkBranchedToTrunk.cs | 3 +- .../LastCommitOnTrunkWithPreReleaseTag.cs | 2 +- .../Trunk/LastCommitOnTrunkWithStableTag.cs | 2 +- .../TrunkBased/TrunkBasedIteration.cs | 7 ++- .../TaggedCommitVersionStrategy.cs | 7 ++- 18 files changed, 99 insertions(+), 27 deletions(-) diff --git a/docs/input/docs/reference/configuration.md b/docs/input/docs/reference/configuration.md index 6209359ea1..536c5a1299 100644 --- a/docs/input/docs/reference/configuration.md +++ b/docs/input/docs/reference/configuration.md @@ -341,8 +341,7 @@ The details of the available options are as follows: ### workflow -The base template of the configuration to use. Possible values are: GitFlow/v1 or GitHubFlow/v1. Defaults to GitFlow/v1 if not set. To create a configuration from scratch without using a base template please specify an empty string. - +The base template of the configuration to use. Possible values are `GitFlow/v1` or `GitHubFlow/v1`. Defaults to `GitFlow/v1` if not set. To create a configuration from scratch without using a base template, please specify an empty string. ### next-version Allows you to bump the next version explicitly. Useful for bumping `main` or a diff --git a/src/GitVersion.Configuration/Workflows/TrunkBased/v1.yml b/src/GitVersion.Configuration/Workflows/TrunkBased/v1.yml index 3bf2f474e4..07f190c332 100644 --- a/src/GitVersion.Configuration/Workflows/TrunkBased/v1.yml +++ b/src/GitVersion.Configuration/Workflows/TrunkBased/v1.yml @@ -21,7 +21,7 @@ branches: increment: Patch prevent-increment: of-merged-branch: true - when-current-commit-tagged: trueS + when-current-commit-tagged: true track-merge-target: false regex: ^master$|^main$ source-branches: [] diff --git a/src/GitVersion.Core.Tests/IntegrationTests/OtherScenarios.cs b/src/GitVersion.Core.Tests/IntegrationTests/OtherScenarios.cs index 37dc154eaa..34e7881620 100644 --- a/src/GitVersion.Core.Tests/IntegrationTests/OtherScenarios.cs +++ b/src/GitVersion.Core.Tests/IntegrationTests/OtherScenarios.cs @@ -1307,4 +1307,61 @@ public void EnsureVersionAfterMainIsMergedBackToDevelopIsCorrectForGitFlow(bool // ✅ succeeds as expected fixture.AssertFullSemver(semanticVersion, configuration); } + + /// + /// see https://github.com/GitTools/GitVersion/issues/2394 + /// + [Test] + public void EnsureVersionSourceIsSetToTheRightTag() + { + // Arrange + var configuration = GitFlowConfigurationBuilder.New.Build(); + + // Act + using var fixture = new BaseGitFlowRepositoryFixture("0.1.0"); + + fixture.Checkout("main"); + fixture.MergeNoFF("develop"); + fixture.Checkout("develop"); + fixture.MakeACommit("Feature commit 1"); + fixture.BranchTo("release/0.2.0"); + fixture.MakeACommit("Release commit 1"); + fixture.Checkout("main"); + fixture.MergeNoFF("release/0.2.0"); + fixture.ApplyTag("0.2.0"); + var tag = fixture.Repository.Head.Tip; + fixture.Checkout("develop"); + fixture.MergeNoFF("main"); + var version = fixture.GetVersion(configuration); + + // Assert + version.VersionSourceSha.ShouldBe(tag.Sha); + } + + /// + /// see https://github.com/GitTools/GitVersion/issues/3689 + /// + [Test] + public void UnversionedHotfix() + { + var configuration = GitFlowConfigurationBuilder.New.Build(); + + using var fixture = new BaseGitFlowRepositoryFixture("1.2.0"); + + // create hotfix + Commands.Checkout(fixture.Repository, "main"); + Commands.Checkout(fixture.Repository, fixture.Repository.CreateBranch("hotfix/put-out-the-fire")); + fixture.Repository.MakeACommit(); + + fixture.AssertFullSemver("1.2.1-beta.1+1", configuration); + fixture.Repository.MakeACommit(); + fixture.AssertFullSemver("1.2.1-beta.1+2", configuration); + + // Merge hotfix branch to main + Commands.Checkout(fixture.Repository, "main"); + + fixture.Repository.MergeNoFF("hotfix/put-out-the-fire", Generate.SignatureNow()); + + fixture.AssertFullSemver("1.2.1-3", configuration); + } } diff --git a/src/GitVersion.Core/VersionCalculation/TrunkBased/NonTrunk/CommitOnNonTrunk.cs b/src/GitVersion.Core/VersionCalculation/TrunkBased/NonTrunk/CommitOnNonTrunk.cs index bd1ac19de9..dea1beb8bc 100644 --- a/src/GitVersion.Core/VersionCalculation/TrunkBased/NonTrunk/CommitOnNonTrunk.cs +++ b/src/GitVersion.Core/VersionCalculation/TrunkBased/NonTrunk/CommitOnNonTrunk.cs @@ -11,8 +11,9 @@ internal sealed class CommitOnNonTrunk : ITrunkBasedIncrementer // A 58 minutes ago public bool MatchPrecondition(TrunkBasedIteration iteration, TrunkBasedCommit commit, TrunkBasedContext context) - => !commit.HasChildIteration && !commit.GetEffectiveConfiguration(context.Configuration).IsMainBranch - && context.SemanticVersion is null; + => !commit.HasChildIteration + && !commit.GetEffectiveConfiguration(context.Configuration).IsMainBranch + && context.SemanticVersion is null; public IEnumerable GetIncrements( TrunkBasedIteration iteration, TrunkBasedCommit commit, TrunkBasedContext context) diff --git a/src/GitVersion.Core/VersionCalculation/TrunkBased/NonTrunk/CommitOnNonTrunkBranchedBase.cs b/src/GitVersion.Core/VersionCalculation/TrunkBased/NonTrunk/CommitOnNonTrunkBranchedBase.cs index e2eac0a217..ff8a6b12ac 100644 --- a/src/GitVersion.Core/VersionCalculation/TrunkBased/NonTrunk/CommitOnNonTrunkBranchedBase.cs +++ b/src/GitVersion.Core/VersionCalculation/TrunkBased/NonTrunk/CommitOnNonTrunkBranchedBase.cs @@ -6,7 +6,9 @@ namespace GitVersion.VersionCalculation.TrunkBased.NonTrunk; internal abstract class CommitOnNonTrunkBranchedBase : ITrunkBasedIncrementer { public virtual bool MatchPrecondition(TrunkBasedIteration iteration, TrunkBasedCommit commit, TrunkBasedContext context) - => !commit.GetEffectiveConfiguration(context.Configuration).IsMainBranch && commit.BranchName != iteration.BranchName && commit.Successor is null; + => !commit.GetEffectiveConfiguration(context.Configuration).IsMainBranch + && commit.BranchName != iteration.BranchName + && commit.Successor is null; public virtual IEnumerable GetIncrements( TrunkBasedIteration iteration, TrunkBasedCommit commit, TrunkBasedContext context) diff --git a/src/GitVersion.Core/VersionCalculation/TrunkBased/NonTrunk/CommitOnNonTrunkBranchedToNonTrunk.cs b/src/GitVersion.Core/VersionCalculation/TrunkBased/NonTrunk/CommitOnNonTrunkBranchedToNonTrunk.cs index c6e10bebf3..c870cd297c 100644 --- a/src/GitVersion.Core/VersionCalculation/TrunkBased/NonTrunk/CommitOnNonTrunkBranchedToNonTrunk.cs +++ b/src/GitVersion.Core/VersionCalculation/TrunkBased/NonTrunk/CommitOnNonTrunkBranchedToNonTrunk.cs @@ -14,5 +14,6 @@ internal sealed class CommitOnNonTrunkBranchedToNonTrunk : CommitOnNonTrunkBranc // A 58 minutes ago <<-- public override bool MatchPrecondition(TrunkBasedIteration iteration, TrunkBasedCommit commit, TrunkBasedContext context) - => base.MatchPrecondition(iteration, commit, context) && !(iteration.Configuration.IsMainBranch == true); + => base.MatchPrecondition(iteration, commit, context) + && !iteration.GetEffectiveConfiguration(context.Configuration).IsMainBranch; } diff --git a/src/GitVersion.Core/VersionCalculation/TrunkBased/NonTrunk/CommitOnNonTrunkBranchedToTrunk.cs b/src/GitVersion.Core/VersionCalculation/TrunkBased/NonTrunk/CommitOnNonTrunkBranchedToTrunk.cs index 6ea2c066a6..22ff874504 100644 --- a/src/GitVersion.Core/VersionCalculation/TrunkBased/NonTrunk/CommitOnNonTrunkBranchedToTrunk.cs +++ b/src/GitVersion.Core/VersionCalculation/TrunkBased/NonTrunk/CommitOnNonTrunkBranchedToTrunk.cs @@ -14,5 +14,6 @@ internal sealed class CommitOnNonTrunkBranchedToTrunk : CommitOnNonTrunkBranched // A 58 minutes ago <<-- public override bool MatchPrecondition(TrunkBasedIteration iteration, TrunkBasedCommit commit, TrunkBasedContext context) - => base.MatchPrecondition(iteration, commit, context) && iteration.Configuration.IsMainBranch == true; + => base.MatchPrecondition(iteration, commit, context) + && iteration.GetEffectiveConfiguration(context.Configuration).IsMainBranch; } diff --git a/src/GitVersion.Core/VersionCalculation/TrunkBased/NonTrunk/CommitOnNonTrunkWithPreReleaseTagBase.cs b/src/GitVersion.Core/VersionCalculation/TrunkBased/NonTrunk/CommitOnNonTrunkWithPreReleaseTagBase.cs index 54a48ead70..ba31a411b7 100644 --- a/src/GitVersion.Core/VersionCalculation/TrunkBased/NonTrunk/CommitOnNonTrunkWithPreReleaseTagBase.cs +++ b/src/GitVersion.Core/VersionCalculation/TrunkBased/NonTrunk/CommitOnNonTrunkWithPreReleaseTagBase.cs @@ -6,7 +6,8 @@ namespace GitVersion.VersionCalculation.TrunkBased.NonTrunk; internal abstract class CommitOnNonTrunkWithPreReleaseTagBase : ITrunkBasedIncrementer { public virtual bool MatchPrecondition(TrunkBasedIteration iteration, TrunkBasedCommit commit, TrunkBasedContext context) - => !commit.HasChildIteration && !commit.GetEffectiveConfiguration(context.Configuration).IsMainBranch + => !commit.HasChildIteration + && !commit.GetEffectiveConfiguration(context.Configuration).IsMainBranch && context.SemanticVersion?.IsPreRelease == true; public virtual IEnumerable GetIncrements( diff --git a/src/GitVersion.Core/VersionCalculation/TrunkBased/NonTrunk/CommitOnNonTrunkWithStableTagBase.cs b/src/GitVersion.Core/VersionCalculation/TrunkBased/NonTrunk/CommitOnNonTrunkWithStableTagBase.cs index 436f1a8b81..65be56f8ec 100644 --- a/src/GitVersion.Core/VersionCalculation/TrunkBased/NonTrunk/CommitOnNonTrunkWithStableTagBase.cs +++ b/src/GitVersion.Core/VersionCalculation/TrunkBased/NonTrunk/CommitOnNonTrunkWithStableTagBase.cs @@ -6,8 +6,9 @@ namespace GitVersion.VersionCalculation.TrunkBased.NonTrunk; internal abstract class CommitOnNonTrunkWithStableTagBase : ITrunkBasedIncrementer { public virtual bool MatchPrecondition(TrunkBasedIteration iteration, TrunkBasedCommit commit, TrunkBasedContext context) - => !commit.GetEffectiveConfiguration(context.Configuration).IsMainBranch && !commit.HasChildIteration - && context.SemanticVersion?.IsPreRelease == false; + => !commit.GetEffectiveConfiguration(context.Configuration).IsMainBranch + && !commit.HasChildIteration + && context.SemanticVersion?.IsPreRelease == false; public virtual IEnumerable GetIncrements( TrunkBasedIteration iteration, TrunkBasedCommit commit, TrunkBasedContext context) diff --git a/src/GitVersion.Core/VersionCalculation/TrunkBased/NonTrunk/FirstCommitOnRelease.cs b/src/GitVersion.Core/VersionCalculation/TrunkBased/NonTrunk/FirstCommitOnRelease.cs index 4bcc2efe57..21bdc365dd 100644 --- a/src/GitVersion.Core/VersionCalculation/TrunkBased/NonTrunk/FirstCommitOnRelease.cs +++ b/src/GitVersion.Core/VersionCalculation/TrunkBased/NonTrunk/FirstCommitOnRelease.cs @@ -14,10 +14,12 @@ internal sealed class FirstCommitOnRelease : ITrunkBasedIncrementer // A 58 minutes ago (main) public bool MatchPrecondition(TrunkBasedIteration iteration, TrunkBasedCommit commit, TrunkBasedContext context) - => !commit.HasChildIteration && !commit.GetEffectiveConfiguration(context.Configuration).IsMainBranch - && commit.GetEffectiveConfiguration(context.Configuration).IsReleaseBranch - && context.SemanticVersion is null && (commit.Predecessor is null - || commit.Predecessor?.BranchName != commit.BranchName); + => !commit.HasChildIteration + && !commit.GetEffectiveConfiguration(context.Configuration).IsMainBranch + && commit.GetEffectiveConfiguration(context.Configuration).IsReleaseBranch + && context.SemanticVersion is null + && (commit.Predecessor is null + || commit.Predecessor?.BranchName != commit.BranchName); public IEnumerable GetIncrements( TrunkBasedIteration iteration, TrunkBasedCommit commit, TrunkBasedContext context) diff --git a/src/GitVersion.Core/VersionCalculation/TrunkBased/NonTrunk/MergeCommitOnNonTrunkBase.cs b/src/GitVersion.Core/VersionCalculation/TrunkBased/NonTrunk/MergeCommitOnNonTrunkBase.cs index 92aaea67ad..1479cb33cb 100644 --- a/src/GitVersion.Core/VersionCalculation/TrunkBased/NonTrunk/MergeCommitOnNonTrunkBase.cs +++ b/src/GitVersion.Core/VersionCalculation/TrunkBased/NonTrunk/MergeCommitOnNonTrunkBase.cs @@ -5,8 +5,9 @@ namespace GitVersion.VersionCalculation.TrunkBased.NonTrunk; internal abstract class MergeCommitOnNonTrunkBase : ITrunkBasedIncrementer { public virtual bool MatchPrecondition(TrunkBasedIteration iteration, TrunkBasedCommit commit, TrunkBasedContext context) - => commit.HasChildIteration && !commit.GetEffectiveConfiguration(context.Configuration).IsMainBranch - && context.SemanticVersion is null; + => commit.HasChildIteration + && !commit.GetEffectiveConfiguration(context.Configuration).IsMainBranch + && context.SemanticVersion is null; public virtual IEnumerable GetIncrements( TrunkBasedIteration iteration, TrunkBasedCommit commit, TrunkBasedContext context) diff --git a/src/GitVersion.Core/VersionCalculation/TrunkBased/Trunk/CommitOnTrunkBranchedBase.cs b/src/GitVersion.Core/VersionCalculation/TrunkBased/Trunk/CommitOnTrunkBranchedBase.cs index 20dc43edf7..16db6abdf9 100644 --- a/src/GitVersion.Core/VersionCalculation/TrunkBased/Trunk/CommitOnTrunkBranchedBase.cs +++ b/src/GitVersion.Core/VersionCalculation/TrunkBased/Trunk/CommitOnTrunkBranchedBase.cs @@ -15,7 +15,7 @@ public virtual IEnumerable GetIncrements( context.BaseVersionSource = commit.Value; var effectiveConfiguration = iteration.GetEffectiveConfiguration(context.Configuration); - if (iteration.Configuration.IsReleaseBranch == true + if (iteration.GetEffectiveConfiguration(context.Configuration).IsReleaseBranch && iteration.BranchName.TryGetSemanticVersion(out var element, effectiveConfiguration)) { context.AlternativeSemanticVersions.Add(element.Value); diff --git a/src/GitVersion.Core/VersionCalculation/TrunkBased/Trunk/CommitOnTrunkBranchedToNonTrunk.cs b/src/GitVersion.Core/VersionCalculation/TrunkBased/Trunk/CommitOnTrunkBranchedToNonTrunk.cs index 0edd05aede..cc71df969a 100644 --- a/src/GitVersion.Core/VersionCalculation/TrunkBased/Trunk/CommitOnTrunkBranchedToNonTrunk.cs +++ b/src/GitVersion.Core/VersionCalculation/TrunkBased/Trunk/CommitOnTrunkBranchedToNonTrunk.cs @@ -14,5 +14,6 @@ internal sealed class CommitOnTrunkBranchedToNonTrunk : CommitOnTrunkBranchedBas // A 58 minutes ago <<-- public override bool MatchPrecondition(TrunkBasedIteration iteration, TrunkBasedCommit commit, TrunkBasedContext context) - => base.MatchPrecondition(iteration, commit, context) && !(iteration.Configuration.IsMainBranch == true); + => base.MatchPrecondition(iteration, commit, context) + && !iteration.GetEffectiveConfiguration(context.Configuration).IsMainBranch; } diff --git a/src/GitVersion.Core/VersionCalculation/TrunkBased/Trunk/CommitOnTrunkBranchedToTrunk.cs b/src/GitVersion.Core/VersionCalculation/TrunkBased/Trunk/CommitOnTrunkBranchedToTrunk.cs index 136fec9d0c..059fe28941 100644 --- a/src/GitVersion.Core/VersionCalculation/TrunkBased/Trunk/CommitOnTrunkBranchedToTrunk.cs +++ b/src/GitVersion.Core/VersionCalculation/TrunkBased/Trunk/CommitOnTrunkBranchedToTrunk.cs @@ -14,5 +14,6 @@ internal sealed class CommitOnTrunkBranchedToTrunk : CommitOnTrunkBranchedBase // A 58 minutes ago <<-- public override bool MatchPrecondition(TrunkBasedIteration iteration, TrunkBasedCommit commit, TrunkBasedContext context) - => base.MatchPrecondition(iteration, commit, context) && iteration.Configuration.IsMainBranch == true; + => base.MatchPrecondition(iteration, commit, context) + && iteration.GetEffectiveConfiguration(context.Configuration).IsMainBranch; } diff --git a/src/GitVersion.Core/VersionCalculation/TrunkBased/Trunk/LastCommitOnTrunkWithPreReleaseTag.cs b/src/GitVersion.Core/VersionCalculation/TrunkBased/Trunk/LastCommitOnTrunkWithPreReleaseTag.cs index 0a7ce3c916..91e8407ce5 100644 --- a/src/GitVersion.Core/VersionCalculation/TrunkBased/Trunk/LastCommitOnTrunkWithPreReleaseTag.cs +++ b/src/GitVersion.Core/VersionCalculation/TrunkBased/Trunk/LastCommitOnTrunkWithPreReleaseTag.cs @@ -18,7 +18,7 @@ public override IEnumerable GetIncrements( yield return item; } - if (iteration.Configuration.IsMainBranch == true) + if (iteration.GetEffectiveConfiguration(context.Configuration).IsMainBranch) { context.Increment = commit.GetIncrementForcedByBranch(context.Configuration); diff --git a/src/GitVersion.Core/VersionCalculation/TrunkBased/Trunk/LastCommitOnTrunkWithStableTag.cs b/src/GitVersion.Core/VersionCalculation/TrunkBased/Trunk/LastCommitOnTrunkWithStableTag.cs index af4c5fd05c..16e32a59a6 100644 --- a/src/GitVersion.Core/VersionCalculation/TrunkBased/Trunk/LastCommitOnTrunkWithStableTag.cs +++ b/src/GitVersion.Core/VersionCalculation/TrunkBased/Trunk/LastCommitOnTrunkWithStableTag.cs @@ -16,7 +16,7 @@ public override IEnumerable GetIncrements( yield return item; } - if (iteration.Configuration.IsMainBranch == true) + if (iteration.GetEffectiveConfiguration(context.Configuration).IsMainBranch) { context.ForceIncrement = true; diff --git a/src/GitVersion.Core/VersionCalculation/TrunkBased/TrunkBasedIteration.cs b/src/GitVersion.Core/VersionCalculation/TrunkBased/TrunkBasedIteration.cs index 94d5f44454..a0c910ed55 100644 --- a/src/GitVersion.Core/VersionCalculation/TrunkBased/TrunkBasedIteration.cs +++ b/src/GitVersion.Core/VersionCalculation/TrunkBased/TrunkBasedIteration.cs @@ -18,7 +18,10 @@ internal record TrunkBasedIteration public EffectiveConfiguration GetEffectiveConfiguration(IGitVersionConfiguration configuration) { - if (effectiveConfiguration is not null) return effectiveConfiguration; + if (this.effectiveConfiguration is not null) + { + return this.effectiveConfiguration; + } IBranchConfiguration branchConfiguration = Configuration; @@ -28,7 +31,7 @@ public EffectiveConfiguration GetEffectiveConfiguration(IGitVersionConfiguration branchConfiguration = branchConfiguration.Inherit(parentConfiguration); } - return effectiveConfiguration = new EffectiveConfiguration(configuration, branchConfiguration); + return this.effectiveConfiguration = new EffectiveConfiguration(configuration, branchConfiguration); } public TrunkBasedIteration? ParentIteration { get; } diff --git a/src/GitVersion.Core/VersionCalculation/VersionSearchStrategies/TaggedCommitVersionStrategy.cs b/src/GitVersion.Core/VersionCalculation/VersionSearchStrategies/TaggedCommitVersionStrategy.cs index 82a61aed04..a8a2bd7f61 100644 --- a/src/GitVersion.Core/VersionCalculation/VersionSearchStrategies/TaggedCommitVersionStrategy.cs +++ b/src/GitVersion.Core/VersionCalculation/VersionSearchStrategies/TaggedCommitVersionStrategy.cs @@ -41,10 +41,11 @@ private IEnumerable GetBaseVersionsInternal(EffectiveBranchConfigur var label = configuration.Value.GetBranchSpecificLabel(Context.CurrentBranch.Name, null); var maxTaggedSemanticVersion = taggedSemanticVersions - .Where(element => !element.Value.IsMatchForBranchSpecificLabel(label)).Max(); + .Where(element => !element.Value.IsMatchForBranchSpecificLabel(label)) + .Max(); - foreach (var semanticVersionWithTag - in taggedSemanticVersions.Where(element => element.Value.IsMatchForBranchSpecificLabel(label))) + var semanticVersionsWithTag = taggedSemanticVersions.Where(element => element.Value.IsMatchForBranchSpecificLabel(label)); + foreach (var semanticVersionWithTag in semanticVersionsWithTag) { var baseVersionSource = semanticVersionWithTag.Tag.Commit; var increment = incrementStrategyFinder.DetermineIncrementedField(