From f9b69677f643a694ec403666a30de2f3d146dd73 Mon Sep 17 00:00:00 2001 From: Martin Fick Date: Fri, 31 Mar 2017 11:31:47 -0600 Subject: [PATCH] Add parseCommit(AnyObjectId) method to Repository. It is quite common to want to parse a commit without already having a RevWalk. Provide a shortcut to do so to make it more convenient, and to ensure that the RevWalk is released afterwards. Signed-off-by: Martin Fick Change-Id: I9528e80063122ac318f115900422a24ae49a920e --- .../src/org/eclipse/jgit/lib/Repository.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java index ea37e7938..1f2ab9df8 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java @@ -1148,6 +1148,33 @@ public abstract class Repository implements AutoCloseable { return indexFile; } + /** + * Locate a reference to a commit and immediately parse its content. + *

+ * This method only returns successfully if the commit object exists, + * is verified to be a commit, and was parsed without error. + * + * @param id + * name of the commit object. + * @return reference to the commit object. Never null. + * @throws MissingObjectException + * the supplied commit does not exist. + * @throws IncorrectObjectTypeException + * the supplied id is not a commit or an annotated tag. + * @throws IOException + * a pack file or loose object could not be read. + * @since 4.8 + */ + public RevCommit parseCommit(AnyObjectId id) throws IncorrectObjectTypeException, + IOException, MissingObjectException { + if (id instanceof RevCommit && ((RevCommit) id).getRawBuffer() != null) { + return (RevCommit) id; + } + try (RevWalk walk = new RevWalk(this)) { + return walk.parseCommit(id); + } + } + /** * Create a new in-core index representation and read an index from disk. *