Exemple #1
0
        /// <exception cref="System.IO.IOException"></exception>
        private void FormatOldNewPaths(ByteArrayOutputStream o, DiffEntry ent)
        {
            string oldp;
            string newp;

            switch (ent.GetChangeType())
            {
            case DiffEntry.ChangeType.ADD:
            {
                oldp = DiffEntry.DEV_NULL;
                newp = QuotePath(newPrefix + ent.GetNewPath());
                break;
            }

            case DiffEntry.ChangeType.DELETE:
            {
                oldp = QuotePath(oldPrefix + ent.GetOldPath());
                newp = DiffEntry.DEV_NULL;
                break;
            }

            default:
            {
                oldp = QuotePath(oldPrefix + ent.GetOldPath());
                newp = QuotePath(newPrefix + ent.GetNewPath());
                break;
                break;
            }
            }
            o.Write(Constants.Encode("--- " + oldp + "\n"));
            o.Write(Constants.Encode("+++ " + newp + "\n"));
        }
Exemple #2
0
        public virtual void ShouldListAddedFileBetweenTwoCommits()
        {
            // given
            Git       git = new Git(db);
            RevCommit c1  = git.Commit().SetMessage("initial commit").Call();

            WriteTrashFile("a.txt", "content");
            git.Add().AddFilepattern("a.txt").Call();
            RevCommit c2 = git.Commit().SetMessage("second commit").Call();
            // when
            TreeWalk walk = new TreeWalk(db);

            walk.AddTree(c1.Tree);
            walk.AddTree(c2.Tree);
            IList <DiffEntry> result = DiffEntry.Scan(walk);

            // then
            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count);
            DiffEntry entry = result[0];

            Assert.AreEqual(entry.GetChangeType(), DiffEntry.ChangeType.ADD);
            Assert.AreEqual(entry.GetNewPath(), "a.txt");
            Assert.AreEqual(entry.GetOldPath(), DiffEntry.DEV_NULL);
        }
Exemple #3
0
        public virtual void ShouldReportFileModeChange()
        {
            WriteTrashFile("a.txt", "content");
            Git git = new Git(db);

            git.Add().AddFilepattern("a.txt").Call();
            RevCommit      c1     = git.Commit().SetMessage("initial commit").Call();
            DirCache       cache  = db.LockDirCache();
            DirCacheEditor editor = cache.Editor();
            TreeWalk       walk   = new TreeWalk(db);

            walk.AddTree(c1.Tree);
            walk.Recursive = true;
            NUnit.Framework.Assert.IsTrue(walk.Next());
            editor.Add(new _PathEdit_318(walk, "a.txt"));
            NUnit.Framework.Assert.IsTrue(editor.Commit());
            RevCommit c2 = git.Commit().SetMessage("second commit").Call();

            walk.Reset();
            walk.AddTree(c1.Tree);
            walk.AddTree(c2.Tree);
            IList <DiffEntry> diffs = DiffEntry.Scan(walk, false);

            NUnit.Framework.Assert.AreEqual(1, diffs.Count);
            DiffEntry diff = diffs[0];

            NUnit.Framework.Assert.AreEqual(DiffEntry.ChangeType.MODIFY, diff.GetChangeType()
                                            );
            NUnit.Framework.Assert.AreEqual(diff.GetOldId(), diff.GetNewId());
            NUnit.Framework.Assert.AreEqual("a.txt", diff.GetOldPath());
            NUnit.Framework.Assert.AreEqual(diff.GetOldPath(), diff.GetNewPath());
            NUnit.Framework.Assert.AreEqual(FileMode.EXECUTABLE_FILE, diff.GetNewMode());
            NUnit.Framework.Assert.AreEqual(FileMode.REGULAR_FILE, diff.GetOldMode());
        }
Exemple #4
0
        public virtual void ShouldListChangesInWorkingTree()
        {
            // given
            WriteTrashFile("a.txt", "content");
            Git git = new Git(db);

            git.Add().AddFilepattern("a.txt").Call();
            RevCommit c = git.Commit().SetMessage("initial commit").Call();

            WriteTrashFile("b.txt", "new line");
            // when
            TreeWalk walk = new TreeWalk(db);

            walk.AddTree(c.Tree);
            walk.AddTree(new FileTreeIterator(db));
            IList <DiffEntry> result = DiffEntry.Scan(walk, true);

            // then

            Assert.AreEqual(1, result.Count);
            DiffEntry entry = result[0];

            Assert.AreEqual(entry.GetChangeType(), DiffEntry.ChangeType.ADD);
            Assert.AreEqual(entry.GetNewPath(), "b.txt");
        }
Exemple #5
0
        public virtual void ShouldListModificationInDirWithoutModifiedTrees()
        {
            // given
            Git      git  = new Git(db);
            FilePath tree = new FilePath(new FilePath(db.WorkTree, "a"), "b");

            FileUtils.Mkdirs(tree);
            FilePath file = new FilePath(tree, "c.txt");

            FileUtils.CreateNewFile(file);
            Write(file, "content");
            git.Add().AddFilepattern("a").Call();
            RevCommit c1 = git.Commit().SetMessage("initial commit").Call();

            Write(file, "new line");
            RevCommit c2 = git.Commit().SetAll(true).SetMessage("second commit").Call();
            // when
            TreeWalk walk = new TreeWalk(db);

            walk.AddTree(c1.Tree);
            walk.AddTree(c2.Tree);
            walk.Recursive = true;
            IList <DiffEntry> result = DiffEntry.Scan(walk);

            // then
            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count);
            DiffEntry entry = result[0];

            Assert.AreEqual(entry.GetChangeType(), DiffEntry.ChangeType.MODIFY);
            Assert.AreEqual(entry.GetNewPath(), "a/b/c.txt");
        }
 private static void AssertCopy(DiffEntry o, DiffEntry n, int score, DiffEntry copy
                                )
 {
     NUnit.Framework.Assert.AreEqual(DiffEntry.ChangeType.COPY, copy.GetChangeType());
     NUnit.Framework.Assert.AreEqual(o.GetOldPath(), copy.GetOldPath());
     NUnit.Framework.Assert.AreEqual(n.GetNewPath(), copy.GetNewPath());
     NUnit.Framework.Assert.AreEqual(o.GetOldMode(), copy.GetOldMode());
     NUnit.Framework.Assert.AreEqual(n.GetNewMode(), copy.GetNewMode());
     NUnit.Framework.Assert.AreEqual(o.GetOldId(), copy.GetOldId());
     NUnit.Framework.Assert.AreEqual(n.GetNewId(), copy.GetNewId());
     NUnit.Framework.Assert.AreEqual(score, copy.GetScore());
 }
 private static void AssertRename(DiffEntry o, DiffEntry n, int score, DiffEntry rename
                                  )
 {
     NUnit.Framework.Assert.AreEqual(DiffEntry.ChangeType.RENAME, rename.GetChangeType
                                         ());
     NUnit.Framework.Assert.AreEqual(o.GetOldPath(), rename.GetOldPath());
     NUnit.Framework.Assert.AreEqual(n.GetNewPath(), rename.GetNewPath());
     NUnit.Framework.Assert.AreEqual(o.GetOldMode(), rename.GetOldMode());
     NUnit.Framework.Assert.AreEqual(n.GetNewMode(), rename.GetNewMode());
     NUnit.Framework.Assert.AreEqual(o.GetOldId(), rename.GetOldId());
     NUnit.Framework.Assert.AreEqual(n.GetNewId(), rename.GetNewId());
     NUnit.Framework.Assert.AreEqual(score, rename.GetScore());
 }
 /// <summary>Breaks apart a DiffEntry into two entries, one DELETE and one ADD.</summary>
 /// <remarks>Breaks apart a DiffEntry into two entries, one DELETE and one ADD.</remarks>
 /// <param name="entry">the DiffEntry to break apart.</param>
 /// <returns>
 /// a list containing two entries. Calling
 /// <see cref="GetChangeType()">GetChangeType()</see>
 /// on the first entry will return ChangeType.DELETE. Calling it on
 /// the second entry will return ChangeType.ADD.
 /// </returns>
 internal static IList <NGit.Diff.DiffEntry> BreakModify(NGit.Diff.DiffEntry entry)
 {
     NGit.Diff.DiffEntry del = new NGit.Diff.DiffEntry();
     del.oldId      = entry.GetOldId();
     del.oldMode    = entry.GetOldMode();
     del.oldPath    = entry.GetOldPath();
     del.newId      = A_ZERO;
     del.newMode    = FileMode.MISSING;
     del.newPath    = NGit.Diff.DiffEntry.DEV_NULL;
     del.changeType = DiffEntry.ChangeType.DELETE;
     NGit.Diff.DiffEntry add = new NGit.Diff.DiffEntry();
     add.oldId      = A_ZERO;
     add.oldMode    = FileMode.MISSING;
     add.oldPath    = NGit.Diff.DiffEntry.DEV_NULL;
     add.newId      = entry.GetNewId();
     add.newMode    = entry.GetNewMode();
     add.newPath    = entry.GetNewPath();
     add.changeType = DiffEntry.ChangeType.ADD;
     return(Arrays.AsList(del, add));
 }
        private static string Path(DiffEntry difference)
        {
            switch (difference.GetChangeType())
            {
                case DiffEntry.ChangeType.ADD:
                    return difference.GetNewPath();

                case DiffEntry.ChangeType.COPY:
                    return string.Format("{0} -> {1}", difference.GetOldPath(), difference.GetNewPath());

                case DiffEntry.ChangeType.DELETE:
                    return difference.GetOldPath();

                case DiffEntry.ChangeType.MODIFY:
                    return difference.GetOldPath();

                case DiffEntry.ChangeType.RENAME:
                    return string.Format("{0} -> {1}", difference.GetOldPath(), difference.GetNewPath());

                default:
                    return difference.ToString();
            }
        }
Exemple #10
0
        /// <exception cref="System.IO.IOException"></exception>
        private void FormatHeader(ByteArrayOutputStream o, DiffEntry ent)
        {
            DiffEntry.ChangeType type = ent.GetChangeType();
            string   oldp             = ent.GetOldPath();
            string   newp             = ent.GetNewPath();
            FileMode oldMode          = ent.GetOldMode();
            FileMode newMode          = ent.GetNewMode();

            o.Write(Constants.EncodeASCII("diff --git "));
            o.Write(Constants.Encode(QuotePath(oldPrefix + (type == DiffEntry.ChangeType.ADD ?
                                                            newp : oldp))));
            o.Write(' ');
            o.Write(Constants.Encode(QuotePath(newPrefix + (type == DiffEntry.ChangeType.DELETE
                                           ? oldp : newp))));
            o.Write('\n');
            switch (type)
            {
            case DiffEntry.ChangeType.ADD:
            {
                o.Write(Constants.EncodeASCII("new file mode "));
                newMode.CopyTo(o);
                o.Write('\n');
                break;
            }

            case DiffEntry.ChangeType.DELETE:
            {
                o.Write(Constants.EncodeASCII("deleted file mode "));
                oldMode.CopyTo(o);
                o.Write('\n');
                break;
            }

            case DiffEntry.ChangeType.RENAME:
            {
                o.Write(Constants.EncodeASCII("similarity index " + ent.GetScore() + "%"));
                o.Write('\n');
                o.Write(Constants.Encode("rename from " + QuotePath(oldp)));
                o.Write('\n');
                o.Write(Constants.Encode("rename to " + QuotePath(newp)));
                o.Write('\n');
                break;
            }

            case DiffEntry.ChangeType.COPY:
            {
                o.Write(Constants.EncodeASCII("similarity index " + ent.GetScore() + "%"));
                o.Write('\n');
                o.Write(Constants.Encode("copy from " + QuotePath(oldp)));
                o.Write('\n');
                o.Write(Constants.Encode("copy to " + QuotePath(newp)));
                o.Write('\n');
                if (!oldMode.Equals(newMode))
                {
                    o.Write(Constants.EncodeASCII("new file mode "));
                    newMode.CopyTo(o);
                    o.Write('\n');
                }
                break;
            }

            case DiffEntry.ChangeType.MODIFY:
            {
                if (0 < ent.GetScore())
                {
                    o.Write(Constants.EncodeASCII("dissimilarity index " + (100 - ent.GetScore()) + "%"
                                                  ));
                    o.Write('\n');
                }
                break;
            }
            }
            if ((type == DiffEntry.ChangeType.MODIFY || type == DiffEntry.ChangeType.RENAME) &&
                !oldMode.Equals(newMode))
            {
                o.Write(Constants.EncodeASCII("old mode "));
                oldMode.CopyTo(o);
                o.Write('\n');
                o.Write(Constants.EncodeASCII("new mode "));
                newMode.CopyTo(o);
                o.Write('\n');
            }
            if (!ent.GetOldId().Equals(ent.GetNewId()))
            {
                FormatIndexLine(o, ent);
            }
        }
		private static void AssertCopy(DiffEntry o, DiffEntry n, int score, DiffEntry copy
			)
		{
			NUnit.Framework.Assert.AreEqual(DiffEntry.ChangeType.COPY, copy.GetChangeType());
			NUnit.Framework.Assert.AreEqual(o.GetOldPath(), copy.GetOldPath());
			NUnit.Framework.Assert.AreEqual(n.GetNewPath(), copy.GetNewPath());
			NUnit.Framework.Assert.AreEqual(o.GetOldMode(), copy.GetOldMode());
			NUnit.Framework.Assert.AreEqual(n.GetNewMode(), copy.GetNewMode());
			NUnit.Framework.Assert.AreEqual(o.GetOldId(), copy.GetOldId());
			NUnit.Framework.Assert.AreEqual(n.GetNewId(), copy.GetNewId());
			NUnit.Framework.Assert.AreEqual(score, copy.GetScore());
		}
		private static void AssertRename(DiffEntry o, DiffEntry n, int score, DiffEntry rename
			)
		{
			NUnit.Framework.Assert.AreEqual(DiffEntry.ChangeType.RENAME, rename.GetChangeType
				());
			NUnit.Framework.Assert.AreEqual(o.GetOldPath(), rename.GetOldPath());
			NUnit.Framework.Assert.AreEqual(n.GetNewPath(), rename.GetNewPath());
			NUnit.Framework.Assert.AreEqual(o.GetOldMode(), rename.GetOldMode());
			NUnit.Framework.Assert.AreEqual(n.GetNewMode(), rename.GetNewMode());
			NUnit.Framework.Assert.AreEqual(o.GetOldId(), rename.GetOldId());
			NUnit.Framework.Assert.AreEqual(n.GetNewId(), rename.GetNewId());
			NUnit.Framework.Assert.AreEqual(score, rename.GetScore());
		}