Class 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 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 use entrySize(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 is FileMode.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 is FileMode.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 is FileMode.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 of id.
        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 of id.
        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 within nameBuf of the name data.
        nameLen - number of bytes from nameBuf to use as the name.
        mode - mode describing the treatment of id.
        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 within nameBuf of the name data.
        nameLen - number of bytes from nameBuf to use as the name.
        mode - mode describing the treatment of id.
        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 within nameBuf of the name data.
        nameLen - number of bytes from nameBuf to use as the name.
        mode - mode describing the treatment of id.
        idBuf - buffer holding the raw ObjectId of the entry.
        idPos - first position within idBuf 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.
      • 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 using ObjectInserter.insert(TreeFormatter) instead.
        Returns:
        a copy of this formatter's buffer.