Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorShawn O. Pearce2010-07-27 15:36:24 +0000
committerShawn O. Pearce2010-07-27 15:36:24 +0000
commit80fe78969053637a06c34d4a09b88dfc5d075e92 (patch)
tree929ed2ea1356853af9762dbfa9aecbf9132e1e21
parent7ff18f3ec9f28609605dc5cca875cca2401b0ce5 (diff)
downloadjgit-80fe78969053637a06c34d4a09b88dfc5d075e92.tar.gz
jgit-80fe78969053637a06c34d4a09b88dfc5d075e92.tar.xz
jgit-80fe78969053637a06c34d4a09b88dfc5d075e92.zip
Make forPath(ObjectReader) variant in TreeWalk
This simplifies the logic for those who already have an ObjectReader on hand want to reuse it to lookup a single path. Change-Id: Ief17d6b2a0674ddb34bbc9f43121b756eae960fb Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/treewalk/TreeWalk.java48
1 files changed, 40 insertions, 8 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/TreeWalk.java b/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/TreeWalk.java
index 39e09a330c..16859646b6 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/TreeWalk.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/TreeWalk.java
@@ -56,8 +56,8 @@ import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.FileMode;
import org.eclipse.jgit.lib.MutableObjectId;
import org.eclipse.jgit.lib.ObjectId;
-import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.ObjectReader;
+import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevTree;
import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
import org.eclipse.jgit.treewalk.filter.TreeFilter;
@@ -91,6 +91,42 @@ public class TreeWalk {
* the caller should not need to invoke {@link #next()} unless they are
* looking for a possible directory/file name conflict.
*
+ * @param reader
+ * the reader the walker will obtain tree data from.
+ * @param path
+ * single path to advance the tree walk instance into.
+ * @param trees
+ * one or more trees to walk through, all with the same root.
+ * @return a new tree walk configured for exactly this one path; null if no
+ * path was found in any of the trees.
+ * @throws IOException
+ * reading a pack file or loose object failed.
+ * @throws CorruptObjectException
+ * an tree object could not be read as its data stream did not
+ * appear to be a tree, or could not be inflated.
+ * @throws IncorrectObjectTypeException
+ * an object we expected to be a tree was not a tree.
+ * @throws MissingObjectException
+ * a tree object was not found.
+ */
+ public static TreeWalk forPath(final ObjectReader reader, final String path,
+ final AnyObjectId... trees) throws MissingObjectException,
+ IncorrectObjectTypeException, CorruptObjectException, IOException {
+ final TreeWalk r = new TreeWalk(reader);
+ r.setFilter(PathFilterGroup.createFromStrings(Collections
+ .singleton(path)));
+ r.setRecursive(r.getFilter().shouldBeRecursive());
+ r.reset(trees);
+ return r.next() ? r : null;
+ }
+
+ /**
+ * Open a tree walk and filter to exactly one path.
+ * <p>
+ * The returned tree walk is already positioned on the requested path, so
+ * the caller should not need to invoke {@link #next()} unless they are
+ * looking for a possible directory/file name conflict.
+ *
* @param db
* repository to read tree object data from.
* @param path
@@ -112,15 +148,11 @@ public class TreeWalk {
public static TreeWalk forPath(final Repository db, final String path,
final AnyObjectId... trees) throws MissingObjectException,
IncorrectObjectTypeException, CorruptObjectException, IOException {
- final TreeWalk r = new TreeWalk(db);
+ ObjectReader reader = db.newObjectReader();
try {
- r.setFilter(PathFilterGroup.createFromStrings(Collections
- .singleton(path)));
- r.setRecursive(r.getFilter().shouldBeRecursive());
- r.reset(trees);
- return r.next() ? r : null;
+ return forPath(reader, path, trees);
} finally {
- r.release();
+ reader.release();
}
}

Back to the top