Exemplo n.º 1
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());
        }
Exemplo n.º 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);
        }
Exemplo n.º 3
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");
        }
Exemplo n.º 4
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");
        }
Exemplo n.º 5
0
        /// <exception cref="System.Exception"></exception>
        public virtual void ShouldThrowIAEWhenTreeWalkHasLessThanTwoTrees()
        {
            // given - we don't need anything here
            // when
            TreeWalk walk = new TreeWalk(db);

            walk.AddTree(new EmptyTreeIterator());
            DiffEntry.Scan(walk);
        }
Exemplo n.º 6
0
        /// <exception cref="System.Exception"></exception>
        public virtual void ShouldThrowIAEWhenScanShouldIncludeTreesAndWalkIsRecursive()
        {
            // given - we don't need anything here
            // when
            TreeWalk walk = new TreeWalk(db);

            walk.AddTree(new EmptyTreeIterator());
            walk.AddTree(new EmptyTreeIterator());
            walk.Recursive = true;
            DiffEntry.Scan(walk, true);
        }
Exemplo n.º 7
0
        /// <summary>Determine the differences between two trees.</summary>
        /// <remarks>
        /// Determine the differences between two trees.
        /// No output is created, instead only the file paths that are different are
        /// returned. Callers may choose to format these paths themselves, or convert
        /// them into
        /// <see cref="NGit.Patch.FileHeader">NGit.Patch.FileHeader</see>
        /// instances with a complete edit list by
        /// calling
        /// <see cref="ToFileHeader(DiffEntry)">ToFileHeader(DiffEntry)</see>
        /// .
        /// </remarks>
        /// <param name="a">the old (or previous) side.</param>
        /// <param name="b">the new (or updated) side.</param>
        /// <returns>the paths that are different.</returns>
        /// <exception cref="System.IO.IOException">trees cannot be read or file contents cannot be read.
        ///     </exception>
        public virtual IList <DiffEntry> Scan(AbstractTreeIterator a, AbstractTreeIterator
                                              b)
        {
            AssertHaveRepository();
            TreeWalk walk = new TreeWalk(reader);

            walk.AddTree(a);
            walk.AddTree(b);
            walk.Recursive = true;
            TreeFilter filter = GetDiffTreeFilterFor(a, b);

            if (pathFilter is FollowFilter)
            {
                walk.Filter = AndTreeFilter.Create(PathFilter.Create(((FollowFilter)pathFilter).GetPath
                                                                         ()), filter);
            }
            else
            {
                walk.Filter = AndTreeFilter.Create(pathFilter, filter);
            }
            source = new ContentSource.Pair(Source(a), Source(b));
            IList <DiffEntry> files = DiffEntry.Scan(walk);

            if (pathFilter is FollowFilter && IsAdd(files))
            {
                // The file we are following was added here, find where it
                // came from so we can properly show the rename or copy,
                // then continue digging backwards.
                //
                a.Reset();
                b.Reset();
                walk.Reset();
                walk.AddTree(a);
                walk.AddTree(b);
                walk.Filter = filter;
                if (renameDetector == null)
                {
                    SetDetectRenames(true);
                }
                files = UpdateFollowFilter(DetectRenames(DiffEntry.Scan(walk)));
            }
            else
            {
                if (renameDetector != null)
                {
                    files = DetectRenames(files);
                }
            }
            return(files);
        }