Skip to content

Commit

Permalink
Merge pull request #600 from qmfrederik/fixes/alternate
Browse files Browse the repository at this point in the history
Don't require a trailing `\n` character at the end of the alternates file
  • Loading branch information
AArnott committed May 4, 2021
2 parents 2363be0 + 62aadee commit 8c11e8d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
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

0 comments on commit 8c11e8d

Please sign in to comment.