Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Introduce the AmbiguousException type

Partially fix #189
  • Loading branch information...
commit 61a7403292833b55a09cd51187cb3422ee30d2d4 1 parent aecdcbd
nulltoken nulltoken authored
9 LibGit2Sharp.Tests/RepositoryFixture.cs
View
@@ -311,6 +311,15 @@ public void LookingUpWithBadParamsThrows()
}
[Fact]
+ public void LookingUpWithATooShortShaThrows()
+ {
+ using (var repo = new Repository(BareTestRepoPath))
+ {
+ Assert.Throws<AmbiguousException>(() => repo.Lookup("e90"));
+ }
+ }
+
+ [Fact]
public void CanDiscoverABareRepoGivenTheRepoPath()
{
string path = Repository.Discover(BareTestRepoPath);
9 LibGit2Sharp/AbbreviatedObjectId.cs
View
@@ -1,6 +1,4 @@
-using System;
-using System.Globalization;
-using LibGit2Sharp.Core;
+using LibGit2Sharp.Core;
namespace LibGit2Sharp
{
@@ -8,11 +6,6 @@ internal class AbbreviatedObjectId : ObjectId
{
internal AbbreviatedObjectId(GitOid oid, int length) : base(oid)
{
- if (length < MinHexSize || length > HexSize)
- {
- throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Expected length should be comprised between {0} and {1}.", MinHexSize, HexSize), "length");
- }
-
Length = length;
}
49 LibGit2Sharp/AmbiguousException.cs
View
@@ -0,0 +1,49 @@
+using System;
+using System.Runtime.Serialization;
+
+namespace LibGit2Sharp
+{
+ /// <summary>
+ /// The exception that is thrown when provided filtering criteria lead to more
+ /// than one result.
+ /// </summary>
+ [Serializable]
+ public class AmbiguousException : LibGit2SharpException
+ {
+ /// <summary>
+ /// Initializes a new instance of the <see cref = "AmbiguousException" /> class.
+ /// </summary>
+ public AmbiguousException()
+ {
+ }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref = "AmbiguousException" /> class with a specified error message.
+ /// </summary>
+ /// <param name = "message">A message that describes the error. </param>
+ public AmbiguousException(string message)
+ : base(message)
+ {
+ }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref = "AmbiguousException" /> class with a specified error message and a reference to the inner exception that is the cause of this exception.
+ /// </summary>
+ /// <param name = "message">The error message that explains the reason for the exception. </param>
+ /// <param name = "innerException">The exception that is the cause of the current exception. If the <paramref name = "innerException" /> parameter is not a null reference, the current exception is raised in a catch block that handles the inner exception.</param>
+ public AmbiguousException(string message, Exception innerException)
+ : base(message, innerException)
+ {
+ }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref = "AmbiguousException" /> class with a serialized data.
+ /// </summary>
+ /// <param name = "info">The <see cref="SerializationInfo "/> that holds the serialized object data about the exception being thrown.</param>
+ /// <param name = "context">The <see cref="StreamingContext"/> that contains contextual information about the source or destination.</param>
+ protected AmbiguousException(SerializationInfo info, StreamingContext context)
+ : base(info, context)
+ {
+ }
+ }
+}
1  LibGit2Sharp/LibGit2Sharp.csproj
View
@@ -56,6 +56,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="AbbreviatedObjectId.cs" />
+ <Compile Include="AmbiguousException.cs" />
<Compile Include="Blob.cs" />
<Compile Include="BlobExtensions.cs" />
<Compile Include="Branch.cs" />
57 LibGit2Sharp/ObjectId.cs
View
@@ -63,7 +63,7 @@ public ObjectId(byte[] rawId)
/// <param name = "sha">The sha.</param>
public ObjectId(string sha)
{
- GitOid? parsedOid = BuildOidFrom(sha, true, false);
+ GitOid? parsedOid = BuildOidFrom(sha, true, IdentifierSize.Standard);
if (!parsedOid.HasValue)
{
@@ -103,14 +103,19 @@ 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)
{
- result = BuildFrom(sha, false, true);
+ return TryParseInternal(sha, out result, IdentifierSize.Short);
+ }
+
+ internal static bool TryParseInternal(string sha, out ObjectId result, IdentifierSize size)
+ {
+ result = BuildFrom(sha, false, size);
return result != null;
}
- private static GitOid? BuildOidFrom(string sha, bool shouldThrowIfInvalid, bool allowShortIdentifier)
+ private static GitOid? BuildOidFrom(string sha, bool shouldThrowIfInvalid, IdentifierSize size)
{
- if (!LooksValid(sha, shouldThrowIfInvalid, allowShortIdentifier))
+ if (!LooksValid(sha, shouldThrowIfInvalid, size))
{
return null;
}
@@ -118,9 +123,9 @@ public static bool TryParse(string sha, out ObjectId result)
return ToOid(sha);
}
- private static ObjectId BuildFrom(string sha, bool shouldThrowIfInvalid, bool allowShortIdentifier)
+ private static ObjectId BuildFrom(string sha, bool shouldThrowIfInvalid, IdentifierSize size)
{
- GitOid? oid = BuildOidFrom(sha, shouldThrowIfInvalid, allowShortIdentifier);
+ GitOid? oid = BuildOidFrom(sha, shouldThrowIfInvalid, size);
if (!oid.HasValue)
{
@@ -183,9 +188,9 @@ public string ToString(int prefixLength)
private static int NormalizeLength(int prefixLength)
{
- if (prefixLength < MinHexSize)
+ if (prefixLength < 1)
{
- return MinHexSize;
+ return 1;
}
if (prefixLength > HexSize)
@@ -280,7 +285,7 @@ private static GitOid ToOid(string sha)
return oid;
}
- private static bool LooksValid(string objectId, bool throwIfInvalid, bool allowShortIdentifier)
+ private static bool LooksValid(string objectId, bool throwIfInvalid, IdentifierSize size)
{
if (objectId == null)
{
@@ -292,16 +297,35 @@ private static bool LooksValid(string objectId, bool throwIfInvalid, bool allowS
throw new ArgumentNullException("objectId");
}
- if (objectId.Length < MinHexSize || objectId.Length > HexSize)
+ if ((objectId.Length < 1)
+ || (objectId.Length < MinHexSize && size != IdentifierSize.Shortest)
+ || (objectId.Length > HexSize))
{
if (!throwIfInvalid)
{
return false;
}
- string additionalErrorInformation = !allowShortIdentifier ?
- string.Format(CultureInfo.InvariantCulture, "Its length should be {0}", HexSize) :
- string.Format(CultureInfo.InvariantCulture, "Its length should be comprised between {0} and {1}", MinHexSize, HexSize);
+ 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),
@@ -311,4 +335,11 @@ private static bool LooksValid(string objectId, bool throwIfInvalid, bool allowS
return objectId.All(c => hexDigits.Contains(c.ToString(CultureInfo.InvariantCulture)));
}
}
+
+ internal enum IdentifierSize
+ {
+ Standard,
+ Short,
+ Shortest,
+ }
}
24 LibGit2Sharp/Repository.cs
View
@@ -309,19 +309,25 @@ internal GitObject LookupInternal(ObjectId id, GitObjectType type, FilePath know
res = NativeMethods.git_object_lookup(out obj, handle, ref oid, type);
}
- if (res == (int)GitErrorCode.NotFound)
+ switch (res)
{
- return null;
- }
+ case (int)GitErrorCode.NotFound:
+ return null;
- Ensure.Success(res);
+ case (int)GitErrorCode.Ambiguous:
+ throw new AmbiguousException(string.Format(CultureInfo.InvariantCulture, "Provided abbreviated ObjectId '{0}' is too short.", id));
- if (id is AbbreviatedObjectId)
- {
- id = GitObject.ObjectIdOf(obj);
+ default:
+ Ensure.Success(res);
+
+ if (id is AbbreviatedObjectId)
+ {
+ id = GitObject.ObjectIdOf(obj);
+ }
+
+ return GitObject.CreateFromPtr(obj, id, this, knownPath);
}
- return GitObject.CreateFromPtr(obj, id, this, knownPath);
}
finally
{
@@ -351,7 +357,7 @@ internal GitObject Lookup(string shaOrReferenceName, GitObjectType type, LookUpO
}
else
{
- ObjectId.TryParse(shaOrReferenceName, out id);
+ ObjectId.TryParseInternal(shaOrReferenceName, out id, IdentifierSize.Shortest);
}
if (id == null)
Please sign in to comment.
Something went wrong with that request. Please try again.