Skip to content

Commit

Permalink
Merge pull request #546 from qmfrederik/fixes/40-char-references
Browse files Browse the repository at this point in the history
Don't treat 40-char references as object IDs
  • Loading branch information
AArnott committed Dec 4, 2020
2 parents 6500e82 + f1d5fdd commit 90d7e39
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/NerdBank.GitVersioning/ManagedGit/GitReferenceReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,28 @@ internal class GitReferenceReader

public static object ReadReference(Stream stream)
{
if (stream.Length == 41)
{
Span<byte> objectId = stackalloc byte[40];
stream.Read(objectId);
Span<byte> reference = stackalloc byte[(int)stream.Length];
stream.ReadAll(reference);

return ReadReference(reference);
}

return GitObjectId.ParseHex(objectId);
public static object ReadReference(Span<byte> value)
{
if (value.Length == 41 && !value.StartsWith(RefPrefix))
{
// Skip the trailing \n
return GitObjectId.ParseHex(value.Slice(0, 40));
}
else
{
Span<byte> prefix = stackalloc byte[RefPrefix.Length];
stream.Read(prefix);

if (!prefix.SequenceEqual(RefPrefix))
if (!value.StartsWith(RefPrefix))
{
throw new GitException();
}

// Skip the terminating \n character
Span<byte> reference = stackalloc byte[(int)stream.Length - RefPrefix.Length - 1];
stream.Read(reference);

return GitRepository.GetString(reference);
return GitRepository.GetString(value.Slice(RefPrefix.Length, value.Length - RefPrefix.Length - 1));
}
}
}
Expand Down

0 comments on commit 90d7e39

Please sign in to comment.