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 <spearce@spearce.org>
This commit is contained in:
parent
b205597b91
commit
540df6c9fe
|
@ -45,10 +45,13 @@
|
|||
|
||||
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 void testParse_GitStyleMessage() throws Exception {
|
|||
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);
|
||||
}
|
||||
|
|
|
@ -53,12 +53,65 @@
|
|||
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;
|
||||
|
|
Loading…
Reference in New Issue