Package org.eclipse.jgit.lib
Class TreeFormatter
- java.lang.Object
-
- org.eclipse.jgit.lib.TreeFormatter
-
public class TreeFormatter extends Object
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.
-
-
Constructor Summary
Constructors Constructor Description TreeFormatter()
Create an empty formatter with a default buffer size.TreeFormatter(int size)
Create an empty formatter with the specified buffer size.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
append(byte[] nameBuf, int namePos, int nameLen, FileMode mode, byte[] idBuf, int idPos)
Append any entry to the tree.void
append(byte[] nameBuf, int namePos, int nameLen, FileMode mode, AnyObjectId id)
Append any entry to the tree.void
append(byte[] nameBuf, int namePos, int nameLen, FileMode mode, AnyObjectId id, boolean allowEmptyName)
Append any entry to the tree.void
append(byte[] name, FileMode mode, AnyObjectId id)
Append any entry to the tree.void
append(String name, FileMode mode, AnyObjectId id)
Append any entry to the tree.void
append(String name, RevBlob blob)
Add a regular file, mode isFileMode.REGULAR_FILE
.void
append(String name, RevCommit commit)
Add a link to a submodule commit, mode isFileMode.GITLINK
.void
append(String name, RevTree tree)
Add a subtree, mode isFileMode.TREE
.ObjectId
computeId(ObjectInserter ins)
Compute the ObjectId for this treestatic int
entrySize(FileMode mode, int nameLen)
Compute the size of a tree entry record.ObjectId
insertTo(ObjectInserter ins)
Insert this tree and obtain its ObjectId.byte[]
toByteArray()
Copy this formatter's buffer into a byte array.String
toString()
-
-
-
Constructor Detail
-
TreeFormatter
public TreeFormatter()
Create an empty formatter with a default buffer size.
-
TreeFormatter
public TreeFormatter(int size)
Create an empty formatter with the specified buffer size.- Parameters:
size
- estimated size of the tree, in bytes. Callers can useentrySize(FileMode, int)
to estimate the size of each entry in advance of allocating the formatter.
-
-
Method Detail
-
entrySize
public static int entrySize(FileMode mode, int nameLen)
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.- Parameters:
mode
- the mode the entry will have.nameLen
- the length of the name, in bytes.- Returns:
- the length of the record.
-
append
public void append(String name, RevCommit commit)
Add a link to a submodule commit, mode isFileMode.GITLINK
.- Parameters:
name
- name of the entry.commit
- the ObjectId to store in this entry.
-
append
public void append(String name, RevTree tree)
Add a subtree, mode isFileMode.TREE
.- Parameters:
name
- name of the entry.tree
- the ObjectId to store in this entry.
-
append
public void append(String name, RevBlob blob)
Add a regular file, mode isFileMode.REGULAR_FILE
.- Parameters:
name
- name of the entry.blob
- the ObjectId to store in this entry.
-
append
public void append(String name, FileMode mode, AnyObjectId id)
Append any entry to the tree.- Parameters:
name
- name of the entry.mode
- mode describing the treatment ofid
.id
- the ObjectId to store in this entry.
-
append
public void append(byte[] name, FileMode mode, AnyObjectId id)
Append any entry to the tree.- Parameters:
name
- name of the entry. The name should be UTF-8 encoded, but file name encoding is not a well defined concept in Git.mode
- mode describing the treatment ofid
.id
- the ObjectId to store in this entry.
-
append
public void append(byte[] nameBuf, int namePos, int nameLen, FileMode mode, AnyObjectId id)
Append any entry to the tree.- Parameters:
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.namePos
- first position withinnameBuf
of the name data.nameLen
- number of bytes fromnameBuf
to use as the name.mode
- mode describing the treatment ofid
.id
- the ObjectId to store in this entry.
-
append
public void append(byte[] nameBuf, int namePos, int nameLen, FileMode mode, AnyObjectId id, boolean allowEmptyName)
Append any entry to the tree.- Parameters:
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.namePos
- first position withinnameBuf
of the name data.nameLen
- number of bytes fromnameBuf
to use as the name.mode
- mode describing the treatment ofid
.id
- the ObjectId to store in this entry.allowEmptyName
- allow an empty filename (creating a corrupt tree)- Since:
- 4.6
-
append
public void append(byte[] nameBuf, int namePos, int nameLen, FileMode mode, byte[] idBuf, int idPos)
Append any entry to the tree.- Parameters:
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.namePos
- first position withinnameBuf
of the name data.nameLen
- number of bytes fromnameBuf
to use as the name.mode
- mode describing the treatment ofid
.idBuf
- buffer holding the raw ObjectId of the entry.idPos
- first position withinidBuf
to copy the id from.
-
insertTo
public ObjectId insertTo(ObjectInserter ins) throws IOException
Insert this tree and obtain its ObjectId.- Parameters:
ins
- the inserter to store the tree.- Returns:
- computed ObjectId of the tree
- Throws:
IOException
- the tree could not be stored.
-
computeId
public ObjectId computeId(ObjectInserter ins)
Compute the ObjectId for this tree- Parameters:
ins
- aObjectInserter
object.- Returns:
- ObjectId for this tree
-
toByteArray
public byte[] toByteArray()
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 usingObjectInserter.insert(TreeFormatter)
instead.- Returns:
- a copy of this formatter's buffer.
-
-