using LibGit2Sharp.Core;
namespace LibGit2Sharp
{
///
/// Provides helper overloads to a .
///
public static class BranchCollectionExtensions
{
///
/// Create a new local branch with the specified name
///
/// The being worked with.
/// The name of the branch.
/// The target commit.
/// A new .
public static Branch Add(this BranchCollection branches, string name, Commit commit)
{
return branches.Add(name, commit, false);
}
///
/// Create a new local branch with the specified name
///
/// The being worked with.
/// The name of the branch.
/// The target commit.
/// True to allow silent overwriting a potentially existing branch, false otherwise.
/// A new .
public static Branch Add(this BranchCollection branches, string name, Commit commit, bool allowOverwrite)
{
Ensure.ArgumentNotNull(commit, "commit");
return branches.Add(name, commit.Sha, allowOverwrite);
}
///
/// Deletes the branch with the specified name.
///
/// The name of the branch to delete.
/// The being worked with.
public static void Remove(this BranchCollection branches, string name)
{
branches.Remove(name, false);
}
///
/// Deletes the branch with the specified name.
///
/// The name of the branch to delete.
/// True if the provided is the name of a remote branch, false otherwise.
/// The being worked with.
public static void Remove(this BranchCollection branches, string name, bool isRemote)
{
Ensure.ArgumentNotNullOrEmptyString(name, "name");
string branchName = isRemote ? Reference.RemoteTrackingBranchPrefix + name : name;
Branch branch = branches[branchName];
if (branch == null)
{
return;
}
branches.Remove(branch);
}
///
/// Rename an existing local branch, using the default reflog message
///
/// The current branch name.
/// The new name the existing branch should bear.
/// The being worked with.
/// A new .
public static Branch Rename(this BranchCollection branches, string currentName, string newName)
{
return branches.Rename(currentName, newName, false);
}
///
/// Rename an existing local branch, using the default reflog message
///
/// The current branch name.
/// The new name the existing branch should bear.
/// True to allow silent overwriting a potentially existing branch, false otherwise.
/// The being worked with.
/// A new .
public static Branch Rename(this BranchCollection branches, string currentName, string newName, bool allowOverwrite)
{
Ensure.ArgumentNotNullOrEmptyString(currentName, "currentName");
Ensure.ArgumentNotNullOrEmptyString(newName, "newName");
Branch branch = branches[currentName];
if (branch == null)
{
throw new LibGit2SharpException("No branch named '{0}' exists in the repository.");
}
return branches.Rename(branch, newName, allowOverwrite);
}
}
}