Esempio n. 1
0
 private string Format(AbbreviatedObjectId id)
 {
     if (id.IsComplete && db != null)
     {
         try
         {
             id = reader.Abbreviate(id.ToObjectId(), abbreviationLength);
         }
         catch (IOException)
         {
         }
     }
     // Ignore this. We'll report the full identity.
     return(id.Name);
 }
Esempio n. 2
0
        /// <exception cref="NGit.Api.Errors.RefNotFoundException"></exception>
        /// <exception cref="System.IO.IOException"></exception>
        /// <exception cref="NGit.Api.Errors.NoHeadException"></exception>
        /// <exception cref="NGit.Api.Errors.JGitInternalException"></exception>
        private RebaseResult InitFilesAndRewind()
        {
            // we need to store everything into files so that we can implement
            // --skip, --continue, and --abort
            // first of all, we determine the commits to be applied
            IList <RevCommit> cherryPickList = new AList <RevCommit>();
            Ref head = repo.GetRef(Constants.HEAD);

            if (head == null || head.GetObjectId() == null)
            {
                throw new RefNotFoundException(MessageFormat.Format(JGitText.Get().refNotResolved
                                                                    , Constants.HEAD));
            }
            string headName;

            if (head.IsSymbolic())
            {
                headName = head.GetTarget().GetName();
            }
            else
            {
                headName = "detached HEAD";
            }
            ObjectId headId = head.GetObjectId();

            if (headId == null)
            {
                throw new RefNotFoundException(MessageFormat.Format(JGitText.Get().refNotResolved
                                                                    , Constants.HEAD));
            }
            RevCommit headCommit = walk.LookupCommit(headId);

            monitor.BeginTask(JGitText.Get().obtainingCommitsForCherryPick, ProgressMonitor.UNKNOWN
                              );
            LogCommand           cmd          = new Git(repo).Log().AddRange(upstreamCommit, headCommit);
            Iterable <RevCommit> commitsToUse = cmd.Call();

            foreach (RevCommit commit in commitsToUse)
            {
                cherryPickList.AddItem(commit);
            }
            // if the upstream commit is in a direct line to the current head,
            // the log command will not report any commits; in this case,
            // we create the cherry-pick list ourselves
            if (cherryPickList.IsEmpty())
            {
                Iterable <RevCommit> parents = new Git(repo).Log().Add(upstreamCommit).Call();
                foreach (RevCommit parent in parents)
                {
                    if (parent.Equals(headCommit))
                    {
                        break;
                    }
                    if (parent.ParentCount != 1)
                    {
                        throw new JGitInternalException(JGitText.Get().canOnlyCherryPickCommitsWithOneParent
                                                        );
                    }
                    cherryPickList.AddItem(parent);
                }
            }
            // nothing to do: return with UP_TO_DATE_RESULT
            if (cherryPickList.IsEmpty())
            {
                return(RebaseResult.UP_TO_DATE_RESULT);
            }
            Sharpen.Collections.Reverse(cherryPickList);
            // create the folder for the meta information
            FileUtils.Mkdir(rebaseDir);
            CreateFile(repo.Directory, Constants.ORIG_HEAD, headId.Name);
            CreateFile(rebaseDir, REBASE_HEAD, headId.Name);
            CreateFile(rebaseDir, HEAD_NAME, headName);
            CreateFile(rebaseDir, ONTO, upstreamCommit.Name);
            CreateFile(rebaseDir, INTERACTIVE, string.Empty);
            BufferedWriter fw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream
                                                                              (new FilePath(rebaseDir, GIT_REBASE_TODO)), Constants.CHARACTER_ENCODING));

            fw.Write("# Created by EGit: rebasing " + upstreamCommit.Name + " onto " + headId
                     .Name);
            fw.NewLine();
            try
            {
                StringBuilder sb     = new StringBuilder();
                ObjectReader  reader = walk.GetObjectReader();
                foreach (RevCommit commit_1 in cherryPickList)
                {
                    sb.Length = 0;
                    sb.Append(RebaseCommand.Action.PICK.ToToken());
                    sb.Append(" ");
                    sb.Append(reader.Abbreviate(commit_1).Name);
                    sb.Append(" ");
                    sb.Append(commit_1.GetShortMessage());
                    fw.Write(sb.ToString());
                    fw.NewLine();
                }
            }
            finally
            {
                fw.Close();
            }
            monitor.EndTask();
            // we rewind to the upstream commit
            monitor.BeginTask(MessageFormat.Format(JGitText.Get().rewinding, upstreamCommit.GetShortMessage
                                                       ()), ProgressMonitor.UNKNOWN);
            CheckoutCommit(upstreamCommit);
            monitor.EndTask();
            return(null);
        }
Esempio n. 3
0
        public virtual void TestAbbreviateOnEmptyRepository()
        {
            ObjectId id = Id("9d5b926ed164e8ee88d3b8b1e525d699adda01ba");

            NUnit.Framework.Assert.AreEqual(id.Abbreviate(2), reader.Abbreviate(id, 2));
            NUnit.Framework.Assert.AreEqual(id.Abbreviate(7), reader.Abbreviate(id, 7));
            NUnit.Framework.Assert.AreEqual(id.Abbreviate(8), reader.Abbreviate(id, 8));
            NUnit.Framework.Assert.AreEqual(id.Abbreviate(10), reader.Abbreviate(id, 10));
            NUnit.Framework.Assert.AreEqual(id.Abbreviate(16), reader.Abbreviate(id, 16));
            NUnit.Framework.Assert.AreEqual(AbbreviatedObjectId.FromObjectId(id), reader.Abbreviate
                                                (id, Constants.OBJECT_ID_STRING_LENGTH));
            //
            ICollection <ObjectId> matches;

            matches = reader.Resolve(reader.Abbreviate(id, 8));
            NUnit.Framework.Assert.IsNotNull(matches);
            NUnit.Framework.Assert.AreEqual(0, matches.Count);
            matches = reader.Resolve(AbbreviatedObjectId.FromObjectId(id));
            NUnit.Framework.Assert.IsNotNull(matches);
            NUnit.Framework.Assert.AreEqual(1, matches.Count);
            NUnit.Framework.Assert.AreEqual(id, matches.Iterator().Next());
        }
Esempio n. 4
0
        /// <exception cref="NGit.Api.Errors.RefNotFoundException"></exception>
        /// <exception cref="System.IO.IOException"></exception>
        /// <exception cref="NGit.Api.Errors.NoHeadException"></exception>
        /// <exception cref="NGit.Api.Errors.JGitInternalException"></exception>
        private RebaseResult InitFilesAndRewind()
        {
            // we need to store everything into files so that we can implement
            // --skip, --continue, and --abort
            Ref head = repo.GetRef(Constants.HEAD);

            if (head == null || head.GetObjectId() == null)
            {
                throw new RefNotFoundException(MessageFormat.Format(JGitText.Get().refNotResolved
                                                                    , Constants.HEAD));
            }
            string headName;

            if (head.IsSymbolic())
            {
                headName = head.GetTarget().GetName();
            }
            else
            {
                headName = "detached HEAD";
            }
            ObjectId headId = head.GetObjectId();

            if (headId == null)
            {
                throw new RefNotFoundException(MessageFormat.Format(JGitText.Get().refNotResolved
                                                                    , Constants.HEAD));
            }
            RevCommit headCommit = walk.LookupCommit(headId);
            RevCommit upstream   = walk.LookupCommit(upstreamCommit.Id);

            if (walk.IsMergedInto(upstream, headCommit))
            {
                return(RebaseResult.UP_TO_DATE_RESULT);
            }
            else
            {
                if (walk.IsMergedInto(headCommit, upstream))
                {
                    // head is already merged into upstream, fast-foward
                    monitor.BeginTask(MessageFormat.Format(JGitText.Get().resettingHead, upstreamCommit
                                                           .GetShortMessage()), ProgressMonitor.UNKNOWN);
                    CheckoutCommit(upstreamCommit);
                    monitor.EndTask();
                    UpdateHead(headName, upstreamCommit);
                    return(RebaseResult.FAST_FORWARD_RESULT);
                }
            }
            monitor.BeginTask(JGitText.Get().obtainingCommitsForCherryPick, ProgressMonitor.UNKNOWN
                              );
            // determine the commits to be applied
            LogCommand           cmd            = new Git(repo).Log().AddRange(upstreamCommit, headCommit);
            Iterable <RevCommit> commitsToUse   = cmd.Call();
            IList <RevCommit>    cherryPickList = new AList <RevCommit>();

            foreach (RevCommit commit in commitsToUse)
            {
                if (commit.ParentCount != 1)
                {
                    throw new JGitInternalException(JGitText.Get().canOnlyCherryPickCommitsWithOneParent
                                                    );
                }
                cherryPickList.AddItem(commit);
            }
            Sharpen.Collections.Reverse(cherryPickList);
            // create the folder for the meta information
            FileUtils.Mkdir(rebaseDir);
            CreateFile(repo.Directory, Constants.ORIG_HEAD, headId.Name);
            CreateFile(rebaseDir, REBASE_HEAD, headId.Name);
            CreateFile(rebaseDir, HEAD_NAME, headName);
            CreateFile(rebaseDir, ONTO, upstreamCommit.Name);
            CreateFile(rebaseDir, INTERACTIVE, string.Empty);
            BufferedWriter fw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream
                                                                              (new FilePath(rebaseDir, GIT_REBASE_TODO)), Constants.CHARACTER_ENCODING));

            fw.Write("# Created by EGit: rebasing " + upstreamCommit.Name + " onto " + headId
                     .Name);
            fw.NewLine();
            try
            {
                StringBuilder sb     = new StringBuilder();
                ObjectReader  reader = walk.GetObjectReader();
                foreach (RevCommit commit_1 in cherryPickList)
                {
                    sb.Length = 0;
                    sb.Append(RebaseCommand.Action.PICK.ToToken());
                    sb.Append(" ");
                    sb.Append(reader.Abbreviate(commit_1).Name);
                    sb.Append(" ");
                    sb.Append(commit_1.GetShortMessage());
                    fw.Write(sb.ToString());
                    fw.NewLine();
                }
            }
            finally
            {
                fw.Close();
            }
            monitor.EndTask();
            // we rewind to the upstream commit
            monitor.BeginTask(MessageFormat.Format(JGitText.Get().rewinding, upstreamCommit.GetShortMessage
                                                       ()), ProgressMonitor.UNKNOWN);
            bool checkoutOk = false;

            try
            {
                checkoutOk = CheckoutCommit(upstreamCommit);
            }
            finally
            {
                if (!checkoutOk)
                {
                    FileUtils.Delete(rebaseDir, FileUtils.RECURSIVE);
                }
            }
            monitor.EndTask();
            return(null);
        }