Class FileHeader

  • Direct Known Subclasses:
    CombinedFileHeader

    public class FileHeader
    extends DiffEntry
    Patch header describing an action for a single file path.
    • Field Detail

      • OLD_MODE

        private static final byte[] OLD_MODE
      • NEW_MODE

        private static final byte[] NEW_MODE
      • DELETED_FILE_MODE

        static final byte[] DELETED_FILE_MODE
      • NEW_FILE_MODE

        static final byte[] NEW_FILE_MODE
      • COPY_FROM

        private static final byte[] COPY_FROM
      • COPY_TO

        private static final byte[] COPY_TO
      • RENAME_OLD

        private static final byte[] RENAME_OLD
      • RENAME_NEW

        private static final byte[] RENAME_NEW
      • RENAME_FROM

        private static final byte[] RENAME_FROM
      • RENAME_TO

        private static final byte[] RENAME_TO
      • SIMILARITY_INDEX

        private static final byte[] SIMILARITY_INDEX
      • DISSIMILARITY_INDEX

        private static final byte[] DISSIMILARITY_INDEX
      • INDEX

        static final byte[] INDEX
      • OLD_NAME

        static final byte[] OLD_NAME
      • NEW_NAME

        static final byte[] NEW_NAME
      • buf

        final byte[] buf
        Buffer holding the patch data for this file.
      • startOffset

        final int startOffset
        Offset within buf to the "diff ..." line.
      • endOffset

        int endOffset
        Position 1 past the end of this file within buf.
      • hunks

        private java.util.List<HunkHeader> hunks
        The hunks of this file
    • Constructor Detail

      • FileHeader

        public FileHeader​(byte[] headerLines,
                          EditList edits,
                          FileHeader.PatchType type)
        Constructs a new FileHeader
        Parameters:
        headerLines - buffer holding the diff header for this file
        edits - the edits for this file
        type - the type of patch used to modify this file
      • FileHeader

        FileHeader​(byte[] b,
                   int offset)
    • Method Detail

      • getParentCount

        int getParentCount()
      • getBuffer

        public byte[] getBuffer()
        Get the byte array holding this file's patch script.
        Returns:
        the byte array holding this file's patch script.
      • getStartOffset

        public int getStartOffset()
        Get offset of the start of this file's script in getBuffer().
        Returns:
        offset of the start of this file's script in getBuffer().
      • getEndOffset

        public int getEndOffset()
        Get offset one past the end of the file script.
        Returns:
        offset one past the end of the file script.
      • getScriptText

        public java.lang.String getScriptText()
        Convert the patch script for this file into a string.

        The default character encoding (StandardCharsets.UTF_8) is assumed for both the old and new files.

        Returns:
        the patch script, as a Unicode string.
      • getScriptText

        public java.lang.String getScriptText​(java.nio.charset.Charset oldCharset,
                                              java.nio.charset.Charset newCharset)
        Convert the patch script for this file into a string.
        Parameters:
        oldCharset - hint character set to decode the old lines with.
        newCharset - hint character set to decode the new lines with.
        Returns:
        the patch script, as a Unicode string.
      • getScriptText

        java.lang.String getScriptText​(java.nio.charset.Charset[] charsetGuess)
      • trySimpleConversion

        private static boolean trySimpleConversion​(java.nio.charset.Charset[] charsetGuess)
      • extractFileLines

        private java.lang.String[] extractFileLines​(java.nio.charset.Charset[] csGuess)
      • getPatchType

        public FileHeader.PatchType getPatchType()
        Get style of patch used to modify this file.
        Returns:
        style of patch used to modify this file.
      • hasMetaDataChanges

        public boolean hasMetaDataChanges()
        Whether this patch modifies metadata about a file
        Returns:
        true if this patch modifies metadata about a file .
      • getHunks

        public java.util.List<? extends HunkHeader> getHunks()
        Get hunks altering this file; in order of appearance in patch
        Returns:
        hunks altering this file; in order of appearance in patch.
      • newHunkHeader

        HunkHeader newHunkHeader​(int offset)
      • toEditList

        public EditList toEditList()
        Convert to a list describing the content edits performed on this file.
        Returns:
        a list describing the content edits performed on this file.
      • parseGitFileName

        int parseGitFileName​(int ptr,
                             int end)
        Parse a "diff --git" or "diff --cc" line.
        Parameters:
        ptr - first character after the "diff --git " or "diff --cc " part.
        end - one past the last position to parse.
        Returns:
        first character after the LF at the end of the line; -1 on error.
      • parseGitHeaders

        int parseGitHeaders​(int ptr,
                            int end)
      • parseOldName

        void parseOldName​(int ptr,
                          int eol)
      • parseNewName

        void parseNewName​(int ptr,
                          int eol)
      • parseNewFileMode

        void parseNewFileMode​(int ptr,
                              int eol)
      • parseTraditionalHeaders

        int parseTraditionalHeaders​(int ptr,
                                    int end)
      • parseName

        private java.lang.String parseName​(java.lang.String expect,
                                           int ptr,
                                           int end)
      • p1

        private static java.lang.String p1​(java.lang.String r)
      • parseFileMode

        FileMode parseFileMode​(int ptr,
                               int end)
      • parseIndexLine

        void parseIndexLine​(int ptr,
                            int end)
      • eq

        private boolean eq​(int aPtr,
                           int aEnd,
                           int bPtr,
                           int bEnd)
      • isHunkHdr

        static int isHunkHdr​(byte[] buf,
                             int start,
                             int end)
        Determine if this is a patch hunk header.
        Parameters:
        buf - the buffer to scan
        start - first position in the buffer to evaluate
        end - last position to consider; usually the end of the buffer ( buf.length) or the first position on the next line. This is only used to avoid very long runs of '@' from killing the scan loop.
        Returns:
        the number of "ancestor revisions" in the hunk header. A traditional two-way diff ("@@ -...") returns 1; a combined diff for a 3 way-merge returns 3. If this is not a hunk header, 0 is returned instead.