Skip to content

Commit bbf28be

Browse files
authored
Merge pull request #3437 from HHobeck/feature/3436_GitVersion_behaves_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
2 parents fd21d84 + 2402ea8 commit bbf28be

File tree

11 files changed

+204
-133
lines changed

11 files changed

+204
-133
lines changed

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

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public void NoDirtyFlagInCleanRepository()
131131

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

163163
fixture.Repository.DumpGraph();
164164
}
165+
166+
[TestCase(1)]
167+
[TestCase(2)]
168+
public void EnsurePreReleaseTagLabelWillBeConsideredIfNoLabelIsDefined(long patchNumber)
169+
{
170+
var configuration = GitHubFlowConfigurationBuilder.New
171+
.WithLabel(null)
172+
.WithBranch("main", branchBuilder => branchBuilder
173+
.WithLabel(null).WithIncrement(IncrementStrategy.Patch)
174+
).Build();
175+
176+
using var fixture = new EmptyRepositoryFixture("main");
177+
178+
fixture.MakeACommit();
179+
180+
// ✅ succeeds as expected
181+
fixture.AssertFullSemver("0.0.1+1", configuration);
182+
183+
fixture.ApplyTag($"0.0.{patchNumber}-alpha.1");
184+
185+
// ✅ succeeds as expected
186+
fixture.AssertFullSemver($"0.0.{patchNumber}-alpha.1", configuration);
187+
188+
fixture.MakeACommit();
189+
190+
// ✅ succeeds as expected
191+
fixture.AssertFullSemver($"0.0.{patchNumber}-alpha.2+1", configuration);
192+
193+
fixture.MakeACommit();
194+
195+
// ✅ succeeds as expected
196+
fixture.AssertFullSemver($"0.0.{patchNumber}-alpha.2+2", configuration);
197+
198+
fixture.MakeATaggedCommit($"0.0.{patchNumber}-beta.1");
199+
200+
// ✅ succeeds as expected
201+
fixture.AssertFullSemver($"0.0.{patchNumber}-beta.1", configuration);
202+
203+
fixture.MakeACommit();
204+
205+
// ✅ succeeds as expected
206+
fixture.AssertFullSemver($"0.0.{patchNumber}-beta.2+1", configuration);
207+
208+
fixture.MakeATaggedCommit($"0.0.{patchNumber}-beta.2");
209+
210+
// ✅ succeeds as expected
211+
fixture.AssertFullSemver($"0.0.{patchNumber}-beta.2", configuration);
212+
213+
fixture.MakeACommit();
214+
215+
// ✅ succeeds as expected
216+
fixture.AssertFullSemver($"0.0.{patchNumber}-beta.3+1", configuration);
217+
218+
fixture.ApplyTag($"0.0.{patchNumber}");
219+
220+
// ✅ succeeds as expected
221+
fixture.AssertFullSemver($"0.0.{patchNumber}", configuration);
222+
223+
fixture.MakeACommit();
224+
225+
// ✅ succeeds as expected
226+
fixture.AssertFullSemver($"0.0.{patchNumber + 1}+1", configuration);
227+
228+
fixture.Repository.DumpGraph();
229+
}
165230
}

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

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using GitVersion.Configuration;
12
using GitVersion.Core.Tests.Helpers;
23
using LibGit2Sharp;
34

@@ -73,27 +74,33 @@ public void WhenSupportIsBranchedAndTaggedFromAnotherSupportEnsureNewMinorIsUsed
7374
[Test]
7475
public void WhenSupportIsBranchedFromMainWithSpecificTag()
7576
{
77+
var configuration = GitFlowConfigurationBuilder.New.Build();
78+
7679
using var fixture = new EmptyRepositoryFixture();
77-
fixture.Repository.MakeACommit();
80+
81+
fixture.MakeACommit();
7882
fixture.AssertFullSemver("0.0.1+1");
7983

80-
fixture.Repository.ApplyTag("1.4.0-rc");
81-
fixture.Repository.MakeACommit();
82-
fixture.Repository.CreateBranch("support/1");
83-
Commands.Checkout(fixture.Repository, "support/1");
84-
fixture.AssertFullSemver("1.4.0+1");
84+
fixture.ApplyTag("1.4.0-rc");
85+
fixture.MakeACommit();
86+
fixture.BranchTo("support/1");
87+
88+
fixture.AssertFullSemver("1.4.0+1", configuration);
8589
}
8690

8791
[Test]
8892
public void WhenSupportIsBranchedFromMainWithSpecificTagOnCommit()
8993
{
94+
var configuration = GitFlowConfigurationBuilder.New.Build();
95+
9096
using var fixture = new EmptyRepositoryFixture();
91-
fixture.Repository.MakeACommit();
92-
fixture.AssertFullSemver("0.0.1+1");
9397

94-
fixture.Repository.ApplyTag("1.4.0-rc");
95-
fixture.Repository.CreateBranch("support/1");
96-
Commands.Checkout(fixture.Repository, "support/1");
97-
fixture.AssertFullSemver("1.4.0");
98+
fixture.MakeACommit();
99+
fixture.AssertFullSemver("0.0.1+1", configuration);
100+
101+
fixture.ApplyTag("1.4.0-rc");
102+
fixture.BranchTo("support/1");
103+
104+
fixture.AssertFullSemver("1.4.0+0", configuration);
98105
}
99106
}

src/GitVersion.Core.Tests/VersionCalculation/SemanticVersionTests.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace GitVersion.Core.Tests;
66
[TestFixture]
77
public class SemanticVersionTests : TestBase
88
{
9-
[TestCase("1.2.3", 1, 2, 3, null, null, null, null, null, null, null, null, SemanticVersionFormat.Strict)]
9+
[TestCase("1.2.3", 1, 2, 3, "", null, null, null, null, null, null, null, SemanticVersionFormat.Strict)]
1010
[TestCase("1.2.3-beta", 1, 2, 3, "beta", null, null, null, null, null, null, null, SemanticVersionFormat.Strict)]
1111
[TestCase("1.2.3-beta3", 1, 2, 3, "beta", 3, null, null, null, null, "1.2.3-beta.3", null, SemanticVersionFormat.Strict)]
1212
[TestCase("1.2.3-beta.3", 1, 2, 3, "beta", 3, null, null, null, null, "1.2.3-beta.3", null, SemanticVersionFormat.Strict)]
@@ -19,25 +19,24 @@ public class SemanticVersionTests : TestBase
1919
[TestCase("1.2.3-rc3.1", 1, 2, 3, "rc3", 1, null, null, null, null, "1.2.3-rc3.1", null, SemanticVersionFormat.Strict)]
2020
[TestCase("1.2.3-beta3f", 1, 2, 3, "beta3f", null, null, null, null, null, null, null, SemanticVersionFormat.Strict)]
2121
[TestCase("1.2.3-notAStability1", 1, 2, 3, "notAStability", 1, null, null, null, null, "1.2.3-notAStability.1", null, SemanticVersionFormat.Strict)]
22-
[TestCase("1.2.3+4", 1, 2, 3, null, null, 4, null, null, null, null, null, SemanticVersionFormat.Strict)]
23-
[TestCase("1.2.3+4.Branch.Foo", 1, 2, 3, null, null, 4, "Foo", null, null, null, null, SemanticVersionFormat.Strict)]
24-
[TestCase("1.2.3+randomMetaData", 1, 2, 3, null, null, null, null, null, "randomMetaData", null, null, SemanticVersionFormat.Strict)]
22+
[TestCase("1.2.3+4", 1, 2, 3, "", null, 4, null, null, null, null, null, SemanticVersionFormat.Strict)]
23+
[TestCase("1.2.3+4.Branch.Foo", 1, 2, 3, "", null, 4, "Foo", null, null, null, null, SemanticVersionFormat.Strict)]
24+
[TestCase("1.2.3+randomMetaData", 1, 2, 3, "", null, null, null, null, "randomMetaData", null, null, SemanticVersionFormat.Strict)]
2525
[TestCase("1.2.3-beta.1+4.Sha.12234.Othershiz", 1, 2, 3, "beta", 1, 4, null, "12234", "Othershiz", null, null, SemanticVersionFormat.Strict)]
26-
[TestCase("1.2.3", 1, 2, 3, null, null, null, null, null, null, null, ConfigurationConstants.DefaultLabelPrefix, SemanticVersionFormat.Strict)]
27-
[TestCase("v1.2.3", 1, 2, 3, null, null, null, null, null, null, "1.2.3", ConfigurationConstants.DefaultLabelPrefix, SemanticVersionFormat.Strict)]
28-
[TestCase("V1.2.3", 1, 2, 3, null, null, null, null, null, null, "1.2.3", ConfigurationConstants.DefaultLabelPrefix, SemanticVersionFormat.Strict)]
29-
[TestCase("version-1.2.3", 1, 2, 3, null, null, null, null, null, null, "1.2.3", "version-", SemanticVersionFormat.Strict)]
26+
[TestCase("1.2.3", 1, 2, 3, "", null, null, null, null, null, null, ConfigurationConstants.DefaultLabelPrefix, SemanticVersionFormat.Strict)]
27+
[TestCase("v1.2.3", 1, 2, 3, "", null, null, null, null, null, "1.2.3", ConfigurationConstants.DefaultLabelPrefix, SemanticVersionFormat.Strict)]
28+
[TestCase("V1.2.3", 1, 2, 3, "", null, null, null, null, null, "1.2.3", ConfigurationConstants.DefaultLabelPrefix, SemanticVersionFormat.Strict)]
29+
[TestCase("version-1.2.3", 1, 2, 3, "", null, null, null, null, null, "1.2.3", "version-", SemanticVersionFormat.Strict)]
3030
[TestCase("1.0.0-develop-20201007113711", 1, 0, 0, "develop-20201007113711", null, null, null, null, null, "1.0.0-develop-20201007113711", null, SemanticVersionFormat.Strict)]
3131
[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)]
32-
33-
[TestCase("1.2", 1, 2, 0, null, null, null, null, null, null, "1.2.0", null, SemanticVersionFormat.Loose)]
32+
[TestCase("1.2", 1, 2, 0, "", null, null, null, null, null, "1.2.0", null, SemanticVersionFormat.Loose)]
3433
[TestCase("1.2-alpha4", 1, 2, 0, "alpha", 4, null, null, null, null, "1.2.0-alpha.4", null, SemanticVersionFormat.Loose)]
3534
[TestCase("01.02.03-rc03", 1, 2, 3, "rc", 3, null, null, null, null, "1.2.3-rc.3", null, SemanticVersionFormat.Loose)]
36-
[TestCase("1.2.3.4", 1, 2, 3, null, null, 4, null, null, null, "1.2.3+4", null, SemanticVersionFormat.Loose)]
37-
[TestCase("1", 1, 0, 0, null, null, null, null, null, null, "1.0.0", null, SemanticVersionFormat.Loose)]
38-
[TestCase("1.1", 1, 1, 0, null, null, null, null, null, null, "1.1.0", null, SemanticVersionFormat.Loose)]
35+
[TestCase("1.2.3.4", 1, 2, 3, "", null, 4, null, null, null, "1.2.3+4", null, SemanticVersionFormat.Loose)]
36+
[TestCase("1", 1, 0, 0, "", null, null, null, null, null, "1.0.0", null, SemanticVersionFormat.Loose)]
37+
[TestCase("1.1", 1, 1, 0, "", null, null, null, null, null, "1.1.0", null, SemanticVersionFormat.Loose)]
3938
public void ValidateVersionParsing(
40-
string? versionString, long major, long minor, long patch, string? tag, long? tagNumber, long? numberOfBuilds,
39+
string? versionString, long major, long minor, long patch, string label, long? tagNumber, long? numberOfBuilds,
4140
string? branchName, string? sha, string? otherMetaData, string? fullFormattedVersionString, string? tagPrefixRegex, SemanticVersionFormat format = SemanticVersionFormat.Strict)
4241
{
4342
fullFormattedVersionString ??= versionString;
@@ -51,7 +50,7 @@ public void ValidateVersionParsing(
5150
Assert.That(version.Major, Is.EqualTo(major));
5251
Assert.That(version.Minor, Is.EqualTo(minor));
5352
Assert.That(version.Patch, Is.EqualTo(patch));
54-
Assert.That(version.PreReleaseTag.Name, Is.EqualTo(tag));
53+
Assert.That(version.PreReleaseTag.Name, Is.EqualTo(label));
5554
Assert.That(version.PreReleaseTag.Number, Is.EqualTo(tagNumber));
5655

5756
Assert.That(version.BuildMetaData.CommitsSinceTag, Is.EqualTo(numberOfBuilds));

src/GitVersion.Core/Configuration/ConfigurationExtensions.cs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,32 +59,41 @@ public static bool IsReleaseBranch(this IGitVersionConfiguration configuration,
5959
public static bool IsReleaseBranch(this IGitVersionConfiguration configuration, ReferenceName branchName)
6060
=> configuration.GetBranchConfiguration(branchName).IsReleaseBranch ?? false;
6161

62-
public static string GetBranchSpecificTag(this EffectiveConfiguration configuration, ILog log, string? branchFriendlyName,
63-
string? branchNameOverride)
62+
public static string? GetBranchSpecificLabel(
63+
this EffectiveConfiguration configuration, ILog log, ReferenceName branchName, string? branchNameOverride)
64+
=> GetBranchSpecificLabel(configuration, log, branchName.WithoutOrigin, branchNameOverride);
65+
66+
public static string? GetBranchSpecificLabel(
67+
this EffectiveConfiguration configuration, ILog log, string? branchName, string? branchNameOverride)
6468
{
65-
var tagToUse = configuration.Label;
66-
if (tagToUse == "useBranchName")
69+
configuration.NotNull();
70+
71+
var label = configuration.Label;
72+
if (label == "useBranchName")
6773
{
68-
tagToUse = ConfigurationConstants.BranchNamePlaceholder;
74+
label = ConfigurationConstants.BranchNamePlaceholder;
6975
}
7076

71-
if (tagToUse.Contains(ConfigurationConstants.BranchNamePlaceholder))
77+
if (label?.Contains(ConfigurationConstants.BranchNamePlaceholder) == true)
7278
{
7379
log.Info("Using branch name to calculate version tag");
7480

75-
var branchName = branchNameOverride ?? branchFriendlyName;
81+
var value = branchNameOverride ?? branchName;
82+
7683
if (!configuration.BranchPrefixToTrim.IsNullOrWhiteSpace())
7784
{
78-
var branchNameTrimmed = branchName?.RegexReplace(configuration.BranchPrefixToTrim, string.Empty, RegexOptions.IgnoreCase);
79-
branchName = branchNameTrimmed.IsNullOrEmpty() ? branchName : branchNameTrimmed;
85+
var branchNameTrimmed = value?.RegexReplace(
86+
configuration.BranchPrefixToTrim, string.Empty, RegexOptions.IgnoreCase
87+
);
88+
value = branchNameTrimmed.IsNullOrEmpty() ? value : branchNameTrimmed;
8089
}
8190

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

84-
tagToUse = tagToUse.Replace(ConfigurationConstants.BranchNamePlaceholder, branchName);
93+
label = label.Replace(ConfigurationConstants.BranchNamePlaceholder, value);
8594
}
8695

87-
return tagToUse;
96+
return label;
8897
}
8998

9099
public static (string GitDirectory, string WorkingTreeDirectory)? FindGitDir(this string path)

src/GitVersion.Core/Configuration/EffectiveConfiguration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public EffectiveConfiguration(IGitVersionConfiguration configuration, IBranchCon
3939
AssemblyFileVersioningFormat = configuration.AssemblyFileVersioningFormat;
4040
VersioningMode = branchConfiguration.VersioningMode.Value;
4141
LabelPrefix = configuration.LabelPrefix;
42-
Label = branchConfiguration.Label ?? string.Empty;
42+
Label = branchConfiguration.Label;
4343
NextVersion = configuration.NextVersion;
4444
Increment = branchConfiguration.Increment;
4545
BranchPrefixToTrim = branchConfiguration.RegularExpression;
@@ -140,7 +140,7 @@ protected EffectiveConfiguration(AssemblyVersioningScheme assemblyVersioningSche
140140
/// <summary>
141141
/// Label to use when calculating SemVer
142142
/// </summary>
143-
public string Label { get; }
143+
public string? Label { get; }
144144

145145
public string? NextVersion { get; }
146146

0 commit comments

Comments
 (0)