Exemplo n.º 1
0
        /// <summary>
        /// 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.
        /// </summary>
        /// <param name="tree">The <see cref="Tree"/> to checkout.</param>
        /// <param name="checkoutModifiers"><see cref="CheckoutModifiers"/> controlling checkout behavior.</param>
        /// <param name="onCheckoutProgress"><see cref="CheckoutProgressHandler"/> that checkout progress is reported through.</param>
        /// <param name="checkoutNotificationOptions"><see cref="CheckoutNotificationOptions"/> to manage checkout notifications.</param>
        /// <param name="headTarget">Target for the new HEAD.</param>
        /// <param name="refLogHeadSpec">The spec which will be written as target in the reflog.</param>
        /// <param name="writeReflogEntry">Will a reflog entry be created.</param>
        private void Checkout(
            Tree tree,
            CheckoutModifiers checkoutModifiers,
            CheckoutProgressHandler onCheckoutProgress,
            CheckoutNotificationOptions checkoutNotificationOptions,
            string headTarget, string refLogHeadSpec, bool writeReflogEntry)
        {
            var previousHeadName = Info.IsHeadDetached ? Head.Tip.Sha : Head.Name;

            var opts = new CheckoutOptions
            {
                CheckoutModifiers           = checkoutModifiers,
                OnCheckoutProgress          = onCheckoutProgress,
                CheckoutNotificationOptions = checkoutNotificationOptions
            };

            CheckoutTree(tree, null, opts);

            Refs.UpdateTarget("HEAD", headTarget);

            if (writeReflogEntry)
            {
                LogCheckout(previousHeadName, Head.Tip.Id, refLogHeadSpec);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///   Sets the current <see cref="Head"/> to the specified commit and optionally resets the <see cref="Index"/> and
        ///   the content of the working tree to match.
        /// </summary>
        /// <param name = "resetOptions">Flavor of reset operation to perform.</param>
        /// <param name = "shaOrReferenceName">The sha or reference canonical name of the target commit object.</param>
        public void Reset(ResetOptions resetOptions, string shaOrReferenceName)
        {
            Ensure.ArgumentNotNullOrEmptyString(shaOrReferenceName, "shaOrReferenceName");

            if (resetOptions.Has(ResetOptions.Mixed) && Info.IsBare)
            {
                throw new LibGit2Exception("Mixed reset is not allowed in a bare repository");
            }

            GitObject commit = Lookup(shaOrReferenceName, GitObjectType.Any, LookUpOptions.ThrowWhenNoGitObjectHasBeenFound | LookUpOptions.DereferenceResultToCommit | LookUpOptions.ThrowWhenCanNotBeDereferencedToACommit);

            //TODO: Check for unmerged entries

            string refToUpdate = Info.IsHeadDetached ? "HEAD" : Head.CanonicalName;

            Refs.UpdateTarget(refToUpdate, commit.Sha);

            if (resetOptions == ResetOptions.Soft)
            {
                return;
            }

            Index.ReplaceContentWithTree(((Commit)commit).Tree);

            if (resetOptions == ResetOptions.Mixed)
            {
                return;
            }

            throw new NotImplementedException();
        }
Exemplo n.º 3
0
        /// <summary>
        ///   Checkout the tip commit of the specified <see cref = "Branch" /> 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.
        /// </summary>
        /// <param name="branch">The <see cref = "Branch" /> to check out. </param>
        /// <param name="checkoutOptions"><see cref = "CheckoutOptions" /> controlling checkout behavior.</param>
        /// <param name="onCheckoutProgress"><see cref = "CheckoutProgressHandler" /> that checkout progress is reported through.</param>
        /// <returns>The <see cref = "Branch" /> that was checked out.</returns>
        public Branch Checkout(Branch branch, CheckoutOptions checkoutOptions, CheckoutProgressHandler onCheckoutProgress)
        {
            Ensure.ArgumentNotNull(branch, "branch");

            // Make sure this is not an unborn branch.
            if (branch.Tip == null)
            {
                throw new OrphanedHeadException(
                          string.Format(CultureInfo.InvariantCulture,
                                        "The tip of branch '{0}' is null. There's nothing to checkout.", branch.Name));
            }

            CheckoutTree(branch.Tip.Tree, checkoutOptions, onCheckoutProgress);

            // Update HEAD.
            if (!branch.IsRemote &&
                string.Equals(Refs[branch.CanonicalName].TargetIdentifier, branch.Tip.Id.Sha,
                              StringComparison.OrdinalIgnoreCase))
            {
                Refs.UpdateTarget("HEAD", branch.CanonicalName);
            }
            else
            {
                Refs.UpdateTarget("HEAD", branch.Tip.Id.Sha);
            }

            return(Head);
        }
Exemplo n.º 4
0
        /// <summary>
        ///   Sets the current <see cref = "Head" /> to the specified commit and optionally resets the <see cref = "Index" /> and
        ///   the content of the working tree to match.
        /// </summary>
        /// <param name = "resetOptions">Flavor of reset operation to perform.</param>
        /// <param name = "shaOrReferenceName">The sha or reference canonical name of the target commit object.</param>
        public void Reset(ResetOptions resetOptions, string shaOrReferenceName)
        {
            Ensure.ArgumentNotNullOrEmptyString(shaOrReferenceName, "shaOrReferenceName");

            if (resetOptions.Has(ResetOptions.Mixed) && Info.IsBare)
            {
                throw new LibGit2Exception("Mixed reset is not allowed in a bare repository");
            }

            var commit = LookupCommit(shaOrReferenceName);

            //TODO: Check for unmerged entries

            string refToUpdate = Info.IsHeadDetached ? "HEAD" : Head.CanonicalName;

            Refs.UpdateTarget(refToUpdate, commit.Sha);

            if (resetOptions == ResetOptions.Soft)
            {
                return;
            }

            Index.ReplaceContentWithTree(commit.Tree);

            if (resetOptions == ResetOptions.Mixed)
            {
                return;
            }

            throw new NotImplementedException();
        }
Exemplo n.º 5
0
        /// <summary>
        ///   Checkout the specified <see cref = "Branch" />, reference or SHA.
        /// </summary>
        /// <param name = "committishOrBranchSpec">A revparse spec for the commit or branch to checkout.</param>
        /// <param name="checkoutOptions"><see cref = "CheckoutOptions" /> controlling checkout behavior.</param>
        /// <param name="onCheckoutProgress"><see cref = "CheckoutProgressHandler" /> that checkout progress is reported through.</param>
        /// <returns>The <see cref = "Branch" /> that was checked out.</returns>
        public Branch Checkout(string committishOrBranchSpec, CheckoutOptions checkoutOptions, CheckoutProgressHandler onCheckoutProgress)
        {
            Ensure.ArgumentNotNullOrEmptyString(committishOrBranchSpec, "committishOrBranchSpec");

            Branch branch = TryResolveBranch(committishOrBranchSpec);

            if (branch != null)
            {
                return(Checkout(branch, checkoutOptions, onCheckoutProgress));
            }

            var previousHeadName = Info.IsHeadDetached ? Head.Tip.Sha : Head.Name;

            Commit commit = LookupCommit(committishOrBranchSpec);

            CheckoutTree(commit.Tree, checkoutOptions, onCheckoutProgress);

            // Update HEAD.
            Refs.UpdateTarget("HEAD", commit.Id.Sha);
            if (committishOrBranchSpec != "HEAD")
            {
                LogCheckout(previousHeadName, commit.Id, committishOrBranchSpec);
            }

            return(Head);
        }
Exemplo n.º 6
0
        private void FastForward(Commit fastForwardCommit)
        {
            var checkoutOpts = new CheckoutOptions
            {
                CheckoutModifiers = CheckoutModifiers.None,
            };

            CheckoutTree(fastForwardCommit.Tree, null, checkoutOpts);

            var reference = Refs.Head.ResolveToDirectReference();

            Refs.UpdateTarget(reference, fastForwardCommit.Id.Sha);

            // TODO: Update Reflog...
        }
Exemplo n.º 7
0
        /// <summary>
        ///   Checkout the specified branch, reference or SHA.
        /// </summary>
        /// <param name = "shaOrReferenceName">The sha of the commit, a canonical reference name or the name of the branch to checkout.</param>
        /// <returns>The new HEAD.</returns>
        public Branch Checkout(string shaOrReferenceName)
        {
            // TODO: This does not yet checkout (write) the working directory

            var branch = Branches[shaOrReferenceName];

            if (branch != null)
            {
                return(Checkout(branch));
            }

            var commitId = LookupCommit(shaOrReferenceName).Id;

            Refs.UpdateTarget("HEAD", commitId.Sha);
            return(Head);
        }
Exemplo n.º 8
0
        /// <summary>
        ///   Checkout the specified <see cref = "Branch" />, reference or SHA.
        /// </summary>
        /// <param name = "committishOrBranchSpec">A revparse spec for the commit or branch to checkout.</param>
        /// <param name="checkoutOptions"><see cref = "CheckoutOptions" /> controlling checkout behavior.</param>
        /// <param name="onCheckoutProgress"><see cref = "CheckoutProgressHandler" /> that checkout progress is reported through.</param>
        /// <returns>The <see cref = "Branch" /> that was checked out.</returns>
        public Branch Checkout(string committishOrBranchSpec, CheckoutOptions checkoutOptions, CheckoutProgressHandler onCheckoutProgress)
        {
            Ensure.ArgumentNotNullOrEmptyString(committishOrBranchSpec, "committishOrBranchSpec");

            var branch = Branches[committishOrBranchSpec];

            if (branch != null)
            {
                return(Checkout(branch, checkoutOptions, onCheckoutProgress));
            }

            Commit commit = LookupCommit(committishOrBranchSpec);

            CheckoutTree(commit.Tree, checkoutOptions, onCheckoutProgress);

            // Update HEAD.
            Refs.UpdateTarget("HEAD", commit.Id.Sha);

            return(Head);
        }
Exemplo n.º 9
0
 /// <summary>
 ///   Checkout the specified branch.
 /// </summary>
 /// <param name="branch">The branch to checkout.</param>
 /// <returns>The branch.</returns>
 public Branch Checkout(Branch branch)
 {
     Ensure.ArgumentNotNull(branch, "branch");
     Refs.UpdateTarget("HEAD", branch.CanonicalName);
     return(branch);
 }