Beispiel #1
0
        public virtual void ResolvePreviousBranch()
        {
            Git git = new Git(db);

            WriteTrashFile("file.txt", "content");
            git.Add().AddFilepattern("file.txt").Call();
            RevCommit c1 = git.Commit().SetMessage("create file").Call();

            WriteTrashFile("file.txt", "content2");
            git.Add().AddFilepattern("file.txt").Call();
            RevCommit c2 = git.Commit().SetMessage("edit file").Call();

            git.Checkout().SetCreateBranch(true).SetName("newbranch").SetStartPoint(c1).Call(
                );
            git.Checkout().SetName(c1.GetName()).Call();
            git.Checkout().SetName("master").Call();
            NUnit.Framework.Assert.AreEqual(c1.GetName(), db.Simplify("@{-1}"));
            NUnit.Framework.Assert.AreEqual("newbranch", db.Simplify("@{-2}"));
            NUnit.Framework.Assert.AreEqual("master", db.Simplify("@{-3}"));
            // chained expression
            try
            {
                // Cannot refer to reflog of detached head
                db.Resolve("@{-1}@{0}");
                NUnit.Framework.Assert.Fail();
            }
            catch (RevisionSyntaxException)
            {
            }
            // good
            NUnit.Framework.Assert.AreEqual(c1.GetName(), db.Resolve("@{-2}@{0}").GetName());
            NUnit.Framework.Assert.AreEqual(c2.GetName(), db.Resolve("@{-3}@{0}").GetName());
        }
        public virtual void TestHardResetAfterSquashMerge()
        {
            Git g = new Git(db);

            WriteTrashFile("file1", "file1");
            g.Add().AddFilepattern("file1").Call();
            RevCommit first = g.Commit().SetMessage("initial commit").Call();

            NUnit.Framework.Assert.IsTrue(new FilePath(db.WorkTree, "file1").Exists());
            CreateBranch(first, "refs/heads/branch1");
            CheckoutBranch("refs/heads/branch1");
            WriteTrashFile("file2", "file2");
            g.Add().AddFilepattern("file2").Call();
            g.Commit().SetMessage("second commit").Call();
            NUnit.Framework.Assert.IsTrue(new FilePath(db.WorkTree, "file2").Exists());
            CheckoutBranch("refs/heads/master");
            MergeCommandResult result = g.Merge().Include(db.GetRef("branch1")).SetSquash(true
                                                                                          ).Call();

            NUnit.Framework.Assert.AreEqual(MergeStatus.FAST_FORWARD_SQUASHED, result.GetMergeStatus
                                                ());
            NUnit.Framework.Assert.IsNotNull(db.ReadSquashCommitMsg());
            g.Reset().SetMode(ResetCommand.ResetType.HARD).SetRef(first.GetName()).Call();
            NUnit.Framework.Assert.IsNull(db.ReadSquashCommitMsg());
        }
Beispiel #3
0
        public virtual void ResolveUnnamedCurrentBranchCommits()
        {
            Git git = new Git(db);

            WriteTrashFile("file.txt", "content");
            git.Add().AddFilepattern("file.txt").Call();
            RevCommit c1 = git.Commit().SetMessage("create file").Call();

            WriteTrashFile("file.txt", "content2");
            git.Add().AddFilepattern("file.txt").Call();
            RevCommit c2 = git.Commit().SetMessage("edit file").Call();

            NUnit.Framework.Assert.AreEqual(c2, db.Resolve("master@{0}"));
            NUnit.Framework.Assert.AreEqual(c1, db.Resolve("master@{1}"));
            git.Checkout().SetCreateBranch(true).SetName("newbranch").SetStartPoint(c1).Call(
                );
            // same as current branch, e.g. master
            NUnit.Framework.Assert.AreEqual(c1, db.Resolve("@{0}"));
            try
            {
                NUnit.Framework.Assert.AreEqual(c1, db.Resolve("@{1}"));
                NUnit.Framework.Assert.Fail();
            }
            catch (RevisionSyntaxException e)
            {
                // Looking at wrong ref, e.g HEAD
                NUnit.Framework.Assert.IsNotNull(e);
            }
            // detached head, read HEAD reflog
            git.Checkout().SetName(c2.GetName()).Call();
            NUnit.Framework.Assert.AreEqual(c2, db.Resolve("@{0}"));
            NUnit.Framework.Assert.AreEqual(c1, db.Resolve("@{1}"));
            NUnit.Framework.Assert.AreEqual(c2, db.Resolve("@{2}"));
        }
Beispiel #4
0
        public virtual void TestHardReset()
        {
            SetupRepository();
            git.Reset().SetMode(ResetCommand.ResetType.HARD).SetRef(initialCommit.GetName()).
            Call();
            // check if HEAD points to initial commit now
            ObjectId head = db.Resolve(Constants.HEAD);

            NUnit.Framework.Assert.IsTrue(head.Equals(initialCommit));
            // check if files were removed
            NUnit.Framework.Assert.IsFalse(indexFile.Exists());
            NUnit.Framework.Assert.IsTrue(untrackedFile.Exists());
            // fileInIndex must no longer be in HEAD and in the index
            string fileInIndexPath = indexFile.GetAbsolutePath();

            NUnit.Framework.Assert.IsFalse(InHead(fileInIndexPath));
            NUnit.Framework.Assert.IsFalse(InIndex(indexFile.GetName()));
        }
        public virtual void TestCommit()
        {
            Git git = new Git(db);

            revCommit = git.Commit().SetMessage("squash_me").Call();
            Ref    master  = db.GetRef("refs/heads/master");
            string message = msgFormatter.Format(Arrays.AsList(revCommit), master);

            NUnit.Framework.Assert.AreEqual("Squashed commit of the following:\n\ncommit " +
                                            revCommit.GetName() + "\nAuthor: " + revCommit.GetAuthorIdent().GetName() + " <"
                                            + revCommit.GetAuthorIdent().GetEmailAddress() + ">\nDate:   " + dateFormatter.
                                            FormatDate(author) + "\n\n\tsquash_me\n", message);
        }
Beispiel #6
0
        public virtual void TestResetHardFromIndexEntryWithoutFileToTreeWithoutFile()
        {
            Git git = new Git(db);

            WriteTrashFile("x", "x");
            git.Add().AddFilepattern("x").Call();
            RevCommit id1 = git.Commit().SetMessage("c1").Call();

            WriteTrashFile("f/g", "f/g");
            git.Rm().AddFilepattern("x").Call();
            git.Add().AddFilepattern("f/g").Call();
            git.Commit().SetMessage("c2").Call();
            DeleteTrashFile("f/g");
            DeleteTrashFile("f");
            // The actual test
            git.Reset().SetMode(ResetCommand.ResetType.HARD).SetRef(id1.GetName()).Call();
            AssertIndex(Mkmap("x", "x"));
        }
        public virtual void TestMixedResetRetainsSizeAndModifiedTime()
        {
            git = new Git(db);
            WriteTrashFile("a.txt", "a").SetLastModified(Runtime.CurrentTimeMillis() - 60 * 1000
                                                         );
            NUnit.Framework.Assert.IsNotNull(git.Add().AddFilepattern("a.txt").Call());
            NUnit.Framework.Assert.IsNotNull(git.Commit().SetMessage("a commit").Call());
            WriteTrashFile("b.txt", "b").SetLastModified(Runtime.CurrentTimeMillis() - 60 * 1000
                                                         );
            NUnit.Framework.Assert.IsNotNull(git.Add().AddFilepattern("b.txt").Call());
            RevCommit commit2 = git.Commit().SetMessage("b commit").Call();

            NUnit.Framework.Assert.IsNotNull(commit2);
            DirCache      cache  = db.ReadDirCache();
            DirCacheEntry aEntry = cache.GetEntry("a.txt");

            NUnit.Framework.Assert.IsNotNull(aEntry);
            NUnit.Framework.Assert.IsTrue(aEntry.Length > 0);
            NUnit.Framework.Assert.IsTrue(aEntry.LastModified > 0);
            DirCacheEntry bEntry = cache.GetEntry("b.txt");

            NUnit.Framework.Assert.IsNotNull(bEntry);
            NUnit.Framework.Assert.IsTrue(bEntry.Length > 0);
            NUnit.Framework.Assert.IsTrue(bEntry.LastModified > 0);
            git.Reset().SetMode(ResetCommand.ResetType.MIXED).SetRef(commit2.GetName()).Call(
                );
            cache = db.ReadDirCache();
            DirCacheEntry mixedAEntry = cache.GetEntry("a.txt");

            NUnit.Framework.Assert.IsNotNull(mixedAEntry);
            NUnit.Framework.Assert.AreEqual(aEntry.LastModified, mixedAEntry.LastModified);
            NUnit.Framework.Assert.AreEqual(aEntry.LastModified, mixedAEntry.LastModified);
            DirCacheEntry mixedBEntry = cache.GetEntry("b.txt");

            NUnit.Framework.Assert.IsNotNull(mixedBEntry);
            NUnit.Framework.Assert.AreEqual(bEntry.LastModified, mixedBEntry.LastModified);
            NUnit.Framework.Assert.AreEqual(bEntry.LastModified, mixedBEntry.LastModified);
        }
Beispiel #8
0
        /// <exception cref="NGit.Api.Errors.RefAlreadyExistsException">
        /// when trying to create (without force) a branch with a name
        /// that already exists
        /// </exception>
        /// <exception cref="NGit.Api.Errors.RefNotFoundException">if the start point or branch can not be found
        ///     </exception>
        /// <exception cref="NGit.Api.Errors.InvalidRefNameException">
        /// if the provided name is <code>null</code> or otherwise
        /// invalid
        /// </exception>
        /// <returns>the newly created branch</returns>
        /// <exception cref="NGit.Api.Errors.JGitInternalException"></exception>
        public override Ref Call()
        {
            CheckCallable();
            ProcessOptions();
            try
            {
                if (createBranch)
                {
                    Git git = new Git(repo);
                    CreateBranchCommand command = git.BranchCreate();
                    command.SetName(name);
                    command.SetStartPoint(GetStartPoint().Name);
                    if (upstreamMode != null)
                    {
                        command.SetUpstreamMode(upstreamMode);
                    }
                    command.Call();
                }
                Ref      headRef       = repo.GetRef(Constants.HEAD);
                string   refLogMessage = "checkout: moving from " + headRef.GetTarget().GetName();
                ObjectId branch        = repo.Resolve(name);
                if (branch == null)
                {
                    throw new RefNotFoundException(MessageFormat.Format(JGitText.Get().refNotResolved
                                                                        , name));
                }
                RevWalk          revWalk    = new RevWalk(repo);
                AnyObjectId      headId     = headRef.GetObjectId();
                RevCommit        headCommit = headId == null ? null : revWalk.ParseCommit(headId);
                RevCommit        newCommit  = revWalk.ParseCommit(branch);
                RevTree          headTree   = headCommit == null ? null : headCommit.Tree;
                DirCacheCheckout dco        = new DirCacheCheckout(repo, headTree, repo.LockDirCache(),
                                                                   newCommit.Tree);
                dco.SetFailOnConflict(true);
                try
                {
                    dco.Checkout();
                }
                catch (NGit.Errors.CheckoutConflictException e)
                {
                    status = new CheckoutResult(CheckoutResult.Status.CONFLICTS, dco.GetConflicts());
                    throw;
                }
                Ref @ref = repo.GetRef(name);
                if (@ref != null && [email protected]().StartsWith(Constants.R_HEADS))
                {
                    @ref = null;
                }
                RefUpdate refUpdate = repo.UpdateRef(Constants.HEAD, @ref == null);
                refUpdate.SetForceUpdate(force);
                refUpdate.SetRefLogMessage(refLogMessage + " to " + newCommit.GetName(), false);
                RefUpdate.Result updateResult;
                if (@ref != null)
                {
                    updateResult = refUpdate.Link(@ref.GetName());
                }
                else
                {
                    refUpdate.SetNewObjectId(newCommit);
                    updateResult = refUpdate.ForceUpdate();
                }
                SetCallable(false);
                bool ok = false;
                switch (updateResult)
                {
                case RefUpdate.Result.NEW:
                {
                    ok = true;
                    break;
                }

                case RefUpdate.Result.NO_CHANGE:
                case RefUpdate.Result.FAST_FORWARD:
                case RefUpdate.Result.FORCED:
                {
                    ok = true;
                    break;
                }

                default:
                {
                    break;
                    break;
                }
                }
                if (!ok)
                {
                    throw new JGitInternalException(MessageFormat.Format(JGitText.Get().checkoutUnexpectedResult
                                                                         , updateResult.ToString()));
                }
                if (!dco.GetToBeDeleted().IsEmpty())
                {
                    status = new CheckoutResult(CheckoutResult.Status.NONDELETED, dco.GetToBeDeleted(
                                                    ));
                }
                else
                {
                    status = CheckoutResult.OK_RESULT;
                }
                return(@ref);
            }
            catch (IOException ioe)
            {
                throw new JGitInternalException(ioe.Message, ioe);
            }
            finally
            {
                if (status == null)
                {
                    status = CheckoutResult.ERROR_RESULT;
                }
            }
        }