[JGIT PATCH 04/12] Allow FileHeader to create its HunkHeader children

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 <spearce@xxxxxxxxxxx>
---
 .../src/org/spearce/jgit/patch/FileHeader.java     |    8 ++++++++
 .../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 5fe2acf..f93129d 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 @@ FileHeader(final byte[] b, final int offset) {
 		patchType = PatchType.UNIFIED;
 	}
 
+	int getParentCount() {
+		return 1;
+	}
+
 	/**
 	 * Get the old name associated with this file.
 	 * <p>
@@ -274,6 +278,10 @@ void addHunk(final HunkHeader h) {
 		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 77ae02f..05d034d 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 @@ private int parseHunks(final FileHeader fh, int c, final int end) {
 			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;
-- 
1.6.1.rc2.306.ge5d5e

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux