aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse/jgit
diff options
context:
space:
mode:
authorShawn O. Pearce <spearce@spearce.org>2010-08-23 10:13:25 -0700
committerShawn O. Pearce <spearce@spearce.org>2010-08-23 10:13:29 -0700
commit6df5d3397c5c9354409d21a8e207a061f2e6efc2 (patch)
treeac334cbeffe7a1616a0b24452e5c64004843106a /org.eclipse.jgit/src/org/eclipse/jgit
parent22b285695a2980824c15363ef5fec709ebc3d434 (diff)
downloadjgit-6df5d3397c5c9354409d21a8e207a061f2e6efc2.tar.gz
jgit-6df5d3397c5c9354409d21a8e207a061f2e6efc2.zip
Move commit and tag formatting to CommitBuilder, TagBuilder
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>
Diffstat (limited to 'org.eclipse.jgit/src/org/eclipse/jgit')
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java3
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/lib/CommitBuilder.java99
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectInserter.java148
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectWriter.java6
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/lib/TagBuilder.java68
5 files changed, 202 insertions, 122 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java
index a88899cc58..e385e7cb81 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java
@@ -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);
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/CommitBuilder.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/CommitBuilder.java
index 8457d45496..8bd02ee298 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/CommitBuilder.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/CommitBuilder.java
@@ -45,6 +45,10 @@
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();
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectInserter.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectInserter.java
index 94d792858f..1024459225 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectInserter.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectInserter.java
@@ -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
@@ -195,6 +182,38 @@ public abstract class ObjectInserter {
}
/**
+ * 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.
*
* @param type
@@ -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();
- }
}
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectWriter.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectWriter.java
index 5cb4b21689..339986be07 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectWriter.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectWriter.java
@@ -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 {
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/TagBuilder.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/TagBuilder.java
index 6cd0e9e882..71e46494c6 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/TagBuilder.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/TagBuilder.java
@@ -45,6 +45,10 @@
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();