Skip to content

Commit

Permalink
Deprecate repo.Refs.Move() in favor of repo.Refs.Rename()
Browse files Browse the repository at this point in the history
  • Loading branch information
JPaulDuncan authored and nulltoken committed Sep 13, 2014
1 parent 329c652 commit ad02fed
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 30 deletions.
50 changes: 25 additions & 25 deletions LibGit2Sharp.Tests/ReferenceFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -601,21 +601,21 @@ public void UpdatingADirectReferenceTargetWithARevparsePointingAtAnUnknownObject
}

[Fact]
public void CanMoveAReferenceToADeeperReferenceHierarchy()
public void CanRenameAReferenceToADeeperReferenceHierarchy()
{
string path = CloneBareTestRepo();
using (var repo = new Repository(path))
{
const string newName = "refs/tags/test/deep";

Reference moved = repo.Refs.Move("refs/tags/test", newName);
Assert.NotNull(moved);
Assert.Equal(newName, moved.CanonicalName);
Reference renamed = repo.Refs.Rename("refs/tags/test", newName);
Assert.NotNull(renamed);
Assert.Equal(newName, renamed.CanonicalName);
}
}

[Fact]
public void CanMoveAReferenceToAUpperReferenceHierarchy()
public void CanRenameAReferenceToAUpperReferenceHierarchy()
{
string path = CloneBareTestRepo();
using (var repo = new Repository(path))
Expand All @@ -624,14 +624,14 @@ public void CanMoveAReferenceToAUpperReferenceHierarchy()
const string oldName = newName + "/mio";

repo.Refs.Add(oldName, repo.Head.CanonicalName);
Reference moved = repo.Refs.Move(oldName, newName);
Assert.NotNull(moved);
Assert.Equal(newName, moved.CanonicalName);
Reference renamed = repo.Refs.Rename(oldName, newName);
Assert.NotNull(renamed);
Assert.Equal(newName, renamed.CanonicalName);
}
}

[Fact]
public void CanMoveAReferenceToADifferentReferenceHierarchy()
public void CanRenameAReferenceToADifferentReferenceHierarchy()
{
string path = CloneBareTestRepo();
using (var repo = new Repository(path))
Expand All @@ -643,38 +643,38 @@ public void CanMoveAReferenceToADifferentReferenceHierarchy()

var oldId = repo.Refs[oldName].ResolveToDirectReference().Target.Id;

Reference moved = repo.Refs.Move(oldName, newName);
Assert.NotNull(moved);
Assert.Equal(newName, moved.CanonicalName);
Assert.Equal(oldId, moved.ResolveToDirectReference().Target.Id);
Reference renamed = repo.Refs.Rename(oldName, newName);
Assert.NotNull(renamed);
Assert.Equal(newName, renamed.CanonicalName);
Assert.Equal(oldId, renamed.ResolveToDirectReference().Target.Id);

AssertRefLogEntry(repo, newName, moved.ResolveToDirectReference().Target.Id,
AssertRefLogEntry(repo, newName, renamed.ResolveToDirectReference().Target.Id,
string.Format("reference: renamed {0} to {1}", oldName, newName));
}
}

[Fact]
public void MovingANonExistingReferenceThrows()
public void RenamingANonExistingReferenceThrows()
{
using (var repo = new Repository(BareTestRepoPath))
{
Assert.Throws<LibGit2SharpException>(() => repo.Refs.Move("refs/tags/i-am-void", "refs/atic/tagtest"));
Assert.Throws<LibGit2SharpException>(() => repo.Refs.Rename("refs/tags/i-am-void", "refs/atic/tagtest"));
}
}

[Fact]
public void CanMoveAndOverWriteAExistingReference()
public void CanRenameAndOverWriteAExistingReference()
{
string path = CloneBareTestRepo();
using (var repo = new Repository(path))
{
const string oldName = "refs/heads/packed";
const string newName = "refs/heads/br2";

Reference moved = repo.Refs.Move(oldName, newName, allowOverwrite: true);
Reference renamed = repo.Refs.Rename(oldName, newName, allowOverwrite: true);

Assert.Null(repo.Refs[oldName]);
Assert.NotNull(repo.Refs[moved.CanonicalName]);
Assert.NotNull(repo.Refs[renamed.CanonicalName]);
}
}

Expand All @@ -683,12 +683,12 @@ public void BlindlyOverwritingAExistingReferenceThrows()
{
using (var repo = new Repository(BareTestRepoPath))
{
Assert.Throws<NameConflictException>(() => repo.Refs.Move("refs/heads/packed", "refs/heads/br2"));
Assert.Throws<NameConflictException>(() => repo.Refs.Rename("refs/heads/packed", "refs/heads/br2"));
}
}

[Fact]
public void MovingAReferenceDoesNotDecreaseTheRefsCount()
public void RenamingAReferenceDoesNotDecreaseTheRefsCount()
{
string path = CloneBareTestRepo();
using (var repo = new Repository(path))
Expand All @@ -699,7 +699,7 @@ public void MovingAReferenceDoesNotDecreaseTheRefsCount()
List<string> refs = repo.Refs.Select(r => r.CanonicalName).ToList();
Assert.True(refs.Contains(oldName));

repo.Refs.Move(oldName, newName);
repo.Refs.Rename(oldName, newName);

List<string> refs2 = repo.Refs.Select(r => r.CanonicalName).ToList();
Assert.False(refs2.Contains(oldName));
Expand All @@ -710,18 +710,18 @@ public void MovingAReferenceDoesNotDecreaseTheRefsCount()
}

[Fact]
public void CanLookupAMovedReference()
public void CanLookupARenamedReference()
{
string path = CloneBareTestRepo();
using (var repo = new Repository(path))
{
const string oldName = "refs/tags/test";
const string newName = "refs/atic/tagtest";

Reference moved = repo.Refs.Move(oldName, newName);
Reference renamed = repo.Refs.Rename(oldName, newName);

Reference lookedUp = repo.Refs[newName];
Assert.Equal(lookedUp, moved);
Assert.Equal(lookedUp, renamed);
}
}

Expand Down
4 changes: 2 additions & 2 deletions LibGit2Sharp/Core/HistoryRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ private Reference RewriteReference<TRef, TTarget>(
return refMap[oldRef] = newRef;
}

var movedRef = repo.Refs.Move(newRef, newRefName);
rollbackActions.Enqueue(() => repo.Refs.Move(newRef, oldRef.CanonicalName));
var movedRef = repo.Refs.Rename(newRef, newRefName);
rollbackActions.Enqueue(() => repo.Refs.Rename(newRef, oldRef.CanonicalName));
return refMap[oldRef] = movedRef;
}

Expand Down
32 changes: 30 additions & 2 deletions LibGit2Sharp/ReferenceCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,35 @@ public virtual void Remove(Reference reference)
/// <param name="logMessage">Message added to the reflog.</param>
/// <param name="allowOverwrite">True to allow silent overwriting a potentially existing reference, false otherwise.</param>
/// <returns>A new <see cref="Reference"/>.</returns>
[Obsolete("This method will be removed in the next release. Please use Rename() instead.")]
public virtual Reference Move(Reference reference, string newName, Signature signature, string logMessage = null, bool allowOverwrite = false)
{
return Rename(reference, newName, signature, logMessage, allowOverwrite);
}

/// <summary>
/// Rename an existing reference with a new name
/// </summary>
/// <param name="reference">The reference to rename.</param>
/// <param name="newName">The new canonical name.</param>
/// <param name="allowOverwrite">True to allow silent overwriting a potentially existing reference, false otherwise.</param>
/// <returns>A new <see cref="Reference"/>.</returns>
[Obsolete("This method will be removed in the next release. Please use Rename() instead.")]
public virtual Reference Move(Reference reference, string newName, bool allowOverwrite = false)
{
return Rename(reference, newName, null, null, allowOverwrite);
}

/// <summary>
/// Rename an existing reference with a new name, and update the reflog
/// </summary>
/// <param name="reference">The reference to rename.</param>
/// <param name="newName">The new canonical name.</param>
/// <param name="signature">Identity used for updating the reflog.</param>
/// <param name="logMessage">Message added to the reflog.</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 Reference Rename(Reference reference, string newName, Signature signature, string logMessage = null, bool allowOverwrite = false)
{
Ensure.ArgumentNotNull(reference, "reference");
Ensure.ArgumentNotNullOrEmptyString(newName, "newName");
Expand All @@ -176,9 +204,9 @@ public virtual Reference Move(Reference reference, string newName, Signature sig
/// <param name="newName">The new canonical name.</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 Reference Move(Reference reference, string newName, bool allowOverwrite = false)
public virtual Reference Rename(Reference reference, string newName, bool allowOverwrite = false)
{
return Move(reference, newName, null, null, allowOverwrite);
return Rename(reference, newName, null, null, allowOverwrite);
}

internal T Resolve<T>(string name) where T : Reference
Expand Down
19 changes: 18 additions & 1 deletion LibGit2Sharp/ReferenceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,25 @@ public static Reference UpdateTarget(this ReferenceCollection refsColl, Referenc
/// <param name="allowOverwrite">True to allow silent overwriting a potentially existing reference, false otherwise.</param>
/// <param name="refsColl">The <see cref="ReferenceCollection"/> being worked with.</param>
/// <returns>A new <see cref="Reference"/>.</returns>
[Obsolete("This method will be removed in the next release. Please use Rename() instead.")]
public static Reference Move(this ReferenceCollection refsColl, string currentName, string newName,
Signature signature = null, string logMessage = null, bool allowOverwrite = false)
{
return refsColl.Rename(currentName, newName, signature, logMessage, allowOverwrite);
}

/// <summary>
/// Rename an existing reference with a new name
/// </summary>
/// <param name="currentName">The canonical name of the reference to rename.</param>
/// <param name="newName">The new canonical name.</param>
/// <param name="signature">The identity used for updating the reflog</param>
/// <param name="logMessage">The optional message to log in the <see cref="ReflogCollection"/></param>
/// <param name="allowOverwrite">True to allow silent overwriting a potentially existing reference, false otherwise.</param>
/// <param name="refsColl">The <see cref="ReferenceCollection"/> being worked with.</param>
/// <returns>A new <see cref="Reference"/>.</returns>
public static Reference Rename(this ReferenceCollection refsColl, string currentName, string newName,
Signature signature = null, string logMessage = null, bool allowOverwrite = false)
{
Ensure.ArgumentNotNullOrEmptyString(currentName, "currentName");

Expand All @@ -148,7 +165,7 @@ public static Reference Move(this ReferenceCollection refsColl, string currentNa
"Reference '{0}' doesn't exist. One cannot move a non existing reference.", currentName));
}

return refsColl.Move(reference, newName, signature, logMessage, allowOverwrite);
return refsColl.Rename(reference, newName, signature, logMessage, allowOverwrite);
}

/// <summary>
Expand Down

0 comments on commit ad02fed

Please sign in to comment.