From 9cc63e8e26c8644118b644b0190b1211eb1fd20b Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Thu, 11 Dec 2008 18:46:20 -0800 Subject: [PATCH] Abstract the hunk header testing into a method This way we can test for "@@@ -" and "@@@@@@@ -" for combined diffs in octopus merges. We use the same scan test for the basic two-way case, as the format is identical but has less marker characters. Signed-off-by: Shawn O. Pearce Signed-off-by: Robin Rosenberg --- .../src/org/spearce/jgit/patch/FileHeader.java | 36 +++++++++++++++++++--- .../src/org/spearce/jgit/patch/Patch.java | 8 ++--- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/org.spearce.jgit/src/org/spearce/jgit/patch/FileHeader.java b/org.spearce.jgit/src/org/spearce/jgit/patch/FileHeader.java index cbb4ea78..67310644 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/patch/FileHeader.java +++ b/org.spearce.jgit/src/org/spearce/jgit/patch/FileHeader.java @@ -87,8 +87,6 @@ public class FileHeader { static final byte[] NEW_NAME = encodeASCII("+++ "); - static final byte[] HUNK_HDR = encodeASCII("@@ -"); - /** General type of change a single file-level patch describes. */ public static enum ChangeType { /** Add a new file to the project */ @@ -358,7 +356,7 @@ public class FileHeader { int parseGitHeaders(int ptr, final int end) { while (ptr < end) { final int eol = nextLF(buf, ptr); - if (match(buf, ptr, HUNK_HDR) >= 0) { + if (isHunkHdr(buf, ptr, eol) >= 1) { // First hunk header; break out and parse them later. break; @@ -432,7 +430,7 @@ public class FileHeader { int parseTraditionalHeaders(int ptr, final int end) { while (ptr < end) { final int eol = nextLF(buf, ptr); - if (match(buf, ptr, HUNK_HDR) >= 0) { + if (isHunkHdr(buf, ptr, eol) >= 1) { // First hunk header; break out and parse them later. break; @@ -519,4 +517,34 @@ public class FileHeader { } return true; } + + /** + * Determine if this is a patch hunk header. + * + * @param buf + * the buffer to scan + * @param start + * first position in the buffer to evaluate + * @param 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. + * @return 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. + */ + static int isHunkHdr(final byte[] buf, final int start, final int end) { + int ptr = start; + while (ptr < end && buf[ptr] == '@') + ptr++; + if (ptr - start < 2) + return 0; + if (ptr == end || buf[ptr++] != ' ') + return 0; + if (ptr == end || buf[ptr++] != '-') + return 0; + return (ptr - 3) - start; + } } diff --git a/org.spearce.jgit/src/org/spearce/jgit/patch/Patch.java b/org.spearce.jgit/src/org/spearce/jgit/patch/Patch.java index a0b030bd..f9949354 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/patch/Patch.java +++ b/org.spearce.jgit/src/org/spearce/jgit/patch/Patch.java @@ -38,7 +38,7 @@ package org.spearce.jgit.patch; import static org.spearce.jgit.lib.Constants.encodeASCII; -import static org.spearce.jgit.patch.FileHeader.HUNK_HDR; +import static org.spearce.jgit.patch.FileHeader.isHunkHdr; import static org.spearce.jgit.patch.FileHeader.NEW_NAME; import static org.spearce.jgit.patch.FileHeader.OLD_NAME; import static org.spearce.jgit.util.RawParseUtils.match; @@ -162,7 +162,7 @@ public class Patch { private int parseFile(final byte[] buf, int c, final int end) { while (c < end) { - if (match(buf, c, HUNK_HDR) >= 0) { + if (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. @@ -205,7 +205,7 @@ public class Patch { final int f = nextLF(buf, n); if (f >= end) return end; - if (match(buf, f, HUNK_HDR) >= 0) + if (isHunkHdr(buf, f, end) == 1) return parseTraditionalPatch(buf, c, end); } @@ -274,7 +274,7 @@ public class Patch { if (match(buf, c, NEW_NAME) >= 0) break; - if (match(buf, c, HUNK_HDR) >= 0) { + if (isHunkHdr(buf, c, end) == 1) { final HunkHeader h = new HunkHeader(fh, c); h.parseHeader(end); c = h.parseBody(this, end); -- 2.11.4.GIT