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
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
10 changes: 7 additions & 3 deletions src/NerdBank.GitVersioning/ManagedGit/GitRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -751,14 +751,18 @@ public static List<string> ParseAlternates(ReadOnlySpan<byte> alternates, int sk
List<string> values = new List<string>();

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<byte>.Empty;
}

return values;
Expand Down