using System; using LibGit2Sharp.Core; namespace LibGit2Sharp { public static partial class Commands { /// /// Checkout the specified , reference or SHA. /// /// If the committishOrBranchSpec parameter resolves to a branch name, then the checked out HEAD will /// will point to the branch. Otherwise, the HEAD will be detached, pointing at the commit sha. /// /// /// The repository to act on /// A revparse spec for the commit or branch to checkout. /// The that was checked out. public static Branch Checkout(IRepository repository, string committishOrBranchSpec) { return Checkout(repository, committishOrBranchSpec, new CheckoutOptions()); } /// /// Checkout the specified , reference or SHA. /// /// If the committishOrBranchSpec parameter resolves to a branch name, then the checked out HEAD will /// will point to the branch. Otherwise, the HEAD will be detached, pointing at the commit sha. /// /// /// The repository to act on /// A revparse spec for the commit or branch to checkout. /// controlling checkout behavior. /// The that was checked out. public static Branch Checkout(IRepository repository, string committishOrBranchSpec, CheckoutOptions options) { Ensure.ArgumentNotNull(repository, "repository"); Ensure.ArgumentNotNullOrEmptyString(committishOrBranchSpec, "committishOrBranchSpec"); Ensure.ArgumentNotNull(options, "options"); Reference reference; GitObject obj; repository.RevParse(committishOrBranchSpec, out reference, out obj); if (reference != null && reference.IsLocalBranch) { Branch branch = repository.Branches[reference.CanonicalName]; return Checkout(repository, branch, options); } Commit commit = obj.Peel(true); Checkout(repository, commit.Tree, options, committishOrBranchSpec); return repository.Head; } /// /// Checkout the tip commit of the specified object. If this commit is the /// current tip of the branch, will checkout the named branch. Otherwise, will checkout the tip commit /// as a detached HEAD. /// /// The repository to act on /// The to check out. /// The that was checked out. public static Branch Checkout(IRepository repository, Branch branch) { return Checkout(repository, branch, new CheckoutOptions()); } /// /// Checkout the tip commit of the specified object. If this commit is the /// current tip of the branch, will checkout the named branch. Otherwise, will checkout the tip commit /// as a detached HEAD. /// /// The repository to act on /// The to check out. /// controlling checkout behavior. /// The that was checked out. public static Branch Checkout(IRepository repository, Branch branch, CheckoutOptions options) { Ensure.ArgumentNotNull(repository, "repository"); Ensure.ArgumentNotNull(branch, "branch"); Ensure.ArgumentNotNull(options, "options"); // Make sure this is not an unborn branch. if (branch.Tip == null) { throw new UnbornBranchException("The tip of branch '{0}' is null. There's nothing to checkout.", branch.FriendlyName); } if (!branch.IsRemote && !(branch is DetachedHead) && string.Equals(repository.Refs[branch.CanonicalName].TargetIdentifier, branch.Tip.Id.Sha, StringComparison.OrdinalIgnoreCase)) { Checkout(repository, branch.Tip.Tree, options, branch.CanonicalName); } else { Checkout(repository, branch.Tip.Tree, options, branch.Tip.Id.Sha); } return repository.Head; } /// /// Checkout the specified . /// /// Will detach the HEAD and make it point to this commit sha. /// /// /// The repository to act on /// The to check out. /// The that was checked out. public static Branch Checkout(IRepository repository, Commit commit) { return Checkout(repository, commit, new CheckoutOptions()); } /// /// Checkout the specified . /// /// Will detach the HEAD and make it point to this commit sha. /// /// /// The repository to act on /// The to check out. /// controlling checkout behavior. /// The that was checked out. public static Branch Checkout(IRepository repository, Commit commit, CheckoutOptions options) { Ensure.ArgumentNotNull(repository, "repository"); Ensure.ArgumentNotNull(commit, "commit"); Ensure.ArgumentNotNull(options, "options"); Checkout(repository, commit.Tree, options, commit.Id.Sha); return repository.Head; } /// /// Internal implementation of Checkout that expects the ID of the checkout target /// to already be in the form of a canonical branch name or a commit ID. /// /// The repository to act on /// The to checkout. /// controlling checkout behavior. /// The spec which will be written as target in the reflog. public static void Checkout(IRepository repository, Tree tree, CheckoutOptions checkoutOptions, string refLogHeadSpec) { repository.Checkout(tree, null, checkoutOptions); repository.Refs.MoveHeadTarget(refLogHeadSpec); } } }