From 540df6c9fe67bc6424547101b1e260e3b2149603 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Fri, 20 Aug 2010 15:41:49 -0700 Subject: [PATCH] Add a public RevTag.parse() method Callers might have a canonical tag 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" tag being written in an editor.) Change-Id: I387a462afb70754aa7ee20891e6c0262438fdf32 Signed-off-by: Shawn O. Pearce --- .../eclipse/jgit/revwalk/RevTagParseTest.java | 20 +++++++ .../src/org/eclipse/jgit/revwalk/RevTag.java | 53 +++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevTagParseTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevTagParseTest.java index b3e62f49e3..af70eb5a0d 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevTagParseTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevTagParseTest.java @@ -45,10 +45,13 @@ package org.eclipse.jgit.revwalk; import java.io.ByteArrayOutputStream; +import org.eclipse.jgit.errors.CorruptObjectException; 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; +import org.eclipse.jgit.lib.Tag; public class RevTagParseTest extends RepositoryTestCase { public void testTagBlob() throws Exception { @@ -395,6 +398,23 @@ public class RevTagParseTest extends RepositoryTestCase { assertEquals(shortMsg, c.getShortMessage()); } + public void testParse_PublicParseMethod() throws CorruptObjectException { + ObjectInserter.Formatter fmt = new ObjectInserter.Formatter(); + Tag src = new Tag(); + src.setObjectId(fmt.idFor(Constants.OBJ_TREE, new byte[] {}), + Constants.OBJ_TREE); + src.setTagger(committer); + src.setTag("a.test"); + src.setMessage("Test tag\n\nThis is a test.\n"); + + RevTag p = RevTag.parse(fmt.format(src)); + assertEquals(src.getObjectId(), p.getObject()); + assertEquals(committer, p.getTaggerIdent()); + assertEquals("a.test", p.getTagName()); + assertEquals("Test tag", p.getShortMessage()); + assertEquals(src.getMessage(), p.getFullMessage()); + } + private static ObjectId id(final String str) { return ObjectId.fromString(str); } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevTag.java b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevTag.java index dadebe9cb5..aa33d91a55 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevTag.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevTag.java @@ -53,12 +53,65 @@ import org.eclipse.jgit.errors.IncorrectObjectTypeException; import org.eclipse.jgit.errors.MissingObjectException; import org.eclipse.jgit.lib.AnyObjectId; import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.ObjectInserter; +import org.eclipse.jgit.lib.ObjectReader; import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.util.MutableInteger; import org.eclipse.jgit.util.RawParseUtils; /** An annotated tag. */ public class RevTag extends RevObject { + /** + * Parse an annotated tag from its canonical format. + * + * This method constructs a temporary revision pool, parses the tag as + * supplied, and returns it to the caller. Since the tag was built inside of + * a private revision pool its object pointer will be initialized, but will + * not have its headers loaded. + * + * Applications are discouraged from using this API. Callers usually need + * more than one object. Use {@link RevWalk#parseTag(AnyObjectId)} to obtain + * a RevTag from an existing repository. + * + * @param raw + * the canonical formatted tag to be parsed. + * @return the parsed tag, in an isolated revision pool that is not + * available to the caller. + * @throws CorruptObjectException + * the tag contains a malformed header that cannot be handled. + */ + public static RevTag parse(byte[] raw) throws CorruptObjectException { + return parse(new RevWalk((ObjectReader) null), raw); + } + + /** + * Parse an annotated tag from its canonical format. + * + * This method inserts the tag directly into the caller supplied revision + * pool, making it appear as though the tag 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 tag within. The tag's object + * pointer will be obtained from this pool. + * @param raw + * the canonical formatted tag to be parsed. + * @return the parsed tag, in an isolated revision pool that is not + * available to the caller. + * @throws CorruptObjectException + * the tag contains a malformed header that cannot be handled. + */ + public static RevTag parse(RevWalk rw, byte[] raw) + throws CorruptObjectException { + ObjectInserter.Formatter fmt = new ObjectInserter.Formatter(); + boolean retain = rw.isRetainBody(); + rw.setRetainBody(true); + RevTag r = rw.lookupTag(fmt.idFor(Constants.OBJ_TAG, raw)); + r.parseCanonical(rw, raw); + rw.setRetainBody(retain); + return r; + } + private RevObject object; private byte[] buffer; -- 2.39.5