10000 Add handling of abbreviated sha to Repository.Lookup() and ObjectId.T… · MicrosoftWebMatrix/libgit2sharp@87879f3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 87879f3

Browse files
committed
Add handling of abbreviated sha to Repository.Lookup() and ObjectId.ToString()
1 parent 0c61d1d commit 87879f3

10 files changed

+303
-69
lines changed

LibGit2Sharp.Tests/ObjectIdFixture.cs

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ namespace LibGit2Sharp.Tests
77
[TestFixture]
88
public class ObjectIdFixture
99
{
10+
private const string validSha1 = "ce08fe4884650f067bd5703b6a59a8b3b3c99a09";
11+
private const string validSha2 = "de08fe4884650f067bd5703b6a59a8b3b3c99a09";
12+
1013
[TestCase("Dummy", typeof(ArgumentException))]
1114
[TestCase("", typeof(ArgumentException))]
1215
[TestCase("8e", typeof(ArgumentException))]
1316
[TestCase(null, typeof(ArgumentNullException))]
14-
[TestCase("ce08fe4884650f067bd5703b6a59a8b3b3c99a09dd", typeof(ArgumentException))]
17+
[TestCase(validSha1 + "dd", typeof(ArgumentException))]
1518
public void PreventsFromBuildingWithAnInvalidSha(string malformedSha, Type expectedExceptionType)
1619
{
1720
Assert.Throws(expectedExceptionType, () => new ObjectId(malformedSha));
@@ -24,14 +27,15 @@ public void CanConvertOidToSha()
2427

2528
var id = new ObjectId(bytes);
2629

27-
id.Sha.ShouldEqual("ce08fe4884650f067bd5703b6a59a8b3b3c99a09");
28-
id.ToString().ShouldEqual("ce08fe4884650f067bd5703b6a59a8b3b3c99a09");
30+
id.Sha.ShouldEqual(validSha1);
31+
id.ToString().ShouldEqual(validSha1);
2932
}
3033

3134
[Test]
3235
public void CanConvertShaToOid()
3336
{
34-
var id = new ObjectId("ce08fe4884650f067bd5703b6a59a8b3b3c99a09");
37+
var id = new ObjectId(validSha1);
38+
3539
id.RawId.ShouldEqual(new byte[] { 206, 8, 254, 72, 132, 101, 15, 6, 123, 213, 112, 59, 106, 89, 168, 179, 179, 201, 154, 9 });
3640
}
3741

@@ -46,39 +50,72 @@ public void CreatingObjectIdWithWrongNumberOfBytesThrows()
4650
[Test]
4751
public void DifferentObjectIdsAreEqual()
4852
{
49-
var a = new ObjectId("ce08fe4884650f067bd5703b6a59a8b3b3c99a09");
50-
var b = new ObjectId("de08fe4884650f067bd5703b6a59a8b3b3c99a09");
53+
var a = new ObjectId(validSha1);
54+
var b = new ObjectId(validSha2);
55+
5156
(a.Equals(b)).ShouldBeFalse();
5257
(b.Equals(a)).ShouldBeFalse();
58+
5359
(a == b).ShouldBeFalse();
5460
(a != b).ShouldBeTrue();
5561
}
5662

5763
[Test]
5864
public void DifferentObjectIdsDoesNotHaveSameHashCode()
5965
{
60-
var a = new ObjectId("ce08fe4884650f067bd5703b6a59a8b3b3c99a09");
61-
var b = new ObjectId("de08fe4884650f067bd5703b6a59a8b3b3c99a09");
66+
var a = new ObjectId(validSha1);
67+
var b = new ObjectId(validSha2);
68+
6269
a.GetHashCode().ShouldNotEqual(b.GetHashCode());
6370
}
6471

6572
[Test]
6673
public void SimilarObjectIdsAreEqual()
6774
{
68-
var a = new ObjectId("ce08fe4884650f067bd5703b6a59a8b3b3c99a09");
69-
var b = new ObjectId("ce08fe4884650f067bd5703b6a59a8b3b3c99a09");
75+
var a = new ObjectId(validSha1);
76+
var b = new ObjectId(validSha1);
77+
7078
(a.Equals(b)).ShouldBeTrue();
7179
(b.Equals(a)).ShouldBeTrue();
80+
7281
(a == b).ShouldBeTrue();
7382
(a != b).ShouldBeFalse();
7483
}
7584

7685
[Test]
7786
public void SimilarObjectIdsHaveSameHashCode()
7887
{
79-
var a = new ObjectId("ce08fe4884650f067bd5703b6a59a8b3b3c99a09");
80-
var b = new ObjectId("ce08fe4884650f067bd5703b6a59a8b3b3c99a09");
88+
var a = new ObjectId(validSha1);
89+
var b = new ObjectId(validSha1);
90+
8191
a.GetHashCode().ShouldEqual(b.GetHashCode());
8292
}
93+
94+
[TestCase("Dummy", false)]
95+
[TestCase(null, false)]
96+
[TestCase("", false)]
97+
[TestCase("0", false)]
98+
[TestCase(" F438 01", false)]
99+
[TestCase("012", false)]
100+
[TestCase("0123", true)]
101+
[TestCase("0123456", true)]
102+
[TestCase(validSha1 + "d", false)]
103+
[TestCase(validSha1, true)]
104+
public void TryParse(string maybeSha, bool isValidSha)
105+
{
106+
ObjectId parsedObjectId;
107+
bool result = ObjectId.TryParse(maybeSha, out parsedObjectId);
108+
result.ShouldEqual(isValidSha);
109+
110+
if (!result)
111+
{
112+
return;
113+
}
114+
115+
parsedObjectId.ShouldNotBeNull();
116+
parsedObjectId.Sha.ShouldEqual(maybeSha);
117+
maybeSha.StartsWith(parsedObjectId.ToString(3)).ShouldBeTrue();
118+
parsedObjectId.ToString(42).ShouldEqual(maybeSha);
119+
}
83120
}
84121
}

LibGit2Sharp.Tests/RepositoryFixture.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,37 @@ public void LookupObjectByUnknownReferenceNameReturnsNull()
217217
}
218218
}
219219

220+
[Test]
221+
public void CanLookupWhithShortIdentifers()
222+
{
223+
const string expectedAbbrevSha = "edfecad";
224+
const string expectedSha = expectedAbbrevSha + "02d96c9dbf64f6e238c45ddcfa762eef0";
225+
226+
using (var scd = new SelfCleaningDirectory())
227+
{
228+
var dir = Repository.Init(scd.DirectoryPath);
229+
230+
using (var repo = new Repository(dir))
231+
{
232+
string filePath = Path.Combine(repo.Info.WorkingDirectory, "new.txt");
233+
234+
File.WriteAllText(filePath, "one ");
235+
repo.Index.Stage(filePath);
236+
237+
var author = Constants.Signature;
238+
var commit = repo.Commit(author, author, "Initial commit");
239+
240+
commit.Sha.ShouldEqual(expectedSha);
241+
242+
var lookedUp1 = repo.Lookup(expectedSha);
243+
lookedUp1.ShouldEqual(commit);
244+
245+
var lookedUp2 = repo.Lookup(expectedAbbrevSha);
246+
lookedUp2.ShouldEqual(commit);
247+
}
248+
}
249+
}
250+
220251
[Test]
221252
public void LookingUpWithBadParamsThrows()
222253
{

LibGit2Sharp/BranchCollection.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ public Branch Checkout(string name)
7979
/// <returns></returns>
8080
public Branch Create(string name, string target)
8181
{
82-
ObjectId id = ObjectId.CreateFromMaybeSha(target);
83-
84-
if (id == null)
82+
ObjectId id;
83+
84+
if (!ObjectId.TryParse(target, out id))
8585
{
8686
var reference = repo.Refs[NormalizeToCanonicalName(target)].ResolveToDirectReference();
8787
target = reference.TargetIdentifier;

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ internal static class NativeMethods
7777
[DllImport(libgit2)]
7878
public static extern int git_object_lookup(out IntPtr obj, RepositorySafeHandle repo, ref GitOid id, GitObjectType type);
7979

80+
[DllImport(libgit2)]
81+
public static extern int git_object_lookup_prefix(out IntPtr obj, RepositorySafeHandle repo, ref GitOid id, uint len, GitObjectType type);
82+
8083
[DllImport(libgit2)]
8184
public static extern GitObjectType git_object_type(IntPtr obj);
8285

@@ -90,12 +93,6 @@ internal static class NativeMethods
9093
[DllImport(libgit2)]
9194
public static extern int git_oid_cmp(ref GitOid a, ref GitOid b);
9295

93-
[DllImport(libgit2)]
94-
public static extern void git_oid_fmt(byte[] str, ref GitOid oid);
95-
96-
[DllImport(libgit2)]
97-
public static extern int git_oid_fromstr(out GitOid oid, string str);
98-
9996
[DllImport(libgit2)]
10097
public static extern int git_reference_create_oid(out IntPtr reference, RepositorySafeHandle repo, string name, ref GitOid oid);
10198

LibGit2Sharp/GitObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ internal static GitObject CreateFromPtr(IntPtr obj, ObjectId id, Repository repo
6060
case GitObjectType.Blob:
6161
return Blob.BuildFromPtr(obj, id, repo);
6262
default:
63-
return new GitObject(id);
63+
throw new InvalidOperationException(string.Format("Unexpected type '{0}' for object '{1}'.", type, id));
6464
}
6565
}
6666
finally

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<Reference Include="System.Core" />
4545
</ItemGroup>
4646
<ItemGroup>
47+
<Compile Include="AbbreviatedObjectId.cs" />
4748
<Compile Include="Blob.cs" />
4849
<Compile Include="Branch.cs" />
4950
<Compile Include="BranchCollection.cs" />

0 commit comments

Comments
 (0)
0