Permalink
Browse files

Make Refs.Add() accept a Reference as its target

  • Loading branch information...
1 parent a69653f commit 8fa70ffce8bca7ff66fe4b84293d14b66bbed29f @nulltoken nulltoken committed Aug 27, 2012
Showing with 48 additions and 7 deletions.
  1. +30 −7 LibGit2Sharp.Tests/ReferenceFixture.cs
  2. +18 −0 LibGit2Sharp/ReferenceCollection.cs
@@ -52,7 +52,7 @@ public void CanAddADirectReferenceFromRevParseSpec()
}
[Fact]
- public void CanAddASymbolicReference()
+ public void CanAddASymbolicReferenceFromTheTargetName()
{
const string name = "refs/heads/unit_test";
const string target = "refs/heads/master";
@@ -61,15 +61,38 @@ public void CanAddASymbolicReference()
using (var repo = new Repository(path.RepositoryPath))
{
var newRef = (SymbolicReference)repo.Refs.Add(name, target);
- Assert.NotNull(newRef);
- Assert.Equal(name, newRef.CanonicalName);
- Assert.Equal(target, newRef.Target.CanonicalName);
- Assert.Equal(newRef.Target.CanonicalName, newRef.TargetIdentifier);
- Assert.Equal("4c062a6361ae6959e06292c1fa5e2822d9c96345", newRef.ResolveToDirectReference().Target.Sha);
- Assert.NotNull(repo.Refs[name]);
+
+ AssertSymbolicRef(newRef, repo, target, name);
+ }
+ }
+
+ [Fact]
+ public void CanAddASymbolicReferenceFromTheTargetReference()
+ {
+ const string name = "refs/heads/unit_test";
+ const string target = "refs/heads/master";
+
+ TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo();
+ using (var repo = new Repository(path.RepositoryPath))
+ {
+ var targetRef = repo.Refs[target];
+
+ var newRef = repo.Refs.Add(name, targetRef);
+
+ AssertSymbolicRef(newRef, repo, target, name);
}
}
+ private static void AssertSymbolicRef(SymbolicReference newRef, Repository repo, string expectedTargetName, string expectedName)
+ {
+ Assert.NotNull(newRef);
+ Assert.Equal(expectedName, newRef.CanonicalName);
+ Assert.Equal(expectedTargetName, newRef.Target.CanonicalName);
+ Assert.Equal(newRef.Target.CanonicalName, newRef.TargetIdentifier);
+ Assert.Equal("4c062a6361ae6959e06292c1fa5e2822d9c96345", newRef.ResolveToDirectReference().Target.Sha);
+ Assert.NotNull(repo.Refs[expectedName]);
+ }
+
[Fact]
public void BlindlyCreatingADirectReferenceOverAnExistingOneThrows()
{
@@ -142,6 +142,24 @@ public virtual DirectReference Add(string name, ObjectId targetId, bool allowOve
}
/// <summary>
+ /// Creates a symbolic reference with the specified name and target
+ /// </summary>
+ /// <param name = "name">The name of the reference to create.</param>
+ /// <param name = "targetRef">The target reference.</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 SymbolicReference Add(string name, Reference targetRef, bool allowOverwrite = false)
+ {
+ Ensure.ArgumentNotNullOrEmptyString(name, "name");
+ Ensure.ArgumentNotNull(targetRef, "targetRef");
+
+ using (ReferenceSafeHandle handle = CreateSymbolicReference(name, targetRef.CanonicalName, allowOverwrite))
+ {
+ return (SymbolicReference)Reference.BuildFromPtr<Reference>(handle, repo);
+ }
+ }
+
+ /// <summary>
/// Creates a direct or symbolic reference with the specified name and target
/// </summary>
/// <param name = "name">The name of the reference to create.</param>

0 comments on commit 8fa70ff

Please sign in to comment.