isHunkHdr() public static method

Determine if this is a patch hunk header.
public static isHunkHdr ( byte buf, int start, int end ) : int
buf byte the buffer to scan
start int first position in the buffer to evaluate
end int /// 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. ///
return int
Beispiel #1
0
        private int ParseHunks(FileHeader fh, int c, int end)
        {
            byte[] buf = fh.Buffer;
            while (c < end)
            {
                // If we see a file header at this point, we have all of the
                // hunks for our current file. We should stop and report back
                // with this position so it can be parsed again later.
                //
                if (RawParseUtils.match(buf, c, DiffGit) >= 0)
                {
                    break;
                }
                if (RawParseUtils.match(buf, c, DiffCc) >= 0)
                {
                    break;
                }
                if (RawParseUtils.match(buf, c, DiffCombined) >= 0)
                {
                    break;
                }
                if (RawParseUtils.match(buf, c, FileHeader.OLD_NAME) >= 0)
                {
                    break;
                }
                if (RawParseUtils.match(buf, c, FileHeader.NEW_NAME) >= 0)
                {
                    break;
                }

                if (FileHeader.isHunkHdr(buf, c, end) == fh.ParentCount)
                {
                    HunkHeader h = fh.newHunkHeader(c);
                    h.parseHeader();
                    c           = h.parseBody(this, end);
                    h.EndOffset = c;
                    fh.addHunk(h);
                    if (c < end)
                    {
                        switch (buf[c])
                        {
                        case (byte)'@':
                        case (byte)'d':
                        case (byte)'\n':
                            break;

                        default:
                            if (RawParseUtils.match(buf, c, SigFooter) < 0)
                            {
                                warn(buf, c, "Unexpected hunk trailer");
                            }
                            break;
                        }
                    }
                    continue;
                }

                int eol = RawParseUtils.nextLF(buf, c);
                if (fh.Hunks.isEmpty() && RawParseUtils.match(buf, c, GitBinary) >= 0)
                {
                    fh.PatchType = FileHeader.PatchTypeEnum.GIT_BINARY;
                    return(ParseGitBinary(fh, eol, end));
                }

                if (fh.Hunks.isEmpty() && BinTrailer.Length < eol - c &&
                    RawParseUtils.match(buf, eol - BinTrailer.Length, BinTrailer) >= 0 &&
                    MatchAny(buf, c, BinHeaders))
                {
                    // The patch is a binary file diff, with no deltas.
                    //
                    fh.PatchType = FileHeader.PatchTypeEnum.BINARY;
                    return(eol);
                }

                // Skip this line and move to the next. Its probably garbage
                // After the last hunk of a file.
                //
                c = eol;
            }

            if (fh.Hunks.isEmpty() &&
                fh.getPatchType() == FileHeader.PatchTypeEnum.UNIFIED &&
                !fh.hasMetaDataChanges())
            {
                // Hmm, an empty patch? If there is no metadata here we
                // really have a binary patch that we didn't notice above.
                //
                fh.PatchType = FileHeader.PatchTypeEnum.BINARY;
            }

            return(c);
        }
Beispiel #2
0
        private int ParseFile(byte[] buf, int c, int end)
        {
            while (c < end)
            {
                if (FileHeader.isHunkHdr(buf, c, end) >= 1)
                {
                    // If we find a disconnected hunk header we might
                    // have missed a file header previously. The hunk
                    // isn't valid without knowing where it comes from.
                    //
                    error(buf, c, "Hunk disconnected from file");
                    c = RawParseUtils.nextLF(buf, c);
                    continue;
                }

                // Valid git style patch?
                //
                if (RawParseUtils.match(buf, c, DiffGit) >= 0)
                {
                    return(ParseDiffGit(buf, c, end));
                }
                if (RawParseUtils.match(buf, c, DiffCc) >= 0)
                {
                    return(ParseDiffCombined(DiffCc, buf, c, end));
                }
                if (RawParseUtils.match(buf, c, DiffCombined) >= 0)
                {
                    return(ParseDiffCombined(DiffCombined, buf, c, end));
                }

                // Junk between files? Leading junk? Traditional
                // (non-git generated) patch?
                //
                int n = RawParseUtils.nextLF(buf, c);
                if (n >= end)
                {
                    // Patches cannot be only one line long. This must be
                    // trailing junk that we should ignore.
                    //
                    return(end);
                }

                if (n - c < 6)
                {
                    // A valid header must be at least 6 bytes on the
                    // first line, e.g. "--- a/b\n".
                    //
                    c = n;
                    continue;
                }

                if (RawParseUtils.match(buf, c, FileHeader.OLD_NAME) >= 0 &&
                    RawParseUtils.match(buf, n, FileHeader.NEW_NAME) >= 0)
                {
                    // Probably a traditional patch. Ensure we have at least
                    // a "@@ -0,0" smelling line next. We only check the "@@ -".
                    //
                    int f = RawParseUtils.nextLF(buf, n);
                    if (f >= end)
                    {
                        return(end);
                    }
                    if (FileHeader.isHunkHdr(buf, f, end) == 1)
                    {
                        return(ParseTraditionalPatch(buf, c, end));
                    }
                }

                c = n;
            }
            return(c);
        }