Skip to content

Commit 872a4ee

Browse files
committed
Turn repo.Branches string based overloads into extension methods
1 parent 27378ee commit 872a4ee

File tree

3 files changed

+65
-51
lines changed

3 files changed

+65
-51
lines changed

LibGit2Sharp/BranchCollection.cs

Lines changed: 5 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace LibGit2Sharp
1313
/// </summary>
1414
public class BranchCollection : IEnumerable<Branch>
1515
{
16-
private readonly Repository repo;
16+
internal readonly Repository repo;
1717

1818
/// <summary>
1919
/// Needed for mocking purposes.
@@ -133,23 +133,6 @@ IEnumerator IEnumerable.GetEnumerator()
133133

134134
#endregion
135135

136-
/// <summary>
137-
/// Create a new local branch with the specified name
138-
/// </summary>
139-
/// <param name = "name">The name of the branch.</param>
140-
/// <param name = "commitish">Revparse spec for the target commit.</param>
141-
/// <param name = "allowOverwrite">True to allow silent overwriting a potentially existing branch, false otherwise.</param>
142-
/// <returns>A new <see cref="Branch"/>.</returns>
143-
public virtual Branch Add(string name, string commitish, bool allowOverwrite = false)
144-
{
145-
Ensure.ArgumentNotNullOrEmptyString(name, "name");
146-
Ensure.ArgumentNotNullOrEmptyString(commitish, "commitish");
147-
148-
Commit commit = repo.LookupCommit(commitish);
149-
150-
return Add(name, commit, allowOverwrite);
151-
}
152-
153136
/// <summary>
154137
/// Create a new local branch with the specified name
155138
/// </summary>
@@ -177,19 +160,7 @@ public virtual Branch Add(string name, Commit commit, bool allowOverwrite = fals
177160
[Obsolete("This method will be removed in the next release. Please use Add() instead.")]
178161
public virtual Branch Create(string name, string commitish, bool allowOverwrite = false)
179162
{
180-
return Add(name, commitish, allowOverwrite);
181-
}
182-
183-
/// <summary>
184-
/// Deletes the branch with the specified name.
185-
/// </summary>
186-
/// <param name = "name">The name of the branch to delete.</param>
187-
/// <param name = "isRemote">True if the provided <paramref name="name"/> is the name of a remote branch, false otherwise.</param>
188-
public virtual void Remove(string name, bool isRemote = false)
189-
{
190-
Ensure.ArgumentNotNullOrEmptyString(name, "name");
191-
192-
Proxy.git_branch_delete(repo.Handle, name, isRemote ? GitBranchType.GIT_BRANCH_REMOTE : GitBranchType.GIT_BRANCH_LOCAL);
163+
return this.Add(name, commitish, allowOverwrite);
193164
}
194165

195166
/// <summary>
@@ -200,7 +171,7 @@ public virtual void Remove(Branch branch)
200171
{
201172
Ensure.ArgumentNotNull(branch, "branch");
202173

203-
Remove(branch.Name, branch.IsRemote);
174+
this.Remove(branch.Name, branch.IsRemote);
204175
}
205176

206177
/// <summary>
@@ -211,24 +182,7 @@ public virtual void Remove(Branch branch)
211182
[Obsolete("This method will be removed in the next release. Please use Remove() instead.")]
212183
public virtual void Delete(string name, bool isRemote = false)
213184
{
214-
Remove(name, isRemote);
215-
}
216-
217-
/// <summary>
218-
/// Renames an existing local branch with a new name.
219-
/// </summary>
220-
/// <param name = "currentName">The current branch name.</param>
221-
/// <param name = "newName">The new name the existing branch should bear.</param>
222-
/// <param name = "allowOverwrite">True to allow silent overwriting a potentially existing branch, false otherwise.</param>
223-
/// <returns>A new <see cref="Branch"/>.</returns>
224-
public virtual Branch Move(string currentName, string newName, bool allowOverwrite = false)
225-
{
226-
Ensure.ArgumentNotNullOrEmptyString(currentName, "currentName");
227-
Ensure.ArgumentNotNullOrEmptyString(newName, "newName");
228-
229-
Proxy.git_branch_move(repo.Handle, currentName, newName, allowOverwrite);
230-
231-
return this[newName];
185+
this.Remove(name, isRemote);
232186
}
233187

234188
/// <summary>
@@ -248,7 +202,7 @@ public virtual Branch Move(Branch branch, string newName, bool allowOverwrite =
248202
throw new LibGit2SharpException(string.Format("Cannot rename branch '{0}'. It's a remote tracking branch.", branch.Name));
249203
}
250204

251-
return Move(branch.Name, newName, allowOverwrite);
205+
return this.Move(branch.Name, newName, allowOverwrite);
252206
}
253207

254208
private static bool LooksLikeABranchName(string referenceName)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using LibGit2Sharp.Core;
2+
3+
namespace LibGit2Sharp
4+
{
5+
/// <summary>
6+
/// Provides helper overloads to a <see cref = "BranchCollection" />.
7+
/// </summary>
8+
public static class BranchCollectionExtensions
9+
{
10+
/// <summary>
11+
/// Create a new local branch with the specified name
12+
/// </summary>
13+
/// <param name = "name">The name of the branch.</param>
14+
/// <param name = "commitish">Revparse spec for the target commit.</param>
15+
/// <param name = "allowOverwrite">True to allow silent overwriting a potentially existing branch, false otherwise.</param>
16+
/// <param name = "branches">The <see cref="BranchCollection"/> being worked with.</param>
17+
/// <returns>A new <see cref="Branch"/>.</returns>
18+
public static Branch Add(this BranchCollection branches, string name, string commitish, bool allowOverwrite = false)
19+
{
20+
Ensure.ArgumentNotNullOrEmptyString(name, "name");
21+
Ensure.ArgumentNotNullOrEmptyString(commitish, "commitish");
22+
23+
Commit commit = branches.repo.LookupCommit(commitish);
24+
25+
return branches.Add(name, commit, allowOverwrite);
26+
}
27+
28+
/// <summary>
29+
/// Deletes the branch with the specified name.
30+
/// </summary>
31+
/// <param name = "name">The name of the branch to delete.</param>
32+
/// <param name = "isRemote">True if the provided <paramref name="name"/> is the name of a remote branch, false otherwise.</param>
33+
/// <param name = "branches">The <see cref="BranchCollection"/> being worked with.</param>
34+
public static void Remove(this BranchCollection branches, string name, bool isRemote = false)
35+
{
36+
Ensure.ArgumentNotNullOrEmptyString(name, "name");
37+
38+
Proxy.git_branch_delete(branches.repo.Handle, name, isRemote ? GitBranchType.GIT_BRANCH_REMOTE : GitBranchType.GIT_BRANCH_LOCAL);
39+
}
40+
41+
/// <summary>
42+
/// Renames an existing local branch with a new name.
43+
/// </summary>
44+
/// <param name = "currentName">The current branch name.</param>
45+
/// <param name = "newName">The new name the existing branch should bear.</param>
46+
/// <param name = "allowOverwrite">True to allow silent overwriting a potentially existing branch, false otherwise.</param>
47+
/// <param name = "branches">The <see cref="BranchCollection"/> being worked with.</param>
48+
/// <returns>A new <see cref="Branch"/>.</returns>
49+
public static Branch Move(this BranchCollection branches, string currentName, string newName, bool allowOverwrite = false)
50+
{
51+
Ensure.ArgumentNotNullOrEmptyString(currentName, "currentName");
52+
Ensure.ArgumentNotNullOrEmptyString(newName, "newName");
53+
54+
Proxy.git_branch_move(branches.repo.Handle, currentName, newName, allowOverwrite);
55+
56+
return branches[newName];
57+
}
58+
}
59+
}

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<Compile Include="BlobExtensions.cs" />
6262
<Compile Include="Branch.cs" />
6363
<Compile Include="BranchCollection.cs" />
64+
<Compile Include="BranchCollectionExtensions.cs" />
6465
<Compile Include="Changes.cs" />
6566
<Compile Include="Commit.cs" />
6667
<Compile Include="CommitLog.cs" />

0 commit comments

Comments
 (0)