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

[JGIT PATCH 08/28] Add parseTree method to RevWalk to obtain a RevTree from AnyObjectId

From
Shawn O. Pearce <spearce@spearce.org>
Date
Jul 18, 2008, 01:44 UTC
Message-ID
<1216345461-59382-9-git-send-email-spearce@spearce.org>
In-Reply-To
<1216345461-59382-8-git-send-email-spearce@spearce.org>

Some applications want to dereference an ObjectId back to a tree, and they don't care what type of object the ObjectId supplied is actually of. If its an annotated tag we can peel the tag back to either a tree, or a commit, and a commit can be peeled back to its only tree. So parseTree is a simple API for ^{tree}.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 .../src/org/spearce/jgit/revwalk/RevWalk.java      |   35 ++++++++++++++++++++
 1 files changed, 35 insertions(+), 0 deletions(-)
diff --git a/org.spearce.jgit/src/org/spearce/jgit/revwalk/RevWalk.java b/org.spearce.jgit/src/org/spearce/jgit/revwalk/RevWalk.java
index 946486c..42ec2a2 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/revwalk/RevWalk.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/revwalk/RevWalk.java
@@ -612,6 +612,41 @@ public class RevWalk implements Iterable<RevCommit> {
 	}
 
 	/**
+	 * Locate a reference to a tree.
+	 * <p>
+	 * This method only returns successfully if the tree object exists, is
+	 * verified to be a tree, and was parsed without error.
+	 * 
+	 * @param id
+	 *            name of the tree object, or a commit or annotated tag that may
+	 *            reference a tree.
+	 * @return reference to the tree object. Never null.
+	 * @throws MissingObjectException
+	 *             the supplied tree does not exist.
+	 * @throws IncorrectObjectTypeException
+	 *             the supplied id is not a tree, a commit or an annotated tag.
+	 * @throws IOException
+	 *             a pack file or loose object could not be read.
+	 */
+	public RevTree parseTree(final AnyObjectId id)
+			throws MissingObjectException, IncorrectObjectTypeException,
+			IOException {
+		RevObject c = parseAny(id);
+		while (c instanceof RevTag) {
+			c = ((RevTag) c).getObject();
+			parse(c);
+		}
+		if (c instanceof RevCommit) {
+			c = ((RevCommit) c).getTree();
+			parse(c);
+		}
+		final RevTree t = (RevTree) c;
+		if (db.openObject(t).getType() != Constants.OBJ_TREE)
+			throw new IncorrectObjectTypeException(t, Constants.TYPE_TREE);
+		return t;
+	}
+
+	/**
 	 * Locate a reference to any object and immediately parse its content.
 	 * <p>
 	 * This method only returns successfully if the object exists and was parsed
-- 
1.5.6.3.569.ga9185
Previous: Shawn O. PearceNext: Shawn O. Pearce
Message 9 of 29 in “Convert command line parsing to args4j”
  1. 00/28 Convert command line parsing to args4jShawn O. Pearce, Jul 18, 2008
  2. 01/28 Fix deadlock caused by push over SSHShawn O. Pearce, Jul 18, 2008
  3. 02/28 Use die utility method in glogShawn O. Pearce, Jul 18, 2008
  4. 03/28 Add args4j library for command line switch processingShawn O. Pearce, Jul 18, 2008
  5. 04/28 Move org.spearce.jgit.pgm to its own Java projectShawn O. Pearce, Jul 18, 2008
  6. 05/28 Make TextBuiltin public so other packages can implement and use itShawn O. Pearce, Jul 18, 2008
  7. 06/28 Initialize TextBuiltins with the repository before executionShawn O. Pearce, Jul 18, 2008
  8. 07/28 Define our own extended CmdLineParser for extra parsing supportShawn O. Pearce, Jul 18, 2008
  9. 08/28 Add parseTree method to RevWalk to obtain a RevTree from AnyObjectIdShawn O. Pearce, Jul 18, 2008
  10. 09/28 Add option handler for AbstractTreeIterator valuesShawn O. Pearce, Jul 18, 2008
  11. 10/28 Add option handler for ObjectId valuesShawn O. Pearce, Jul 18, 2008
  12. 11/28 Add option handler for PathTreeFilter valuesShawn O. Pearce, Jul 18, 2008
  13. 12/28 Add option handler for RefSpec valuesShawn O. Pearce, Jul 18, 2008
  14. 13/28 Add option handler for RevCommit valuesShawn O. Pearce, Jul 18, 2008
  15. 14/28 Add option handler for RevTree valuesShawn O. Pearce, Jul 18, 2008
  16. 15/28 Register most of our OptionHandler implementations for automatic useShawn O. Pearce, Jul 18, 2008
  17. 16/28 Convert jgit's Main to use args4j for basic parsing servicesShawn O. Pearce, Jul 18, 2008
  18. 17/28 Support automatic command line parsing for TextBuiltin subclassesShawn O. Pearce, Jul 18, 2008
  19. 18/28 Convert diff-tree program to args4jShawn O. Pearce, Jul 18, 2008
  20. 19/28 Convert fetch program to args4jShawn O. Pearce, Jul 18, 2008
  21. 20/28 Convert index-pack program to args4jShawn O. Pearce, Jul 18, 2008
  22. 21/28 Convert ls-remote program to args4jShawn O. Pearce, Jul 18, 2008
  23. 22/28 Convert ls-tree program to args4jShawn O. Pearce, Jul 18, 2008
  24. 23/28 Convert merge-base program to args4jShawn O. Pearce, Jul 18, 2008
  25. 24/28 Convert push program to args4jShawn O. Pearce, Jul 18, 2008
  26. 25/28 Convert show-ref program to args4jShawn O. Pearce, Jul 18, 2008
  27. 26/28 Convert tag program to args4jShawn O. Pearce, Jul 18, 2008
  28. 27/28 Convert rev-list, log, glog programs to args4jShawn O. Pearce, Jul 18, 2008
  29. 28/28 Remove support for legacy style TextBuiltinsShawn O. Pearce, Jul 18, 2008

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.