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

[JGIT PATCH 6/8] Recursively load an entire tree into a DirCacheBuilder

From
Shawn O. Pearce <spearce@spearce.org>
Date
Oct 13, 2008, 21:10 UTC
Message-ID
<1223932217-4771-7-git-send-email-spearce@spearce.org>
In-Reply-To
<1223932217-4771-6-git-send-email-spearce@spearce.org>

This implements the DirCache portion of "git read-tree", where a tree can be recursively read into a DirCache instance without an impact on the working directory.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 .../org/spearce/jgit/dircache/DirCacheBuilder.java |   58 ++++++++++++++++++++
 1 files changed, 58 insertions(+), 0 deletions(-)
diff --git a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheBuilder.java b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheBuilder.java
index 3a37054..88bda4d 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheBuilder.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheBuilder.java
@@ -37,8 +37,15 @@
 
 package org.spearce.jgit.dircache;
 
+import java.io.IOException;
 import java.util.Arrays;
 
+import org.spearce.jgit.lib.AnyObjectId;
+import org.spearce.jgit.lib.Repository;
+import org.spearce.jgit.treewalk.AbstractTreeIterator;
+import org.spearce.jgit.treewalk.CanonicalTreeParser;
+import org.spearce.jgit.treewalk.TreeWalk;
+
 /**
  * Updates a {@link DirCache} by adding individual {@link DirCacheEntry}s.
  * <p>
@@ -112,6 +119,57 @@ public void keep(final int pos, int cnt) {
 		fastKeep(pos, cnt);
 	}
 
+	/**
+	 * Recursively add an entire tree into this builder.
+	 * <p>
+	 * If pathPrefix is "a/b" and the tree contains file "c" then the resulting
+	 * DirCacheEntry will have the path "a/b/c".
+	 * <p>
+	 * All entries are inserted at stage 0, therefore assuming that the
+	 * application will not insert any other paths with the same pathPrefix.
+	 * 
+	 * @param pathPrefix
+	 *            UTF-8 encoded prefix to mount the tree's entries at. If the
+	 *            path does not end with '/' one will be automatically inserted
+	 *            as necessary.
+	 * @param db
+	 *            repository the tree(s) will be read from during recursive
+	 *            traversal. This must be the same repository that the resulting
+	 *            DirCache would be written out to (or used in) otherwise the
+	 *            caller is simply asking for deferred MissingObjectExceptions.
+	 * @param tree
+	 *            the tree to recursively add. This tree's contents will appear
+	 *            under <code>pathPrefix</code>. The ObjectId must be that of a
+	 *            tree; the caller is responsible for dereferencing a tag or
+	 *            commit (if necessary).
+	 * @throws IOException
+	 *             a tree cannot be read to iterate through its entries.
+	 */
+	public void addTree(final byte[] pathPrefix, final Repository db,
+			final AnyObjectId tree) throws IOException {
+		final TreeWalk tw = new TreeWalk(db);
+		tw.reset();
+		tw.addTree(new CanonicalTreeParser(pathPrefix, db, tree.toObjectId()));
+		tw.setRecursive(true);
+		if (tw.next()) {
+			final DirCacheEntry newEntry = toEntry(tw);
+			beforeAdd(newEntry);
+			fastAdd(newEntry);
+			while (tw.next())
+				fastAdd(toEntry(tw));
+		}
+	}
+
+	private DirCacheEntry toEntry(final TreeWalk tw) {
+		final DirCacheEntry e = new DirCacheEntry(tw.getRawPath());
+		final AbstractTreeIterator i;
+
+		i = tw.getTree(0, AbstractTreeIterator.class);
+		e.setFileMode(tw.getFileMode(0));
+		e.setObjectIdFromRaw(i.idBuffer(), i.idOffset());
+		return e;
+	}
+
 	public void finish() {
 		if (!sorted)
 			resort();
-- 
1.6.0.2.706.g340fc
Previous: Shawn O. PearceNext: Shawn O. Pearce
Message 7 of 16 in “Crude merge support”
  1. 0/8 Crude merge supportShawn O. Pearce, Oct 13, 2008
  2. 1/8 Expose the raw path for the current entry of a TreeWalkShawn O. Pearce, Oct 13, 2008
  3. 2/8 Expose DirCacheEntry.getFileMode as a utility functionShawn O. Pearce, Oct 13, 2008
  4. 3/8 Add writeTree support to DirCacheShawn O. Pearce, Oct 13, 2008
  5. 4/8 Allow a DirCache to be created with no backing store fileShawn O. Pearce, Oct 13, 2008
  6. 5/8 Allow CanonicalTreeParsers to be created with a UTF-8 path prefixShawn O. Pearce, Oct 13, 2008
  7. 6/8 Recursively load an entire tree into a DirCacheBuilderShawn O. Pearce, Oct 13, 2008
  8. 7/8 Allow DirCacheEntry instances to be created with stage > 0Shawn O. Pearce, Oct 13, 2008
  9. 8/8 Define a basic merge API, and a two-way tree merge strategyShawn O. Pearce, Oct 13, 2008
  10. Robin RosenbergOct 23, 2008
  11. Robin RosenbergJan 15, 2009
  12. Shawn O. PearceJan 15, 2009
  13. Tomi PakarinenJan 17, 2009
  14. Robin RosenbergJan 18, 2009
  15. Shawn O. PearceJan 19, 2009
  16. Shawn O. PearceJan 19, 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.