Permalink
Browse files

Make Refs.Add() accept an ObjectId as its target

  • Loading branch information...
nulltoken committed Aug 27, 2012
1 parent 41364ef commit a69653f29d9e19daeba71dc3b66952b7d5d8612b
Showing with 23 additions and 29 deletions.
  1. +2 −1 LibGit2Sharp.Tests/ReferenceFixture.cs
  2. +21 −28 LibGit2Sharp/ReferenceCollection.cs
@@ -149,7 +149,8 @@ public void AddWithNullForTargetThrows()
{
using (var repo = new Repository(BareTestRepoPath))
{
- Assert.Throws<ArgumentNullException>(() => repo.Refs.Add("refs/heads/newref", null));
+ Assert.Throws<ArgumentNullException>(() => repo.Refs.Add("refs/heads/newref", (string)null));
+ Assert.Throws<ArgumentNullException>(() => repo.Refs.Add("refs/heads/newref", (ObjectId)null));
}
}
@@ -107,25 +107,37 @@ public virtual Reference Add(string name, string canonicalRefNameOrObjectish, bo
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNullOrEmptyString(canonicalRefNameOrObjectish, "canonicalRefNameOrObjectish");
- Func<string, bool, ReferenceSafeHandle> referenceCreator;
-
Reference reference;
RefState refState = TryResolveReference(out reference, canonicalRefNameOrObjectish);
var gitObject = repo.Lookup(canonicalRefNameOrObjectish, GitObjectType.Any, LookUpOptions.None);
if (refState == RefState.Exists || (refState == RefState.DoesNotExistButLooksValid && gitObject == null))
{
- referenceCreator = (n, o) => CreateSymbolicReference(n, canonicalRefNameOrObjectish, o);
- }
- else
- {
- referenceCreator = (n, o) => CreateDirectReference(n, gitObject.Id, o);
+ using (ReferenceSafeHandle handle = CreateSymbolicReference(name, canonicalRefNameOrObjectish, allowOverwrite))
+ {
+ return Reference.BuildFromPtr<Reference>(handle, repo);
+ }
}
- using (ReferenceSafeHandle handle = referenceCreator(name, allowOverwrite))
+ return Add(name, gitObject.Id, allowOverwrite);
+ }
+
+ /// <summary>
+ /// Creates a direct reference with the specified name and target
+ /// </summary>
+ /// <param name = "name">The name of the reference to create.</param>
+ /// <param name = "targetId">Id of the target object.</param>
+ /// <param name = "allowOverwrite">True to allow silent overwriting a potentially existing reference, false otherwise.</param>
+ /// <returns>A new <see cref = "Reference" />.</returns>
+ public virtual DirectReference Add(string name, ObjectId targetId, bool allowOverwrite = false)
+ {
+ Ensure.ArgumentNotNullOrEmptyString(name, "name");
+ Ensure.ArgumentNotNull(targetId, "targetId");
+
+ using (ReferenceSafeHandle handle = CreateDirectReference(name, targetId, allowOverwrite))
{
- return Reference.BuildFromPtr<Reference>(handle, repo);
+ return (DirectReference)Reference.BuildFromPtr<Reference>(handle, repo);
}
}
@@ -151,32 +163,13 @@ private ReferenceSafeHandle CreateSymbolicReference(string name, string target,
private ReferenceSafeHandle CreateDirectReference(string name, ObjectId targetId, bool allowOverwrite)
{
- targetId = Unabbreviate(targetId);
-
GitOid oid = targetId.Oid;
ReferenceSafeHandle handle;
Ensure.Success(NativeMethods.git_reference_create_oid(out handle, repo.Handle, name, ref oid, allowOverwrite));
return handle;
}
- private ObjectId Unabbreviate(ObjectId targetId)
- {
- if (!(targetId is AbbreviatedObjectId))
- {
- return targetId;
- }
-
- GitObject obj = repo.Lookup(targetId);
-
- if (obj == null)
- {
- Ensure.Success((int)GitErrorCode.NotFound);
- }
-
- return obj.Id;
- }
-
/// <summary>
/// Delete a reference with the specified name
/// </summary>

0 comments on commit a69653f

Please sign in to comment.