示例#1
0
        public virtual void TestAbbreviateLooseBlob()
        {
            ObjectId id = test.Blob("test");

            NUnit.Framework.Assert.AreEqual(id.Abbreviate(2), reader.Abbreviate(id, 2));
            NUnit.Framework.Assert.AreEqual(id.Abbreviate(7), reader.Abbreviate(id, 7));
            NUnit.Framework.Assert.AreEqual(id.Abbreviate(8), reader.Abbreviate(id, 8));
            NUnit.Framework.Assert.AreEqual(id.Abbreviate(10), reader.Abbreviate(id, 10));
            NUnit.Framework.Assert.AreEqual(id.Abbreviate(16), reader.Abbreviate(id, 16));
            ICollection <ObjectId> matches = reader.Resolve(reader.Abbreviate(id, 8));

            NUnit.Framework.Assert.IsNotNull(matches);
            NUnit.Framework.Assert.AreEqual(1, matches.Count);
            NUnit.Framework.Assert.AreEqual(id, matches.Iterator().Next());
            NUnit.Framework.Assert.AreEqual(id, db.Resolve(reader.Abbreviate(id, 8).Name));
        }
示例#2
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"));
        }
示例#3
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"));
		}
示例#4
0
        /// <summary>
        /// Return a list of those objects in the index which differ from whats in
        /// HEAD
        /// </summary>
        /// <returns>a set of ObjectIds of changed objects in the index</returns>
        /// <exception cref="System.IO.IOException">System.IO.IOException</exception>
        /// <exception cref="NGit.Errors.CorruptObjectException">NGit.Errors.CorruptObjectException
        ///     </exception>
        /// <exception cref="NGit.Errors.NoWorkTreeException">NGit.Errors.NoWorkTreeException
        ///     </exception>
        private ICollection <ObjectId> ListNonHEADIndexObjects()
        {
            RevWalk revWalk = null;

            try
            {
                if (repo.GetIndexFile() == null)
                {
                    return(Sharpen.Collections.EmptySet <ObjectId>());
                }
            }
            catch (NoWorkTreeException)
            {
                return(Sharpen.Collections.EmptySet <ObjectId>());
            }
            TreeWalk treeWalk = new TreeWalk(repo);

            try
            {
                treeWalk.AddTree(new DirCacheIterator(repo.ReadDirCache()));
                ObjectId headID = repo.Resolve(Constants.HEAD);
                if (headID != null)
                {
                    revWalk = new RevWalk(repo);
                    treeWalk.AddTree(revWalk.ParseTree(headID));
                    revWalk.Dispose();
                    revWalk = null;
                }
                treeWalk.Filter    = TreeFilter.ANY_DIFF;
                treeWalk.Recursive = true;
                ICollection <ObjectId> ret = new HashSet <ObjectId>();
                while (treeWalk.Next())
                {
                    ObjectId objectId = treeWalk.GetObjectId(0);
                    switch (treeWalk.GetRawMode(0) & FileMode.TYPE_MASK)
                    {
                    case FileMode.TYPE_MISSING:
                    case FileMode.TYPE_GITLINK:
                    {
                        continue;
                        goto case FileMode.TYPE_TREE;
                    }

                    case FileMode.TYPE_TREE:
                    case FileMode.TYPE_FILE:
                    case FileMode.TYPE_SYMLINK:
                    {
                        ret.AddItem(objectId);
                        continue;
                        goto default;
                    }

                    default:
                    {
                        throw new IOException(MessageFormat.Format(JGitText.Get().corruptObjectInvalidMode3
                                                                   , string.Format("%o", Sharpen.Extensions.ValueOf(treeWalk.GetRawMode(0)), (objectId
                                                                                                                                              == null) ? "null" : objectId.Name, treeWalk.PathString, repo.GetIndexFile())));
                    }
                    }
                }
                return(ret);
            }
            finally
            {
                if (revWalk != null)
                {
                    revWalk.Dispose();
                }
                treeWalk.Release();
            }
        }