Skip to content

Commit 99ebe32

Browse files
committed
Delegate support of abbreviated oids to revparse
1 parent f8302e2 commit 99ebe32

File tree

8 files changed

+19
-113
lines changed

8 files changed

+19
-113
lines changed

LibGit2Sharp.Tests/CommitFixture.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,9 @@ public void CanEnumerateCommitsFromTwoHeads()
280280
public void CanEnumerateCommitsFromMixedStartingPoints()
281281
{
282282
AssertEnumerationOfCommits(
283-
repo => new Filter { Since = new object[] { repo.Branches["br2"], "refs/heads/master", new ObjectId("e90810b") } },
283+
repo => new Filter { Since = new object[] { repo.Branches["br2"],
284+
"refs/heads/master",
285+
new ObjectId("e90810b8df3e80c413d903f631643c716887138d") } },
284286
new[]
285287
{
286288
"4c062a6", "e90810b", "6dcf9bf", "a4a7dce",

LibGit2Sharp.Tests/ObjectIdFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ public void SimilarObjectIdsHaveSameHashCode()
9898
[InlineData("0", false)]
9999
[InlineData("01", false)]
100100
[InlineData("012", false)]
101-
[InlineData("0123", true)]
102-
[InlineData("0123456", true)]
101+
[InlineData("0123", false)]
102+
[InlineData("0123456", false)]
103103
[InlineData(validSha1 + "d", false)]
104104
[InlineData(validSha1, true)]
105105
public void TryParse(string maybeSha, bool isValidSha)

LibGit2Sharp/AbbreviatedObjectId.cs

Lines changed: 0 additions & 19 deletions
This file was deleted.

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -444,9 +444,6 @@ internal static extern int git_note_foreach(
444444
[DllImport(libgit2)]
445445
internal static extern int git_object_lookup(out GitObjectSafeHandle obj, RepositorySafeHandle repo, ref GitOid id, GitObjectType type);
446446

447-
[DllImport(libgit2)]
448-
internal static extern int git_object_lookup_prefix(out GitObjectSafeHandle obj, RepositorySafeHandle repo, ref GitOid id, uint len, GitObjectType type);
449-
450447
[DllImport(libgit2)]
451448
internal static extern GitObjectType git_object_type(GitObjectSafeHandle obj);
452449

LibGit2Sharp/Core/Proxy.cs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -767,31 +767,6 @@ public static GitObjectSafeHandle git_object_lookup(RepositorySafeHandle repo, O
767767
}
768768
}
769769

770-
public static GitObjectSafeHandle git_object_lookup_prefix(RepositorySafeHandle repo, ObjectId id, GitObjectType type)
771-
{
772-
using (ThreadAffinity())
773-
{
774-
GitObjectSafeHandle handle;
775-
GitOid oid = id.Oid;
776-
777-
int res = NativeMethods.git_object_lookup_prefix(out handle, repo, ref oid, (uint)((AbbreviatedObjectId)id).Length, type);
778-
switch (res)
779-
{
780-
case (int)GitErrorCode.NotFound:
781-
return null;
782-
783-
case (int)GitErrorCode.Ambiguous:
784-
throw new AmbiguousException(string.Format(CultureInfo.InvariantCulture, "Provided abbreviated ObjectId '{0}' is too short.", id));
785-
786-
default:
787-
Ensure.Success(res);
788-
break;
789-
}
790-
791-
return handle;
792-
}
793-
}
794-
795770
public static GitObjectType git_object_type(GitObjectSafeHandle obj)
796771
{
797772
return NativeMethods.git_object_type(obj);

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
<Reference Include="System.Core" />
5656
</ItemGroup>
5757
<ItemGroup>
58-
<Compile Include="AbbreviatedObjectId.cs" />
5958
<Compile Include="AmbiguousException.cs" />
6059
<Compile Include="Blob.cs" />
6160
<Compile Include="BlobExtensions.cs" />

LibGit2Sharp/ObjectId.cs

Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@ public class ObjectId : IEquatable<ObjectId>
1717
/// <summary>
1818
/// Size of the string-based representation of a SHA-1.
1919
/// </summary>
20-
protected const int HexSize = rawSize*2;
21-
22-
/// <summary>
23-
/// Mininum size of the string-based representation of an abbreviated SHA-1.
24-
/// </summary>
25-
protected const int MinHexSize = 4;
20+
protected const int HexSize = rawSize * 2;
2621

2722
private const string hexDigits = "0123456789abcdef";
2823
private static readonly byte[] reverseHexDigits = BuildReverseHexDigits();
@@ -63,7 +58,7 @@ public ObjectId(byte[] rawId)
6358
/// <param name = "sha">The sha.</param>
6459
public ObjectId(string sha)
6560
{
66-
GitOid? parsedOid = BuildOidFrom(sha, true, IdentifierSize.Standard);
61+
GitOid? parsedOid = BuildOidFrom(sha, true);
6762

6863
if (!parsedOid.HasValue)
6964
{
@@ -103,36 +98,31 @@ public virtual string Sha
10398
/// <returns>true if the <paramref name = "sha" /> parameter was converted successfully; otherwise, false.</returns>
10499
public static bool TryParse(string sha, out ObjectId result)
105100
{
106-
return TryParseInternal(sha, out result, IdentifierSize.Short);
107-
}
108-
109-
internal static bool TryParseInternal(string sha, out ObjectId result, IdentifierSize size)
110-
{
111-
result = BuildFrom(sha, false, size);
101+
result = BuildFrom(sha, false);
112102

113103
return result != null;
114104
}
115105

116-
private static GitOid? BuildOidFrom(string sha, bool shouldThrowIfInvalid, IdentifierSize size)
106+
private static GitOid? BuildOidFrom(string sha, bool shouldThrowIfInvalid)
117107
{
118-
if (!LooksValid(sha, shouldThrowIfInvalid, size))
108+
if (!LooksValid(sha, shouldThrowIfInvalid))
119109
{
120110
return null;
121111
}
122112

123113
return ToOid(sha);
124114
}
125115

126-
private static ObjectId BuildFrom(string sha, bool shouldThrowIfInvalid, IdentifierSize size)
116+
private static ObjectId BuildFrom(string sha, bool shouldThrowIfInvalid)
127117
{
128-
GitOid? oid = BuildOidFrom(sha, shouldThrowIfInvalid, size);
118+
GitOid? oid = BuildOidFrom(sha, shouldThrowIfInvalid);
129119

130120
if (!oid.HasValue)
131121
{
132122
return null;
133123
}
134124

135-
ObjectId objectId = sha.Length == HexSize ? new ObjectId(oid.Value) : new AbbreviatedObjectId(oid.Value, sha.Length);
125+
var objectId = new ObjectId(oid.Value);
136126

137127
return objectId;
138128
}
@@ -285,61 +275,31 @@ private static GitOid ToOid(string sha)
285275
return oid;
286276
}
287277

288-
private static bool LooksValid(string objectId, bool throwIfInvalid, IdentifierSize size)
278+
private static bool LooksValid(string objectId, bool throwIfInvalid)
289279
{
290-
if (objectId == null)
280+
if (string.IsNullOrEmpty(objectId))
291281
{
292282
if (!throwIfInvalid)
293283
{
294284
return false;
295285
}
296286

297-
throw new ArgumentNullException("objectId");
287+
Ensure.ArgumentNotNullOrEmptyString(objectId, "objectId");
298288
}
299289

300-
if ((objectId.Length < 1)
301-
|| (objectId.Length < MinHexSize && size != IdentifierSize.Shortest)
302-
|| (objectId.Length > HexSize))
290+
if ((objectId.Length != HexSize))
303291
{
304292
if (!throwIfInvalid)
305293
{
306294
return false;
307295
}
308296

309-
string format;
310-
311-
switch (size)
312-
{
313-
case IdentifierSize.Standard:
314-
format = "Its length should be {0}";
315-
break;
316-
317-
case IdentifierSize.Short:
318-
format = "Its length should be comprised between {1} and {0}";
319-
break;
320-
321-
case IdentifierSize.Shortest:
322-
format = "Its length should not exceed {0}";
323-
break;
324-
325-
default:
326-
throw new ArgumentOutOfRangeException("size");
327-
}
328-
string additionalErrorInformation = string.Format(CultureInfo.InvariantCulture, format, HexSize, MinHexSize);
329-
330297
throw new ArgumentException(
331-
string.Format(CultureInfo.InvariantCulture, "'{0}' is not a valid object identifier. {1}.", objectId, additionalErrorInformation),
298+
string.Format(CultureInfo.InvariantCulture, "'{0}' is not a valid object identifier. Its length should be {1}.", objectId, HexSize),
332299
"objectId");
333300
}
334301

335302
return objectId.All(c => hexDigits.Contains(c.ToString(CultureInfo.InvariantCulture)));
336303
}
337304
}
338-
339-
internal enum IdentifierSize
340-
{
341-
Standard,
342-
Short,
343-
Shortest,
344-
}
345305
}

LibGit2Sharp/Repository.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -296,15 +296,7 @@ internal GitObject LookupInternal(ObjectId id, GitObjectType type, FilePath know
296296

297297
try
298298
{
299-
if (id is AbbreviatedObjectId)
300-
{
301-
obj = Proxy.git_object_lookup_prefix(handle, id, type);
302-
id = GitObject.ObjectIdOf(obj);
303-
}
304-
else
305-
{
306-
obj = Proxy.git_object_lookup(handle, id, type);
307-
}
299+
obj = Proxy.git_object_lookup(handle, id, type);
308300

309301
return obj == null ? null : GitObject.BuildFromPtr(obj, id, this, knownPath);
310302
}

0 commit comments

Comments
 (0)