示例#1
0
        public virtual void TestRefsCacheAfterUpdate()
        {
            // Do not use the defalt repo for this case.
            IDictionary <string, Ref> allRefs = db.GetAllRefs();
            ObjectId oldValue = db.Resolve("HEAD");
            ObjectId newValue = db.Resolve("HEAD^");
            // first make HEAD refer to loose ref
            RefUpdate updateRef = db.UpdateRef(Constants.HEAD);

            updateRef.SetForceUpdate(true);
            updateRef.SetNewObjectId(newValue);
            RefUpdate.Result update = updateRef.Update();
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.FORCED, update);
            // now update that ref
            updateRef = db.UpdateRef(Constants.HEAD);
            updateRef.SetForceUpdate(true);
            updateRef.SetNewObjectId(oldValue);
            update = updateRef.Update();
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.FAST_FORWARD, update);
            allRefs = db.GetAllRefs();
            Ref master = allRefs.Get("refs/heads/master");
            Ref head   = allRefs.Get("HEAD");

            NUnit.Framework.Assert.AreEqual("refs/heads/master", master.GetName());
            NUnit.Framework.Assert.AreEqual("HEAD", head.GetName());
            NUnit.Framework.Assert.IsTrue(head.IsSymbolic(), "is symbolic reference");
            NUnit.Framework.Assert.AreSame(master, head.GetTarget());
        }
示例#2
0
        public virtual void DeleteMergedBranch_historyNotPruned()
        {
            RevCommit parent = tr.Commit().Create();
            RevCommit b1Tip  = tr.Branch("b1").Commit().Parent(parent).Add("x", "x").Create();
            RevCommit b2Tip  = tr.Branch("b2").Commit().Parent(parent).Add("y", "y").Create();
            // merge b1Tip and b2Tip and update refs/heads/b1 to the merge commit
            Merger merger = ((ThreeWayMerger)MergeStrategy.SIMPLE_TWO_WAY_IN_CORE.NewMerger(repo
                                                                                            ));

            merger.Merge(b1Tip, b2Tip);
            NGit.Junit.CommitBuilder cb = tr.Commit();
            cb.Parent(b1Tip).Parent(b2Tip);
            cb.SetTopLevelTree(merger.GetResultTreeId());
            RevCommit mergeCommit = cb.Create();
            RefUpdate u           = repo.UpdateRef("refs/heads/b1");

            u.SetNewObjectId(mergeCommit);
            u.Update();
            RefUpdate update = repo.UpdateRef("refs/heads/b2");

            update.SetForceUpdate(true);
            update.Delete();
            gc.SetExpireAgeMillis(0);
            gc.Prune(Collections.EmptySet <ObjectId>());
            NUnit.Framework.Assert.IsTrue(repo.HasObject(b2Tip));
        }
示例#3
0
        public virtual void TestDeleteLooseAndItsDirectory()
        {
            ObjectId  pid       = db.Resolve("refs/heads/c^");
            RefUpdate updateRef = db.UpdateRef("refs/heads/z/c");

            updateRef.SetNewObjectId(pid);
            updateRef.SetForceUpdate(true);
            updateRef.SetRefLogMessage("new test ref", false);
            RefUpdate.Result update = updateRef.Update();
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.NEW, update);
            // internal
            NUnit.Framework.Assert.IsTrue(new FilePath(db.Directory, Constants.R_HEADS + "z")
                                          .Exists());
            NUnit.Framework.Assert.IsTrue(new FilePath(db.Directory, "logs/refs/heads/z").Exists
                                              ());
            // The real test here
            RefUpdate updateRef2 = db.UpdateRef("refs/heads/z/c");

            updateRef2.SetForceUpdate(true);
            RefUpdate.Result delete = updateRef2.Delete();
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.FORCED, delete);
            NUnit.Framework.Assert.IsNull(db.Resolve("refs/heads/z/c"));
            NUnit.Framework.Assert.IsFalse(new FilePath(db.Directory, Constants.R_HEADS + "z"
                                                        ).Exists());
            NUnit.Framework.Assert.IsFalse(new FilePath(db.Directory, "logs/refs/heads/z").Exists
                                               ());
        }
示例#4
0
        public virtual void TestUpdateRefDetachedUnbornHead()
        {
            ObjectId ppid = db.Resolve("refs/heads/master^");

            WriteSymref("HEAD", "refs/heads/unborn");
            RefUpdate updateRef = db.UpdateRef("HEAD", true);

            updateRef.SetForceUpdate(true);
            updateRef.SetNewObjectId(ppid);
            RefUpdate.Result update = updateRef.Update();
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.NEW, update);
            NUnit.Framework.Assert.AreEqual(ppid, db.Resolve("HEAD"));
            Ref @ref = db.GetRef("HEAD");

            NUnit.Framework.Assert.AreEqual("HEAD", @ref.GetName());
            NUnit.Framework.Assert.IsTrue([email protected](), "is detached");
            // the branch HEAD referred to is left untouched
            NUnit.Framework.Assert.IsNull(db.Resolve("refs/heads/unborn"));
            ReflogReader reflogReader = new ReflogReader(db, "HEAD");
            ReflogEntry  e            = reflogReader.GetReverseEntries()[0];

            NUnit.Framework.Assert.AreEqual(ObjectId.ZeroId, e.GetOldId());
            NUnit.Framework.Assert.AreEqual(ppid, e.GetNewId());
            NUnit.Framework.Assert.AreEqual("GIT_COMMITTER_EMAIL", e.GetWho().GetEmailAddress
                                                ());
            NUnit.Framework.Assert.AreEqual("GIT_COMMITTER_NAME", e.GetWho().GetName());
            NUnit.Framework.Assert.AreEqual(1250379778000L, e.GetWho().GetWhen().GetTime());
        }
示例#5
0
        public virtual void TestDeleteForce()
        {
            RefUpdate @ref = db.UpdateRef("refs/heads/b");

            @ref.SetNewObjectId(db.Resolve("refs/heads/a"));
            Delete(@ref, RefUpdate.Result.REJECTED, true, false);
            @ref.SetForceUpdate(true);
            Delete(@ref, RefUpdate.Result.FORCED);
        }
        /// <exception cref="System.Exception"></exception>
        public virtual void TestPullEmptyRepository()
        {
            Repository empty  = CreateWorkRepository();
            RefUpdate  delete = empty.UpdateRef(Constants.HEAD, true);

            delete.SetForceUpdate(true);
            delete.Delete();
            Git.Wrap(empty).Pull().Call();
        }
示例#7
0
        /// <exception cref="System.IO.IOException"></exception>
        private RefUpdate CreateRefUpdate(Ref stashRef)
        {
            RefUpdate update = repo.UpdateRef(Constants.R_STASH);

            update.DisableRefLog();
            update.SetExpectedOldObjectId(stashRef.GetObjectId());
            update.SetForceUpdate(true);
            return(update);
        }
示例#8
0
 /// <exception cref="System.IO.IOException"></exception>
 internal TrackingRefUpdate(Repository db, string localName, string remoteName, bool
                            forceUpdate, AnyObjectId nv, string msg)
 {
     this.remoteName = remoteName;
     update          = db.UpdateRef(localName);
     update.SetForceUpdate(forceUpdate);
     update.SetNewObjectId(nv);
     update.SetRefLogMessage(msg, true);
 }
示例#9
0
 /// <summary>Construct remote ref update request by providing an update specification.
 ///     </summary>
 /// <remarks>
 /// Construct remote ref update request by providing an update specification.
 /// Object is created with default
 /// <see cref="Status.NOT_ATTEMPTED">Status.NOT_ATTEMPTED</see>
 /// status and no
 /// message.
 /// </remarks>
 /// <param name="localDb">local repository to push from.</param>
 /// <param name="srcRef">
 /// source revision to label srcId with. If null srcId.name() will
 /// be used instead.
 /// </param>
 /// <param name="srcId">
 /// The new object that the caller wants remote ref to be after
 /// update. Use null or
 /// <see cref="NGit.ObjectId.ZeroId()">NGit.ObjectId.ZeroId()</see>
 /// for delete
 /// request.
 /// </param>
 /// <param name="remoteName">
 /// full name of a remote ref to update, e.g. "refs/heads/master"
 /// (no wildcard, no short name).
 /// </param>
 /// <param name="forceUpdate">
 /// true when caller want remote ref to be updated regardless
 /// whether it is fast-forward update (old object is ancestor of
 /// new object).
 /// </param>
 /// <param name="localName">
 /// optional full name of a local stored tracking branch, to
 /// update after push, e.g. "refs/remotes/zawir/dirty" (no
 /// wildcard, no short name); null if no local tracking branch
 /// should be updated.
 /// </param>
 /// <param name="expectedOldObjectId">
 /// optional object id that caller is expecting, requiring to be
 /// advertised by remote side before update; update will take
 /// place ONLY if remote side advertise exactly this expected id;
 /// null if caller doesn't care what object id remote side
 /// advertise. Use
 /// <see cref="NGit.ObjectId.ZeroId()">NGit.ObjectId.ZeroId()</see>
 /// when expecting no
 /// remote ref with this name.
 /// </param>
 /// <exception cref="System.IO.IOException">
 /// when I/O error occurred during creating
 /// <see cref="TrackingRefUpdate">TrackingRefUpdate</see>
 /// for local tracking branch or srcRef
 /// can't be resolved to any object.
 /// </exception>
 /// <exception cref="System.ArgumentException">if some required parameter was null</exception>
 public RemoteRefUpdate(Repository localDb, string srcRef, ObjectId srcId, string
                        remoteName, bool forceUpdate, string localName, ObjectId expectedOldObjectId)
 {
     if (remoteName == null)
     {
         throw new ArgumentException(JGitText.Get().remoteNameCantBeNull);
     }
     if (srcId == null && srcRef != null)
     {
         throw new IOException(MessageFormat.Format(JGitText.Get().sourceRefDoesntResolveToAnyObject
                                                    , srcRef));
     }
     if (srcRef != null)
     {
         this.srcRef = srcRef;
     }
     else
     {
         if (srcId != null && !srcId.Equals(ObjectId.ZeroId))
         {
             this.srcRef = srcId.Name;
         }
         else
         {
             this.srcRef = null;
         }
     }
     if (srcId != null)
     {
         this.newObjectId = srcId;
     }
     else
     {
         this.newObjectId = ObjectId.ZeroId;
     }
     this.remoteName  = remoteName;
     this.forceUpdate = forceUpdate;
     if (localName != null && localDb != null)
     {
         localUpdate = localDb.UpdateRef(localName);
         localUpdate.SetForceUpdate(true);
         localUpdate.SetRefLogMessage("push", true);
         localUpdate.SetNewObjectId(newObjectId);
         trackingRefUpdate = new TrackingRefUpdate(true, remoteName, localName, localUpdate
                                                   .GetOldObjectId() != null ? localUpdate.GetOldObjectId() : ObjectId.ZeroId, newObjectId
                                                   );
     }
     else
     {
         trackingRefUpdate = null;
     }
     this.localDb             = localDb;
     this.expectedOldObjectId = expectedOldObjectId;
     this.status = RemoteRefUpdate.Status.NOT_ATTEMPTED;
 }
        /// <summary>Execute this command during a receive-pack session.</summary>
        /// <remarks>
        /// Execute this command during a receive-pack session.
        /// <p>
        /// Sets the status of the command as a side effect.
        /// </remarks>
        /// <param name="rp">receive-pack session.</param>
        /// <since>2.0</since>
        public virtual void Execute(BaseReceivePack rp)
        {
            try
            {
                RefUpdate ru = rp.GetRepository().UpdateRef(GetRefName());
                ru.SetRefLogIdent(rp.GetRefLogIdent());
                switch (GetType())
                {
                case ReceiveCommand.Type.DELETE:
                {
                    if (!ObjectId.ZeroId.Equals(GetOldId()))
                    {
                        // We can only do a CAS style delete if the client
                        // didn't bork its delete request by sending the
                        // wrong zero id rather than the advertised one.
                        //
                        ru.SetExpectedOldObjectId(GetOldId());
                    }
                    ru.SetForceUpdate(true);
                    SetResult(ru.Delete(rp.GetRevWalk()));
                    break;
                }

                case ReceiveCommand.Type.CREATE:
                case ReceiveCommand.Type.UPDATE:
                case ReceiveCommand.Type.UPDATE_NONFASTFORWARD:
                {
                    ru.SetForceUpdate(rp.IsAllowNonFastForwards());
                    ru.SetExpectedOldObjectId(GetOldId());
                    ru.SetNewObjectId(GetNewId());
                    ru.SetRefLogMessage("push", true);
                    SetResult(ru.Update(rp.GetRevWalk()));
                    break;
                }
                }
            }
            catch (IOException err)
            {
                Reject(err);
            }
        }
示例#11
0
        public virtual void DeleteBranch_historyPruned()
        {
            RevCommit tip = CommitChain(10);

            tr.Branch("b").Update(tip);
            RefUpdate update = repo.UpdateRef("refs/heads/b");

            update.SetForceUpdate(true);
            update.Delete();
            gc.SetExpireAgeMillis(0);
            gc.Prune(Collections.EmptySet <ObjectId>());
            NUnit.Framework.Assert.IsTrue(gc.GetStatistics().numberOfLooseObjects == 0);
        }
示例#12
0
        public virtual void TestDeleteLoosePacked()
        {
            ObjectId  pid       = db.Resolve("refs/heads/c^");
            RefUpdate updateRef = db.UpdateRef("refs/heads/c");

            updateRef.SetNewObjectId(pid);
            updateRef.SetForceUpdate(true);
            RefUpdate.Result update = updateRef.Update();
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.FORCED, update);
            // internal
            // The real test here
            RefUpdate updateRef2 = db.UpdateRef("refs/heads/c");

            updateRef2.SetForceUpdate(true);
            RefUpdate.Result delete = updateRef2.Delete();
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.FORCED, delete);
            NUnit.Framework.Assert.IsNull(db.Resolve("refs/heads/c"));
        }
        public static void HardReset(NGit.Repository repo, ObjectId newHead)
        {
            DirCache dc = null;

            try
            {
                // Reset head to upstream
                RefUpdate ru = repo.UpdateRef(Constants.HEAD);
                ru.SetNewObjectId(newHead);
                ru.SetForceUpdate(true);
                RefUpdate.Result rc = ru.Update();

                switch (rc)
                {
                case RefUpdate.Result.NO_CHANGE:
                case RefUpdate.Result.NEW:
                case RefUpdate.Result.FAST_FORWARD:
                case RefUpdate.Result.FORCED:
                    break;

                case RefUpdate.Result.REJECTED:
                case RefUpdate.Result.LOCK_FAILURE:
                    throw new ConcurrentRefUpdateException(JGitText.Get().couldNotLockHEAD, ru.GetRef(), rc);

                default:
                    throw new JGitInternalException("Reference update failed: " + rc);
                }

                dc = repo.LockDirCache();
                RevWalk          rw       = new RevWalk(repo);
                RevCommit        c        = rw.ParseCommit(newHead);
                DirCacheCheckout checkout = new DirCacheCheckout(repo, null, dc, c.Tree);
                checkout.Checkout();
            }
            catch
            {
                if (dc != null)
                {
                    dc.Unlock();
                }
                throw;
            }
        }
示例#14
0
        public virtual void TestRenameBranchAlsoInPack()
        {
            ObjectId rb  = db.Resolve("refs/heads/b");
            ObjectId rb2 = db.Resolve("refs/heads/b~1");

            NUnit.Framework.Assert.AreEqual(RefStorage.PACKED, db.GetRef("refs/heads/b").GetStorage
                                                ());
            RefUpdate updateRef = db.UpdateRef("refs/heads/b");

            updateRef.SetNewObjectId(rb2);
            updateRef.SetForceUpdate(true);
            RefUpdate.Result update = updateRef.Update();
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.FORCED, update, "internal check new ref is loose"
                                            );
            NUnit.Framework.Assert.AreEqual(RefStorage.LOOSE, db.GetRef("refs/heads/b").GetStorage
                                                ());
            WriteReflog(db, rb, "Just a message", "refs/heads/b");
            NUnit.Framework.Assert.IsTrue(new FilePath(db.Directory, "logs/refs/heads/b").Exists
                                              (), "log on old branch");
            RefRename renameRef = db.RenameRef("refs/heads/b", "refs/heads/new/name");

            RefUpdate.Result result = renameRef.Rename();
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.RENAMED, result);
            NUnit.Framework.Assert.AreEqual(rb2, db.Resolve("refs/heads/new/name"));
            NUnit.Framework.Assert.IsNull(db.Resolve("refs/heads/b"));
            NUnit.Framework.Assert.AreEqual("Branch: renamed b to new/name", db.GetReflogReader
                                                ("new/name").GetLastEntry().GetComment());
            NUnit.Framework.Assert.AreEqual(3, db.GetReflogReader("refs/heads/new/name").GetReverseEntries
                                                ().Count);
            NUnit.Framework.Assert.AreEqual("Branch: renamed b to new/name", db.GetReflogReader
                                                ("refs/heads/new/name").GetReverseEntries()[0].GetComment());
            NUnit.Framework.Assert.AreEqual(0, db.GetReflogReader("HEAD").GetReverseEntries()
                                            .Count);
            // make sure b's log file is gone too.
            NUnit.Framework.Assert.IsFalse(new FilePath(db.Directory, "logs/refs/heads/b").Exists
                                               ());
            // Create new Repository instance, to reread caches and make sure our
            // assumptions are persistent.
            Repository ndb = new FileRepository(db.Directory);

            NUnit.Framework.Assert.AreEqual(rb2, ndb.Resolve("refs/heads/new/name"));
            NUnit.Framework.Assert.IsNull(ndb.Resolve("refs/heads/b"));
        }
示例#15
0
        public virtual void TestUpdateRefForward()
        {
            ObjectId  ppid      = db.Resolve("refs/heads/master^");
            ObjectId  pid       = db.Resolve("refs/heads/master");
            RefUpdate updateRef = db.UpdateRef("refs/heads/master");

            updateRef.SetNewObjectId(ppid);
            updateRef.SetForceUpdate(true);
            RefUpdate.Result update = updateRef.Update();
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.FORCED, update);
            NUnit.Framework.Assert.AreEqual(ppid, db.Resolve("refs/heads/master"));
            // real test
            RefUpdate updateRef2 = db.UpdateRef("refs/heads/master");

            updateRef2.SetNewObjectId(pid);
            RefUpdate.Result update2 = updateRef2.Update();
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.FAST_FORWARD, update2);
            NUnit.Framework.Assert.AreEqual(pid, db.Resolve("refs/heads/master"));
        }
示例#16
0
        public override void SetUp()
        {
            base.SetUp();
            git = new Git(db);
            // checkout master
            git.Commit().SetMessage("initial commit").Call();
            // commit something
            WriteTrashFile("Test.txt", "Hello world");
            git.Add().AddFilepattern("Test.txt").Call();
            initialCommit = git.Commit().SetMessage("Initial commit").Call();
            WriteTrashFile("Test.txt", "Some change");
            git.Add().AddFilepattern("Test.txt").Call();
            secondCommit = git.Commit().SetMessage("Second commit").Call();
            // create a master branch
            RefUpdate rup = db.UpdateRef("refs/heads/master");

            rup.SetNewObjectId(initialCommit.Id);
            rup.SetForceUpdate(true);
            rup.Update();
        }
示例#17
0
        public virtual void TestDeleteHEADreferencedRef()
        {
            ObjectId  pid       = db.Resolve("refs/heads/master^");
            RefUpdate updateRef = db.UpdateRef("refs/heads/master");

            updateRef.SetNewObjectId(pid);
            updateRef.SetForceUpdate(true);
            RefUpdate.Result update = updateRef.Update();
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.FORCED, update);
            // internal
            RefUpdate updateRef2 = db.UpdateRef("refs/heads/master");

            RefUpdate.Result delete = updateRef2.Delete();
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.REJECTED_CURRENT_BRANCH, delete);
            NUnit.Framework.Assert.AreEqual(pid, db.Resolve("refs/heads/master"));
            NUnit.Framework.Assert.AreEqual(1, db.GetReflogReader("refs/heads/master").GetReverseEntries
                                                ().Count);
            NUnit.Framework.Assert.AreEqual(0, db.GetReflogReader("HEAD").GetReverseEntries()
                                            .Count);
        }
示例#18
0
        public virtual void TestUpdateRefForwardWithCheck2()
        {
            ObjectId  ppid      = db.Resolve("refs/heads/master^");
            ObjectId  pid       = db.Resolve("refs/heads/master");
            RefUpdate updateRef = db.UpdateRef("refs/heads/master");

            updateRef.SetNewObjectId(ppid);
            updateRef.SetForceUpdate(true);
            RefUpdate.Result update = updateRef.Update();
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.FORCED, update);
            NUnit.Framework.Assert.AreEqual(ppid, db.Resolve("refs/heads/master"));
            // real test
            RevCommit old        = new RevWalk(db).ParseCommit(ppid);
            RefUpdate updateRef2 = db.UpdateRef("refs/heads/master");

            updateRef2.SetExpectedOldObjectId(old);
            updateRef2.SetNewObjectId(pid);
            RefUpdate.Result update2 = updateRef2.Update();
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.FAST_FORWARD, update2);
            NUnit.Framework.Assert.AreEqual(pid, db.Resolve("refs/heads/master"));
        }
示例#19
0
        public virtual void TestRefsCacheAfterUpdateLooseOnly()
        {
            // Do not use the defalt repo for this case.
            IDictionary <string, Ref> allRefs = db.GetAllRefs();
            ObjectId oldValue = db.Resolve("HEAD");

            WriteSymref(Constants.HEAD, "refs/heads/newref");
            RefUpdate updateRef = db.UpdateRef(Constants.HEAD);

            updateRef.SetForceUpdate(true);
            updateRef.SetNewObjectId(oldValue);
            RefUpdate.Result update = updateRef.Update();
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.NEW, update);
            allRefs = db.GetAllRefs();
            Ref head   = allRefs.Get("HEAD");
            Ref newref = allRefs.Get("refs/heads/newref");

            NUnit.Framework.Assert.AreEqual("refs/heads/newref", newref.GetName());
            NUnit.Framework.Assert.AreEqual("HEAD", head.GetName());
            NUnit.Framework.Assert.IsTrue(head.IsSymbolic(), "is symbolic reference");
            NUnit.Framework.Assert.AreSame(newref, head.GetTarget());
        }
示例#20
0
        public virtual void TestRenameRefNameColission2avoided()
        {
            // setup
            ObjectId rb = db.Resolve("refs/heads/b");

            WriteSymref(Constants.HEAD, "refs/heads/prefix/a");
            RefUpdate updateRef = db.UpdateRef("refs/heads/prefix/a");

            updateRef.SetNewObjectId(rb);
            updateRef.SetRefLogMessage("Setup", false);
            updateRef.SetForceUpdate(true);
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.FORCED, updateRef.Update());
            ObjectId oldHead = db.Resolve(Constants.HEAD);

            NUnit.Framework.Assert.AreEqual(oldHead, rb);
            // assumption for this test
            WriteReflog(db, rb, "Just a message", "refs/heads/prefix/a");
            NUnit.Framework.Assert.IsTrue(new FilePath(db.Directory, "logs/refs/heads/prefix/a"
                                                       ).Exists(), "internal check, we have a log");
            // Now this is our test
            RefRename renameRef = db.RenameRef("refs/heads/prefix/a", "refs/heads/prefix");

            RefUpdate.Result result = renameRef.Rename();
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.RENAMED, result);
            NUnit.Framework.Assert.IsNull(db.Resolve("refs/heads/prefix/a"));
            NUnit.Framework.Assert.AreEqual(rb, db.Resolve("refs/heads/prefix"));
            NUnit.Framework.Assert.AreEqual(3, db.GetReflogReader("prefix").GetReverseEntries
                                                ().Count);
            NUnit.Framework.Assert.AreEqual("Branch: renamed prefix/a to prefix", db.GetReflogReader
                                                ("prefix").GetReverseEntries()[0].GetComment());
            NUnit.Framework.Assert.AreEqual("Just a message", db.GetReflogReader("prefix").GetReverseEntries
                                                ()[1].GetComment());
            NUnit.Framework.Assert.AreEqual("Setup", db.GetReflogReader("prefix").GetReverseEntries
                                                ()[2].GetComment());
            NUnit.Framework.Assert.AreEqual("Branch: renamed prefix/a to prefix", db.GetReflogReader
                                                ("HEAD").GetReverseEntries()[0].GetComment());
        }
示例#21
0
        /// <exception cref="NGit.Api.Errors.NotMergedException">
        /// when trying to delete a branch which has not been merged into
        /// the currently checked out branch without force
        /// </exception>
        /// <exception cref="NGit.Api.Errors.CannotDeleteCurrentBranchException">NGit.Api.Errors.CannotDeleteCurrentBranchException
        ///     </exception>
        /// <returns>the list with the (full) names of the deleted branches</returns>
        /// <exception cref="NGit.Api.Errors.GitAPIException"></exception>
        public override IList <string> Call()
        {
            CheckCallable();
            IList <string> result = new AList <string>();

            if (branchNames.IsEmpty())
            {
                return(result);
            }
            try
            {
                string currentBranch = repo.GetFullBranch();
                if (!force)
                {
                    // check if the branches to be deleted
                    // are all merged into the current branch
                    RevWalk   walk = new RevWalk(repo);
                    RevCommit tip  = walk.ParseCommit(repo.Resolve(Constants.HEAD));
                    foreach (string branchName in branchNames)
                    {
                        if (branchName == null)
                        {
                            continue;
                        }
                        Ref currentRef = repo.GetRef(branchName);
                        if (currentRef == null)
                        {
                            continue;
                        }
                        RevCommit @base = walk.ParseCommit(repo.Resolve(branchName));
                        if (!walk.IsMergedInto(@base, tip))
                        {
                            throw new NotMergedException();
                        }
                    }
                }
                SetCallable(false);
                foreach (string branchName_1 in branchNames)
                {
                    if (branchName_1 == null)
                    {
                        continue;
                    }
                    Ref currentRef = repo.GetRef(branchName_1);
                    if (currentRef == null)
                    {
                        continue;
                    }
                    string fullName = currentRef.GetName();
                    if (fullName.Equals(currentBranch))
                    {
                        throw new CannotDeleteCurrentBranchException(MessageFormat.Format(JGitText.Get().
                                                                                          cannotDeleteCheckedOutBranch, branchName_1));
                    }
                    RefUpdate update = repo.UpdateRef(fullName);
                    update.SetRefLogMessage("branch deleted", false);
                    update.SetForceUpdate(true);
                    RefUpdate.Result deleteResult = update.Delete();
                    bool             ok           = true;
                    switch (deleteResult)
                    {
                    case RefUpdate.Result.IO_FAILURE:
                    case RefUpdate.Result.LOCK_FAILURE:
                    case RefUpdate.Result.REJECTED:
                    {
                        ok = false;
                        break;
                    }

                    default:
                    {
                        break;
                        break;
                    }
                    }
                    if (ok)
                    {
                        result.AddItem(fullName);
                        if (fullName.StartsWith(Constants.R_HEADS))
                        {
                            string shortenedName = Sharpen.Runtime.Substring(fullName, Constants.R_HEADS.Length
                                                                             );
                            // remove upstream configuration if any
                            StoredConfig cfg = repo.GetConfig();
                            cfg.UnsetSection(ConfigConstants.CONFIG_BRANCH_SECTION, shortenedName);
                            cfg.Save();
                        }
                    }
                    else
                    {
                        throw new JGitInternalException(MessageFormat.Format(JGitText.Get().deleteBranchUnexpectedResult
                                                                             , deleteResult.ToString()));
                    }
                }
                return(result);
            }
            catch (IOException ioe)
            {
                throw new JGitInternalException(ioe.Message, ioe);
            }
        }
示例#22
0
        /// <summary>
        /// Executes the
        /// <code>tag</code>
        /// command with all the options and parameters
        /// collected by the setter methods of this class. Each instance of this
        /// class should only be used for one invocation of the command (means: one
        /// call to
        /// <see cref="Call()">Call()</see>
        /// )
        /// </summary>
        /// <returns>
        /// a
        /// <see cref="NGit.Ref">NGit.Ref</see>
        /// a ref pointing to a tag
        /// </returns>
        /// <exception cref="NGit.Api.Errors.NoHeadException">when called on a git repo without a HEAD reference
        ///     </exception>
        /// <since>2.0</since>
        /// <exception cref="NGit.Api.Errors.GitAPIException"></exception>
        /// <exception cref="NGit.Api.Errors.ConcurrentRefUpdateException"></exception>
        /// <exception cref="NGit.Api.Errors.InvalidTagNameException"></exception>
        public override Ref Call()
        {
            CheckCallable();
            RepositoryState state = repo.GetRepositoryState();

            ProcessOptions(state);
            try
            {
                // create the tag object
                TagBuilder newTag = new TagBuilder();
                newTag.SetTag(name);
                newTag.SetMessage(message);
                newTag.SetTagger(tagger);
                // if no id is set, we should attempt to use HEAD
                if (id == null)
                {
                    ObjectId objectId = repo.Resolve(Constants.HEAD + "^{commit}");
                    if (objectId == null)
                    {
                        throw new NoHeadException(JGitText.Get().tagOnRepoWithoutHEADCurrentlyNotSupported
                                                  );
                    }
                    newTag.SetObjectId(objectId, Constants.OBJ_COMMIT);
                }
                else
                {
                    newTag.SetObjectId(id);
                }
                // write the tag object
                ObjectInserter inserter = repo.NewObjectInserter();
                try
                {
                    ObjectId tagId = inserter.Insert(newTag);
                    inserter.Flush();
                    RevWalk revWalk = new RevWalk(repo);
                    try
                    {
                        string    refName = Constants.R_TAGS + newTag.GetTag();
                        RefUpdate tagRef  = repo.UpdateRef(refName);
                        tagRef.SetNewObjectId(tagId);
                        tagRef.SetForceUpdate(forceUpdate);
                        tagRef.SetRefLogMessage("tagged " + name, false);
                        RefUpdate.Result updateResult = tagRef.Update(revWalk);
                        switch (updateResult)
                        {
                        case RefUpdate.Result.NEW:
                        case RefUpdate.Result.FORCED:
                        {
                            return(repo.GetRef(refName));
                        }

                        case RefUpdate.Result.LOCK_FAILURE:
                        {
                            throw new ConcurrentRefUpdateException(JGitText.Get().couldNotLockHEAD, tagRef.GetRef
                                                                       (), updateResult);
                        }

                        default:
                        {
                            throw new JGitInternalException(MessageFormat.Format(JGitText.Get().updatingRefFailed
                                                                                 , refName, newTag.ToString(), updateResult));
                        }
                        }
                    }
                    finally
                    {
                        revWalk.Release();
                    }
                }
                finally
                {
                    inserter.Release();
                }
            }
            catch (IOException e)
            {
                throw new JGitInternalException(JGitText.Get().exceptionCaughtDuringExecutionOfTagCommand
                                                , e);
            }
        }
示例#23
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;
                }
            }
        }
示例#24
0
        /// <returns>the list with the full names of the deleted tags</returns>
        /// <exception cref="NGit.Api.Errors.GitAPIException"></exception>
        public override IList <string> Call()
        {
            CheckCallable();
            IList <string> result = new AList <string>();

            if (tags.IsEmpty())
            {
                return(result);
            }
            try
            {
                SetCallable(false);
                foreach (string tagName in tags)
                {
                    if (tagName == null)
                    {
                        continue;
                    }
                    Ref currentRef = repo.GetRef(tagName);
                    if (currentRef == null)
                    {
                        continue;
                    }
                    string    fullName = currentRef.GetName();
                    RefUpdate update   = repo.UpdateRef(fullName);
                    update.SetForceUpdate(true);
                    RefUpdate.Result deleteResult = update.Delete();
                    bool             ok           = true;
                    switch (deleteResult)
                    {
                    case RefUpdate.Result.IO_FAILURE:
                    case RefUpdate.Result.LOCK_FAILURE:
                    case RefUpdate.Result.REJECTED:
                    {
                        ok = false;
                        break;
                    }

                    default:
                    {
                        break;
                        break;
                    }
                    }
                    if (ok)
                    {
                        result.AddItem(fullName);
                    }
                    else
                    {
                        throw new JGitInternalException(MessageFormat.Format(JGitText.Get().deleteTagUnexpectedResult
                                                                             , deleteResult.ToString()));
                    }
                }
                return(result);
            }
            catch (IOException ioe)
            {
                throw new JGitInternalException(ioe.Message, ioe);
            }
        }