Skip to content

Commit 775a2b0

Browse files
qmfrederikAArnott
authored andcommitted
Improve alternate parsing
1 parent 96b6548 commit 775a2b0

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,25 @@ public void GetMissingObjectByShaTest()
286286
}
287287
}
288288

289+
[Fact]
290+
public void ParseAlternates_SingleValue_Test()
291+
{
292+
var alternates = GitRepository.ParseAlternates(Encoding.UTF8.GetBytes("/home/git/nbgv/.git/objects\n"));
293+
Assert.Collection(
294+
alternates,
295+
a => Assert.Equal("/home/git/nbgv/.git/objects", a));
296+
}
297+
298+
[Fact]
299+
public void ParseAlternates_TwoValues_Test()
300+
{
301+
var alternates = GitRepository.ParseAlternates(Encoding.UTF8.GetBytes("/home/git/nbgv/.git/objects:../../clone/.git/objects\n"));
302+
Assert.Collection(
303+
alternates,
304+
a => Assert.Equal("/home/git/nbgv/.git/objects", a),
305+
a => Assert.Equal("../../clone/.git/objects", a));
306+
}
307+
289308
private static void AssertPath(string expected, string actual)
290309
{
291310
Assert.Equal(

src/NerdBank.GitVersioning/ManagedGit/GitRepository.cs

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Globalization;
77
using System.IO;
88
using System.Linq;
9+
using System.Runtime.InteropServices;
910
using System.Text;
1011

1112
namespace Nerdbank.GitVersioning.ManagedGit
@@ -110,16 +111,14 @@ public GitRepository(string workingDirectory, string gitDirectory, string common
110111
var length = alternateStream!.Read(alternates);
111112
alternates = alternates.Slice(0, length);
112113

113-
int index = 0;
114-
115-
while ((index = alternates.IndexOf((byte)':')) > 0)
114+
foreach(var alternate in ParseAlternates(alternates))
116115
{
117-
var alternate = GetString(alternates.Slice(0, index));
118-
alternate = Path.GetFullPath(Path.Combine(this.ObjectDirectory, alternate));
119-
120-
this.alternates.Add(GitRepository.Create(workingDirectory, gitDirectory, commonDirectory, alternate));
121-
122-
alternates = alternates.Slice(index + 1);
116+
this.alternates.Add(
117+
GitRepository.Create(
118+
workingDirectory,
119+
gitDirectory,
120+
commonDirectory,
121+
objectDirectory: Path.GetFullPath(Path.Combine(this.ObjectDirectory, alternate))));
123122
}
124123
}
125124

@@ -714,5 +713,33 @@ public static unsafe string GetString(ReadOnlySpan<byte> bytes)
714713
return Encoding.GetString(pBytes, bytes.Length);
715714
}
716715
}
716+
717+
/// <summary>
718+
/// Parses the contents of the alternates file, and returns a list of (relative) paths to the alternate object directories.
719+
/// </summary>
720+
/// <param name="alternates">
721+
/// The contents of the alternates files.
722+
/// </param>
723+
/// <returns>
724+
/// A list of (relative) paths to the alternate object directories.</returns>
725+
public static List<string> ParseAlternates(ReadOnlySpan<byte> alternates)
726+
{
727+
List<string> values = new List<string>();
728+
729+
int index = 0;
730+
731+
// The alternates path is colon (:)-separated. On Windows, there may be full paths, such as
732+
// C:/Users/username/source/repos/nbgv/.git, which also contain a colon. Because the colon
733+
// can only appear at the second position, we skip the first two characters (e.g. C:) on Windows.
734+
int skipCount = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 2 : 0;
735+
736+
while (alternates.Length > skipCount && (index = alternates.Slice(skipCount).IndexOfAny((byte)':', (byte)'\n')) > 0)
737+
{
738+
values.Add(GetString(alternates.Slice(0, skipCount + index)));
739+
alternates = alternates.Slice(skipCount + index + 1);
740+
}
741+
742+
return values;
743+
}
717744
}
718745
}

0 commit comments

Comments
 (0)