Skip to content

Commit a97cb87

Browse files
authored
Merge pull request #2249 from carlos-vicente/allow-for-numeric-pre-release-tags
Allow for numeric pre release tags
2 parents a6fb735 + fc4f55a commit a97cb87

File tree

5 files changed

+55
-4
lines changed

5 files changed

+55
-4
lines changed

src/GitVersionCore.Tests/IntegrationTests/OtherScenarios.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void AllowHavingMainInsteadOfMaster()
7878
Commands.Checkout(fixture.Repository, fixture.Repository.CreateBranch("main"));
7979
fixture.Repository.Branches.Remove(fixture.Repository.Branches["master"]);
8080

81-
fixture.AssertFullSemver("0.1.0+0", config);
81+
fixture.AssertFullSemver("0.1.0-1+0", config);
8282
}
8383

8484
[Test]

src/GitVersionCore.Tests/IntegrationTests/ReleaseBranchScenarios.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ public void CanTakeVersionFromReleasesBranch()
8484
fixture.AssertFullSemver("2.0.0-beta.1+2");
8585
}
8686

87+
[Test]
88+
public void CanTakePreReleaseVersionFromReleasesBranchWithNumericPreReleaseTag()
89+
{
90+
using var fixture = new EmptyRepositoryFixture();
91+
fixture.Repository.MakeCommits(5);
92+
fixture.Repository.CreateBranch("releases/2.0.0");
93+
fixture.Checkout("releases/2.0.0");
94+
fixture.Repository.ApplyTag("v2.0.0-1");
95+
96+
var variables = fixture.GetVersion();
97+
Assert.AreEqual("2.0.0-1", variables.FullSemVer);
98+
}
99+
87100
[Test]
88101
public void ReleaseBranchWithNextVersionSetInConfig()
89102
{

src/GitVersionCore.Tests/VersionCalculation/SemanticVersionTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class SemanticVersionTests : TestBase
2020
[TestCase("1.2-alpha4", 1, 2, 0, "alpha", 4, null, null, null, null, "1.2.0-alpha.4", null)]
2121
[TestCase("1.2.3-rc", 1, 2, 3, "rc", null, null, null, null, null, null, null)]
2222
[TestCase("1.2.3-rc3", 1, 2, 3, "rc", 3, null, null, null, null, "1.2.3-rc.3", null)]
23+
[TestCase("1.2.3-3", 1, 2, 3, "", 3, null, null, null, null, "1.2.3-3", null)]
2324
[TestCase("1.2.3-RC3", 1, 2, 3, "RC", 3, null, null, null, null, "1.2.3-RC.3", null)]
2425
[TestCase("1.2.3-rc3.1", 1, 2, 3, "rc3", 1, null, null, null, null, "1.2.3-rc3.1", null)]
2526
[TestCase("01.02.03-rc03", 1, 2, 3, "rc", 3, null, null, null, null, "1.2.3-rc.3", null)]
@@ -119,6 +120,21 @@ public void ToStringSTests()
119120
}
120121
};
121122
Assert.AreEqual("1.2.3-beta.4", fullSemVer.ToString("s"));
123+
var fullSemVerNoPreReleaseName = new SemanticVersion
124+
{
125+
Major = 1,
126+
Minor = 2,
127+
Patch = 3,
128+
PreReleaseTag = new SemanticVersionPreReleaseTag("", 4),
129+
BuildMetaData = new SemanticVersionBuildMetaData
130+
{
131+
Sha = "theSha",
132+
Branch = "TheBranch",
133+
CommitsSinceTag = 5,
134+
OtherMetaData = "TheOtherMetaData"
135+
}
136+
};
137+
Assert.AreEqual("1.2.3-4", fullSemVerNoPreReleaseName.ToString("s"));
122138
}
123139
[Test]
124140
public void ToStringLTests()
@@ -183,6 +199,21 @@ public void ToStringTests()
183199
}
184200
};
185201
Assert.AreEqual("1.2.3-beta.4", fullSemVer.ToString());
202+
var fullSemVerNoPreReleaseName = new SemanticVersion
203+
{
204+
Major = 1,
205+
Minor = 2,
206+
Patch = 3,
207+
PreReleaseTag = new SemanticVersionPreReleaseTag("", 4),
208+
BuildMetaData = new SemanticVersionBuildMetaData
209+
{
210+
Sha = "theSha",
211+
Branch = "TheBranch",
212+
CommitsSinceTag = 5,
213+
OtherMetaData = "TheOtherMetaData"
214+
}
215+
};
216+
Assert.AreEqual("1.2.3-4", fullSemVerNoPreReleaseName.ToString());
186217
}
187218
[Test]
188219
public void ToStringFTests()

src/GitVersionCore/VersionCalculation/SemanticVersioning/SemanticVersionPreReleaseTag.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ public SemanticVersionPreReleaseTag(SemanticVersionPreReleaseTag preReleaseTag)
2525
{
2626
Name = preReleaseTag.Name;
2727
Number = preReleaseTag.Number;
28+
PromotedFromCommits = preReleaseTag.PromotedFromCommits;
2829
}
2930

3031
public string Name { get; set; }
3132
public int? Number { get; set; }
33+
public bool PromotedFromCommits { get; set; }
3234

3335
public override bool Equals(object obj)
3436
{
@@ -168,15 +170,15 @@ public string ToString(string format, IFormatProvider formatProvider = null)
168170

169171
return format switch
170172
{
171-
"t" => (Number.HasValue ? $"{Name}.{Number}" : Name),
173+
"t" => (Number.HasValue ? string.IsNullOrEmpty(Name) ? $"{Number}" : $"{Name}.{Number}" : Name),
172174
"l" => (Number.HasValue ? FormatLegacy(GetLegacyName(), Number.Value.ToString()) : FormatLegacy(GetLegacyName())),
173175
_ => throw new ArgumentException("Unknown format", nameof(format))
174176
};
175177
}
176178

177179
private string FormatLegacy(string tag, string number = "")
178180
{
179-
var tagEndsWithANumber = char.IsNumber(tag.Last());
181+
var tagEndsWithANumber = char.IsNumber(tag.LastOrDefault());
180182
if (tagEndsWithANumber && number.Length > 0)
181183
number = "-" + number;
182184

@@ -188,13 +190,17 @@ private string FormatLegacy(string tag, string number = "")
188190

189191
private string GetLegacyName()
190192
{
193+
if (string.IsNullOrEmpty(Name))
194+
{
195+
return string.Empty;
196+
}
191197
var firstPart = Name.Split('_')[0];
192198
return firstPart.Replace(".", string.Empty);
193199
}
194200

195201
public bool HasTag()
196202
{
197-
return !string.IsNullOrEmpty(Name);
203+
return !string.IsNullOrEmpty(Name) || (Number.HasValue && !PromotedFromCommits);
198204
}
199205
}
200206
}

src/GitVersionCore/VersionCalculation/VariableProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ private static void PromoteNumberOfCommitsToTagNumber(SemanticVersion semanticVe
116116
else
117117
{
118118
semanticVersion.PreReleaseTag.Number = semanticVersion.BuildMetaData.CommitsSinceTag;
119+
semanticVersion.PreReleaseTag.PromotedFromCommits = true;
119120
}
120121
semanticVersion.BuildMetaData.CommitsSinceVersionSource = semanticVersion.BuildMetaData.CommitsSinceTag.Value;
121122
semanticVersion.BuildMetaData.CommitsSinceTag = null; // why is this set to null ?

0 commit comments

Comments
 (0)