using LibGit2Sharp.Core;
namespace LibGit2Sharp
{
///
/// Provides helper overloads to a .
///
public static class BranchCollectionExtensions
{
///
/// Create a new local branch with the specified name, using the default reflog message
///
/// The name of the branch.
/// Revparse spec for the target commit.
/// True to allow silent overwriting a potentially existing branch, false otherwise.
/// The being worked with.
/// A new .
public static Branch Add(this BranchCollection branches, string name, string committish, bool allowOverwrite = false)
{
return Add(branches, name, committish, null, null, allowOverwrite);
}
///
/// Create a new local branch with the specified name
///
/// The being worked with.
/// The name of the branch.
/// Revparse spec for the target commit.
/// The identity used for updating the reflog
/// The optional message to log in the
/// True to allow silent overwriting a potentially existing branch, false otherwise.
/// A new .
public static Branch Add(this BranchCollection branches, string name, string committish, Signature signature,
string logMessage = null, bool allowOverwrite = false)
{
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNullOrEmptyString(committish, "committish");
var commit = branches.repo.LookupCommit(committish);
if (logMessage == null)
{
var createdFrom = committish != "HEAD"
? committish
: branches.repo.Info.IsHeadDetached
? commit.Sha
: branches.repo.Head.Name;
logMessage = "branch: Created from " + createdFrom;
}
return branches.Add(name, commit, signature, logMessage, allowOverwrite);
}
///
/// 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 = false)
{
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.
/// True to allow silent overwriting a potentially existing branch, false otherwise.
/// The being worked with.
/// A new .
public static Branch Move(this BranchCollection branches, string currentName, string newName, bool allowOverwrite = false)
{
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.Move(branch, newName, allowOverwrite);
}
}
}