Skip to content

Commit aa760b3

Browse files
Match pre release tag formatting to parsing
Pre release tag parsing correctly identifies when there is no name, but there is a number, this changes the formatting method and tag verification so that a single pre release number is also considered
1 parent 65baddc commit aa760b3

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,15 @@ public string ToString(string format, IFormatProvider formatProvider = null)
168168

169169
return format switch
170170
{
171-
"t" => (Number.HasValue ? $"{Name}.{Number}" : Name),
171+
"t" => (Number.HasValue ? string.IsNullOrEmpty(Name) ? $"{Number}" : $"{Name}.{Number}" : Name),
172172
"l" => (Number.HasValue ? FormatLegacy(GetLegacyName(), Number.Value.ToString()) : FormatLegacy(GetLegacyName())),
173173
_ => throw new ArgumentException("Unknown format", nameof(format))
174174
};
175175
}
176176

177177
private string FormatLegacy(string tag, string number = "")
178178
{
179-
var tagEndsWithANumber = char.IsNumber(tag.Last());
179+
var tagEndsWithANumber = char.IsNumber(tag.LastOrDefault());
180180
if (tagEndsWithANumber && number.Length > 0)
181181
number = "-" + number;
182182

@@ -194,7 +194,7 @@ private string GetLegacyName()
194194

195195
public bool HasTag()
196196
{
197-
return !string.IsNullOrEmpty(Name);
197+
return !string.IsNullOrEmpty(Name) || Number.HasValue;
198198
}
199199
}
200200
}

0 commit comments

Comments
 (0)