예제 #1
0
        private Ref FindBranchToCheckout(FetchResult result)
        {
            Ref idHEAD = result.GetAdvertisedRef(Constants.HEAD);

            if (idHEAD == null)
            {
                return(null);
            }
            Ref master = result.GetAdvertisedRef(Constants.R_HEADS + Constants.MASTER);

            if (master != null && master.GetObjectId().Equals(idHEAD.GetObjectId()))
            {
                return(master);
            }
            Ref foundBranch = null;

            foreach (Ref r in result.GetAdvertisedRefs())
            {
                string n = r.GetName();
                if (!n.StartsWith(Constants.R_HEADS))
                {
                    continue;
                }
                if (r.GetObjectId().Equals(idHEAD.GetObjectId()))
                {
                    foundBranch = r;
                    break;
                }
            }
            return(foundBranch);
        }
예제 #2
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();
            }
        }
예제 #3
0
        private static GitSharp.Core.Ref guessHEAD(FetchResult result)
        {
            GitSharp.Core.Ref        idHEAD        = result.GetAdvertisedRef(Constants.HEAD);
            List <GitSharp.Core.Ref> availableRefs = new List <GitSharp.Core.Ref>();

            GitSharp.Core.Ref head = null;

            foreach (GitSharp.Core.Ref r in result.AdvertisedRefs)
            {
                string n = r.Name;
                if (!n.StartsWith(Constants.R_HEADS))
                {
                    continue;
                }
                availableRefs.Add(r);
                if (idHEAD == null || head != null)
                {
                    continue;
                }

                if (r.ObjectId.Equals(idHEAD.ObjectId))
                {
                    head = r;
                }
            }
            availableRefs.Sort(RefComparator.INSTANCE);
            if (idHEAD != null && head == null)
            {
                head = idHEAD;
            }
            return(head);
        }
예제 #4
0
        public void testWrite1()
        {
            // Create a small bundle, an early commit
            byte[] bundle = makeBundle("refs/heads/aa", db.Resolve("a").Name, null);

            // Then we clone a new repo from that bundle and do a simple test. This
            // makes sure
            // we could Read the bundle we created.
            Core.Repository newRepo     = createBareRepository();
            FetchResult     fetchResult = fetchFromBundle(newRepo, bundle);

            Core.Ref advertisedRef = fetchResult.GetAdvertisedRef("refs/heads/aa");

            Assert.AreEqual(db.Resolve("a").Name, advertisedRef.ObjectId.Name);
            Assert.AreEqual(db.Resolve("a").Name, newRepo.Resolve("refs/heads/aa").Name);
            Assert.IsNull(newRepo.Resolve("refs/heads/a"));

            // Next an incremental bundle
            bundle = makeBundle(
                "refs/heads/cc",
                db.Resolve("c").Name,
                new GitSharp.Core.RevWalk.RevWalk(db).parseCommit(db.Resolve("a").ToObjectId()));

            fetchResult   = fetchFromBundle(newRepo, bundle);
            advertisedRef = fetchResult.GetAdvertisedRef("refs/heads/cc");
            Assert.AreEqual(db.Resolve("c").Name, advertisedRef.ObjectId.Name);
            Assert.AreEqual(db.Resolve("c").Name, newRepo.Resolve("refs/heads/cc").Name);
            Assert.IsNull(newRepo.Resolve("refs/heads/c"));
            Assert.IsNull(newRepo.Resolve("refs/heads/a")); // still unknown

            try
            {
                // Check that we actually needed the first bundle
                Core.Repository newRepo2 = createBareRepository();
                fetchResult = fetchFromBundle(newRepo2, bundle);
                Assert.Fail("We should not be able to fetch from bundle with prerequisites that are not fulfilled");
            }
            catch (MissingBundlePrerequisiteException e)
            {
                Assert.IsTrue(e.Message.IndexOf(db.Resolve("refs/heads/a").Name) >= 0);
            }
        }
예제 #5
0
        private static GitSharp.Core.Ref guessHEAD(FetchResult result)
        {
            // Some transports allow us to see where HEAD points to. If that is not so,
            // we'll have to guess.
            GitSharp.Core.Ref head = result.GetAdvertisedRef(Constants.HEAD);
            if (head != null)
            {
                return(head);
            }

            var availableHeads = result.AdvertisedRefs.Where(r => r.Name.StartsWith(Constants.R_HEADS));

            // master is our preferred guess, so if it's advertised, return that.
            GitSharp.Core.Ref guessedHead = result.GetAdvertisedRef(Constants.R_HEADS + Constants.MASTER);
            if (guessedHead == null && availableHeads.Count() > 0)
            {
                // if master is not advertised, return any other head.
                guessedHead = availableHeads.First();
            }

            return(guessedHead);
        }
예제 #6
0
        public void testWrite0()
        {
            // Create a tiny bundle, (well one of) the first commits only
            byte[] bundle = makeBundle("refs/heads/firstcommit", "42e4e7c5e507e113ebbb7801b16b52cf867b7ce1", null);

            // Then we clone a new repo from that bundle and do a simple test. This
            // makes sure
            // we could Read the bundle we created.
            Core.Repository newRepo     = createBareRepository();
            FetchResult     fetchResult = fetchFromBundle(newRepo, bundle);

            Core.Ref advertisedRef = fetchResult.GetAdvertisedRef("refs/heads/firstcommit");

            // We expect firstcommit to appear by id
            Assert.AreEqual("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1", advertisedRef.ObjectId.Name);
            // ..and by name as the bundle created a new ref
            Assert.AreEqual("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1", newRepo.Resolve(("refs/heads/firstcommit")).Name);
        }
예제 #7
0
        /// <exception cref="NGit.Errors.MissingObjectException"></exception>
        /// <exception cref="NGit.Errors.IncorrectObjectTypeException"></exception>
        /// <exception cref="System.IO.IOException"></exception>
        /// <exception cref="NGit.Api.Errors.GitAPIException"></exception>
        private void Checkout(Repository clonedRepo, FetchResult result)
        {
            Ref head = result.GetAdvertisedRef(branch);

            if (branch.Equals(Constants.HEAD))
            {
                Ref foundBranch = FindBranchToCheckout(result);
                if (foundBranch != null)
                {
                    head = foundBranch;
                }
            }
            if (head == null || head.GetObjectId() == null)
            {
                return;
            }
            // throw exception?
            if (head.GetName().StartsWith(Constants.R_HEADS))
            {
                RefUpdate newHead = clonedRepo.UpdateRef(Constants.HEAD);
                newHead.DisableRefLog();
                newHead.Link(head.GetName());
                AddMergeConfig(clonedRepo, head);
            }
            RevCommit commit   = ParseCommit(clonedRepo, head);
            bool      detached = !head.GetName().StartsWith(Constants.R_HEADS);
            RefUpdate u        = clonedRepo.UpdateRef(Constants.HEAD, detached);

            u.SetNewObjectId(commit.Id);
            u.ForceUpdate();
            if (!bare)
            {
                DirCache         dc = clonedRepo.LockDirCache();
                DirCacheCheckout co = new DirCacheCheckout(clonedRepo, dc, commit.Tree);
                co.Checkout();
                if (cloneSubmodules)
                {
                    CloneSubmodules(clonedRepo);
                }
            }
        }