Permalink
Browse files

Add explicit cast from string to ObjectId

  • Loading branch information...
1 parent b76cf61 commit 7b6d5bc8c08028c6fada6724a5517b24ff5ec8c8 @dahlbyk dahlbyk committed with nulltoken Mar 2, 2013
Showing with 28 additions and 0 deletions.
  1. +18 −0 LibGit2Sharp.Tests/ObjectIdFixture.cs
  2. +10 −0 LibGit2Sharp/ObjectId.cs
View
18 LibGit2Sharp.Tests/ObjectIdFixture.cs
@@ -40,6 +40,24 @@ public void CanConvertShaToOid()
}
[Fact]
+ public void CanCastShaToObjectId()
+ {
+ var id = (ObjectId)validSha1;
+
+ Assert.Equal(bytes, id.RawId);
+ }
+
+ [Fact]
+ public void CanCastNullToObjectId()
+ {
+ string sha = null;
+
+ var id = (ObjectId)sha;
+
+ Assert.Null(id);
+ }
+
+ [Fact]
public void CreatingObjectIdWithWrongNumberOfBytesThrows()
{
var invalidBytes = new byte[] { 206, 8, 254, 72, 132, 101, 15, 6, 123, 213, 112, 59, 106, 89, 168, 179, 179, 201, 154 };
View
10 LibGit2Sharp/ObjectId.cs
@@ -199,6 +199,16 @@ private static int NormalizeLength(int prefixLength)
return !Equals(left, right);
}
+ /// <summary>
+ /// Create an <see cref="ObjectId"/> for the given <paramref name="sha"/>.
+ /// </summary>
+ /// <param name="sha">The object SHA.</param>
+ /// <returns>An <see cref="ObjectId"/>, or null if <paramref name="sha"/> is null.</returns>
+ public static explicit operator ObjectId(string sha)
+ {
+ return sha == null ? null : new ObjectId(sha);
+ }
+
private static byte[] BuildReverseHexDigits()
{
var bytes = new byte['f' - '0' + 1];

0 comments on commit 7b6d5bc

Please sign in to comment.