From 62aadee1bc2847f48ef95a0c044705a0fc82441f Mon Sep 17 00:00:00 2001 From: Frederik Carlier Date: Tue, 4 May 2021 17:45:01 +0200 Subject: [PATCH] Don't require a trailing `\n` character at the end of the alternates file --- .../ManagedGit/GitRepositoryTests.cs | 9 +++++++++ src/NerdBank.GitVersioning/ManagedGit/GitRepository.cs | 10 +++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/NerdBank.GitVersioning.Tests/ManagedGit/GitRepositoryTests.cs b/src/NerdBank.GitVersioning.Tests/ManagedGit/GitRepositoryTests.cs index 82174ee9..edbbbb80 100644 --- a/src/NerdBank.GitVersioning.Tests/ManagedGit/GitRepositoryTests.cs +++ b/src/NerdBank.GitVersioning.Tests/ManagedGit/GitRepositoryTests.cs @@ -295,6 +295,15 @@ public void ParseAlternates_SingleValue_Test() a => Assert.Equal("/home/git/nbgv/.git/objects", a)); } + [Fact] + public void ParseAlternates_SingleValue_NoTrailingNewline_Test() + { + var alternates = GitRepository.ParseAlternates(Encoding.UTF8.GetBytes("../repo/.git/objects")); + Assert.Collection( + alternates, + a => Assert.Equal("../repo/.git/objects", a)); + } + [Fact] public void ParseAlternates_TwoValues_Test() { diff --git a/src/NerdBank.GitVersioning/ManagedGit/GitRepository.cs b/src/NerdBank.GitVersioning/ManagedGit/GitRepository.cs index a2346b8c..40cfc644 100644 --- a/src/NerdBank.GitVersioning/ManagedGit/GitRepository.cs +++ b/src/NerdBank.GitVersioning/ManagedGit/GitRepository.cs @@ -751,14 +751,18 @@ public static List ParseAlternates(ReadOnlySpan alternates, int sk List values = new List(); int index; + int length; // The alternates path is colon (:)-separated. On Windows, there may be full paths, such as // C:/Users/username/source/repos/nbgv/.git, which also contain a colon. Because the colon // can only appear at the second position, we skip the first two characters (e.g. C:) on Windows. - while (alternates.Length > skipCount && (index = alternates.Slice(skipCount).IndexOfAny((byte)':', (byte)'\n')) > 0) + while (alternates.Length > skipCount) { - values.Add(GetString(alternates.Slice(0, skipCount + index))); - alternates = alternates.Slice(skipCount + index + 1); + index = alternates.Slice(skipCount).IndexOfAny((byte)':', (byte)'\n'); + length = index > 0 ? skipCount + index : alternates.Length; + + values.Add(GetString(alternates.Slice(0, length))); + alternates = index > 0 ? alternates.Slice(length + 1) : Span.Empty; } return values;