From 6befabcb1584118fb02188f602f7ca34b9a7efc5 Mon Sep 17 00:00:00 2001 From: Jens Baumgart Date: Wed, 5 Oct 2011 13:56:23 +0200 Subject: [PATCH] Extend IndexDiff to calculate ignored files and folders IndexDiff was extended to calculate ignored files and folders. The calculation only considers files that are NOT in the index. This functionality is required by the new EGit decorator implementation. Bug: 359264 Change-Id: I8f09d6a4d61b64aeea80fd22bf3a2963c2bca347 Signed-off-by: Jens Baumgart --- .../src/org/eclipse/jgit/lib/IndexDiff.java | 19 +++++++++++++++- .../jgit/treewalk/filter/IndexDiffFilter.java | 22 +++++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/IndexDiff.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/IndexDiff.java index 5d121dd92..af64f2094 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/IndexDiff.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/IndexDiff.java @@ -157,6 +157,8 @@ public class IndexDiff { private Set conflicts = new HashSet(); + private Set ignored; + private Set assumeUnchanged; private DirCache dirCache; @@ -276,7 +278,8 @@ public class IndexDiff { if (filter != null) filters.add(filter); filters.add(new SkipWorkTreeFilter(INDEX)); - filters.add(new IndexDiffFilter(INDEX, WORKDIR)); + IndexDiffFilter indexDiffFilter = new IndexDiffFilter(INDEX, WORKDIR); + filters.add(indexDiffFilter); treeWalk.setFilter(AndTreeFilter.create(filters)); while (treeWalk.next()) { AbstractTreeIterator treeIterator = treeWalk.getTree(TREE, @@ -340,6 +343,7 @@ public class IndexDiff { if (monitor != null) monitor.endTask(); + ignored = indexDiffFilter.getIgnoredPaths(); if (added.isEmpty() && changed.isEmpty() && removed.isEmpty() && missing.isEmpty() && modified.isEmpty() && untracked.isEmpty()) @@ -397,6 +401,19 @@ public class IndexDiff { return conflicts; } + /** + * The method returns the list of ignored files and folders. Only the root + * folder of an ignored folder hierarchy is reported. If a/b/c is listed in + * the .gitignore then you should not expect a/b/c/d/e/f to be reported + * here. Only a/b/c will be reported. Furthermore only ignored files / + * folders are returned that are NOT in the index. + * + * @return list of files / folders that are ignored + */ + public Set getIgnoredNotInIndex() { + return ignored; + } + /** * @return list of files with the flag assume-unchanged */ diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/filter/IndexDiffFilter.java b/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/filter/IndexDiffFilter.java index 8499970ba..2f8608ee4 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/filter/IndexDiffFilter.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/filter/IndexDiffFilter.java @@ -43,6 +43,8 @@ package org.eclipse.jgit.treewalk.filter; import java.io.IOException; +import java.util.HashSet; +import java.util.Set; import org.eclipse.jgit.dircache.DirCacheIterator; import org.eclipse.jgit.errors.IncorrectObjectTypeException; @@ -79,6 +81,8 @@ public class IndexDiffFilter extends TreeFilter { private final boolean honorIgnores; + private final Set ignoredPaths = new HashSet(); + /** * Creates a new instance of this filter. Do not use an instance of this * filter in multiple treewalks. @@ -129,8 +133,10 @@ public class IndexDiffFilter extends TreeFilter { // other tree. final int cnt = tw.getTreeCount(); final int dm = tw.getRawMode(dirCache); + WorkingTreeIterator wi = workingTree(tw); if (dm == 0) { - if (honorIgnores && workingTree(tw).isEntryIgnored()) { + if (honorIgnores && wi.isEntryIgnored()) { + ignoredPaths.add(wi.getEntryPathString()); int i = 0; for (; i < cnt; i++) { if (i == dirCache || i == workingTree) @@ -166,7 +172,6 @@ public class IndexDiffFilter extends TreeFilter { // Only one chance left to detect a diff: between index and working // tree. Make use of the WorkingTreeIterator#isModified() method to // avoid computing SHA1 on filesystem content if not really needed. - WorkingTreeIterator wi = workingTree(tw); DirCacheIterator di = tw.getTree(dirCache, DirCacheIterator.class); return wi.isModified(di.getDirCacheEntry(), true); } @@ -191,4 +196,17 @@ public class IndexDiffFilter extends TreeFilter { public String toString() { return "INDEX_DIFF_FILTER"; } + + /** + * The method returns the list of ignored files and folders. Only the root + * folder of an ignored folder hierarchy is reported. If a/b/c is listed in + * the .gitignore then you should not expect a/b/c/d/e/f to be reported + * here. Only a/b/c will be reported. Furthermore only ignored files / + * folders are returned that are NOT in the index. + * + * @return ignored paths + */ + public Set getIgnoredPaths() { + return ignoredPaths; + } }