Skip to content

Commit 27378ee

Browse files
committed
Make Branches.Move() accept a Branch as its target
1 parent c803339 commit 27378ee

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

LibGit2Sharp.Tests/BranchFixture.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,18 @@ public void CanGetTrackingInformationForLocalTrackingBranch()
319319
}
320320
}
321321

322+
[Fact]
323+
public void MovingARemoteTrackingBranchThrows()
324+
{
325+
using (var repo = new Repository(StandardTestRepoPath))
326+
{
327+
Branch master = repo.Branches["refs/remotes/origin/master"];
328+
Assert.True(master.IsRemote);
329+
330+
Assert.Throws<LibGit2SharpException>(() => repo.Branches.Move(master, "new_name", true));
331+
}
332+
}
333+
322334
[Fact]
323335
public void CanWalkCommitsFromAnotherBranch()
324336
{

LibGit2Sharp/BranchCollection.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public virtual void Delete(string name, bool isRemote = false)
215215
}
216216

217217
/// <summary>
218-
/// Rename an existing local branch with a new name.
218+
/// Renames an existing local branch with a new name.
219219
/// </summary>
220220
/// <param name = "currentName">The current branch name.</param>
221221
/// <param name = "newName">The new name the existing branch should bear.</param>
@@ -231,6 +231,26 @@ public virtual Branch Move(string currentName, string newName, bool allowOverwri
231231
return this[newName];
232232
}
233233

234+
/// <summary>
235+
/// Renames an existing local branch with a new name.
236+
/// </summary>
237+
/// <param name = "branch">The current local branch.</param>
238+
/// <param name = "newName">The new name the existing branch should bear.</param>
239+
/// <param name = "allowOverwrite">True to allow silent overwriting a potentially existing branch, false otherwise.</param>
240+
/// <returns>A new <see cref="Branch"/>.</returns>
241+
public virtual Branch Move(Branch branch, string newName, bool allowOverwrite = false)
242+
{
243+
Ensure.ArgumentNotNull(branch, "branch");
244+
Ensure.ArgumentNotNullOrEmptyString(newName, "newName");
245+
246+
if (branch.IsRemote)
247+
{
248+
throw new LibGit2SharpException(string.Format("Cannot rename branch '{0}'. It's a remote tracking branch.", branch.Name));
249+
}
250+
251+
return Move(branch.Name, newName, allowOverwrite);
252+
}
253+
234254
private static bool LooksLikeABranchName(string referenceName)
235255
{
236256
return referenceName == "HEAD" ||

0 commit comments

Comments
 (0)