Skip to content
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
23 changes: 23 additions & 0 deletions GitIntegration.Test/Parsing/GitVersionParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,29 @@ public void ParsesTheWindowsBuildSuffix()
Assert.AreEqual("2.50.1.windows.1", version.Raw);
}

[TestMethod]
public void ParsesApplesBuildNoteAfterThePatchComponent()
{
// The stock git on macOS and GitHub's macOS runners. The note follows the patch number
// with a space, so the third component is "3 (Apple Git-145)", not "3".
GitVersion version = GitVersionParser.Parse("git version 2.39.3 (Apple Git-145)\n");

Assert.AreEqual(2, version.Major);
Assert.AreEqual(39, version.Minor);
Assert.AreEqual(3, version.Patch);
Assert.AreEqual("2.39.3 (Apple Git-145)", version.Raw);
}

[TestMethod]
public void ParsesAReleaseCandidateSuffixOnThePatchComponent()
{
GitVersion version = GitVersionParser.Parse("git version 2.41.1-rc0\n");

Assert.AreEqual(2, version.Major);
Assert.AreEqual(41, version.Minor);
Assert.AreEqual(1, version.Patch);
}

[TestMethod]
public void ParsesAVersionWithNoPatchComponent()
{
Expand Down
23 changes: 20 additions & 3 deletions GitIntegration/Parsing/GitVersionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,26 @@ internal static GitVersion Parse(string output)
};
}

private static int ReadComponent(string[] components, int index) =>
index < components.Length &&
int.TryParse(components[index], NumberStyles.None, CultureInfo.InvariantCulture, out int value)
// Reads the component's leading digits, so a build note glued to the last component still
// leaves its number: Apple's git reports "2.39.3 (Apple Git-145)", whose third component is
// "3 (Apple Git-145)", and an rc build reports "0-rc0". A component with no leading digit,
// such as "windows", reads as zero.
private static int ReadComponent(string[] components, int index)
{
if (index >= components.Length)
{
return 0;
}

string component = components[index];
int length = 0;
while (length < component.Length && char.IsAsciiDigit(component[length]))
{
length++;
}

return int.TryParse(component.AsSpan(0, length), NumberStyles.None, CultureInfo.InvariantCulture, out int value)
? value
: 0;
}
}
Loading