Class DirCacheEntry


  • public class DirCacheEntry
    extends java.lang.Object
    A single file (or stage of a file) in a DirCache.

    An entry represents exactly one stage of a file. If a file path is unmerged then multiple DirCacheEntry instances may appear for the same path name.

    • Constructor Detail

      • DirCacheEntry

        DirCacheEntry​(byte[] sharedInfo,
                      MutableInteger infoAt,
                      java.io.InputStream in,
                      java.security.MessageDigest md,
                      java.time.Instant smudge,
                      DirCache.DirCacheVersion version,
                      DirCacheEntry previous)
               throws java.io.IOException
        Throws:
        java.io.IOException
      • DirCacheEntry

        public DirCacheEntry​(java.lang.String newPath)
        Create an empty entry at stage 0.
        Parameters:
        newPath - name of the cache entry.
        Throws:
        java.lang.IllegalArgumentException - If the path starts or ends with "/", or contains "//" either "\0". These sequences are not permitted in a git tree object or DirCache file.
      • DirCacheEntry

        public DirCacheEntry​(java.lang.String newPath,
                             int stage)
        Create an empty entry at the specified stage.
        Parameters:
        newPath - name of the cache entry.
        stage - the stage index of the new entry.
        Throws:
        java.lang.IllegalArgumentException - If the path starts or ends with "/", or contains "//" either "\0". These sequences are not permitted in a git tree object or DirCache file. Or if stage is outside of the range 0..3, inclusive.
      • DirCacheEntry

        public DirCacheEntry​(byte[] newPath)
        Create an empty entry at stage 0.
        Parameters:
        newPath - name of the cache entry, in the standard encoding.
        Throws:
        java.lang.IllegalArgumentException - If the path starts or ends with "/", or contains "//" either "\0". These sequences are not permitted in a git tree object or DirCache file.
      • DirCacheEntry

        public DirCacheEntry​(byte[] path,
                             int stage)
        Create an empty entry at the specified stage.
        Parameters:
        path - name of the cache entry, in the standard encoding.
        stage - the stage index of the new entry.
        Throws:
        java.lang.IllegalArgumentException - If the path starts or ends with "/", or contains "//" either "\0". These sequences are not permitted in a git tree object or DirCache file. Or if stage is outside of the range 0..3, inclusive.
      • DirCacheEntry

        public DirCacheEntry​(DirCacheEntry src)
        Duplicate DirCacheEntry with same path and copied info.

        The same path buffer is reused (avoiding copying), however a new info buffer is created and its contents are copied.

        Parameters:
        src - entry to clone.
        Since:
        4.2
    • Method Detail

      • readNulTerminatedString

        private int readNulTerminatedString​(java.io.InputStream in,
                                            java.io.OutputStream out)
                                     throws java.io.IOException
        Throws:
        java.io.IOException
      • mightBeRacilyClean

        @Deprecated
        public final boolean mightBeRacilyClean​(int smudge_s,
                                                int smudge_ns)
        Deprecated.
        Is it possible for this entry to be accidentally assumed clean?

        The "racy git" problem happens when a work file can be updated faster than the filesystem records file modification timestamps. It is possible for an application to edit a work file, update the index, then edit it again before the filesystem will give the work file a new modification timestamp. This method tests to see if file was written out at the same time as the index.

        Parameters:
        smudge_s - seconds component of the index's last modified time.
        smudge_ns - nanoseconds component of the index's last modified time.
        Returns:
        true if extra careful checks should be used.
      • mightBeRacilyClean

        public final boolean mightBeRacilyClean​(java.time.Instant smudge)
        Is it possible for this entry to be accidentally assumed clean?

        The "racy git" problem happens when a work file can be updated faster than the filesystem records file modification timestamps. It is possible for an application to edit a work file, update the index, then edit it again before the filesystem will give the work file a new modification timestamp. This method tests to see if file was written out at the same time as the index.

        Parameters:
        smudge - index's last modified time.
        Returns:
        true if extra careful checks should be used.
        Since:
        5.1.9
      • smudgeRacilyClean

        public final void smudgeRacilyClean()
        Force this entry to no longer match its working tree file.

        This avoids the "racy git" problem by making this index entry no longer match the file in the working directory. Later git will be forced to compare the file content to ensure the file matches the working tree.

      • isSmudged

        public final boolean isSmudged()
        Check whether this entry has been smudged or not

        If a blob has length 0 we know its id, see Constants.EMPTY_BLOB_ID. If an entry has length 0 and an ID different from the one for empty blob we know this entry was smudged.

        Returns:
        true if the entry is smudged, false otherwise
      • idBuffer

        final byte[] idBuffer()
      • idOffset

        final int idOffset()
      • isAssumeValid

        public boolean isAssumeValid()
        Is this entry always thought to be unmodified?

        Most entries in the index do not have this flag set. Users may however set them on if the file system stat() costs are too high on this working directory, such as on NFS or SMB volumes.

        Returns:
        true if we must assume the entry is unmodified.
      • setAssumeValid

        public void setAssumeValid​(boolean assume)
        Set the assume valid flag for this entry,
        Parameters:
        assume - true to ignore apparent modifications; false to look at last modified to detect file modifications.
      • isUpdateNeeded

        public boolean isUpdateNeeded()
        Whether this entry should be checked for changes
        Returns:
        true if this entry should be checked for changes
      • setUpdateNeeded

        public void setUpdateNeeded​(boolean updateNeeded)
        Set whether this entry must be checked for changes
        Parameters:
        updateNeeded - whether this entry must be checked for changes
      • getStage

        public int getStage()
        Get the stage of this entry.

        Entries have one of 4 possible stages: 0-3.

        Returns:
        the stage of this entry.
      • isSkipWorkTree

        public boolean isSkipWorkTree()
        Returns whether this entry should be skipped from the working tree.
        Returns:
        true if this entry should be skipepd.
      • isIntentToAdd

        public boolean isIntentToAdd()
        Returns whether this entry is intent to be added to the Index.
        Returns:
        true if this entry is intent to add.
      • isMerged

        public boolean isMerged()
        Returns whether this entry is in the fully-merged stage (0).
        Returns:
        true if this entry is merged
        Since:
        2.2
      • getRawMode

        public int getRawMode()
        Obtain the raw FileMode bits for this entry.
        Returns:
        mode bits for the entry.
        See Also:
        FileMode.fromBits(int)
      • getFileMode

        public FileMode getFileMode()
        Obtain the FileMode for this entry.
        Returns:
        the file mode singleton for this entry.
      • setFileMode

        public void setFileMode​(FileMode mode)
        Set the file mode for this entry.
        Parameters:
        mode - the new mode constant.
        Throws:
        java.lang.IllegalArgumentException - If mode is FileMode.MISSING, FileMode.TREE, or any other type code not permitted in a tree object.
      • setFileMode

        void setFileMode​(int mode)
      • getCreationTime

        public long getCreationTime()
        Get the cached creation time of this file, in milliseconds.
        Returns:
        cached creation time of this file, in milliseconds since the Java epoch (midnight Jan 1, 1970 UTC).
      • setCreationTime

        public void setCreationTime​(long when)
        Set the cached creation time of this file, using milliseconds.
        Parameters:
        when - new cached creation time of the file, in milliseconds.
      • getLastModified

        @Deprecated
        public long getLastModified()
        Deprecated.
        Get the cached last modification date of this file, in milliseconds.

        One of the indicators that the file has been modified by an application changing the working tree is if the last modification time for the file differs from the time stored in this entry.

        Returns:
        last modification time of this file, in milliseconds since the Java epoch (midnight Jan 1, 1970 UTC).
      • getLastModifiedInstant

        public java.time.Instant getLastModifiedInstant()
        Get the cached last modification date of this file.

        One of the indicators that the file has been modified by an application changing the working tree is if the last modification time for the file differs from the time stored in this entry.

        Returns:
        last modification time of this file.
        Since:
        5.1.9
      • setLastModified

        @Deprecated
        public void setLastModified​(long when)
        Deprecated.
        Set the cached last modification date of this file, using milliseconds.
        Parameters:
        when - new cached modification date of the file, in milliseconds.
      • setLastModified

        public void setLastModified​(java.time.Instant when)
        Set the cached last modification date of this file.
        Parameters:
        when - new cached modification date of the file.
        Since:
        5.1.9
      • getLength

        public int getLength()
        Get the cached size (mod 4 GB) (in bytes) of this file.

        One of the indicators that the file has been modified by an application changing the working tree is if the size of the file (in bytes) differs from the size stored in this entry.

        Note that this is the length of the file in the working directory, which may differ from the size of the decompressed blob if work tree filters are being used, such as LF<->CRLF conversion.

        Note also that for very large files, this is the size of the on-disk file truncated to 32 bits, i.e. modulo 4294967296. If that value is larger than 2GB, it will appear negative.

        Returns:
        cached size of the working directory file, in bytes.
      • setLength

        public void setLength​(int sz)
        Set the cached size (in bytes) of this file.
        Parameters:
        sz - new cached size of the file, as bytes. If the file is larger than 2G, cast it to (int) before calling this method.
      • setLength

        public void setLength​(long sz)
        Set the cached size (in bytes) of this file.
        Parameters:
        sz - new cached size of the file, as bytes.
      • getObjectId

        public ObjectId getObjectId()
        Obtain the ObjectId for the entry.

        Using this method to compare ObjectId values between entries is inefficient as it causes memory allocation.

        Returns:
        object identifier for the entry.
      • setObjectId

        public void setObjectId​(AnyObjectId id)
        Set the ObjectId for the entry.
        Parameters:
        id - new object identifier for the entry. May be ObjectId.zeroId() to remove the current identifier.
      • setObjectIdFromRaw

        public void setObjectIdFromRaw​(byte[] bs,
                                       int p)
        Set the ObjectId for the entry from the raw binary representation.
        Parameters:
        bs - the raw byte buffer to read from. At least 20 bytes after p must be available within this byte array.
        p - position to read the first byte of data from.
      • getPathString

        public java.lang.String getPathString()
        Get the entry's complete path.

        This method is not very efficient and is primarily meant for debugging and final output generation. Applications should try to avoid calling it, and if invoked do so only once per interesting entry, where the name is absolutely required for correct function.

        Returns:
        complete path of the entry, from the root of the repository. If the entry is in a subtree there will be at least one '/' in the returned string.
      • getRawPath

        public byte[] getRawPath()
        Get a copy of the entry's raw path bytes.
        Returns:
        raw path bytes.
        Since:
        3.4
      • toString

        public java.lang.String toString()

        Use for debugging only !

        Overrides:
        toString in class java.lang.Object
      • copyMetaData

        public void copyMetaData​(DirCacheEntry src)
        Copy the ObjectId and other meta fields from an existing entry.

        This method copies everything except the path from one entry to another, supporting renaming.

        Parameters:
        src - the entry to copy ObjectId and meta fields from.
      • copyMetaData

        void copyMetaData​(DirCacheEntry src,
                          boolean keepStage)
        Copy the ObjectId and other meta fields from an existing entry.

        This method copies everything except the path and possibly stage from one entry to another, supporting renaming.

        Parameters:
        src - the entry to copy ObjectId and meta fields from.
        keepStage - if true, the stage attribute will not be copied
      • isExtended

        boolean isExtended()
        Returns:
        true if the entry contains extended flags.
      • decodeTS

        private long decodeTS​(int pIdx)
      • decodeTSInstant

        private java.time.Instant decodeTSInstant​(int pIdx)
      • encodeTS

        private void encodeTS​(int pIdx,
                              long when)
      • encodeTS

        private void encodeTS​(int pIdx,
                              java.time.Instant when)
      • getExtendedFlags

        private int getExtendedFlags()
      • checkPath

        private static void checkPath​(byte[] path)
      • toString

        static java.lang.String toString​(byte[] path)
      • getMaximumInfoLength

        static int getMaximumInfoLength​(boolean extended)