]> source.dussan.org Git - jgit.git/commitdiff
Add a public RevCommit.parse() method 66/1366/1
authorShawn O. Pearce <spearce@spearce.org>
Fri, 20 Aug 2010 16:32:07 +0000 (09:32 -0700)
committerShawn O. Pearce <spearce@spearce.org>
Sat, 21 Aug 2010 00:38:53 +0000 (17:38 -0700)
Callers might have a canonical commit encoding on hand that they
wish to convert into a clean structure for presentation purposes,
and the object may not be available in a repository.  (E.g. maybe
its a "draft" commit being written in an editor.)

Change-Id: I21759cff337cbbb34dbdde91aec5aa4448a1ef37
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevCommitParseTest.java
org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevCommit.java

index b7e84419c9b6a02886492a785b8fc9a5d5b68fc2..93543733792d3bd438435bad0173cbbf1d855300 100644 (file)
 package org.eclipse.jgit.revwalk;
 
 import java.io.ByteArrayOutputStream;
+import java.io.UnsupportedEncodingException;
 
+import org.eclipse.jgit.lib.Commit;
 import org.eclipse.jgit.lib.Constants;
 import org.eclipse.jgit.lib.ObjectId;
+import org.eclipse.jgit.lib.ObjectInserter;
 import org.eclipse.jgit.lib.PersonIdent;
 import org.eclipse.jgit.lib.RepositoryTestCase;
 
@@ -315,6 +318,24 @@ public class RevCommitParseTest extends RepositoryTestCase {
                assertEquals(shortMsg, c.getShortMessage());
        }
 
+       public void testParse_PublicParseMethod()
+                       throws UnsupportedEncodingException {
+               ObjectInserter.Formatter fmt = new ObjectInserter.Formatter();
+               Commit src = new Commit();
+               src.setTreeId(fmt.idFor(Constants.OBJ_TREE, new byte[] {}));
+               src.setAuthor(author);
+               src.setCommitter(committer);
+               src.setMessage("Test commit\n\nThis is a test.\n");
+
+               RevCommit p = RevCommit.parse(fmt.format(src));
+               assertEquals(src.getTreeId(), p.getTree());
+               assertEquals(0, p.getParentCount());
+               assertEquals(author, p.getAuthorIdent());
+               assertEquals(committer, p.getCommitterIdent());
+               assertEquals("Test commit", p.getShortMessage());
+               assertEquals(src.getMessage(), p.getFullMessage());
+       }
+
        private static ObjectId id(final String str) {
                return ObjectId.fromString(str);
        }
index 74a51c8a4aa48d55baadb6c02d1c8d5b5b8d6a65..9e018b7a0663060035fbbe2246303d25b816a329 100644 (file)
@@ -55,11 +55,59 @@ import org.eclipse.jgit.errors.MissingObjectException;
 import org.eclipse.jgit.lib.AnyObjectId;
 import org.eclipse.jgit.lib.Constants;
 import org.eclipse.jgit.lib.MutableObjectId;
+import org.eclipse.jgit.lib.ObjectInserter;
+import org.eclipse.jgit.lib.ObjectReader;
 import org.eclipse.jgit.lib.PersonIdent;
 import org.eclipse.jgit.util.RawParseUtils;
 
 /** A commit reference to a commit in the DAG. */
 public class RevCommit extends RevObject {
+       /**
+        * Parse a commit from its canonical format.
+        *
+        * This method constructs a temporary revision pool, parses the commit as
+        * supplied, and returns it to the caller. Since the commit was built inside
+        * of a private revision pool its parent pointers will be initialized, but
+        * will not have their headers loaded.
+        *
+        * Applications are discouraged from using this API. Callers usually need
+        * more than one commit. Use {@link RevWalk#parseCommit(AnyObjectId)} to
+        * obtain a RevCommit from an existing repository.
+        *
+        * @param raw
+        *            the canonical formatted commit to be parsed.
+        * @return the parsed commit, in an isolated revision pool that is not
+        *         available to the caller.
+        */
+       public static RevCommit parse(byte[] raw) {
+               return parse(new RevWalk((ObjectReader) null), raw);
+       }
+
+       /**
+        * Parse a commit from its canonical format.
+        *
+        * This method inserts the commit directly into the caller supplied revision
+        * pool, making it appear as though the commit exists in the repository,
+        * even if it doesn't.  The repository under the pool is not affected.
+        *
+        * @param rw
+        *            the revision pool to allocate the commit within. The commit's
+        *            tree and parent pointers will be obtained from this pool.
+        * @param raw
+        *            the canonical formatted commit to be parsed.
+        * @return the parsed commit, in an isolated revision pool that is not
+        *         available to the caller.
+        */
+       public static RevCommit parse(RevWalk rw, byte[] raw) {
+               ObjectInserter.Formatter fmt = new ObjectInserter.Formatter();
+               boolean retain = rw.isRetainBody();
+               rw.setRetainBody(true);
+               RevCommit r = rw.lookupCommit(fmt.idFor(Constants.OBJ_COMMIT, raw));
+               r.parseCanonical(rw, raw);
+               rw.setRetainBody(retain);
+               return r;
+       }
+
        static final RevCommit[] NO_PARENTS = {};
 
        private RevTree tree;