Browse files

Make Refs.UpdateTarget() accept an revparse spec as its target

  • Loading branch information...
1 parent 8f24ef6 commit 0548d4bedd256766b9d4ec372d2f3e32e75a8e39 @nulltoken nulltoken committed Aug 31, 2012
Showing with 77 additions and 8 deletions.
  1. +60 −8 LibGit2Sharp.Tests/ReferenceFixture.cs
  2. +17 −0 LibGit2Sharp/ReferenceCollection.cs
View
68 LibGit2Sharp.Tests/ReferenceFixture.cs
@@ -358,7 +358,7 @@ public void ResolvingWithNullThrows()
}
[Fact]
- public void CanUpdateTargetOnReference()
+ public void CanUpdateTargetOfADirectReference()
{
const string masterRef = "refs/heads/master";
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo();
@@ -378,7 +378,27 @@ public void CanUpdateTargetOnReference()
}
[Fact]
- public void CanUpdateTargetOnSymbolicReference()
+ public void CanUpdateTargetOfADirectReferenceWithAnAbbreviatedSha()
+ {
+ const string masterRef = "refs/heads/master";
+ TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo();
+ using (var repo = new Repository(path.RepositoryPath))
+ {
+ string sha = repo.Refs["refs/heads/test"].ResolveToDirectReference().Target.Sha;
+ Reference master = repo.Refs[masterRef];
+ Assert.NotEqual(sha, master.ResolveToDirectReference().Target.Sha);
+
+ Reference updated = repo.Refs.UpdateTarget(masterRef, sha.Substring(0,4));
+
+ master = repo.Refs[masterRef];
+ Assert.Equal(updated, master);
+
+ Assert.Equal(sha, master.ResolveToDirectReference().Target.Sha);
+ }
+ }
+
+ [Fact]
+ public void CanUpdateTargetOfASymbolicReference()
{
const string name = "refs/heads/unit_test";
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo();
@@ -436,6 +456,26 @@ public void CanUpdateHeadWithEitherAnObjectIdOrAReference()
}
[Fact]
+ public void CanUpdateTargetOfADirectReferenceWithARevparseSpec()
+ {
+ TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo();
+ using (var repo = new Repository(path.RepositoryPath))
+ {
+ const string name = "refs/heads/master";
+
+ var master = (DirectReference) repo.Refs[name];
+
+ var newRef = (DirectReference)repo.Refs.UpdateTarget(master, "master^1^2");
+ Assert.NotNull(newRef);
+ Assert.Equal(name, newRef.CanonicalName);
+ Assert.NotNull(newRef.Target);
+ Assert.Equal("c47800c7266a2be04c571c04d5a6614691ea99bd", newRef.Target.Sha);
+ Assert.Equal(newRef.Target.Sha, newRef.TargetIdentifier);
+ Assert.NotNull(repo.Refs[name]);
+ }
+ }
+
+ [Fact]
public void UpdatingADirectRefWithSymbolFails()
{
const string name = "refs/heads/unit_test";
@@ -453,12 +493,14 @@ public void UpdatingADirectRefWithSymbolFails()
}
[Fact]
- public void UpdatingASymbolicRefWithOidFails()
+ public void CanUpdateTargetOfADirectReferenceWithAShortReferenceNameAsARevparseSpec()
{
const string masterRef = "refs/heads/master";
- using (var repo = new Repository(BareTestRepoPath))
+ TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo();
+ using (var repo = new Repository(path.RepositoryPath))
{
- Assert.Throws<ArgumentException>(() => repo.Refs.UpdateTarget(masterRef, "refs/heads/test"));
+ Reference updatedMaster = repo.Refs.UpdateTarget(masterRef, "heads/test");
+ Assert.Equal(repo.Refs["refs/heads/test"].TargetIdentifier, updatedMaster.TargetIdentifier);
}
}
@@ -468,9 +510,19 @@ public void UpdatingAReferenceTargetWithBadParametersFails()
using (var repo = new Repository(BareTestRepoPath))
{
Assert.Throws<ArgumentException>(() => repo.Refs.UpdateTarget(string.Empty, "refs/heads/packed"));
- Assert.Throws<ArgumentException>(() => repo.Refs.UpdateTarget("master", string.Empty));
- Assert.Throws<ArgumentNullException>(() => repo.Refs.UpdateTarget(null, "refs/heads/packed"));
- Assert.Throws<ArgumentNullException>(() => repo.Refs.UpdateTarget("master", null));
+ Assert.Throws<ArgumentException>(() => repo.Refs.UpdateTarget("refs/heads/master", string.Empty));
+ Assert.Throws<ArgumentNullException>(() => repo.Refs.UpdateTarget((string)null, "refs/heads/packed"));
+ Assert.Throws<ArgumentNullException>(() => repo.Refs.UpdateTarget((DirectReference)null, "refs/heads/packed"));
+ Assert.Throws<ArgumentNullException>(() => repo.Refs.UpdateTarget("refs/heads/master", null));
+ }
+ }
+
+ [Fact]
+ public void UpdatingADirectReferenceTargetWithARevparsePointingAtAnUnknownObjectFails()
+ {
+ using (var repo = new Repository(BareTestRepoPath))
+ {
+ Assert.Throws<LibGit2SharpException>(() => repo.Refs.UpdateTarget(repo.Refs["refs/heads/master"], "refs/heads/nope"));
}
}
View
17 LibGit2Sharp/ReferenceCollection.cs
@@ -278,6 +278,23 @@ public virtual Reference Move(Reference reference, string newName, bool allowOve
/// Updates the target of a direct reference.
/// </summary>
/// <param name = "directRef">The direct reference which target should be updated.</param>
+ /// <param name = "objectish">The revparse spec of the target.</param>
+ public virtual Reference UpdateTarget(Reference directRef, string objectish)
+ {
+ Ensure.ArgumentNotNull(directRef, "directRef");
+ Ensure.ArgumentNotNull(objectish, "objectish");
+
+ GitObject target = repo.Lookup(objectish);
+
+ Ensure.GitObjectIsNotNull(target, objectish);
+
+ return UpdateTarget(directRef, target.Id);
+ }
+
+ /// <summary>
+ /// Updates the target of a direct reference.
+ /// </summary>
+ /// <param name = "directRef">The direct reference which target should be updated.</param>
/// <param name = "targetId">The new target.</param>
public virtual Reference UpdateTarget(Reference directRef, ObjectId targetId)
{

0 comments on commit 0548d4b

Please sign in to comment.