From 5e94c5ab4da7f0d8693008732940991d0b90c813 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Thu, 22 Jan 2009 15:28:06 -0800 Subject: [PATCH] Allow CanonicalTreeParsers to be created with a UTF-8 path prefix Creating an iterator with a path prefix permits a tree to be "mounted" at a different part of a repository, permitting more sophisticated merge strategies beyond just 1:1 path matching. Signed-off-by: Shawn O. Pearce Signed-off-by: Robin Rosenberg --- .../jgit/treewalk/AbstractTreeIterator.java | 31 ++++++++++++++++++++++ .../spearce/jgit/treewalk/CanonicalTreeParser.java | 30 +++++++++++++++++++++ .../src/org/spearce/jgit/treewalk/TreeWalk.java | 2 +- 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/org.spearce.jgit/src/org/spearce/jgit/treewalk/AbstractTreeIterator.java b/org.spearce.jgit/src/org/spearce/jgit/treewalk/AbstractTreeIterator.java index 791056ca..2ff3b991 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/treewalk/AbstractTreeIterator.java +++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/AbstractTreeIterator.java @@ -172,6 +172,37 @@ public abstract class AbstractTreeIterator { } /** + * Create a new iterator with no parent and a prefix. + *

+ * The prefix path supplied is inserted in front of all paths generated by + * this iterator. It is intended to be used when an iterator is being + * created for a subsection of an overall repository and needs to be + * combined with other iterators that are created to run over the entire + * repository namespace. + * + * @param prefix + * position of this iterator in the repository tree. The value + * may be null or the empty array to indicate the prefix is the + * root of the repository. A trailing slash ('/') is + * automatically appended if the prefix does not end in '/'. + */ + protected AbstractTreeIterator(final byte[] prefix) { + parent = null; + + if (prefix != null && prefix.length > 0) { + pathLen = prefix.length; + path = new byte[Math.max(DEFAULT_PATH_SIZE, pathLen + 1)]; + System.arraycopy(prefix, 0, path, 0, pathLen); + if (path[pathLen - 1] != '/') + path[pathLen++] = '/'; + pathOffset = pathLen; + } else { + path = new byte[DEFAULT_PATH_SIZE]; + pathOffset = 0; + } + } + + /** * Create an iterator for a subtree of an existing iterator. * * @param p diff --git a/org.spearce.jgit/src/org/spearce/jgit/treewalk/CanonicalTreeParser.java b/org.spearce.jgit/src/org/spearce/jgit/treewalk/CanonicalTreeParser.java index 2bcf7922..8028b141 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/treewalk/CanonicalTreeParser.java +++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/CanonicalTreeParser.java @@ -67,6 +67,36 @@ public class CanonicalTreeParser extends AbstractTreeIterator { raw = EMPTY; } + /** + * Create a new parser for a tree appearing in a subset of a repository. + * + * @param prefix + * position of this iterator in the repository tree. The value + * may be null or the empty array to indicate the prefix is the + * root of the repository. A trailing slash ('/') is + * automatically appended if the prefix does not end in '/'. + * @param repo + * repository to load the tree data from. + * @param treeId + * identity of the tree being parsed; used only in exception + * messages if data corruption is found. + * @param curs + * a window cursor to use during data access from the repository. + * @throws MissingObjectException + * the object supplied is not available from the repository. + * @throws IncorrectObjectTypeException + * the object supplied as an argument is not actually a tree and + * cannot be parsed as though it were a tree. + * @throws IOException + * a loose object or pack file could not be read. + */ + public CanonicalTreeParser(final byte[] prefix, final Repository repo, + final ObjectId treeId, final WindowCursor curs) + throws IncorrectObjectTypeException, IOException { + super(prefix); + reset(repo, treeId, curs); + } + private CanonicalTreeParser(final CanonicalTreeParser p) { super(p); } diff --git a/org.spearce.jgit/src/org/spearce/jgit/treewalk/TreeWalk.java b/org.spearce.jgit/src/org/spearce/jgit/treewalk/TreeWalk.java index 7c6ca7c0..cbecb05e 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/treewalk/TreeWalk.java +++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/TreeWalk.java @@ -354,7 +354,7 @@ public class TreeWalk { o = trees[i]; while (o.parent != null) o = o.parent; - if (o instanceof CanonicalTreeParser) { + if (o instanceof CanonicalTreeParser && o.pathOffset == 0) { o.matches = null; o.matchShift = 0; ((CanonicalTreeParser) o).reset(db, ids[i], curs); -- 2.11.4.GIT