]> source.dussan.org Git - jgit.git/commitdiff
Move commit and tag formatting to CommitBuilder, TagBuilder 84/1384/1
authorShawn O. Pearce <spearce@spearce.org>
Mon, 23 Aug 2010 17:13:25 +0000 (10:13 -0700)
committerShawn O. Pearce <spearce@spearce.org>
Mon, 23 Aug 2010 17:13:29 +0000 (10:13 -0700)
These objects should be responsible for their own formatting,
rather than delegating it to some obtuse type called ObjectInserter.

While we are at it, simplify the way we insert these into a database.
Passing in the type and calling format in application code turned
out to be a huge mistake in terms of ease-of-use of the insert API.

Change-Id: Id5bb95ee56aa2a002243e9b7853b84ec8df1d7bf
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
13 files changed:
org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Tag.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/RebuildCommitGraph.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/CherryPickTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/SimpleMergeTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevCommitParseTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevTagParseTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/T0003_Basic.java
org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/CommitBuilder.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectInserter.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectWriter.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/TagBuilder.java

index 6e1f1db3975dc14ec27ed4c01f36929beec0445c..7a95ccd3681890a84d43de218de90c2b7a38cda6 100644 (file)
@@ -368,7 +368,7 @@ public class TestRepository<R extends Repository> {
                c.setMessage("");
                ObjectId id;
                try {
-                       id = inserter.insert(Constants.OBJ_COMMIT, inserter.format(c));
+                       id = inserter.insert(c);
                        inserter.flush();
                } finally {
                        inserter.release();
@@ -405,7 +405,7 @@ public class TestRepository<R extends Repository> {
                t.setMessage("");
                ObjectId id;
                try {
-                       id = inserter.insert(Constants.OBJ_TAG, inserter.format(t));
+                       id = inserter.insert(t);
                        inserter.flush();
                } finally {
                        inserter.release();
@@ -822,8 +822,7 @@ public class TestRepository<R extends Repository> {
                                ObjectId commitId;
                                try {
                                        c.setTreeId(tree.writeTree(inserter));
-                                       commitId = inserter.insert(Constants.OBJ_COMMIT, inserter
-                                                       .format(c));
+                                       commitId = inserter.insert(c);
                                        inserter.flush();
                                } finally {
                                        inserter.release();
index 0ab19b6bb9c09fb230db47134f3cae7b916d36fb..19454b18682e8f8b5538f038a2193791112ab72a 100644 (file)
@@ -100,7 +100,7 @@ class Tag extends TextBuiltin {
                        tag.setTagger(new PersonIdent(db));
                        tag.setMessage(message.replaceAll("\r", ""));
                        tag.setTag(shortName);
-                       id = inserter.insert(Constants.OBJ_TAG, inserter.format(tag));
+                       id = inserter.insert(tag);
                        inserter.flush();
                } finally {
                        inserter.release();
index ab4412871317f372bb87a13029a76a91eee238b2..b01734e00af643a823a8f5d443d4fda592775ba8 100644 (file)
@@ -197,7 +197,7 @@ class RebuildCommitGraph extends TextBuiltin {
                                newc.setCommitter(newc.getAuthor());
                                newc.setParentIds(newParents);
                                newc.setMessage("ORIGINAL " + t.oldId.name() + "\n");
-                               t.newId = oi.insert(Constants.OBJ_COMMIT,oi.format(newc));
+                               t.newId = oi.insert(newc);
                                rewrites.put(t.oldId, t.newId);
                                pm.update(1);
                        }
index 876b98636c53f918e953d0ea2939d7ef2975d725..12fdec2b63d45846cfb264e8cc9e3ae7c7b27105 100644 (file)
@@ -45,7 +45,6 @@
 package org.eclipse.jgit.merge;
 
 import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
-import static org.eclipse.jgit.lib.Constants.OBJ_COMMIT;
 
 import org.eclipse.jgit.dircache.DirCache;
 import org.eclipse.jgit.dircache.DirCacheBuilder;
@@ -137,7 +136,7 @@ public class CherryPickTest extends RepositoryTestCase {
                c.setCommitter(c.getAuthor());
                c.setParentIds(parentIds);
                c.setMessage("Tree " + c.getTreeId().name());
-               ObjectId id = odi.insert(OBJ_COMMIT, odi.format(c));
+               ObjectId id = odi.insert(c);
                odi.flush();
                return id;
        }
index 5e7bcee1e7f518ceb2104a6caff95c466d3d39c6..659c9e3b2d7696bf32b343f7bb7d2de212d2ba56 100644 (file)
@@ -45,7 +45,6 @@
 package org.eclipse.jgit.merge;
 
 import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
-import static org.eclipse.jgit.lib.Constants.OBJ_COMMIT;
 
 import java.io.IOException;
 
@@ -373,7 +372,7 @@ public class SimpleMergeTest extends SampleDataRepositoryTestCase {
                c.setCommitter(c.getAuthor());
                c.setParentIds(parentIds);
                c.setMessage("Tree " + c.getTreeId().name());
-               ObjectId id = odi.insert(OBJ_COMMIT, odi.format(c));
+               ObjectId id = odi.insert(c);
                odi.flush();
                return id;
        }
index 36730c153f80c5913c06771ee358662175cf2a29..7be7dbc8144c562d00a1693641755da2984effa7 100644 (file)
@@ -327,7 +327,7 @@ public class RevCommitParseTest extends RepositoryTestCase {
                src.setCommitter(committer);
                src.setMessage("Test commit\n\nThis is a test.\n");
 
-               RevCommit p = RevCommit.parse(fmt.format(src));
+               RevCommit p = RevCommit.parse(src.format());
                assertEquals(src.getTreeId(), p.getTree());
                assertEquals(0, p.getParentCount());
                assertEquals(author, p.getAuthorIdent());
index 727c48e8eff7f951dc9d112aa4acac53d1027eb4..c694add449a49205c6106d33588a45b97873ca1f 100644 (file)
@@ -407,7 +407,7 @@ public class RevTagParseTest extends RepositoryTestCase {
                src.setTag("a.test");
                src.setMessage("Test tag\n\nThis is a test.\n");
 
-               RevTag p = RevTag.parse(fmt.format(src));
+               RevTag p = RevTag.parse(src.format());
                assertEquals(src.getObjectId(), p.getObject());
                assertEquals(committer, p.getTaggerIdent());
                assertEquals("a.test", p.getTagName());
index cd802d41ecdb4c58b67f9b7bbda61aaaf2772398..0352a40bfd888ab41cea644d210d099c65e34c16 100644 (file)
@@ -693,13 +693,12 @@ public class T0003_Basic extends SampleDataRepositoryTestCase {
 
        }
 
-       private ObjectId insertCommit(final CommitBuilder commit) throws IOException,
+       private ObjectId insertCommit(final CommitBuilder builder) throws IOException,
                        UnsupportedEncodingException {
                ObjectInserter oi = db.newObjectInserter();
                try {
-                       ObjectId id = oi.insert(Constants.OBJ_COMMIT, oi.format(commit));
+                       ObjectId id = oi.insert(builder);
                        oi.flush();
-                       commit.setCommitId(id);
                        return id;
                } finally {
                        oi.release();
@@ -721,9 +720,8 @@ public class T0003_Basic extends SampleDataRepositoryTestCase {
                        UnsupportedEncodingException {
                ObjectInserter oi = db.newObjectInserter();
                try {
-                       ObjectId id = oi.insert(Constants.OBJ_TAG, oi.format(tag));
+                       ObjectId id = oi.insert(tag);
                        oi.flush();
-                       tag.setTagId(id);
                        return id;
                } finally {
                        oi.release();
index a88899cc58a15e8d5405a3d5d15c02d12d2516eb..e385e7cb812c5b30e38b43b60d8ff13179b6b850 100644 (file)
@@ -169,8 +169,7 @@ public class CommitCommand extends GitCommand<RevCommit> {
 
                                        commit.setParentIds(parents);
                                        commit.setTreeId(indexTreeId);
-                                       ObjectId commitId = odi.insert(Constants.OBJ_COMMIT, odi
-                                                       .format(commit));
+                                       ObjectId commitId = odi.insert(commit);
                                        odi.flush();
 
                                        RevWalk revWalk = new RevWalk(repo);
index 8457d45496475dae5e58a40447f2e0ef7dff610f..8bd02ee298cbb90a83bd4fc40298895efaee7390 100644 (file)
 
 package org.eclipse.jgit.lib;
 
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.UnsupportedEncodingException;
 import java.nio.charset.Charset;
 import java.util.List;
 
@@ -62,6 +66,16 @@ import java.util.List;
 public class CommitBuilder {
        private static final ObjectId[] EMPTY_OBJECTID_LIST = new ObjectId[0];
 
+       private static final byte[] htree = Constants.encodeASCII("tree");
+
+       private static final byte[] hparent = Constants.encodeASCII("parent");
+
+       private static final byte[] hauthor = Constants.encodeASCII("author");
+
+       private static final byte[] hcommitter = Constants.encodeASCII("committer");
+
+       private static final byte[] hencoding = Constants.encodeASCII("encoding");
+
        private ObjectId commitId;
 
        private ObjectId treeId;
@@ -262,6 +276,91 @@ public class CommitBuilder {
                return encoding;
        }
 
+       /**
+        * Format this builder's state as a commit object.
+        *
+        * As a side effect, {@link #getCommitId()} will be populated with the
+        * proper ObjectId for the formatted content.
+        *
+        * @return this object in the canonical commit format, suitable for storage
+        *         in a repository.
+        * @throws UnsupportedEncodingException
+        *             the encoding specified by {@link #getEncoding()} is not
+        *             supported by this Java runtime.
+        */
+       public byte[] format() throws UnsupportedEncodingException {
+               return format(new ObjectInserter.Formatter());
+       }
+
+       /**
+        * Format this builder's state as a commit object.
+        *
+        * As a side effect, {@link #getCommitId()} will be populated with the
+        * proper ObjectId for the formatted content.
+        *
+        * @param oi
+        *            the inserter whose formatting support will be reused. The
+        *            inserter itself is not affected, and the commit is not
+        *            actually inserted into the repository.
+        * @return this object in the canonical commit format, suitable for storage
+        *         in a repository.
+        * @throws UnsupportedEncodingException
+        *             the encoding specified by {@link #getEncoding()} is not
+        *             supported by this Java runtime.
+        */
+       public byte[] format(ObjectInserter oi) throws UnsupportedEncodingException {
+               ByteArrayOutputStream os = new ByteArrayOutputStream();
+               OutputStreamWriter w = new OutputStreamWriter(os, getEncoding());
+               try {
+                       os.write(htree);
+                       os.write(' ');
+                       getTreeId().copyTo(os);
+                       os.write('\n');
+
+                       for (ObjectId p : getParentIds()) {
+                               os.write(hparent);
+                               os.write(' ');
+                               p.copyTo(os);
+                               os.write('\n');
+                       }
+
+                       os.write(hauthor);
+                       os.write(' ');
+                       w.write(getAuthor().toExternalString());
+                       w.flush();
+                       os.write('\n');
+
+                       os.write(hcommitter);
+                       os.write(' ');
+                       w.write(getCommitter().toExternalString());
+                       w.flush();
+                       os.write('\n');
+
+                       if (getEncoding() != Constants.CHARSET) {
+                               os.write(hencoding);
+                               os.write(' ');
+                               os.write(Constants.encodeASCII(getEncoding().name()));
+                               os.write('\n');
+                       }
+
+                       os.write('\n');
+
+                       if (getMessage() != null) {
+                               w.write(getMessage());
+                               w.flush();
+                       }
+               } catch (IOException err) {
+                       // This should never occur, the only way to get it above is
+                       // for the ByteArrayOutputStream to throw, but it doesn't.
+                       //
+                       throw new RuntimeException(err);
+               }
+
+               byte[] content = os.toByteArray();
+               setCommitId(oi.idFor(Constants.OBJ_COMMIT, content));
+               return content;
+       }
+
        @Override
        public String toString() {
                StringBuilder r = new StringBuilder();
index 94d792858f3c578165e1e01bb5a13a0b5c6d27c1..102445922521bc42e973b9879c7260074a9d082a 100644 (file)
@@ -51,9 +51,6 @@ import java.io.ByteArrayOutputStream;
 import java.io.EOFException;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.OutputStreamWriter;
-import java.io.UnsupportedEncodingException;
-import java.nio.charset.Charset;
 import java.security.MessageDigest;
 import java.text.MessageFormat;
 
@@ -73,16 +70,6 @@ import org.eclipse.jgit.errors.ObjectWritingException;
  * otherwise making the returned ObjectIds visible to other code.
  */
 public abstract class ObjectInserter {
-       private static final byte[] htree = Constants.encodeASCII("tree");
-
-       private static final byte[] hparent = Constants.encodeASCII("parent");
-
-       private static final byte[] hauthor = Constants.encodeASCII("author");
-
-       private static final byte[] hcommitter = Constants.encodeASCII("committer");
-
-       private static final byte[] hencoding = Constants.encodeASCII("encoding");
-
        /** An inserter that can be used for formatting and id generation only. */
        public static class Formatter extends ObjectInserter {
                @Override
@@ -194,6 +181,38 @@ public abstract class ObjectInserter {
                return ObjectId.fromRaw(md.digest());
        }
 
+       /**
+        * Insert a single commit into the store, returning its unique name.
+        *
+        * As a side effect, {@link CommitBuilder#getCommitId()} will also be
+        * populated with the returned ObjectId.
+        *
+        * @param builder
+        *            the builder containing the proposed commit's data.
+        * @return the name of the commit object.
+        * @throws IOException
+        *             the object could not be stored.
+        */
+       public final ObjectId insert(CommitBuilder builder) throws IOException {
+               return insert(Constants.OBJ_COMMIT, builder.format(this));
+       }
+
+       /**
+        * Insert a single annotated tag into the store, returning its unique name.
+        *
+        * As a side effect, {@link TagBuilder#getTagId()} will also be populated
+        * with the returned ObjectId.
+        *
+        * @param builder
+        *            the builder containing the proposed tag's data.
+        * @return the name of the tag object.
+        * @throws IOException
+        *             the object could not be stored.
+        */
+       public final ObjectId insert(TagBuilder builder) throws IOException {
+               return insert(Constants.OBJ_TAG, builder.format(this));
+       }
+
        /**
         * Insert a single object into the store, returning its unique name.
         *
@@ -293,107 +312,4 @@ public abstract class ObjectInserter {
                }
                return o.toByteArray();
        }
-
-       /**
-        * Format a Commit in canonical format.
-        *
-        * @param commit
-        *            the commit object to format
-        * @return canonical encoding of the commit object.
-        * @throws UnsupportedEncodingException
-        *             the commit's chosen encoding isn't supported on this JVM.
-        */
-       public final byte[] format(CommitBuilder commit)
-                       throws UnsupportedEncodingException {
-               Charset encoding = commit.getEncoding();
-               ByteArrayOutputStream os = new ByteArrayOutputStream();
-               OutputStreamWriter w = new OutputStreamWriter(os, encoding);
-               try {
-                       os.write(htree);
-                       os.write(' ');
-                       commit.getTreeId().copyTo(os);
-                       os.write('\n');
-
-                       for (ObjectId p : commit.getParentIds()) {
-                               os.write(hparent);
-                               os.write(' ');
-                               p.copyTo(os);
-                               os.write('\n');
-                       }
-
-                       os.write(hauthor);
-                       os.write(' ');
-                       w.write(commit.getAuthor().toExternalString());
-                       w.flush();
-                       os.write('\n');
-
-                       os.write(hcommitter);
-                       os.write(' ');
-                       w.write(commit.getCommitter().toExternalString());
-                       w.flush();
-                       os.write('\n');
-
-                       if (encoding != Constants.CHARSET) {
-                               os.write(hencoding);
-                               os.write(' ');
-                               os.write(Constants.encodeASCII(encoding.name()));
-                               os.write('\n');
-                       }
-
-                       os.write('\n');
-
-                       if (commit.getMessage() != null) {
-                               w.write(commit.getMessage());
-                               w.flush();
-                       }
-               } catch (IOException err) {
-                       // This should never occur, the only way to get it above is
-                       // for the ByteArrayOutputStream to throw, but it doesn't.
-                       //
-                       throw new RuntimeException(err);
-               }
-               return os.toByteArray();
-       }
-
-       /**
-        * Format a Tag in canonical format.
-        *
-        * @param tag
-        *            the tag object to format
-        * @return canonical encoding of the tag object.
-        */
-       public final byte[] format(TagBuilder tag) {
-               ByteArrayOutputStream os = new ByteArrayOutputStream();
-               OutputStreamWriter w = new OutputStreamWriter(os, Constants.CHARSET);
-               try {
-                       w.write("object ");
-                       tag.getObjectId().copyTo(w);
-                       w.write('\n');
-
-                       w.write("type ");
-                       w.write(Constants.typeString(tag.getObjectType()));
-                       w.write("\n");
-
-                       w.write("tag ");
-                       w.write(tag.getTag());
-                       w.write("\n");
-
-                       if (tag.getTagger() != null) {
-                               w.write("tagger ");
-                               w.write(tag.getTagger().toExternalString());
-                               w.write('\n');
-                       }
-
-                       w.write('\n');
-                       if (tag.getMessage() != null)
-                               w.write(tag.getMessage());
-                       w.close();
-               } catch (IOException err) {
-                       // This should never occur, the only way to get it above is
-                       // for the ByteArrayOutputStream to throw, but it doesn't.
-                       //
-                       throw new RuntimeException(err);
-               }
-               return os.toByteArray();
-       }
 }
index 5cb4b216897465e07be1288cc060f06bd0740e2b..339986be07dec09510ea6c9384df3bd3539213a5 100644 (file)
@@ -46,8 +46,6 @@
 package org.eclipse.jgit.lib;
 
 import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
-import static org.eclipse.jgit.lib.Constants.OBJ_COMMIT;
-import static org.eclipse.jgit.lib.Constants.OBJ_TAG;
 import static org.eclipse.jgit.lib.Constants.OBJ_TREE;
 
 import java.io.File;
@@ -174,7 +172,7 @@ public class ObjectWriter {
         */
        public ObjectId writeCommit(CommitBuilder commit) throws IOException {
                try {
-                       ObjectId id = inserter.insert(OBJ_COMMIT, inserter.format(commit));
+                       ObjectId id = inserter.insert(commit);
                        inserter.flush();
                        return id;
                } finally {
@@ -192,7 +190,7 @@ public class ObjectWriter {
         */
        public ObjectId writeTag(TagBuilder tag) throws IOException {
                try {
-                       ObjectId id = inserter.insert(OBJ_TAG, inserter.format(tag));
+                       ObjectId id = inserter.insert(tag);
                        inserter.flush();
                        return id;
                } finally {
index 6cd0e9e8820fc3e3ae8fccbb795a4677d6923bb8..71e46494c61bb51812dae597a09c547ba86880b0 100644 (file)
 
 package org.eclipse.jgit.lib;
 
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+
 import org.eclipse.jgit.revwalk.RevObject;
 
 /**
@@ -169,6 +173,70 @@ public class TagBuilder {
                tagId = null;
        }
 
+       /**
+        * Format this builder's state as an annotated tag object.
+        *
+        * As a side effect, {@link #getTagId()} will be populated with the proper
+        * ObjectId for the formatted content.
+        *
+        * @return this object in the canonical annotated tag format, suitable for
+        *         storage in a repository.
+        */
+       public byte[] format() {
+               return format(new ObjectInserter.Formatter());
+       }
+
+       /**
+        * Format this builder's state as an annotated tag object.
+        *
+        * As a side effect, {@link #getTagId()} will be populated with the proper
+        * ObjectId for the formatted content.
+        *
+        * @param oi
+        *            the inserter whose formatting support will be reused. The
+        *            inserter itself is not affected, and the annotated tag is not
+        *            actually inserted into the repository.
+        * @return this object in the canonical annotated tag format, suitable for
+        *         storage in a repository.
+        */
+       public byte[] format(ObjectInserter oi) {
+               ByteArrayOutputStream os = new ByteArrayOutputStream();
+               OutputStreamWriter w = new OutputStreamWriter(os, Constants.CHARSET);
+               try {
+                       w.write("object ");
+                       getObjectId().copyTo(w);
+                       w.write('\n');
+
+                       w.write("type ");
+                       w.write(Constants.typeString(getObjectType()));
+                       w.write("\n");
+
+                       w.write("tag ");
+                       w.write(getTag());
+                       w.write("\n");
+
+                       if (getTagger() != null) {
+                               w.write("tagger ");
+                               w.write(getTagger().toExternalString());
+                               w.write('\n');
+                       }
+
+                       w.write('\n');
+                       if (getMessage() != null)
+                               w.write(getMessage());
+                       w.close();
+               } catch (IOException err) {
+                       // This should never occur, the only way to get it above is
+                       // for the ByteArrayOutputStream to throw, but it doesn't.
+                       //
+                       throw new RuntimeException(err);
+               }
+
+               byte[] content = os.toByteArray();
+               setTagId(oi.idFor(Constants.OBJ_TAG, content));
+               return content;
+       }
+
        @Override
        public String toString() {
                StringBuilder r = new StringBuilder();