Skip to content

Commit ad02fed

Browse files
JPaulDuncannulltoken
authored andcommitted
Deprecate repo.Refs.Move() in favor of repo.Refs.Rename()
Fix libgit2#752
1 parent 329c652 commit ad02fed

File tree

4 files changed

+75
-30
lines changed

4 files changed

+75
-30
lines changed

LibGit2Sharp.Tests/ReferenceFixture.cs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -601,21 +601,21 @@ public void UpdatingADirectReferenceTargetWithARevparsePointingAtAnUnknownObject
601601
}
602602

603603
[Fact]
604-
public void CanMoveAReferenceToADeeperReferenceHierarchy()
604+
public void CanRenameAReferenceToADeeperReferenceHierarchy()
605605
{
606606
string path = CloneBareTestRepo();
607607
using (var repo = new Repository(path))
608608
{
609609
const string newName = "refs/tags/test/deep";
610610

611-
Reference moved = repo.Refs.Move("refs/tags/test", newName);
612-
Assert.NotNull(moved);
613-
Assert.Equal(newName, moved.CanonicalName);
611+
Reference renamed = repo.Refs.Rename("refs/tags/test", newName);
612+
Assert.NotNull(renamed);
613+
Assert.Equal(newName, renamed.CanonicalName);
614614
}
615615
}
616616

617617
[Fact]
618-
public void CanMoveAReferenceToAUpperReferenceHierarchy()
618+
public void CanRenameAReferenceToAUpperReferenceHierarchy()
619619
{
620620
string path = CloneBareTestRepo();
621621
using (var repo = new Repository(path))
@@ -624,14 +624,14 @@ public void CanMoveAReferenceToAUpperReferenceHierarchy()
624624
const string oldName = newName + "/mio";
625625

626626
repo.Refs.Add(oldName, repo.Head.CanonicalName);
627-
Reference moved = repo.Refs.Move(oldName, newName);
628-
Assert.NotNull(moved);
629-
Assert.Equal(newName, moved.CanonicalName);
627+
Reference renamed = repo.Refs.Rename(oldName, newName);
628+
Assert.NotNull(renamed);
629+
Assert.Equal(newName, renamed.CanonicalName);
630630
}
631631
}
632632

633633
[Fact]
634-
public void CanMoveAReferenceToADifferentReferenceHierarchy()
634+
public void CanRenameAReferenceToADifferentReferenceHierarchy()
635635
{
636636
string path = CloneBareTestRepo();
637637
using (var repo = new Repository(path))
@@ -643,38 +643,38 @@ public void CanMoveAReferenceToADifferentReferenceHierarchy()
643643

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

646-
Reference moved = repo.Refs.Move(oldName, newName);
647-
Assert.NotNull(moved);
648-
Assert.Equal(newName, moved.CanonicalName);
649-
Assert.Equal(oldId, moved.ResolveToDirectReference().Target.Id);
646+
Reference renamed = repo.Refs.Rename(oldName, newName);
647+
Assert.NotNull(renamed);
648+
Assert.Equal(newName, renamed.CanonicalName);
649+
Assert.Equal(oldId, renamed.ResolveToDirectReference().Target.Id);
650650

651-
AssertRefLogEntry(repo, newName, moved.ResolveToDirectReference().Target.Id,
651+
AssertRefLogEntry(repo, newName, renamed.ResolveToDirectReference().Target.Id,
652652
string.Format("reference: renamed {0} to {1}", oldName, newName));
653653
}
654654
}
655655

656656
[Fact]
657-
public void MovingANonExistingReferenceThrows()
657+
public void RenamingANonExistingReferenceThrows()
658658
{
659659
using (var repo = new Repository(BareTestRepoPath))
660660
{
661-
Assert.Throws<LibGit2SharpException>(() => repo.Refs.Move("refs/tags/i-am-void", "refs/atic/tagtest"));
661+
Assert.Throws<LibGit2SharpException>(() => repo.Refs.Rename("refs/tags/i-am-void", "refs/atic/tagtest"));
662662
}
663663
}
664664

665665
[Fact]
666-
public void CanMoveAndOverWriteAExistingReference()
666+
public void CanRenameAndOverWriteAExistingReference()
667667
{
668668
string path = CloneBareTestRepo();
669669
using (var repo = new Repository(path))
670670
{
671671
const string oldName = "refs/heads/packed";
672672
const string newName = "refs/heads/br2";
673673

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

676676
Assert.Null(repo.Refs[oldName]);
677-
Assert.NotNull(repo.Refs[moved.CanonicalName]);
677+
Assert.NotNull(repo.Refs[renamed.CanonicalName]);
678678
}
679679
}
680680

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

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

702-
repo.Refs.Move(oldName, newName);
702+
repo.Refs.Rename(oldName, newName);
703703

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

712712
[Fact]
713-
public void CanLookupAMovedReference()
713+
public void CanLookupARenamedReference()
714714
{
715715
string path = CloneBareTestRepo();
716716
using (var repo = new Repository(path))
717717
{
718718
const string oldName = "refs/tags/test";
719719
const string newName = "refs/atic/tagtest";
720720

721-
Reference moved = repo.Refs.Move(oldName, newName);
721+
Reference renamed = repo.Refs.Rename(oldName, newName);
722722

723723
Reference lookedUp = repo.Refs[newName];
724-
Assert.Equal(lookedUp, moved);
724+
Assert.Equal(lookedUp, renamed);
725725
}
726726
}
727727

LibGit2Sharp/Core/HistoryRewriter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ private Reference RewriteReference<TRef, TTarget>(
181181
return refMap[oldRef] = newRef;
182182
}
183183

184-
var movedRef = repo.Refs.Move(newRef, newRefName);
185-
rollbackActions.Enqueue(() => repo.Refs.Move(newRef, oldRef.CanonicalName));
184+
var movedRef = repo.Refs.Rename(newRef, newRefName);
185+
rollbackActions.Enqueue(() => repo.Refs.Rename(newRef, oldRef.CanonicalName));
186186
return refMap[oldRef] = movedRef;
187187
}
188188

LibGit2Sharp/ReferenceCollection.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,35 @@ public virtual void Remove(Reference reference)
151151
/// <param name="logMessage">Message added to the reflog.</param>
152152
/// <param name="allowOverwrite">True to allow silent overwriting a potentially existing reference, false otherwise.</param>
153153
/// <returns>A new <see cref="Reference"/>.</returns>
154+
[Obsolete("This method will be removed in the next release. Please use Rename() instead.")]
154155
public virtual Reference Move(Reference reference, string newName, Signature signature, string logMessage = null, bool allowOverwrite = false)
156+
{
157+
return Rename(reference, newName, signature, logMessage, allowOverwrite);
158+
}
159+
160+
/// <summary>
161+
/// Rename an existing reference with a new name
162+
/// </summary>
163+
/// <param name="reference">The reference to rename.</param>
164+
/// <param name="newName">The new canonical name.</param>
165+
/// <param name="allowOverwrite">True to allow silent overwriting a potentially existing reference, false otherwise.</param>
166+
/// <returns>A new <see cref="Reference"/>.</returns>
167+
[Obsolete("This method will be removed in the next release. Please use Rename() instead.")]
168+
public virtual Reference Move(Reference reference, string newName, bool allowOverwrite = false)
169+
{
170+
return Rename(reference, newName, null, null, allowOverwrite);
171+
}
172+
173+
/// <summary>
174+
/// Rename an existing reference with a new name, and update the reflog
175+
/// </summary>
176+
/// <param name="reference">The reference to rename.</param>
177+
/// <param name="newName">The new canonical name.</param>
178+
/// <param name="signature">Identity used for updating the reflog.</param>
179+
/// <param name="logMessage">Message added to the reflog.</param>
180+
/// <param name="allowOverwrite">True to allow silent overwriting a potentially existing reference, false otherwise.</param>
181+
/// <returns>A new <see cref="Reference"/>.</returns>
182+
public virtual Reference Rename(Reference reference, string newName, Signature signature, string logMessage = null, bool allowOverwrite = false)
155183
{
156184
Ensure.ArgumentNotNull(reference, "reference");
157185
Ensure.ArgumentNotNullOrEmptyString(newName, "newName");
@@ -176,9 +204,9 @@ public virtual Reference Move(Reference reference, string newName, Signature sig
176204
/// <param name="newName">The new canonical name.</param>
177205
/// <param name="allowOverwrite">True to allow silent overwriting a potentially existing reference, false otherwise.</param>
178206
/// <returns>A new <see cref="Reference"/>.</returns>
179-
public virtual Reference Move(Reference reference, string newName, bool allowOverwrite = false)
207+
public virtual Reference Rename(Reference reference, string newName, bool allowOverwrite = false)
180208
{
181-
return Move(reference, newName, null, null, allowOverwrite);
209+
return Rename(reference, newName, null, null, allowOverwrite);
182210
}
183211

184212
internal T Resolve<T>(string name) where T : Reference

LibGit2Sharp/ReferenceCollectionExtensions.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,25 @@ public static Reference UpdateTarget(this ReferenceCollection refsColl, Referenc
134134
/// <param name="allowOverwrite">True to allow silent overwriting a potentially existing reference, false otherwise.</param>
135135
/// <param name="refsColl">The <see cref="ReferenceCollection"/> being worked with.</param>
136136
/// <returns>A new <see cref="Reference"/>.</returns>
137+
[Obsolete("This method will be removed in the next release. Please use Rename() instead.")]
137138
public static Reference Move(this ReferenceCollection refsColl, string currentName, string newName,
138139
Signature signature = null, string logMessage = null, bool allowOverwrite = false)
140+
{
141+
return refsColl.Rename(currentName, newName, signature, logMessage, allowOverwrite);
142+
}
143+
144+
/// <summary>
145+
/// Rename an existing reference with a new name
146+
/// </summary>
147+
/// <param name="currentName">The canonical name of the reference to rename.</param>
148+
/// <param name="newName">The new canonical name.</param>
149+
/// <param name="signature">The identity used for updating the reflog</param>
150+
/// <param name="logMessage">The optional message to log in the <see cref="ReflogCollection"/></param>
151+
/// <param name="allowOverwrite">True to allow silent overwriting a potentially existing reference, false otherwise.</param>
152+
/// <param name="refsColl">The <see cref="ReferenceCollection"/> being worked with.</param>
153+
/// <returns>A new <see cref="Reference"/>.</returns>
154+
public static Reference Rename(this ReferenceCollection refsColl, string currentName, string newName,
155+
Signature signature = null, string logMessage = null, bool allowOverwrite = false)
139156
{
140157
Ensure.ArgumentNotNullOrEmptyString(currentName, "currentName");
141158

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

151-
return refsColl.Move(reference, newName, signature, logMessage, allowOverwrite);
168+
return refsColl.Rename(reference, newName, signature, logMessage, allowOverwrite);
152169
}
153170

154171
/// <summary>

0 commit comments

Comments
 (0)