From cf34c3fcc676f6603f2dfdbcb149b859bf55044c Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Fri, 12 Dec 2008 14:05:50 -0800 Subject: [PATCH] Allow FileHeader to create its HunkHeader children By using a factory method on FileHeader we can later subclass the FileHeader class to handle "diff --cc" style patches, and let it create its own subclass of HunkHeader to handle the specialized form of the n-way diff. The getParentCount() method is hard-coded to return 1 in the 2-way diff case as there is exactly one parent. But in a "diff --cc" we need to verify the hunk header has the same number of parents as the file header in order to parse the hunk. So a subclass of the FileHeader would need to override getParentCount() to return the actual number of '@' symbols (less 1) that should appear in each hunk header line. (E.g. a 3-way diff shows "@@@ -" so the parent count should be 2.) Signed-off-by: Shawn O. Pearce Signed-off-by: Robin Rosenberg --- org.spearce.jgit/src/org/spearce/jgit/patch/FileHeader.java | 8 ++++++++ org.spearce.jgit/src/org/spearce/jgit/patch/Patch.java | 4 ++-- 2 files changed, 10 insertions(+), 2 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 67310644..44830796 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/patch/FileHeader.java +++ b/org.spearce.jgit/src/org/spearce/jgit/patch/FileHeader.java @@ -169,6 +169,10 @@ public class FileHeader { patchType = PatchType.UNIFIED; } + int getParentCount() { + return 1; + } + /** * Get the old name associated with this file. *

@@ -274,6 +278,10 @@ public class FileHeader { hunks.add(h); } + HunkHeader newHunkHeader(final int offset) { + return new HunkHeader(this, offset); + } + /** @return if a {@link PatchType#GIT_BINARY}, the new-image delta/literal */ public BinaryHunk getForwardBinaryHunk() { return forwardBinaryHunk; 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 1cf17515..503648fe 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/patch/Patch.java +++ b/org.spearce.jgit/src/org/spearce/jgit/patch/Patch.java @@ -281,8 +281,8 @@ public class Patch { if (match(buf, c, NEW_NAME) >= 0) break; - if (isHunkHdr(buf, c, end) == 1) { - final HunkHeader h = new HunkHeader(fh, c); + if (isHunkHdr(buf, c, end) == fh.getParentCount()) { + final HunkHeader h = fh.newHunkHeader(c); h.parseHeader(end); c = h.parseBody(this, end); h.endOffset = c; -- 2.11.4.GIT