Class DirCacheIterator

  • Direct Known Subclasses:
    DirCacheBuildIterator

    public class DirCacheIterator
    extends AbstractTreeIterator
    Iterate a DirCache as part of a TreeWalk.

    This is an iterator to adapt a loaded DirCache instance (such as read from an existing .git/index file) to the tree structure used by a TreeWalk, making it possible for applications to walk over any combination of tree objects already in the object database, index files, or working directories.

    See Also:
    TreeWalk
    • Field Detail

      • DOT_GIT_ATTRIBUTES_BYTES

        private static final byte[] DOT_GIT_ATTRIBUTES_BYTES
        Byte array holding ".gitattributes" string
      • cache

        protected final DirCache cache
        The cache this iterator was created to walk.
      • tree

        private final DirCacheTree tree
        The tree this iterator is walking.
      • treeStart

        private final int treeStart
        First position in this tree.
      • treeEnd

        private final int treeEnd
        Last position in this tree.
      • subtreeId

        private final byte[] subtreeId
        Special buffer to hold the ObjectId of currentSubtree.
      • ptr

        protected int ptr
        Index of entry within cache.
      • nextSubtreePos

        private int nextSubtreePos
        Next subtree to consider within tree.
    • Constructor Detail

      • DirCacheIterator

        public DirCacheIterator​(DirCache dc)
        Create a new iterator for an already loaded DirCache instance.

        The iterator implementation may copy part of the cache's data during construction, so the cache must be read in prior to creating the iterator.

        Parameters:
        dc - the cache to walk. It must be already loaded into memory.
    • Method Detail

      • createSubtreeIterator

        public AbstractTreeIterator createSubtreeIterator​(ObjectReader reader)
                                                   throws IncorrectObjectTypeException,
                                                          java.io.IOException
        Create a new iterator for the current entry's subtree.

        The parent reference of the iterator must be this, otherwise the caller would not be able to exit out of the subtree iterator correctly and return to continue walking this.

        Specified by:
        createSubtreeIterator in class AbstractTreeIterator
        Parameters:
        reader - reader to load the tree data from.
        Returns:
        a new parser that walks over the current subtree.
        Throws:
        IncorrectObjectTypeException - the current entry is not actually a tree and cannot be parsed as though it were a tree.
        java.io.IOException - a loose object or pack file could not be read.
      • hasId

        public boolean hasId()
        Whether the entry has a valid ObjectId.
        Specified by:
        hasId in class AbstractTreeIterator
        Returns:
        true if the entry has a valid ObjectId.
      • idBuffer

        public byte[] idBuffer()
        Get the byte array buffer object IDs must be copied out of.

        The id buffer contains the bytes necessary to construct an ObjectId for the current entry of this iterator. The buffer can be the same buffer for all entries, or it can be a unique buffer per-entry. Implementations are encouraged to expose their private buffer whenever possible to reduce garbage generation and copying costs.

        Specified by:
        idBuffer in class AbstractTreeIterator
        Returns:
        byte array the implementation stores object IDs within.
        See Also:
        AbstractTreeIterator.getEntryObjectId()
      • reset

        public void reset()
        Position this iterator on the first entry. The default implementation of this method uses back(1) until first() is true. This is most likely not the most efficient method of repositioning the iterator to its first entry, so subclasses are strongly encouraged to override the method.
        Overrides:
        reset in class AbstractTreeIterator
      • first

        public boolean first()
        Is this tree iterator positioned on its first entry?

        An iterator is positioned on the first entry if back(1) would be an invalid request as there is no entry before the current one.

        An empty iterator (one with no entries) will be first() && eof().

        Specified by:
        first in class AbstractTreeIterator
        Returns:
        true if the iterator is positioned on the first entry.
      • eof

        public boolean eof()
        Is this tree iterator at its EOF point (no more entries)?

        An iterator is at EOF if there is no current entry.

        Specified by:
        eof in class AbstractTreeIterator
        Returns:
        true if we have walked all entries and have none left.
      • parseEntry

        private void parseEntry()
      • parseEntry

        private void parseEntry​(boolean forward)
      • getDirCacheEntry

        public DirCacheEntry getDirCacheEntry()
        Get the DirCacheEntry for the current file.
        Returns:
        the current cache entry, if this iterator is positioned on a non-tree.
      • getEntryAttributesNode

        public AttributesNode getEntryAttributesNode​(ObjectReader reader)
                                              throws java.io.IOException
        Retrieves the AttributesNode for the current entry.
        Parameters:
        reader - ObjectReader used to parse the .gitattributes entry.
        Returns:
        AttributesNode for the current entry.
        Throws:
        java.io.IOException
        Since:
        3.7