Exemple #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());
        }
Exemple #2
0
        public virtual void Test028_LockPackedRef()
        {
            WriteTrashFile(".git/packed-refs", "7f822839a2fe9760f386cbbbcb3f92c5fe81def7 refs/heads/foobar"
                           );
            WriteTrashFile(".git/HEAD", "ref: refs/heads/foobar\n");
            BUG_WorkAroundRacyGitIssues("packed-refs");
            BUG_WorkAroundRacyGitIssues("HEAD");
            ObjectId resolve = db.Resolve("HEAD");

            NUnit.Framework.Assert.AreEqual("7f822839a2fe9760f386cbbbcb3f92c5fe81def7", resolve
                                            .Name);
            RefUpdate lockRef = db.UpdateRef("HEAD");
            ObjectId  newId   = ObjectId.FromString("07f822839a2fe9760f386cbbbcb3f92c5fe81def");

            lockRef.SetNewObjectId(newId);
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.FORCED, lockRef.ForceUpdate());
            NUnit.Framework.Assert.IsTrue(new FilePath(db.Directory, "refs/heads/foobar").Exists
                                              ());
            NUnit.Framework.Assert.AreEqual(newId, db.Resolve("refs/heads/foobar"));
            // Again. The ref already exists
            RefUpdate lockRef2 = db.UpdateRef("HEAD");
            ObjectId  newId2   = ObjectId.FromString("7f822839a2fe9760f386cbbbcb3f92c5fe81def7");

            lockRef2.SetNewObjectId(newId2);
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.FORCED, lockRef2.ForceUpdate());
            NUnit.Framework.Assert.IsTrue(new FilePath(db.Directory, "refs/heads/foobar").Exists
                                              ());
            NUnit.Framework.Assert.AreEqual(newId2, db.Resolve("refs/heads/foobar"));
        }
Exemple #3
0
        public virtual void TestUpdateRefLockFailureLocked()
        {
            ObjectId  opid      = db.Resolve("refs/heads/master");
            ObjectId  pid       = db.Resolve("refs/heads/master^");
            RefUpdate updateRef = db.UpdateRef("refs/heads/master");

            updateRef.SetNewObjectId(pid);
            LockFile lockFile1 = new LockFile(new FilePath(db.Directory, "refs/heads/master")
                                              , db.FileSystem);

            try
            {
                NUnit.Framework.Assert.IsTrue(lockFile1.Lock());
                // precondition to test
                RefUpdate.Result update = updateRef.Update();
                NUnit.Framework.Assert.AreEqual(RefUpdate.Result.LOCK_FAILURE, update);
                NUnit.Framework.Assert.AreEqual(opid, db.Resolve("refs/heads/master"));
                LockFile lockFile2 = new LockFile(new FilePath(db.Directory, "refs/heads/master")
                                                  , db.FileSystem);
                NUnit.Framework.Assert.IsFalse(lockFile2.Lock());
            }
            finally
            {
                // was locked, still is
                lockFile1.Unlock();
            }
        }
Exemple #4
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));
        }
Exemple #5
0
        public virtual void TestNoCacheObjectIdSubclass()
        {
            string    newRef = "refs/heads/abc";
            RefUpdate ru     = UpdateRef(newRef);

            RefUpdateTest.SubclassedId newid = new RefUpdateTest.SubclassedId(ru.GetNewObjectId
                                                                                  ());
            ru.SetNewObjectId(newid);
            RefUpdate.Result update = ru.Update();
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.NEW, update);
            Ref r = db.GetAllRefs().Get(newRef);

            NUnit.Framework.Assert.IsNotNull(r);
            NUnit.Framework.Assert.AreEqual(newRef, r.GetName());
            NUnit.Framework.Assert.IsNotNull(r.GetObjectId());
            NUnit.Framework.Assert.AreNotSame(newid, r.GetObjectId());
            NUnit.Framework.Assert.AreSame(typeof(ObjectId), r.GetObjectId().GetType());
            NUnit.Framework.Assert.AreEqual(newid, r.GetObjectId());
            IList <ReflogEntry> reverseEntries1 = db.GetReflogReader("refs/heads/abc").GetReverseEntries
                                                      ();
            ReflogEntry entry1 = reverseEntries1[0];

            NUnit.Framework.Assert.AreEqual(1, reverseEntries1.Count);
            NUnit.Framework.Assert.AreEqual(ObjectId.ZeroId, entry1.GetOldId());
            NUnit.Framework.Assert.AreEqual(r.GetObjectId(), entry1.GetNewId());
            NUnit.Framework.Assert.AreEqual(new PersonIdent(db).ToString(), entry1.GetWho().ToString
                                                ());
            NUnit.Framework.Assert.AreEqual(string.Empty, entry1.GetComment());
            IList <ReflogEntry> reverseEntries2 = db.GetReflogReader("HEAD").GetReverseEntries
                                                      ();

            NUnit.Framework.Assert.AreEqual(0, reverseEntries2.Count);
        }
Exemple #6
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
                                               ());
        }
Exemple #7
0
        /// <exception cref="System.IO.IOException"></exception>
        private void CommitNoteMap(RevWalk walk, NoteMap map, RevCommit notesCommit, ObjectInserter
                                   inserter, string msg)
        {
            // commit the note
            NGit.CommitBuilder builder = new NGit.CommitBuilder();
            builder.TreeId    = map.WriteTree(inserter);
            builder.Author    = new PersonIdent(repo);
            builder.Committer = builder.Author;
            builder.Message   = msg;
            if (notesCommit != null)
            {
                builder.SetParentIds(notesCommit);
            }
            ObjectId commit = inserter.Insert(builder);

            inserter.Flush();
            RefUpdate refUpdate = repo.UpdateRef(notesRef);

            if (notesCommit != null)
            {
                refUpdate.SetExpectedOldObjectId(notesCommit);
            }
            else
            {
                refUpdate.SetExpectedOldObjectId(ObjectId.ZeroId);
            }
            refUpdate.SetNewObjectId(commit);
            refUpdate.Update(walk);
        }
Exemple #8
0
        public virtual void TestMergeEmptyBranches()
        {
            Git git = new Git(db);

            git.Commit().SetMessage("initial commit").Call();
            RefUpdate r = db.UpdateRef("refs/heads/side");

            r.SetNewObjectId(db.Resolve(Constants.HEAD));
            NUnit.Framework.Assert.AreEqual(r.ForceUpdate(), RefUpdate.Result.NEW);
            RevCommit second = git.Commit().SetMessage("second commit").SetCommitter(committer
                                                                                     ).Call();

            db.UpdateRef(Constants.HEAD).Link("refs/heads/side");
            RevCommit firstSide = git.Commit().SetMessage("first side commit").SetAuthor(author
                                                                                         ).Call();

            Write(new FilePath(db.Directory, Constants.MERGE_HEAD), ObjectId.ToString(db.Resolve
                                                                                          ("refs/heads/master")));
            Write(new FilePath(db.Directory, Constants.MERGE_MSG), "merging");
            RevCommit commit = git.Commit().Call();

            RevCommit[] parents = commit.Parents;
            NUnit.Framework.Assert.AreEqual(parents[0], firstSide);
            NUnit.Framework.Assert.AreEqual(parents[1], second);
            NUnit.Framework.Assert.IsTrue(parents.Length == 2);
        }
Exemple #9
0
        /// <exception cref="System.IO.IOException"></exception>
        private RefUpdate UpdateRef(string name)
        {
            RefUpdate @ref = db.UpdateRef(name);

            @ref.SetNewObjectId(db.Resolve(Constants.HEAD));
            return(@ref);
        }
Exemple #10
0
        /// <exception cref="System.IO.IOException"></exception>
        /// <exception cref="NGit.Api.Errors.ConcurrentRefUpdateException"></exception>
        private void UpdateHead(StringBuilder refLogMessage, ObjectId newHeadId, ObjectId
                                oldHeadID)
        {
            RefUpdate refUpdate = repo.UpdateRef(Constants.HEAD);

            refUpdate.SetNewObjectId(newHeadId);
            refUpdate.SetRefLogMessage(refLogMessage.ToString(), false);
            refUpdate.SetExpectedOldObjectId(oldHeadID);
            RefUpdate.Result rc = refUpdate.Update();
            switch (rc)
            {
            case RefUpdate.Result.NEW:
            case RefUpdate.Result.FAST_FORWARD:
            {
                return;
            }

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

            default:
            {
                throw new JGitInternalException(MessageFormat.Format(JGitText.Get().updatingRefFailed
                                                                     , Constants.HEAD, newHeadId.ToString(), rc));
            }
            }
        }
Exemple #11
0
        /// <exception cref="System.IO.IOException"></exception>
        private void CreateBranch(ObjectId objectId, string branchName)
        {
            RefUpdate updateRef = db.UpdateRef(branchName);

            updateRef.SetNewObjectId(objectId);
            updateRef.Update();
        }
        private void UpdateRef(Ref stashRef, ObjectId newId)
        {
            try
            {
                RefUpdate update = CreateRefUpdate(stashRef);
                update.SetNewObjectId(newId);
                RefUpdate.Result result = update.Update();
                switch (result)
                {
                case RefUpdate.Result.FORCED:
                case RefUpdate.Result.NEW:
                case RefUpdate.Result.NO_CHANGE:
                {
                    return;
                }

                default:
                {
                    throw new JGitInternalException(MessageFormat.Format(JGitText.Get().updatingRefFailed
                                                                         , Constants.R_STASH, newId, result));
                }
                }
            }
            catch (IOException e)
            {
                throw new JGitInternalException(JGitText.Get().stashDropFailed, e);
            }
        }
Exemple #13
0
        /// <exception cref="NGit.Api.Errors.JGitInternalException"></exception>
        /// <exception cref="NGit.Errors.MissingObjectException"></exception>
        /// <exception cref="NGit.Errors.IncorrectObjectTypeException"></exception>
        /// <exception cref="System.IO.IOException"></exception>
        private void Checkout(Repository repo, FetchResult result)
        {
            if (branch.StartsWith(Constants.R_HEADS))
            {
                RefUpdate head = repo.UpdateRef(Constants.HEAD);
                head.DisableRefLog();
                head.Link(branch);
            }
            Ref head_1 = result.GetAdvertisedRef(branch);

            if (head_1 == null || head_1.GetObjectId() == null)
            {
                return;
            }
            // throw exception?
            RevCommit commit   = ParseCommit(repo, head_1);
            bool      detached = !head_1.GetName().StartsWith(Constants.R_HEADS);
            RefUpdate u        = repo.UpdateRef(Constants.HEAD, detached);

            u.SetNewObjectId(commit.Id);
            u.ForceUpdate();
            if (!bare)
            {
                DirCache         dc = repo.LockDirCache();
                DirCacheCheckout co = new DirCacheCheckout(repo, dc, commit.Tree);
                co.Checkout();
            }
        }
        public virtual void MultipleStashedCommits()
        {
            Git git = Git.Wrap(db);

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

            WriteTrashFile("file.txt", "content2");
            git.Add().AddFilepattern("file.txt").Call();
            RevCommit commit2 = git.Commit().SetMessage("edit file").Call();
            RefUpdate create  = db.UpdateRef(Constants.R_STASH);

            create.SetNewObjectId(commit1);
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.NEW, create.Update());
            RefUpdate update = db.UpdateRef(Constants.R_STASH);

            update.SetNewObjectId(commit2);
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.FAST_FORWARD, update.Update());
            StashListCommand        command = git.StashList();
            ICollection <RevCommit> stashed = command.Call();

            NUnit.Framework.Assert.IsNotNull(stashed);
            NUnit.Framework.Assert.AreEqual(2, stashed.Count);
            Iterator <RevCommit> iter = stashed.Iterator();

            NUnit.Framework.Assert.AreEqual(commit2, iter.Next());
            NUnit.Framework.Assert.AreEqual(commit1, iter.Next());
        }
Exemple #15
0
        /// <exception cref="System.IO.IOException"></exception>
        private void CheckoutCommit(RevCommit commit)
        {
            try
            {
                RevCommit        head = walk.ParseCommit(repo.Resolve(Constants.HEAD));
                DirCacheCheckout dco  = new DirCacheCheckout(repo, head.Tree, repo.LockDirCache(),
                                                             commit.Tree);
                dco.SetFailOnConflict(true);
                dco.Checkout();
                // update the HEAD
                RefUpdate refUpdate = repo.UpdateRef(Constants.HEAD, true);
                refUpdate.SetExpectedOldObjectId(head);
                refUpdate.SetNewObjectId(commit);
                RefUpdate.Result res = refUpdate.ForceUpdate();
                switch (res)
                {
                case RefUpdate.Result.FAST_FORWARD:
                case RefUpdate.Result.NO_CHANGE:
                case RefUpdate.Result.FORCED:
                {
                    break;
                }

                default:
                {
                    throw new IOException("Could not rewind to upstream commit");
                }
                }
            }
            finally
            {
                walk.Release();
                monitor.EndTask();
            }
        }
Exemple #16
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());
        }
Exemple #17
0
        /// <exception cref="System.IO.IOException"></exception>
        /// <exception cref="NGit.Api.Errors.JGitInternalException"></exception>
        private RevCommit TryFastForward(string headName, RevCommit oldCommit, RevCommit
                                         newCommit)
        {
            bool tryRebase = false;

            foreach (RevCommit parentCommit in newCommit.Parents)
            {
                if (parentCommit.Equals(oldCommit))
                {
                    tryRebase = true;
                }
            }
            if (!tryRebase)
            {
                return(null);
            }
            CheckoutCommand co = new CheckoutCommand(repo);

            try
            {
                co.SetName(newCommit.Name).Call();
                if (headName.StartsWith(Constants.R_HEADS))
                {
                    RefUpdate rup = repo.UpdateRef(headName);
                    rup.SetExpectedOldObjectId(oldCommit);
                    rup.SetNewObjectId(newCommit);
                    rup.SetRefLogMessage("Fast-foward from " + oldCommit.Name + " to " + newCommit.Name
                                         , false);
                    RefUpdate.Result res = rup.Update(walk);
                    switch (res)
                    {
                    case RefUpdate.Result.FAST_FORWARD:
                    case RefUpdate.Result.NO_CHANGE:
                    case RefUpdate.Result.FORCED:
                    {
                        break;
                    }

                    default:
                    {
                        throw new IOException("Could not fast-forward");
                    }
                    }
                }
                return(newCommit);
            }
            catch (RefAlreadyExistsException e)
            {
                throw new JGitInternalException(e.Message, e);
            }
            catch (RefNotFoundException e)
            {
                throw new JGitInternalException(e.Message, e);
            }
            catch (InvalidRefNameException e)
            {
                throw new JGitInternalException(e.Message, e);
            }
        }
Exemple #18
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);
 }
Exemple #19
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);
        }
Exemple #20
0
        public virtual void TestUpdateRefNoChange()
        {
            ObjectId  pid       = db.Resolve("refs/heads/master");
            RefUpdate updateRef = db.UpdateRef("refs/heads/master");

            updateRef.SetNewObjectId(pid);
            RefUpdate.Result update = updateRef.Update();
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.NO_CHANGE, update);
            NUnit.Framework.Assert.AreEqual(pid, db.Resolve("refs/heads/master"));
        }
Exemple #21
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;
 }
Exemple #22
0
        public virtual void TestUpdateRefLockFailureWrongOldValue()
        {
            ObjectId  pid       = db.Resolve("refs/heads/master");
            RefUpdate updateRef = db.UpdateRef("refs/heads/master");

            updateRef.SetNewObjectId(pid);
            updateRef.SetExpectedOldObjectId(db.Resolve("refs/heads/master^"));
            RefUpdate.Result update = updateRef.Update();
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.LOCK_FAILURE, update);
            NUnit.Framework.Assert.AreEqual(pid, db.Resolve("refs/heads/master"));
        }
Exemple #23
0
        public virtual void TestDeleteLoosePackedRejected()
        {
            ObjectId  pid       = db.Resolve("refs/heads/c^");
            ObjectId  oldpid    = db.Resolve("refs/heads/c");
            RefUpdate updateRef = db.UpdateRef("refs/heads/c");

            updateRef.SetNewObjectId(pid);
            RefUpdate.Result update = updateRef.Update();
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.REJECTED, update);
            NUnit.Framework.Assert.AreEqual(oldpid, db.Resolve("refs/heads/c"));
        }
        public virtual void TestCreateFromLightweightTag()
        {
            RefUpdate rup = db.UpdateRef("refs/tags/V10");

            rup.SetNewObjectId(initialCommit);
            rup.SetExpectedOldObjectId(ObjectId.ZeroId);
            rup.Update();
            Ref branch = git.BranchCreate().SetName("FromLightweightTag").SetStartPoint("refs/tags/V10"
                                                                                        ).Call();

            NUnit.Framework.Assert.AreEqual(initialCommit.Id, branch.GetObjectId());
        }
Exemple #25
0
        public override void SetUp()
        {
            base.SetUp();
            RefUpdate createRemoteRefA = db.UpdateRef("refs/remotes/origin/remote-a");

            createRemoteRefA.SetNewObjectId(db.Resolve("refs/heads/a"));
            createRemoteRefA.Update();
            RefUpdate createRemoteRefB = db.UpdateRef("refs/remotes/origin/remote-b");

            createRemoteRefB.SetNewObjectId(db.Resolve("refs/heads/b"));
            createRemoteRefB.Update();
            formatter = new MergeMessageFormatter();
        }
        public virtual void RepositoryWithDifferentRevCheckedOutSubmodule()
        {
            ObjectId       id     = ObjectId.FromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
            string         path   = "sub";
            DirCache       cache  = db.LockDirCache();
            DirCacheEditor editor = cache.Editor();

            editor.Add(new _PathEdit_317(id, path));
            editor.Commit();
            string       url    = "git://server/repo.git";
            StoredConfig config = ((FileBasedConfig)db.GetConfig());

            config.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants.
                             CONFIG_KEY_URL, url);
            config.Save();
            FileBasedConfig modulesConfig = new FileBasedConfig(new FilePath(db.WorkTree, Constants
                                                                             .DOT_GIT_MODULES), db.FileSystem);

            modulesConfig.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants
                                    .CONFIG_KEY_PATH, path);
            modulesConfig.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants
                                    .CONFIG_KEY_URL, url);
            modulesConfig.Save();
            Repository subRepo = Git.Init().SetBare(false).SetDirectory(new FilePath(db.WorkTree
                                                                                     , path)).Call().GetRepository();

            NUnit.Framework.Assert.IsNotNull(subRepo);
            RefUpdate update = subRepo.UpdateRef(Constants.HEAD, true);

            update.SetNewObjectId(ObjectId.FromString("aaaa0000aaaa0000aaaa0000aaaa0000aaaa0000"
                                                      ));
            update.ForceUpdate();
            SubmoduleStatusCommand command = new SubmoduleStatusCommand(db);
            IDictionary <string, SubmoduleStatus> statuses = command.Call();

            NUnit.Framework.Assert.IsNotNull(statuses);
            NUnit.Framework.Assert.AreEqual(1, statuses.Count);
            KeyValuePair <string, SubmoduleStatus> module = statuses.EntrySet().Iterator().Next
                                                                ();

            NUnit.Framework.Assert.IsNotNull(module);
            NUnit.Framework.Assert.AreEqual(path, module.Key);
            SubmoduleStatus status = module.Value;

            NUnit.Framework.Assert.IsNotNull(status);
            NUnit.Framework.Assert.AreEqual(path, status.GetPath());
            NUnit.Framework.Assert.AreEqual(id, status.GetIndexId());
            NUnit.Framework.Assert.AreEqual(update.GetNewObjectId(), status.GetHeadId());
            NUnit.Framework.Assert.AreEqual(SubmoduleStatusType.REV_CHECKED_OUT, status.GetType
                                                ());
        }
Exemple #27
0
        /// <exception cref="System.Exception"></exception>
        private Git SetUpRepoWithRemote()
        {
            Repository remoteRepository = CreateWorkRepository();
            Git        remoteGit        = new Git(remoteRepository);

            // commit something
            WriteTrashFile("Test.txt", "Hello world");
            remoteGit.Add().AddFilepattern("Test.txt").Call();
            initialCommit = remoteGit.Commit().SetMessage("Initial commit").Call();
            WriteTrashFile("Test.txt", "Some change");
            remoteGit.Add().AddFilepattern("Test.txt").Call();
            secondCommit = remoteGit.Commit().SetMessage("Second commit").Call();
            // create a master branch
            RefUpdate rup = remoteRepository.UpdateRef("refs/heads/master");

            rup.SetNewObjectId(initialCommit.Id);
            rup.ForceUpdate();
            Repository   localRepository = CreateWorkRepository();
            Git          localGit        = new Git(localRepository);
            StoredConfig config          = localRepository.GetConfig();
            RemoteConfig rc = new RemoteConfig(config, "origin");

            rc.AddURI(new URIish(remoteRepository.Directory.GetPath()));
            rc.AddFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/origin/*"));
            rc.Update(config);
            config.Save();
            FetchResult res = localGit.Fetch().SetRemote("origin").Call();

            NUnit.Framework.Assert.IsFalse(res.GetTrackingRefUpdates().IsEmpty());
            rup = localRepository.UpdateRef("refs/heads/master");
            rup.SetNewObjectId(initialCommit.Id);
            rup.ForceUpdate();
            rup = localRepository.UpdateRef(Constants.HEAD);
            rup.Link("refs/heads/master");
            rup.SetNewObjectId(initialCommit.Id);
            rup.Update();
            return(localGit);
        }
Exemple #28
0
        /// <exception cref="System.InvalidOperationException"></exception>
        /// <exception cref="System.IO.IOException"></exception>
        private void CheckoutCommit(RevCommit commit)
        {
            RevWalk          walk = new RevWalk(db);
            RevCommit        head = walk.ParseCommit(db.Resolve(Constants.HEAD));
            DirCacheCheckout dco  = new DirCacheCheckout(db, head.Tree, db.LockDirCache(), commit
                                                         .Tree);

            dco.SetFailOnConflict(true);
            dco.Checkout();
            walk.Release();
            // update the HEAD
            RefUpdate refUpdate = db.UpdateRef(Constants.HEAD, true);

            refUpdate.SetNewObjectId(commit);
            refUpdate.ForceUpdate();
        }
        public virtual void SingleStashedCommit()
        {
            Git git = Git.Wrap(db);

            WriteTrashFile("file.txt", "content");
            git.Add().AddFilepattern("file.txt").Call();
            RevCommit commit = git.Commit().SetMessage("create file").Call();
            RefUpdate update = db.UpdateRef(Constants.R_STASH);

            update.SetNewObjectId(commit);
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.NEW, update.Update());
            StashListCommand        command = git.StashList();
            ICollection <RevCommit> stashed = command.Call();

            NUnit.Framework.Assert.IsNotNull(stashed);
            NUnit.Framework.Assert.AreEqual(1, stashed.Count);
            NUnit.Framework.Assert.AreEqual(commit, stashed.Iterator().Next());
        }
Exemple #30
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"));
        }