Permalink
Browse files

Turn repo.Branches string based overloads into extension methods

  • Loading branch information...
nulltoken committed Sep 3, 2012
1 parent 27378ee commit 872a4ee4abb4da4d43a3470c2cb1a6d04cce4f44
Showing with 65 additions and 51 deletions.
  1. +5 −51 LibGit2Sharp/BranchCollection.cs
  2. +59 −0 LibGit2Sharp/BranchCollectionExtensions.cs
  3. +1 −0 LibGit2Sharp/LibGit2Sharp.csproj
@@ -13,7 +13,7 @@ namespace LibGit2Sharp
/// </summary>
public class BranchCollection : IEnumerable<Branch>
{
- private readonly Repository repo;
+ internal readonly Repository repo;
/// <summary>
/// Needed for mocking purposes.
@@ -133,23 +133,6 @@ IEnumerator IEnumerable.GetEnumerator()
#endregion
- /// <summary>
- /// Create a new local branch with the specified name
- /// </summary>
- /// <param name = "name">The name of the branch.</param>
- /// <param name = "commitish">Revparse spec for the target commit.</param>
- /// <param name = "allowOverwrite">True to allow silent overwriting a potentially existing branch, false otherwise.</param>
- /// <returns>A new <see cref="Branch"/>.</returns>
- public virtual Branch Add(string name, string commitish, bool allowOverwrite = false)
- {
- Ensure.ArgumentNotNullOrEmptyString(name, "name");
- Ensure.ArgumentNotNullOrEmptyString(commitish, "commitish");
-
- Commit commit = repo.LookupCommit(commitish);
-
- return Add(name, commit, allowOverwrite);
- }
-
/// <summary>
/// Create a new local branch with the specified name
/// </summary>
@@ -177,19 +160,7 @@ public virtual Branch Add(string name, Commit commit, bool allowOverwrite = fals
[Obsolete("This method will be removed in the next release. Please use Add() instead.")]
public virtual Branch Create(string name, string commitish, bool allowOverwrite = false)
{
- return Add(name, commitish, allowOverwrite);
- }
-
- /// <summary>
- /// Deletes the branch with the specified name.
- /// </summary>
- /// <param name = "name">The name of the branch to delete.</param>
- /// <param name = "isRemote">True if the provided <paramref name="name"/> is the name of a remote branch, false otherwise.</param>
- public virtual void Remove(string name, bool isRemote = false)
- {
- Ensure.ArgumentNotNullOrEmptyString(name, "name");
-
- Proxy.git_branch_delete(repo.Handle, name, isRemote ? GitBranchType.GIT_BRANCH_REMOTE : GitBranchType.GIT_BRANCH_LOCAL);
+ return this.Add(name, commitish, allowOverwrite);
}
/// <summary>
@@ -200,7 +171,7 @@ public virtual void Remove(Branch branch)
{
Ensure.ArgumentNotNull(branch, "branch");
- Remove(branch.Name, branch.IsRemote);
+ this.Remove(branch.Name, branch.IsRemote);
}
/// <summary>
@@ -211,24 +182,7 @@ public virtual void Remove(Branch branch)
[Obsolete("This method will be removed in the next release. Please use Remove() instead.")]
public virtual void Delete(string name, bool isRemote = false)
{
- Remove(name, isRemote);
- }
-
- /// <summary>
- /// Renames an existing local branch with a new name.
- /// </summary>
- /// <param name = "currentName">The current branch name.</param>
- /// <param name = "newName">The new name the existing branch should bear.</param>
- /// <param name = "allowOverwrite">True to allow silent overwriting a potentially existing branch, false otherwise.</param>
- /// <returns>A new <see cref="Branch"/>.</returns>
- public virtual Branch Move(string currentName, string newName, bool allowOverwrite = false)
- {
- Ensure.ArgumentNotNullOrEmptyString(currentName, "currentName");
- Ensure.ArgumentNotNullOrEmptyString(newName, "newName");
-
- Proxy.git_branch_move(repo.Handle, currentName, newName, allowOverwrite);
-
- return this[newName];
+ this.Remove(name, isRemote);
}
/// <summary>
@@ -248,7 +202,7 @@ public virtual Branch Move(Branch branch, string newName, bool allowOverwrite =
throw new LibGit2SharpException(string.Format("Cannot rename branch '{0}'. It's a remote tracking branch.", branch.Name));
}
- return Move(branch.Name, newName, allowOverwrite);
+ return this.Move(branch.Name, newName, allowOverwrite);
}
private static bool LooksLikeABranchName(string referenceName)
@@ -0,0 +1,59 @@
+using LibGit2Sharp.Core;
+
+namespace LibGit2Sharp
+{
+ /// <summary>
+ /// Provides helper overloads to a <see cref = "BranchCollection" />.
+ /// </summary>
+ public static class BranchCollectionExtensions
+ {
+ /// <summary>
+ /// Create a new local branch with the specified name
+ /// </summary>
+ /// <param name = "name">The name of the branch.</param>
+ /// <param name = "commitish">Revparse spec for the target commit.</param>
+ /// <param name = "allowOverwrite">True to allow silent overwriting a potentially existing branch, false otherwise.</param>
+ /// <param name = "branches">The <see cref="BranchCollection"/> being worked with.</param>
+ /// <returns>A new <see cref="Branch"/>.</returns>
+ public static Branch Add(this BranchCollection branches, string name, string commitish, bool allowOverwrite = false)
+ {
+ Ensure.ArgumentNotNullOrEmptyString(name, "name");
+ Ensure.ArgumentNotNullOrEmptyString(commitish, "commitish");
+
+ Commit commit = branches.repo.LookupCommit(commitish);
+
+ return branches.Add(name, commit, allowOverwrite);
+ }
+
+ /// <summary>
+ /// Deletes the branch with the specified name.
+ /// </summary>
+ /// <param name = "name">The name of the branch to delete.</param>
+ /// <param name = "isRemote">True if the provided <paramref name="name"/> is the name of a remote branch, false otherwise.</param>
+ /// <param name = "branches">The <see cref="BranchCollection"/> being worked with.</param>
+ public static void Remove(this BranchCollection branches, string name, bool isRemote = false)
+ {
+ Ensure.ArgumentNotNullOrEmptyString(name, "name");
+
+ Proxy.git_branch_delete(branches.repo.Handle, name, isRemote ? GitBranchType.GIT_BRANCH_REMOTE : GitBranchType.GIT_BRANCH_LOCAL);
+ }
+
+ /// <summary>
+ /// Renames an existing local branch with a new name.
+ /// </summary>
+ /// <param name = "currentName">The current branch name.</param>
+ /// <param name = "newName">The new name the existing branch should bear.</param>
+ /// <param name = "allowOverwrite">True to allow silent overwriting a potentially existing branch, false otherwise.</param>
+ /// <param name = "branches">The <see cref="BranchCollection"/> being worked with.</param>
+ /// <returns>A new <see cref="Branch"/>.</returns>
+ public static Branch Move(this BranchCollection branches, string currentName, string newName, bool allowOverwrite = false)
+ {
+ Ensure.ArgumentNotNullOrEmptyString(currentName, "currentName");
+ Ensure.ArgumentNotNullOrEmptyString(newName, "newName");
+
+ Proxy.git_branch_move(branches.repo.Handle, currentName, newName, allowOverwrite);
+
+ return branches[newName];
+ }
+ }
+}
@@ -61,6 +61,7 @@
<Compile Include="BlobExtensions.cs" />
<Compile Include="Branch.cs" />
<Compile Include="BranchCollection.cs" />
+ <Compile Include="BranchCollectionExtensions.cs" />
<Compile Include="Changes.cs" />
<Compile Include="Commit.cs" />
<Compile Include="CommitLog.cs" />

0 comments on commit 872a4ee

Please sign in to comment.