Skip to content

Commit 3f75764

Browse files
authored
Merge pull request #3224 from enriqueraso/bug/resolving-semver-fail-with-an-empty-tag-in-a-configuration-branch
Fix issue with empty prerelease tags.
2 parents 544d4d1 + 3a6b123 commit 3f75764

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

src/GitVersion.Core.Tests/IntegrationTests/SupportBranchScenarios.cs

+27
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,31 @@ public void WhenSupportIsBranchedAndTaggedFromAnotherSupportEnsureNewMinorIsUsed
7171

7272
fixture.AssertFullSemver("1.3.1+2");
7373
}
74+
75+
[Test]
76+
public void WhenSupportIsBranchedFromMainWithSpecificTag()
77+
{
78+
using var fixture = new EmptyRepositoryFixture();
79+
fixture.Repository.MakeACommit();
80+
fixture.AssertFullSemver("0.1.0+0");
81+
82+
fixture.Repository.ApplyTag("1.4.0-rc");
83+
fixture.Repository.MakeACommit();
84+
fixture.Repository.CreateBranch("support/1");
85+
Commands.Checkout(fixture.Repository, "support/1");
86+
fixture.AssertFullSemver("1.4.0+1");
87+
}
88+
89+
[Test]
90+
public void WhenSupportIsBranchedFromMainWithSpecificTagOnCommit()
91+
{
92+
using var fixture = new EmptyRepositoryFixture();
93+
fixture.Repository.MakeACommit();
94+
fixture.AssertFullSemver("0.1.0+0");
95+
96+
fixture.Repository.ApplyTag("1.4.0-rc");
97+
fixture.Repository.CreateBranch("support/1");
98+
Commands.Checkout(fixture.Repository, "support/1");
99+
fixture.AssertFullSemver("1.4.0");
100+
}
74101
}

src/GitVersion.Core/Extensions/StringExtensions.cs

+2
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,6 @@ public static bool IsEquivalentTo(this string self, string? other) =>
102102

103103
/// <inheritdoc cref="string.IsNullOrWhiteSpace"/>
104104
public static bool IsNullOrWhiteSpace([NotNullWhen(false)] this string? value) => string.IsNullOrWhiteSpace(value);
105+
106+
public static bool IsEmpty([NotNullWhen(false)] this string? value) => string.Empty.Equals(value);
105107
}

src/GitVersion.Core/PublicAPI.Shipped.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,7 @@ static GitVersion.Extensions.StringExtensions.IsHelp(this string! singleArgument
13251325
static GitVersion.Extensions.StringExtensions.IsInit(this string! singleArgument) -> bool
13261326
static GitVersion.Extensions.StringExtensions.IsNullOrEmpty(this string? value) -> bool
13271327
static GitVersion.Extensions.StringExtensions.IsNullOrWhiteSpace(this string? value) -> bool
1328+
static GitVersion.Extensions.StringExtensions.IsEmpty(this string? value) -> bool
13281329
static GitVersion.Extensions.StringExtensions.IsSwitch(this string? value, string! switchName) -> bool
13291330
static GitVersion.Extensions.StringExtensions.IsSwitchArgument(this string? value) -> bool
13301331
static GitVersion.Extensions.StringExtensions.IsTrue(this string? value) -> bool

src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs

+18-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,12 @@ public NextVersion FindVersion()
7676
var hasPreReleaseTag = semver.PreReleaseTag?.HasTag() == true;
7777
var tag = configuration.Value.Tag;
7878
var branchConfigHasPreReleaseTagConfigured = !tag.IsNullOrEmpty();
79-
var preReleaseTagDoesNotMatchConfiguration = hasPreReleaseTag && branchConfigHasPreReleaseTagConfigured && semver.PreReleaseTag?.Name != tag;
80-
if (semver.PreReleaseTag?.HasTag() != true && branchConfigHasPreReleaseTagConfigured || preReleaseTagDoesNotMatchConfiguration)
79+
var branchConfigIsMainlineAndHasEmptyPreReleaseTagConfigured = configuration.Value.IsMainline && tag.IsEmpty();
80+
var preReleaseTagDoesNotMatchConfiguration = hasPreReleaseTag
81+
&& (branchConfigHasPreReleaseTagConfigured || branchConfigIsMainlineAndHasEmptyPreReleaseTagConfigured)
82+
&& semver.PreReleaseTag?.Name != tag;
83+
var preReleaseTagOnlyInBranchConfig = !hasPreReleaseTag && branchConfigHasPreReleaseTagConfigured;
84+
if (preReleaseTagOnlyInBranchConfig || preReleaseTagDoesNotMatchConfiguration)
8185
{
8286
UpdatePreReleaseTag(configuration.Value, semver, baseVersion.BranchNameOverride);
8387
}
@@ -93,6 +97,12 @@ public NextVersion FindVersion()
9397
{
9498
// set the commit count on the tagged ver
9599
taggedSemanticVersion.BuildMetaData.CommitsSinceVersionSource = semver.BuildMetaData?.CommitsSinceVersionSource;
100+
101+
// set the updated prerelease tag when it doesn't match with prerelease tag defined in branch configuration
102+
if (preReleaseTagDoesNotMatchConfiguration)
103+
{
104+
taggedSemanticVersion.PreReleaseTag = semver.PreReleaseTag;
105+
}
96106
}
97107
}
98108

@@ -112,6 +122,12 @@ private void UpdatePreReleaseTag(EffectiveConfiguration configuration, SemanticV
112122
{
113123
var tagToUse = configuration.GetBranchSpecificTag(this.log, Context.CurrentBranch.Name.Friendly, branchNameOverride);
114124

125+
if (configuration.IsMainline && tagToUse.IsEmpty())
126+
{
127+
semanticVersion.PreReleaseTag = new SemanticVersionPreReleaseTag(tagToUse, null);
128+
return;
129+
}
130+
115131
long? number = null;
116132

117133
var lastTag = this.repositoryStore

0 commit comments

Comments
 (0)