Skip to content

Commit 8c11e8d

Browse files
authored
Merge pull request #600 from qmfrederik/fixes/alternate
Don't require a trailing `\n` character at the end of the alternates file
2 parents 2363be0 + 62aadee commit 8c11e8d

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/NerdBank.GitVersioning.Tests/ManagedGit/GitRepositoryTests.cs

+9
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,15 @@ public void ParseAlternates_SingleValue_Test()
295295
a => Assert.Equal("/home/git/nbgv/.git/objects", a));
296296
}
297297

298+
[Fact]
299+
public void ParseAlternates_SingleValue_NoTrailingNewline_Test()
300+
{
301+
var alternates = GitRepository.ParseAlternates(Encoding.UTF8.GetBytes("../repo/.git/objects"));
302+
Assert.Collection(
303+
alternates,
304+
a => Assert.Equal("../repo/.git/objects", a));
305+
}
306+
298307
[Fact]
299308
public void ParseAlternates_TwoValues_Test()
300309
{

src/NerdBank.GitVersioning/ManagedGit/GitRepository.cs

+7-3
Original file line numberDiff line numberDiff line change
@@ -751,14 +751,18 @@ public static List<string> ParseAlternates(ReadOnlySpan<byte> alternates, int sk
751751
List<string> values = new List<string>();
752752

753753
int index;
754+
int length;
754755

755756
// The alternates path is colon (:)-separated. On Windows, there may be full paths, such as
756757
// C:/Users/username/source/repos/nbgv/.git, which also contain a colon. Because the colon
757758
// can only appear at the second position, we skip the first two characters (e.g. C:) on Windows.
758-
while (alternates.Length > skipCount && (index = alternates.Slice(skipCount).IndexOfAny((byte)':', (byte)'\n')) > 0)
759+
while (alternates.Length > skipCount)
759760
{
760-
values.Add(GetString(alternates.Slice(0, skipCount + index)));
761-
alternates = alternates.Slice(skipCount + index + 1);
761+
index = alternates.Slice(skipCount).IndexOfAny((byte)':', (byte)'\n');
762+
length = index > 0 ? skipCount + index : alternates.Length;
763+
764+
values.Add(GetString(alternates.Slice(0, length)));
765+
alternates = index > 0 ? alternates.Slice(length + 1) : Span<byte>.Empty;
762766
}
763767

764768
return values;

0 commit comments

Comments
 (0)