Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't require a trailing \n character at the end of the alternates file #600

Merged
merged 1 commit into from May 4, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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