Skip to content

Commit

Permalink
Merge pull request #3437 from HHobeck/feature/3436_GitVersion_behaves…
Browse files Browse the repository at this point in the history
…_different_if_it_is_used_the_first_time

Fix for GitVersion behaves different if it is used the first time where the fallback version strategy applies
  • Loading branch information
arturcic committed Mar 12, 2023
2 parents fd21d84 + 2402ea8 commit bbf28be
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 133 deletions.
67 changes: 66 additions & 1 deletion src/GitVersion.Core.Tests/IntegrationTests/OtherScenarios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public void NoDirtyFlagInCleanRepository()

[TestCase(false, "1.1.0-alpha.2")]
[TestCase(true, "1.2.0-alpha.1")]
public void EnusreTrackMergeTargetStrategyWhichWillLookForTaggedMergecommits(bool trackMergeTarget, string expectedVersion)
public void EnsureTrackMergeTargetStrategyWhichWillLookForTaggedMergecommits(bool trackMergeTarget, string expectedVersion)
{
// * 9daa6ea 53 minutes ago (HEAD -> develop)
// | * 85536f2 55 minutes ago (tag: 1.1.0, main)
Expand Down Expand Up @@ -162,4 +162,69 @@ public void EnusreTrackMergeTargetStrategyWhichWillLookForTaggedMergecommits(boo

fixture.Repository.DumpGraph();
}

[TestCase(1)]
[TestCase(2)]
public void EnsurePreReleaseTagLabelWillBeConsideredIfNoLabelIsDefined(long patchNumber)
{
var configuration = GitHubFlowConfigurationBuilder.New
.WithLabel(null)
.WithBranch("main", branchBuilder => branchBuilder
.WithLabel(null).WithIncrement(IncrementStrategy.Patch)
).Build();

using var fixture = new EmptyRepositoryFixture("main");

fixture.MakeACommit();

// ✅ succeeds as expected
fixture.AssertFullSemver("0.0.1+1", configuration);

fixture.ApplyTag($"0.0.{patchNumber}-alpha.1");

// ✅ succeeds as expected
fixture.AssertFullSemver($"0.0.{patchNumber}-alpha.1", configuration);

fixture.MakeACommit();

// ✅ succeeds as expected
fixture.AssertFullSemver($"0.0.{patchNumber}-alpha.2+1", configuration);

fixture.MakeACommit();

// ✅ succeeds as expected
fixture.AssertFullSemver($"0.0.{patchNumber}-alpha.2+2", configuration);

fixture.MakeATaggedCommit($"0.0.{patchNumber}-beta.1");

// ✅ succeeds as expected
fixture.AssertFullSemver($"0.0.{patchNumber}-beta.1", configuration);

fixture.MakeACommit();

// ✅ succeeds as expected
fixture.AssertFullSemver($"0.0.{patchNumber}-beta.2+1", configuration);

fixture.MakeATaggedCommit($"0.0.{patchNumber}-beta.2");

// ✅ succeeds as expected
fixture.AssertFullSemver($"0.0.{patchNumber}-beta.2", configuration);

fixture.MakeACommit();

// ✅ succeeds as expected
fixture.AssertFullSemver($"0.0.{patchNumber}-beta.3+1", configuration);

fixture.ApplyTag($"0.0.{patchNumber}");

// ✅ succeeds as expected
fixture.AssertFullSemver($"0.0.{patchNumber}", configuration);

fixture.MakeACommit();

// ✅ succeeds as expected
fixture.AssertFullSemver($"0.0.{patchNumber + 1}+1", configuration);

fixture.Repository.DumpGraph();
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using GitVersion.Configuration;
using GitVersion.Core.Tests.Helpers;
using LibGit2Sharp;

Expand Down Expand Up @@ -73,27 +74,33 @@ public void WhenSupportIsBranchedAndTaggedFromAnotherSupportEnsureNewMinorIsUsed
[Test]
public void WhenSupportIsBranchedFromMainWithSpecificTag()
{
var configuration = GitFlowConfigurationBuilder.New.Build();

using var fixture = new EmptyRepositoryFixture();
fixture.Repository.MakeACommit();

fixture.MakeACommit();
fixture.AssertFullSemver("0.0.1+1");

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");
fixture.ApplyTag("1.4.0-rc");
fixture.MakeACommit();
fixture.BranchTo("support/1");

fixture.AssertFullSemver("1.4.0+1", configuration);
}

[Test]
public void WhenSupportIsBranchedFromMainWithSpecificTagOnCommit()
{
var configuration = GitFlowConfigurationBuilder.New.Build();

using var fixture = new EmptyRepositoryFixture();
fixture.Repository.MakeACommit();
fixture.AssertFullSemver("0.0.1+1");

fixture.Repository.ApplyTag("1.4.0-rc");
fixture.Repository.CreateBranch("support/1");
Commands.Checkout(fixture.Repository, "support/1");
fixture.AssertFullSemver("1.4.0");
fixture.MakeACommit();
fixture.AssertFullSemver("0.0.1+1", configuration);

fixture.ApplyTag("1.4.0-rc");
fixture.BranchTo("support/1");

fixture.AssertFullSemver("1.4.0+0", configuration);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace GitVersion.Core.Tests;
[TestFixture]
public class SemanticVersionTests : TestBase
{
[TestCase("1.2.3", 1, 2, 3, null, null, null, null, null, null, null, null, SemanticVersionFormat.Strict)]
[TestCase("1.2.3", 1, 2, 3, "", null, null, null, null, null, null, null, SemanticVersionFormat.Strict)]
[TestCase("1.2.3-beta", 1, 2, 3, "beta", null, null, null, null, null, null, null, SemanticVersionFormat.Strict)]
[TestCase("1.2.3-beta3", 1, 2, 3, "beta", 3, null, null, null, null, "1.2.3-beta.3", null, SemanticVersionFormat.Strict)]
[TestCase("1.2.3-beta.3", 1, 2, 3, "beta", 3, null, null, null, null, "1.2.3-beta.3", null, SemanticVersionFormat.Strict)]
Expand All @@ -19,25 +19,24 @@ public class SemanticVersionTests : TestBase
[TestCase("1.2.3-rc3.1", 1, 2, 3, "rc3", 1, null, null, null, null, "1.2.3-rc3.1", null, SemanticVersionFormat.Strict)]
[TestCase("1.2.3-beta3f", 1, 2, 3, "beta3f", null, null, null, null, null, null, null, SemanticVersionFormat.Strict)]
[TestCase("1.2.3-notAStability1", 1, 2, 3, "notAStability", 1, null, null, null, null, "1.2.3-notAStability.1", null, SemanticVersionFormat.Strict)]
[TestCase("1.2.3+4", 1, 2, 3, null, null, 4, null, null, null, null, null, SemanticVersionFormat.Strict)]
[TestCase("1.2.3+4.Branch.Foo", 1, 2, 3, null, null, 4, "Foo", null, null, null, null, SemanticVersionFormat.Strict)]
[TestCase("1.2.3+randomMetaData", 1, 2, 3, null, null, null, null, null, "randomMetaData", null, null, SemanticVersionFormat.Strict)]
[TestCase("1.2.3+4", 1, 2, 3, "", null, 4, null, null, null, null, null, SemanticVersionFormat.Strict)]
[TestCase("1.2.3+4.Branch.Foo", 1, 2, 3, "", null, 4, "Foo", null, null, null, null, SemanticVersionFormat.Strict)]
[TestCase("1.2.3+randomMetaData", 1, 2, 3, "", null, null, null, null, "randomMetaData", null, null, SemanticVersionFormat.Strict)]
[TestCase("1.2.3-beta.1+4.Sha.12234.Othershiz", 1, 2, 3, "beta", 1, 4, null, "12234", "Othershiz", null, null, SemanticVersionFormat.Strict)]
[TestCase("1.2.3", 1, 2, 3, null, null, null, null, null, null, null, ConfigurationConstants.DefaultLabelPrefix, SemanticVersionFormat.Strict)]
[TestCase("v1.2.3", 1, 2, 3, null, null, null, null, null, null, "1.2.3", ConfigurationConstants.DefaultLabelPrefix, SemanticVersionFormat.Strict)]
[TestCase("V1.2.3", 1, 2, 3, null, null, null, null, null, null, "1.2.3", ConfigurationConstants.DefaultLabelPrefix, SemanticVersionFormat.Strict)]
[TestCase("version-1.2.3", 1, 2, 3, null, null, null, null, null, null, "1.2.3", "version-", SemanticVersionFormat.Strict)]
[TestCase("1.2.3", 1, 2, 3, "", null, null, null, null, null, null, ConfigurationConstants.DefaultLabelPrefix, SemanticVersionFormat.Strict)]
[TestCase("v1.2.3", 1, 2, 3, "", null, null, null, null, null, "1.2.3", ConfigurationConstants.DefaultLabelPrefix, SemanticVersionFormat.Strict)]
[TestCase("V1.2.3", 1, 2, 3, "", null, null, null, null, null, "1.2.3", ConfigurationConstants.DefaultLabelPrefix, SemanticVersionFormat.Strict)]
[TestCase("version-1.2.3", 1, 2, 3, "", null, null, null, null, null, "1.2.3", "version-", SemanticVersionFormat.Strict)]
[TestCase("1.0.0-develop-20201007113711", 1, 0, 0, "develop-20201007113711", null, null, null, null, null, "1.0.0-develop-20201007113711", null, SemanticVersionFormat.Strict)]
[TestCase("20201007113711.658165168461351.64136516984163213-develop-20201007113711.98848747823+65416321321", 20201007113711, 658165168461351, 64136516984163213, "develop-20201007113711", 98848747823, 65416321321, null, null, null, "20201007113711.658165168461351.64136516984163213-develop-20201007113711.98848747823+65416321321", null, SemanticVersionFormat.Strict)]

[TestCase("1.2", 1, 2, 0, null, null, null, null, null, null, "1.2.0", null, SemanticVersionFormat.Loose)]
[TestCase("1.2", 1, 2, 0, "", null, null, null, null, null, "1.2.0", null, SemanticVersionFormat.Loose)]
[TestCase("1.2-alpha4", 1, 2, 0, "alpha", 4, null, null, null, null, "1.2.0-alpha.4", null, SemanticVersionFormat.Loose)]
[TestCase("01.02.03-rc03", 1, 2, 3, "rc", 3, null, null, null, null, "1.2.3-rc.3", null, SemanticVersionFormat.Loose)]
[TestCase("1.2.3.4", 1, 2, 3, null, null, 4, null, null, null, "1.2.3+4", null, SemanticVersionFormat.Loose)]
[TestCase("1", 1, 0, 0, null, null, null, null, null, null, "1.0.0", null, SemanticVersionFormat.Loose)]
[TestCase("1.1", 1, 1, 0, null, null, null, null, null, null, "1.1.0", null, SemanticVersionFormat.Loose)]
[TestCase("1.2.3.4", 1, 2, 3, "", null, 4, null, null, null, "1.2.3+4", null, SemanticVersionFormat.Loose)]
[TestCase("1", 1, 0, 0, "", null, null, null, null, null, "1.0.0", null, SemanticVersionFormat.Loose)]
[TestCase("1.1", 1, 1, 0, "", null, null, null, null, null, "1.1.0", null, SemanticVersionFormat.Loose)]
public void ValidateVersionParsing(
string? versionString, long major, long minor, long patch, string? tag, long? tagNumber, long? numberOfBuilds,
string? versionString, long major, long minor, long patch, string label, long? tagNumber, long? numberOfBuilds,
string? branchName, string? sha, string? otherMetaData, string? fullFormattedVersionString, string? tagPrefixRegex, SemanticVersionFormat format = SemanticVersionFormat.Strict)
{
fullFormattedVersionString ??= versionString;
Expand All @@ -51,7 +50,7 @@ public void ValidateVersionParsing(
Assert.That(version.Major, Is.EqualTo(major));
Assert.That(version.Minor, Is.EqualTo(minor));
Assert.That(version.Patch, Is.EqualTo(patch));
Assert.That(version.PreReleaseTag.Name, Is.EqualTo(tag));
Assert.That(version.PreReleaseTag.Name, Is.EqualTo(label));
Assert.That(version.PreReleaseTag.Number, Is.EqualTo(tagNumber));
Assert.That(version.BuildMetaData.CommitsSinceTag, Is.EqualTo(numberOfBuilds));
Expand Down
33 changes: 21 additions & 12 deletions src/GitVersion.Core/Configuration/ConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,41 @@ public static bool IsReleaseBranch(this IGitVersionConfiguration configuration,
public static bool IsReleaseBranch(this IGitVersionConfiguration configuration, ReferenceName branchName)
=> configuration.GetBranchConfiguration(branchName).IsReleaseBranch ?? false;

public static string GetBranchSpecificTag(this EffectiveConfiguration configuration, ILog log, string? branchFriendlyName,
string? branchNameOverride)
public static string? GetBranchSpecificLabel(
this EffectiveConfiguration configuration, ILog log, ReferenceName branchName, string? branchNameOverride)
=> GetBranchSpecificLabel(configuration, log, branchName.WithoutOrigin, branchNameOverride);

public static string? GetBranchSpecificLabel(
this EffectiveConfiguration configuration, ILog log, string? branchName, string? branchNameOverride)
{
var tagToUse = configuration.Label;
if (tagToUse == "useBranchName")
configuration.NotNull();

var label = configuration.Label;
if (label == "useBranchName")
{
tagToUse = ConfigurationConstants.BranchNamePlaceholder;
label = ConfigurationConstants.BranchNamePlaceholder;
}

if (tagToUse.Contains(ConfigurationConstants.BranchNamePlaceholder))
if (label?.Contains(ConfigurationConstants.BranchNamePlaceholder) == true)
{
log.Info("Using branch name to calculate version tag");

var branchName = branchNameOverride ?? branchFriendlyName;
var value = branchNameOverride ?? branchName;

if (!configuration.BranchPrefixToTrim.IsNullOrWhiteSpace())
{
var branchNameTrimmed = branchName?.RegexReplace(configuration.BranchPrefixToTrim, string.Empty, RegexOptions.IgnoreCase);
branchName = branchNameTrimmed.IsNullOrEmpty() ? branchName : branchNameTrimmed;
var branchNameTrimmed = value?.RegexReplace(
configuration.BranchPrefixToTrim, string.Empty, RegexOptions.IgnoreCase
);
value = branchNameTrimmed.IsNullOrEmpty() ? value : branchNameTrimmed;
}

branchName = branchName?.RegexReplace("[^a-zA-Z0-9-]", "-");
value = value?.RegexReplace("[^a-zA-Z0-9-]", "-");

tagToUse = tagToUse.Replace(ConfigurationConstants.BranchNamePlaceholder, branchName);
label = label.Replace(ConfigurationConstants.BranchNamePlaceholder, value);
}

return tagToUse;
return label;
}

public static (string GitDirectory, string WorkingTreeDirectory)? FindGitDir(this string path)
Expand Down
4 changes: 2 additions & 2 deletions src/GitVersion.Core/Configuration/EffectiveConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public EffectiveConfiguration(IGitVersionConfiguration configuration, IBranchCon
AssemblyFileVersioningFormat = configuration.AssemblyFileVersioningFormat;
VersioningMode = branchConfiguration.VersioningMode.Value;
LabelPrefix = configuration.LabelPrefix;
Label = branchConfiguration.Label ?? string.Empty;
Label = branchConfiguration.Label;
NextVersion = configuration.NextVersion;
Increment = branchConfiguration.Increment;
BranchPrefixToTrim = branchConfiguration.RegularExpression;
Expand Down Expand Up @@ -140,7 +140,7 @@ protected EffectiveConfiguration(AssemblyVersioningScheme assemblyVersioningSche
/// <summary>
/// Label to use when calculating SemVer
/// </summary>
public string Label { get; }
public string? Label { get; }

public string? NextVersion { get; }

Expand Down
Loading

0 comments on commit bbf28be

Please sign in to comment.