aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse/jgit/lib
diff options
context:
space:
mode:
authorChris Aniszczyk <caniszczyk@gmail.com>2010-11-10 17:53:35 -0500
committerCode Review <codereview-daemon@eclipse.org>2010-11-10 17:53:35 -0500
commit453b620e62bc6af590c220f6ec11d7a695098a49 (patch)
treec85b0b53375a43f699dc7d73d651ebc3de915f46 /org.eclipse.jgit/src/org/eclipse/jgit/lib
parent20a5a34444df017ba2232313a8137d220980883d (diff)
parentc27e1daa55255416d93e3a019f556f0ca90fd410 (diff)
downloadjgit-453b620e62bc6af590c220f6ec11d7a695098a49.tar.gz
jgit-453b620e62bc6af590c220f6ec11d7a695098a49.zip
Merge "Refactor tree entry formatting into a common class"
Diffstat (limited to 'org.eclipse.jgit/src/org/eclipse/jgit/lib')
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/lib/FileMode.java17
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/lib/Tree.java11
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/lib/TreeFormatter.java333
3 files changed, 353 insertions, 8 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/FileMode.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/FileMode.java
index 43e4dd9c15..f295f5b72f 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/FileMode.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/FileMode.java
@@ -223,6 +223,23 @@ public abstract class FileMode {
}
/**
+ * Copy this mode as a sequence of octal US-ASCII bytes.
+ *
+ * The mode is copied as a sequence of octal digits using the US-ASCII
+ * character encoding. The sequence does not use a leading '0' prefix to
+ * indicate octal notation. This method is suitable for generation of a mode
+ * string within a GIT tree object.
+ *
+ * @param buf
+ * buffer to copy the mode to.
+ * @param ptr
+ * position within {@code buf} for first digit.
+ */
+ public void copyTo(byte[] buf, int ptr) {
+ System.arraycopy(octalBytes, 0, buf, ptr, octalBytes.length);
+ }
+
+ /**
* @return the number of bytes written by {@link #copyTo(OutputStream)}.
*/
public int copyToLength() {
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Tree.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Tree.java
index 2eb578b88e..06ef429974 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Tree.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Tree.java
@@ -45,7 +45,6 @@
package org.eclipse.jgit.lib;
-import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.MessageFormat;
@@ -614,20 +613,16 @@ public class Tree extends TreeEntry implements Treeish {
* the tree cannot be loaded, or its not in a writable state.
*/
public byte[] format() throws IOException {
- ByteArrayOutputStream o = new ByteArrayOutputStream();
+ TreeFormatter fmt = new TreeFormatter();
for (TreeEntry e : members()) {
ObjectId id = e.getId();
if (id == null)
throw new ObjectWritingException(MessageFormat.format(JGitText
.get().objectAtPathDoesNotHaveId, e.getFullName()));
- e.getMode().copyTo(o);
- o.write(' ');
- o.write(e.getNameUTF8());
- o.write(0);
- id.copyRawTo(o);
+ fmt.append(e.getNameUTF8(), e.getMode(), id);
}
- return o.toByteArray();
+ return fmt.toByteArray();
}
public String toString() {
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/TreeFormatter.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/TreeFormatter.java
new file mode 100644
index 0000000000..e14e81f6e0
--- /dev/null
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/TreeFormatter.java
@@ -0,0 +1,333 @@
+/*
+ * Copyright (C) 2010, Google Inc.
+ * and other copyright owners as documented in the project's IP log.
+ *
+ * This program and the accompanying materials are made available
+ * under the terms of the Eclipse Distribution License v1.0 which
+ * accompanies this distribution, is reproduced below, and is
+ * available at http://www.eclipse.org/org/documents/edl-v10.php
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * - Neither the name of the Eclipse Foundation, Inc. nor the
+ * names of its contributors may be used to endorse or promote
+ * products derived from this software without specific prior
+ * written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.eclipse.jgit.lib;
+
+import static org.eclipse.jgit.lib.Constants.OBJECT_ID_LENGTH;
+import static org.eclipse.jgit.lib.Constants.OBJ_TREE;
+import static org.eclipse.jgit.lib.Constants.encode;
+import static org.eclipse.jgit.lib.FileMode.GITLINK;
+import static org.eclipse.jgit.lib.FileMode.REGULAR_FILE;
+import static org.eclipse.jgit.lib.FileMode.TREE;
+
+import java.io.IOException;
+
+import org.eclipse.jgit.revwalk.RevBlob;
+import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevTree;
+import org.eclipse.jgit.util.TemporaryBuffer;
+
+/**
+ * Mutable formatter to construct a single tree object.
+ *
+ * This formatter does not process subtrees. Callers must handle creating each
+ * subtree on their own.
+ *
+ * To maintain good performance for bulk operations, this formatter does not
+ * validate its input. Callers are responsible for ensuring the resulting tree
+ * object is correctly well formed by writing entries in the correct order.
+ */
+public class TreeFormatter {
+ /**
+ * Compute the size of a tree entry record.
+ *
+ * This method can be used to estimate the correct size of a tree prior to
+ * allocating a formatter. Getting the size correct at allocation time
+ * ensures the internal buffer is sized correctly, reducing copying.
+ *
+ * @param mode
+ * the mode the entry will have.
+ * @param nameLen
+ * the length of the name, in bytes.
+ * @return the length of the record.
+ */
+ public static int entrySize(FileMode mode, int nameLen) {
+ return mode.copyToLength() + nameLen + OBJECT_ID_LENGTH + 2;
+ }
+
+ private byte[] buf;
+
+ private int ptr;
+
+ private TemporaryBuffer.Heap overflowBuffer;
+
+ /** Create an empty formatter with a default buffer size. */
+ public TreeFormatter() {
+ this(8192);
+ }
+
+ /**
+ * Create an empty formatter with the specified buffer size.
+ *
+ * @param size
+ * estimated size of the tree, in bytes. Callers can use
+ * {@link #entrySize(FileMode, int)} to estimate the size of each
+ * entry in advance of allocating the formatter.
+ */
+ public TreeFormatter(int size) {
+ buf = new byte[size];
+ }
+
+ /**
+ * Add a link to a submodule commit, mode is {@link #GITLINK}.
+ *
+ * @param name
+ * name of the entry.
+ * @param commit
+ * the ObjectId to store in this entry.
+ */
+ public void append(String name, RevCommit commit) {
+ append(name, GITLINK, commit);
+ }
+
+ /**
+ * Add a subtree, mode is {@link #TREE}.
+ *
+ * @param name
+ * name of the entry.
+ * @param tree
+ * the ObjectId to store in this entry.
+ */
+ public void append(String name, RevTree tree) {
+ append(name, TREE, tree);
+ }
+
+ /**
+ * Add a regular file, mode is {@link #REGULAR_FILE}.
+ *
+ * @param name
+ * name of the entry.
+ * @param blob
+ * the ObjectId to store in this entry.
+ */
+ public void append(String name, RevBlob blob) {
+ append(name, REGULAR_FILE, blob);
+ }
+
+ /**
+ * Append any entry to the tree.
+ *
+ * @param name
+ * name of the entry.
+ * @param mode
+ * mode describing the treatment of {@code id}.
+ * @param id
+ * the ObjectId to store in this entry.
+ */
+ public void append(String name, FileMode mode, AnyObjectId id) {
+ append(encode(name), mode, id);
+ }
+
+ /**
+ * Append any entry to the tree.
+ *
+ * @param name
+ * name of the entry. The name should be UTF-8 encoded, but file
+ * name encoding is not a well defined concept in Git.
+ * @param mode
+ * mode describing the treatment of {@code id}.
+ * @param id
+ * the ObjectId to store in this entry.
+ */
+ public void append(byte[] name, FileMode mode, AnyObjectId id) {
+ append(name, 0, name.length, mode, id);
+ }
+
+ /**
+ * Append any entry to the tree.
+ *
+ * @param nameBuf
+ * buffer holding the name of the entry. The name should be UTF-8
+ * encoded, but file name encoding is not a well defined concept
+ * in Git.
+ * @param namePos
+ * first position within {@code nameBuf} of the name data.
+ * @param nameLen
+ * number of bytes from {@code nameBuf} to use as the name.
+ * @param mode
+ * mode describing the treatment of {@code id}.
+ * @param id
+ * the ObjectId to store in this entry.
+ */
+ public void append(byte[] nameBuf, int namePos, int nameLen, FileMode mode,
+ AnyObjectId id) {
+ if (fmtBuf(nameBuf, namePos, nameLen, mode)) {
+ id.copyRawTo(buf, ptr);
+ ptr += OBJECT_ID_LENGTH;
+
+ } else {
+ try {
+ fmtOverflowBuffer(nameBuf, namePos, nameLen, mode);
+ id.copyRawTo(overflowBuffer);
+ } catch (IOException badBuffer) {
+ // This should never occur.
+ throw new RuntimeException(badBuffer);
+ }
+ }
+ }
+
+ /**
+ * Append any entry to the tree.
+ *
+ * @param nameBuf
+ * buffer holding the name of the entry. The name should be UTF-8
+ * encoded, but file name encoding is not a well defined concept
+ * in Git.
+ * @param namePos
+ * first position within {@code nameBuf} of the name data.
+ * @param nameLen
+ * number of bytes from {@code nameBuf} to use as the name.
+ * @param mode
+ * mode describing the treatment of {@code id}.
+ * @param idBuf
+ * buffer holding the raw ObjectId of the entry.
+ * @param idPos
+ * first position within {@code idBuf} to copy the id from.
+ */
+ public void append(byte[] nameBuf, int namePos, int nameLen, FileMode mode,
+ byte[] idBuf, int idPos) {
+ if (fmtBuf(nameBuf, namePos, nameLen, mode)) {
+ System.arraycopy(idBuf, idPos, buf, ptr, OBJECT_ID_LENGTH);
+ ptr += OBJECT_ID_LENGTH;
+
+ } else {
+ try {
+ fmtOverflowBuffer(nameBuf, namePos, nameLen, mode);
+ overflowBuffer.write(idBuf, idPos, OBJECT_ID_LENGTH);
+ } catch (IOException badBuffer) {
+ // This should never occur.
+ throw new RuntimeException(badBuffer);
+ }
+ }
+ }
+
+ private boolean fmtBuf(byte[] nameBuf, int namePos, int nameLen,
+ FileMode mode) {
+ if (buf == null || buf.length < ptr + entrySize(mode, nameLen))
+ return false;
+
+ mode.copyTo(buf, ptr);
+ ptr += mode.copyToLength();
+ buf[ptr++] = ' ';
+
+ System.arraycopy(nameBuf, namePos, buf, ptr, nameLen);
+ ptr += nameLen;
+ buf[ptr++] = 0;
+ return true;
+ }
+
+ private void fmtOverflowBuffer(byte[] nameBuf, int namePos, int nameLen,
+ FileMode mode) throws IOException {
+ if (buf != null) {
+ overflowBuffer = new TemporaryBuffer.Heap(Integer.MAX_VALUE);
+ overflowBuffer.write(buf, 0, ptr);
+ buf = null;
+ }
+
+ mode.copyTo(overflowBuffer);
+ overflowBuffer.write((byte) ' ');
+ overflowBuffer.write(nameBuf, namePos, nameLen);
+ overflowBuffer.write((byte) 0);
+ }
+
+ /**
+ * Compute the current tree's ObjectId.
+ *
+ * @return computed ObjectId of the tree
+ */
+ public ObjectId getTreeId() {
+ final ObjectInserter.Formatter fmt = new ObjectInserter.Formatter();
+ if (buf != null)
+ return fmt.idFor(OBJ_TREE, buf, 0, ptr);
+
+ try {
+ final long len = overflowBuffer.length();
+ return fmt.idFor(OBJ_TREE, len, overflowBuffer.openInputStream());
+ } catch (IOException err) {
+ // This should never happen, its read failure on a byte array.
+ throw new RuntimeException(err);
+ }
+ }
+
+ /**
+ * Insert this tree and obtain its ObjectId.
+ *
+ * @param ins
+ * the inserter to store the tree.
+ * @return computed ObjectId of the tree
+ * @throws IOException
+ * the tree could not be stored.
+ */
+ public ObjectId insert(ObjectInserter ins) throws IOException {
+ if (buf != null)
+ return ins.insert(OBJ_TREE, buf, 0, ptr);
+
+ final long len = overflowBuffer.length();
+ return ins.insert(OBJ_TREE, len, overflowBuffer.openInputStream());
+ }
+
+ /**
+ * Copy this formatter's buffer into a byte array.
+ *
+ * This method is not efficient, as it needs to create a copy of the
+ * internal buffer in order to supply an array of the correct size to the
+ * caller. If the buffer is just to pass to an ObjectInserter, consider
+ * using {@link #insert(ObjectInserter)} instead.
+ *
+ * @return a copy of this formatter's buffer.
+ */
+ public byte[] toByteArray() {
+ if (buf != null) {
+ byte[] r = new byte[ptr];
+ System.arraycopy(buf, 0, r, 0, ptr);
+ return r;
+ }
+
+ try {
+ return overflowBuffer.toByteArray();
+ } catch (IOException err) {
+ // This should never happen, its read failure on a byte array.
+ throw new RuntimeException(err);
+ }
+ }
+}