Skip to content

Fix issue with empty prerelease tags. #3224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
2 changes: 2 additions & 0 deletions src/GitVersion.Core/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,6 @@ public static bool IsEquivalentTo(this string self, string? other) =>

/// <inheritdoc cref="string.IsNullOrWhiteSpace"/>
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);
}
1 change: 1 addition & 0 deletions src/GitVersion.Core/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 18 additions & 2 deletions src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised that this logic is so complex why not keep it simple and refactor the expression like:

var expectedTag = configuration.GetBranchSpecificTag(this.log, Context.CurrentBranch.Name.Friendly, branchNameOverride);
var actualTag = semver.PreReleaseTag?.Name ?? string.Empty;
var preReleaseTagDoesNotMatchConfiguration = actualTag != expectedTag;
if(preReleaseTagDoesNotMatchConfiguration)
{
    UpdatePreReleaseTag(configuration.Value, semver, expectedTag);
}

var preReleaseTagDoesNotMatchConfiguration = hasPreReleaseTag
&& (branchConfigHasPreReleaseTagConfigured || branchConfigIsMainlineAndHasEmptyPreReleaseTagConfigured)
&& semver.PreReleaseTag?.Name != tag;
var preReleaseTagOnlyInBranchConfig = !hasPreReleaseTag && branchConfigHasPreReleaseTagConfigured;
if (preReleaseTagOnlyInBranchConfig || preReleaseTagDoesNotMatchConfiguration)
{
UpdatePreReleaseTag(configuration.Value, semver, baseVersion.BranchNameOverride);
}
Expand All @@ -93,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;
}
}
}

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

if (configuration.IsMainline && tagToUse.IsEmpty())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why only in that case the branch is set to IsMainline=true?

{
semanticVersion.PreReleaseTag = new SemanticVersionPreReleaseTag(tagToUse, null);
return;
}

long? number = null;

var lastTag = this.repositoryStore
Expand Down