From dd010e7c862a9cab6d2c491d491e5a9d7952062b Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 9 Oct 2026 01:27:19 +0000 Subject: [PATCH] Read the patch number before Apple's build note in git --version [patch] "git version 2.39.3 (Apple Git-145)" split into a third component of "3 (Apple Git-145)", which failed to parse whole and fell back to 0, so GetVersionAsync reported 2.39.0 on macOS. Each component now reads its leading digits, which also keeps the patch of an rc build ("1-rc0"). Fixes ktsu-dev/GitIntegration#158 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Rt1SUkG3bsTZEWbmGirSx3 --- .../Parsing/GitVersionParserTests.cs | 23 +++++++++++++++++++ GitIntegration/Parsing/GitVersionParser.cs | 23 ++++++++++++++++--- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/GitIntegration.Test/Parsing/GitVersionParserTests.cs b/GitIntegration.Test/Parsing/GitVersionParserTests.cs index 028721e..752f533 100644 --- a/GitIntegration.Test/Parsing/GitVersionParserTests.cs +++ b/GitIntegration.Test/Parsing/GitVersionParserTests.cs @@ -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() { diff --git a/GitIntegration/Parsing/GitVersionParser.cs b/GitIntegration/Parsing/GitVersionParser.cs index 17f5fbf..f6e5904 100644 --- a/GitIntegration/Parsing/GitVersionParser.cs +++ b/GitIntegration/Parsing/GitVersionParser.cs @@ -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; + } }