From 45a020fe6a1ed4a7a9f8c46ab81dd4e655211941 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Wed, 8 Dec 2010 12:05:48 -0800 Subject: [PATCH] DiffFormatter: Use IndexDiffFilter to speed up working tree If DiffFormatter is asked to compare the index to the working tree, it can go faster by using the cached stat information to compare the two entries rather than relying on SHA-1 computation alone. Change-Id: Icb21c15b8279ee8cee382e5e179e0cf8903aee4d Signed-off-by: Shawn O. Pearce --- .../org/eclipse/jgit/diff/DiffFormatter.java | 42 ++++++++++++------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffFormatter.java b/org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffFormatter.java index adcf0d8d6..abab3c30c 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffFormatter.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffFormatter.java @@ -65,6 +65,7 @@ import java.util.List; import org.eclipse.jgit.JGitText; import org.eclipse.jgit.diff.DiffAlgorithm.SupportedAlgorithm; import org.eclipse.jgit.diff.DiffEntry.ChangeType; +import org.eclipse.jgit.dircache.DirCacheIterator; import org.eclipse.jgit.errors.AmbiguousObjectException; import org.eclipse.jgit.errors.CorruptObjectException; import org.eclipse.jgit.errors.LargeObjectException; @@ -91,7 +92,9 @@ import org.eclipse.jgit.treewalk.CanonicalTreeParser; import org.eclipse.jgit.treewalk.TreeWalk; import org.eclipse.jgit.treewalk.WorkingTreeIterator; import org.eclipse.jgit.treewalk.filter.AndTreeFilter; +import org.eclipse.jgit.treewalk.filter.IndexDiffFilter; import org.eclipse.jgit.treewalk.filter.NotIgnoredFilter; +import org.eclipse.jgit.treewalk.filter.PathFilter; import org.eclipse.jgit.treewalk.filter.TreeFilter; import org.eclipse.jgit.util.QuotedString; import org.eclipse.jgit.util.io.DisabledOutputStream; @@ -435,15 +438,14 @@ public class DiffFormatter { walk.addTree(b); walk.setRecursive(true); - TreeFilter filter = pathFilter; - - if (a instanceof WorkingTreeIterator) - filter = AndTreeFilter.create(filter, new NotIgnoredFilter(0)); - if (b instanceof WorkingTreeIterator) - filter = AndTreeFilter.create(filter, new NotIgnoredFilter(1)); - if (!(pathFilter instanceof FollowFilter)) - filter = AndTreeFilter.create(filter, TreeFilter.ANY_DIFF); - walk.setFilter(filter); + TreeFilter filter = getDiffTreeFilterFor(a, b); + if (pathFilter instanceof FollowFilter) { + walk.setFilter(AndTreeFilter.create( + PathFilter.create(((FollowFilter) pathFilter).getPath()), + filter)); + } else { + walk.setFilter(AndTreeFilter.create(pathFilter, filter)); + } source = new ContentSource.Pair(source(a), source(b)); @@ -458,12 +460,6 @@ public class DiffFormatter { walk.reset(); walk.addTree(a); walk.addTree(b); - - filter = TreeFilter.ANY_DIFF; - if (a instanceof WorkingTreeIterator) - filter = AndTreeFilter.create(new NotIgnoredFilter(0), filter); - if (b instanceof WorkingTreeIterator) - filter = AndTreeFilter.create(new NotIgnoredFilter(1), filter); walk.setFilter(filter); if (renameDetector == null) @@ -476,6 +472,22 @@ public class DiffFormatter { return files; } + private static TreeFilter getDiffTreeFilterFor(AbstractTreeIterator a, + AbstractTreeIterator b) { + if (a instanceof DirCacheIterator && b instanceof WorkingTreeIterator) + return new IndexDiffFilter(0, 1); + + if (a instanceof WorkingTreeIterator && b instanceof DirCacheIterator) + return new IndexDiffFilter(1, 0); + + TreeFilter filter = TreeFilter.ANY_DIFF; + if (a instanceof WorkingTreeIterator) + filter = AndTreeFilter.create(new NotIgnoredFilter(0), filter); + if (b instanceof WorkingTreeIterator) + filter = AndTreeFilter.create(new NotIgnoredFilter(1), filter); + return filter; + } + private ContentSource source(AbstractTreeIterator iterator) { if (iterator instanceof WorkingTreeIterator) return ContentSource.create((WorkingTreeIterator) iterator);