Skip to content

Commit

Permalink
Allow handling of odd-sized short Shas in OdbBackend
Browse files Browse the repository at this point in the history
  • Loading branch information
nulltoken committed Aug 15, 2013
1 parent 3e29b4b commit 7eb9ccf
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
54 changes: 52 additions & 2 deletions LibGit2Sharp.Tests/OdbBackendFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;

Expand Down Expand Up @@ -85,6 +86,43 @@ public void CanGeneratePredictableObjectShasWithAProvidedBackend()
}
}

[Fact]
public void CanRetrieveObjectsThroughOddSizedShortShas()
{
string repoPath = InitNewRepository();

using (var repo = new Repository(repoPath))
{
var backend = new MockOdbBackend();
repo.ObjectDatabase.AddBackend(backend, priority: 5);

AddCommitToRepo(repo);

var blob1 = repo.Lookup<Blob>("9daeaf");
Assert.NotNull(blob1);

const string dummy = "dummy\n";

// Inserts a fake blob with a similarly prefixed sha
var fakeId = new ObjectId("9daeaf0000000000000000000000000000000000");
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(dummy)))
{
Assert.Equal(0, backend.Write(fakeId.RawId, ms, dummy.Length, ObjectType.Blob));
}

var blob2 = repo.Lookup<Blob>(fakeId);
Assert.NotNull(blob2);

Assert.Throws<AmbiguousSpecificationException>(() => repo.Lookup<Blob>("9daeaf"));

var newBlob1 = repo.Lookup("9daeafb");
var newBlob2 = repo.Lookup("9daeaf0");

Assert.Equal(blob1, newBlob1);
Assert.Equal(blob2, newBlob2);
}
}

#region MockOdbBackend

private class MockOdbBackend : OdbBackend
Expand Down Expand Up @@ -125,7 +163,7 @@ public override int Read(byte[] oid, out Stream data, out ObjectType objectType)
return GIT_ENOTFOUND;
}

public override int ReadPrefix(byte[] shortOid, out byte[] oid, out Stream data, out ObjectType objectType)
public override int ReadPrefix(byte[] shortOid, int len, out byte[] oid, out Stream data, out ObjectType objectType)
{
oid = null;
data = null;
Expand All @@ -137,7 +175,8 @@ public override int ReadPrefix(byte[] shortOid, out byte[] oid, out Stream data,
{
bool match = true;

for (int i = 0; i < shortOid.Length; i++)
int length = len >> 1;
for (int i = 0; i < length; i++)
{
if (gitObject.ObjectId[i] != shortOid[i])
{
Expand All @@ -146,6 +185,17 @@ public override int ReadPrefix(byte[] shortOid, out byte[] oid, out Stream data,
}
}

if (match && ((len & 1) == 1))
{
var a = gitObject.ObjectId[length] >> 4;
var b = shortOid[length] >> 4;

if (a != b)
{
match = false;
}
}

if (!match)
{
continue;
Expand Down
11 changes: 8 additions & 3 deletions LibGit2Sharp/OdbBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public abstract int Read(byte[] oid,
/// Requests that this backend read an object. The object ID may not be complete (may be a prefix).
/// </summary>
public abstract int ReadPrefix(byte[] shortOid,
int prefixLen,
out byte[] oid,
out Stream data,
out ObjectType objectType);
Expand Down Expand Up @@ -275,10 +276,14 @@ private unsafe static int ReadPrefix(
{
// The length of short_oid is described in characters (40 per full ID) vs. bytes (20)
// which is what we care about.
byte[] shortOidArray = new byte[(long)len >> 1];
Array.Copy(short_oid.Id, shortOidArray, shortOidArray.Length);
var oidLen = (int)len;

int toReturn = odbBackend.ReadPrefix(shortOidArray, out oid, out dataStream, out objectType);
// Ensure we allocate enough space to cope with odd-sized prefix
int arraySize = (oidLen + 1) >> 1;
var shortOidArray = new byte[arraySize];
Array.Copy(short_oid.Id, shortOidArray, arraySize);

int toReturn = odbBackend.ReadPrefix(shortOidArray, oidLen, out oid, out dataStream, out objectType);

if (0 == toReturn)
{
Expand Down

0 comments on commit 7eb9ccf

Please sign in to comment.