git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[JGIT PATCH 06/10] Allow CanonicalTreeParsers to be created with a UTF-8 path prefix

From
Shawn O. Pearce <spearce@spearce.org>
Date
Jan 22, 2009, 23:28 UTC
Message-ID
<1232666890-23488-7-git-send-email-spearce@spearce.org>
In-Reply-To
<1232666890-23488-6-git-send-email-spearce@spearce.org>

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 <spearce@spearce.org>
---
 .../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 deletions(-)
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 791056c..62ca69c 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 @@ protected AbstractTreeIterator(final String prefix) {
 	}
 
 	/**
+	 * Create a new iterator with no parent and a prefix.
+	 * <p>
+	 * 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 2bcf792..ed883bd 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 CanonicalTreeParser() {
 		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 22040e3..416e027 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 void reset(final AnyObjectId[] ids) throws MissingObjectException,
 				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);
-- 
1.6.1.399.g0d272
Previous: Shawn O. PearceNext: Shawn O. Pearce
Message 7 of 12 in “Merge API v2”
  1. 00/10 Merge API v2Shawn O. Pearce, Jan 22, 2009
  2. 01/10 Fix TreeWalk.idEqual when both trees are missing the pathShawn O. Pearce, Jan 22, 2009
  3. 02/10 Expose the raw path for the current entry of a TreeWalkShawn O. Pearce, Jan 22, 2009
  4. 03/10 Expose DirCacheEntry.getFileMode as a utility functionShawn O. Pearce, Jan 22, 2009
  5. 04/10 Add writeTree support to DirCacheShawn O. Pearce, Jan 22, 2009
  6. 05/10 Allow a DirCache to be created with no backing store fileShawn O. Pearce, Jan 22, 2009
  7. 06/10 Allow CanonicalTreeParsers to be created with a UTF-8 path prefixShawn O. Pearce, Jan 22, 2009
  8. 07/10 Recursively load an entire tree into a DirCacheBuilderShawn O. Pearce, Jan 22, 2009
  9. 08/10 Allow DirCacheEntry instances to be created with stage > 0Shawn O. Pearce, Jan 22, 2009
  10. 09/10 Define a basic merge API, and a two-way tree merge strategyShawn O. Pearce, Jan 22, 2009
  11. 10/10 Add a few simple merge test casesShawn O. Pearce, Jan 22, 2009
  12. Robin RosenbergJan 23, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.