From 7576b44b16ecd693f40f5c53bff593ce3e48ec9c Mon Sep 17 00:00:00 2001 From: Enrique Raso Barbero Date: Wed, 5 Oct 2022 16:25:10 +0200 Subject: [PATCH 1/2] Detect if branchConfig is Mainline and has empty prerelease tag configured to update prerelease tag in semver --- src/GitVersion.Core/Extensions/StringExtensions.cs | 2 ++ src/GitVersion.Core/PublicAPI.Shipped.txt | 1 + .../VersionCalculation/NextVersionCalculator.cs | 14 ++++++++++++-- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/GitVersion.Core/Extensions/StringExtensions.cs b/src/GitVersion.Core/Extensions/StringExtensions.cs index 5843ee7e81..a8a19a7a4e 100644 --- a/src/GitVersion.Core/Extensions/StringExtensions.cs +++ b/src/GitVersion.Core/Extensions/StringExtensions.cs @@ -102,4 +102,6 @@ public static bool IsEquivalentTo(this string self, string? other) => /// public static bool IsNullOrWhiteSpace([NotNullWhen(false)] this string? value) => string.IsNullOrWhiteSpace(value); + + public static bool IsEmpty([NotNullWhen(false)] this string? value) => string.Empty.Equals(value); } diff --git a/src/GitVersion.Core/PublicAPI.Shipped.txt b/src/GitVersion.Core/PublicAPI.Shipped.txt index bd3d85d7a2..0c9b73897a 100644 --- a/src/GitVersion.Core/PublicAPI.Shipped.txt +++ b/src/GitVersion.Core/PublicAPI.Shipped.txt @@ -1325,6 +1325,7 @@ static GitVersion.Extensions.StringExtensions.IsHelp(this string! singleArgument static GitVersion.Extensions.StringExtensions.IsInit(this string! singleArgument) -> bool static GitVersion.Extensions.StringExtensions.IsNullOrEmpty(this string? value) -> bool static GitVersion.Extensions.StringExtensions.IsNullOrWhiteSpace(this string? value) -> bool +static GitVersion.Extensions.StringExtensions.IsEmpty(this string? value) -> bool static GitVersion.Extensions.StringExtensions.IsSwitch(this string? value, string! switchName) -> bool static GitVersion.Extensions.StringExtensions.IsSwitchArgument(this string? value) -> bool static GitVersion.Extensions.StringExtensions.IsTrue(this string? value) -> bool diff --git a/src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs b/src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs index 5aa62d2572..32320acc8d 100644 --- a/src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs +++ b/src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs @@ -76,8 +76,12 @@ public NextVersion FindVersion() var hasPreReleaseTag = semver.PreReleaseTag?.HasTag() == true; var tag = configuration.Value.Tag; var branchConfigHasPreReleaseTagConfigured = !tag.IsNullOrEmpty(); - var preReleaseTagDoesNotMatchConfiguration = hasPreReleaseTag && branchConfigHasPreReleaseTagConfigured && semver.PreReleaseTag?.Name != tag; - if (semver.PreReleaseTag?.HasTag() != true && branchConfigHasPreReleaseTagConfigured || preReleaseTagDoesNotMatchConfiguration) + var branchConfigIsMainlineAndHasEmptyPreReleaseTagConfigured = configuration.Value.IsMainline && tag.IsEmpty(); + var preReleaseTagDoesNotMatchConfiguration = hasPreReleaseTag + && (branchConfigHasPreReleaseTagConfigured || branchConfigIsMainlineAndHasEmptyPreReleaseTagConfigured) + && semver.PreReleaseTag?.Name != tag; + var preReleaseTagOnlyInBranchConfig = !hasPreReleaseTag && branchConfigHasPreReleaseTagConfigured; + if (preReleaseTagOnlyInBranchConfig || preReleaseTagDoesNotMatchConfiguration) { UpdatePreReleaseTag(configuration.Value, semver, baseVersion.BranchNameOverride); } @@ -112,6 +116,12 @@ private void UpdatePreReleaseTag(EffectiveConfiguration configuration, SemanticV { var tagToUse = configuration.GetBranchSpecificTag(this.log, Context.CurrentBranch.Name.Friendly, branchNameOverride); + if (configuration.IsMainline && tagToUse.IsEmpty()) + { + semanticVersion.PreReleaseTag = new SemanticVersionPreReleaseTag(tagToUse, null); + return; + } + long? number = null; var lastTag = this.repositoryStore From 3a6b12348aeac7b5ae459e83b71e6d1e74ea23c2 Mon Sep 17 00:00:00 2001 From: Enrique Raso Date: Thu, 6 Oct 2022 12:50:19 +0200 Subject: [PATCH 2/2] Add unit tests in SupportBranchScenarios --- .../SupportBranchScenarios.cs | 27 +++++++++++++++++++ .../NextVersionCalculator.cs | 6 +++++ 2 files changed, 33 insertions(+) diff --git a/src/GitVersion.Core.Tests/IntegrationTests/SupportBranchScenarios.cs b/src/GitVersion.Core.Tests/IntegrationTests/SupportBranchScenarios.cs index 351101afec..d0706e436b 100644 --- a/src/GitVersion.Core.Tests/IntegrationTests/SupportBranchScenarios.cs +++ b/src/GitVersion.Core.Tests/IntegrationTests/SupportBranchScenarios.cs @@ -71,4 +71,31 @@ public void WhenSupportIsBranchedAndTaggedFromAnotherSupportEnsureNewMinorIsUsed fixture.AssertFullSemver("1.3.1+2"); } + + [Test] + public void WhenSupportIsBranchedFromMainWithSpecificTag() + { + using var fixture = new EmptyRepositoryFixture(); + fixture.Repository.MakeACommit(); + fixture.AssertFullSemver("0.1.0+0"); + + fixture.Repository.ApplyTag("1.4.0-rc"); + fixture.Repository.MakeACommit(); + fixture.Repository.CreateBranch("support/1"); + Commands.Checkout(fixture.Repository, "support/1"); + fixture.AssertFullSemver("1.4.0+1"); + } + + [Test] + public void WhenSupportIsBranchedFromMainWithSpecificTagOnCommit() + { + using var fixture = new EmptyRepositoryFixture(); + fixture.Repository.MakeACommit(); + fixture.AssertFullSemver("0.1.0+0"); + + fixture.Repository.ApplyTag("1.4.0-rc"); + fixture.Repository.CreateBranch("support/1"); + Commands.Checkout(fixture.Repository, "support/1"); + fixture.AssertFullSemver("1.4.0"); + } } diff --git a/src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs b/src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs index 32320acc8d..7103ef1c08 100644 --- a/src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs +++ b/src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs @@ -97,6 +97,12 @@ public NextVersion FindVersion() { // set the commit count on the tagged ver taggedSemanticVersion.BuildMetaData.CommitsSinceVersionSource = semver.BuildMetaData?.CommitsSinceVersionSource; + + // set the updated prerelease tag when it doesn't match with prerelease tag defined in branch configuration + if (preReleaseTagDoesNotMatchConfiguration) + { + taggedSemanticVersion.PreReleaseTag = semver.PreReleaseTag; + } } }