Skip to content

Commit

Permalink
Add unit tests for alternates with colons in their path (e.g. C:/Users/)
Browse files Browse the repository at this point in the history
  • Loading branch information
qmfrederik committed Apr 30, 2021
1 parent cb37baa commit c311a3f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
13 changes: 13 additions & 0 deletions src/NerdBank.GitVersioning.Tests/ManagedGit/GitRepositoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,19 @@ public void ParseAlternates_TwoValues_Test()
a => Assert.Equal("../../clone/.git/objects", a));
}

[Fact]
public void ParseAlternates_PathWithColon_Test()
{
var alternates = GitRepository.ParseAlternates(
Encoding.UTF8.GetBytes("C:/Users/nbgv/objects:C:/Users/nbgv2/objects/:../../clone/.git/objects\n"),
2);
Assert.Collection(
alternates,
a => Assert.Equal("C:/Users/nbgv/objects", a),
a => Assert.Equal("C:/Users/nbgv2/objects/", a),
a => Assert.Equal("../../clone/.git/objects", a));
}

private static void AssertPath(string expected, string actual)
{
Assert.Equal(
Expand Down
21 changes: 17 additions & 4 deletions src/NerdBank.GitVersioning/ManagedGit/GitRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public GitRepository(string workingDirectory, string gitDirectory, string common
var length = alternateStream!.Read(alternates);
alternates = alternates.Slice(0, length);

foreach(var alternate in ParseAlternates(alternates))
foreach (var alternate in ParseAlternates(alternates))
{
this.alternates.Add(
GitRepository.Create(
Expand Down Expand Up @@ -724,16 +724,29 @@ public static unsafe string GetString(ReadOnlySpan<byte> bytes)
/// A list of (relative) paths to the alternate object directories.
/// </returns>
public static List<string> ParseAlternates(ReadOnlySpan<byte> alternates)
=> ParseAlternates(alternates, RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 2 : 0);

/// <summary>
/// Parses the contents of the alternates file, and returns a list of (relative) paths to the alternate object directories.
/// </summary>
/// <param name="alternates">
/// The contents of the alternates files.
/// </param>
/// <param name="skipCount">
/// The number of bytes to skip in the span when looking for a delimiter.
/// </param>
/// <returns>
/// A list of (relative) paths to the alternate object directories.
/// </returns>
public static List<string> ParseAlternates(ReadOnlySpan<byte> alternates, int skipCount)
{
List<string> values = new List<string>();

int index = 0;
int index;

// 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.
int skipCount = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 2 : 0;

while (alternates.Length > skipCount && (index = alternates.Slice(skipCount).IndexOfAny((byte)':', (byte)'\n')) > 0)
{
values.Add(GetString(alternates.Slice(0, skipCount + index)));
Expand Down

0 comments on commit c311a3f

Please sign in to comment.