Permalink
Browse files

Delegate support of abbreviated oids to revparse

  • Loading branch information...
1 parent f8302e2 commit 99ebe329418ca96efb2e2f1245c9c9639bf11a99 @nulltoken nulltoken committed Sep 3, 2012
@@ -280,7 +280,9 @@ public void CanEnumerateCommitsFromTwoHeads()
public void CanEnumerateCommitsFromMixedStartingPoints()
{
AssertEnumerationOfCommits(
- repo => new Filter { Since = new object[] { repo.Branches["br2"], "refs/heads/master", new ObjectId("e90810b") } },
+ repo => new Filter { Since = new object[] { repo.Branches["br2"],
+ "refs/heads/master",
+ new ObjectId("e90810b8df3e80c413d903f631643c716887138d") } },
new[]
{
"4c062a6", "e90810b", "6dcf9bf", "a4a7dce",
@@ -98,8 +98,8 @@ public void SimilarObjectIdsHaveSameHashCode()
[InlineData("0", false)]
[InlineData("01", false)]
[InlineData("012", false)]
- [InlineData("0123", true)]
- [InlineData("0123456", true)]
+ [InlineData("0123", false)]
+ [InlineData("0123456", false)]
[InlineData(validSha1 + "d", false)]
[InlineData(validSha1, true)]
public void TryParse(string maybeSha, bool isValidSha)
@@ -1,19 +0,0 @@
-using LibGit2Sharp.Core;
-
-namespace LibGit2Sharp
-{
- internal class AbbreviatedObjectId : ObjectId
- {
- internal AbbreviatedObjectId(GitOid oid, int length) : base(oid)
- {
- Length = length;
- }
-
- public int Length { get; private set; }
-
- public override string Sha
- {
- get { return base.Sha.Substring(0, Length); }
- }
- }
-}
@@ -444,9 +444,6 @@ private static bool IsRunningOnLinux()
[DllImport(libgit2)]
internal static extern int git_object_lookup(out GitObjectSafeHandle obj, RepositorySafeHandle repo, ref GitOid id, GitObjectType type);
- [DllImport(libgit2)]
- internal static extern int git_object_lookup_prefix(out GitObjectSafeHandle obj, RepositorySafeHandle repo, ref GitOid id, uint len, GitObjectType type);
-
[DllImport(libgit2)]
internal static extern GitObjectType git_object_type(GitObjectSafeHandle obj);
View
@@ -767,31 +767,6 @@ public static GitObjectSafeHandle git_object_lookup(RepositorySafeHandle repo, O
}
}
- public static GitObjectSafeHandle git_object_lookup_prefix(RepositorySafeHandle repo, ObjectId id, GitObjectType type)
- {
- using (ThreadAffinity())
- {
- GitObjectSafeHandle handle;
- GitOid oid = id.Oid;
-
- int res = NativeMethods.git_object_lookup_prefix(out handle, repo, ref oid, (uint)((AbbreviatedObjectId)id).Length, type);
- switch (res)
- {
- case (int)GitErrorCode.NotFound:
- return null;
-
- case (int)GitErrorCode.Ambiguous:
- throw new AmbiguousException(string.Format(CultureInfo.InvariantCulture, "Provided abbreviated ObjectId '{0}' is too short.", id));
-
- default:
- Ensure.Success(res);
- break;
- }
-
- return handle;
- }
- }
-
public static GitObjectType git_object_type(GitObjectSafeHandle obj)
{
return NativeMethods.git_object_type(obj);
@@ -55,7 +55,6 @@
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
- <Compile Include="AbbreviatedObjectId.cs" />
<Compile Include="AmbiguousException.cs" />
<Compile Include="Blob.cs" />
<Compile Include="BlobExtensions.cs" />
View
@@ -17,12 +17,7 @@ public class ObjectId : IEquatable<ObjectId>
/// <summary>
/// Size of the string-based representation of a SHA-1.
/// </summary>
- protected const int HexSize = rawSize*2;
-
- /// <summary>
- /// Mininum size of the string-based representation of an abbreviated SHA-1.
- /// </summary>
- protected const int MinHexSize = 4;
+ protected const int HexSize = rawSize * 2;
private const string hexDigits = "0123456789abcdef";
private static readonly byte[] reverseHexDigits = BuildReverseHexDigits();
@@ -63,7 +58,7 @@ public ObjectId(byte[] rawId)
/// <param name = "sha">The sha.</param>
public ObjectId(string sha)
{
- GitOid? parsedOid = BuildOidFrom(sha, true, IdentifierSize.Standard);
+ GitOid? parsedOid = BuildOidFrom(sha, true);
if (!parsedOid.HasValue)
{
@@ -103,36 +98,31 @@ public virtual string Sha
/// <returns>true if the <paramref name = "sha" /> parameter was converted successfully; otherwise, false.</returns>
public static bool TryParse(string sha, out ObjectId result)
{
- return TryParseInternal(sha, out result, IdentifierSize.Short);
- }
-
- internal static bool TryParseInternal(string sha, out ObjectId result, IdentifierSize size)
- {
- result = BuildFrom(sha, false, size);
+ result = BuildFrom(sha, false);
return result != null;
}
- private static GitOid? BuildOidFrom(string sha, bool shouldThrowIfInvalid, IdentifierSize size)
+ private static GitOid? BuildOidFrom(string sha, bool shouldThrowIfInvalid)
{
- if (!LooksValid(sha, shouldThrowIfInvalid, size))
+ if (!LooksValid(sha, shouldThrowIfInvalid))
{
return null;
}
return ToOid(sha);
}
- private static ObjectId BuildFrom(string sha, bool shouldThrowIfInvalid, IdentifierSize size)
+ private static ObjectId BuildFrom(string sha, bool shouldThrowIfInvalid)
{
- GitOid? oid = BuildOidFrom(sha, shouldThrowIfInvalid, size);
+ GitOid? oid = BuildOidFrom(sha, shouldThrowIfInvalid);
if (!oid.HasValue)
{
return null;
}
- ObjectId objectId = sha.Length == HexSize ? new ObjectId(oid.Value) : new AbbreviatedObjectId(oid.Value, sha.Length);
+ var objectId = new ObjectId(oid.Value);
return objectId;
}
@@ -285,61 +275,31 @@ private static GitOid ToOid(string sha)
return oid;
}
- private static bool LooksValid(string objectId, bool throwIfInvalid, IdentifierSize size)
+ private static bool LooksValid(string objectId, bool throwIfInvalid)
{
- if (objectId == null)
+ if (string.IsNullOrEmpty(objectId))
{
if (!throwIfInvalid)
{
return false;
}
- throw new ArgumentNullException("objectId");
+ Ensure.ArgumentNotNullOrEmptyString(objectId, "objectId");
}
- if ((objectId.Length < 1)
- || (objectId.Length < MinHexSize && size != IdentifierSize.Shortest)
- || (objectId.Length > HexSize))
+ if ((objectId.Length != HexSize))
{
if (!throwIfInvalid)
{
return false;
}
- string format;
-
- switch (size)
- {
- case IdentifierSize.Standard:
- format = "Its length should be {0}";
- break;
-
- case IdentifierSize.Short:
- format = "Its length should be comprised between {1} and {0}";
- break;
-
- case IdentifierSize.Shortest:
- format = "Its length should not exceed {0}";
- break;
-
- default:
- throw new ArgumentOutOfRangeException("size");
- }
- string additionalErrorInformation = string.Format(CultureInfo.InvariantCulture, format, HexSize, MinHexSize);
-
throw new ArgumentException(
- string.Format(CultureInfo.InvariantCulture, "'{0}' is not a valid object identifier. {1}.", objectId, additionalErrorInformation),
+ string.Format(CultureInfo.InvariantCulture, "'{0}' is not a valid object identifier. Its length should be {1}.", objectId, HexSize),
"objectId");
}
return objectId.All(c => hexDigits.Contains(c.ToString(CultureInfo.InvariantCulture)));
}
}
-
- internal enum IdentifierSize
- {
- Standard,
- Short,
- Shortest,
- }
}
View
@@ -296,15 +296,7 @@ internal GitObject LookupInternal(ObjectId id, GitObjectType type, FilePath know
try
{
- if (id is AbbreviatedObjectId)
- {
- obj = Proxy.git_object_lookup_prefix(handle, id, type);
- id = GitObject.ObjectIdOf(obj);
- }
- else
- {
- obj = Proxy.git_object_lookup(handle, id, type);
- }
+ obj = Proxy.git_object_lookup(handle, id, type);
return obj == null ? null : GitObject.BuildFromPtr(obj, id, this, knownPath);
}

0 comments on commit 99ebe32

Please sign in to comment.