git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Shawn O. Pearce <spearce@spearce.org>
Date
Dec 12, 2008, 22:05 UTC
Message-ID
<1229119558-1293-5-git-send-email-spearce@spearce.org>
In-Reply-To
<1229119558-1293-4-git-send-email-spearce@spearce.org>

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@spearce.org>
---
 .../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
Previous: Shawn O. PearceNext: Shawn O. Pearce
Message 5 of 14 in “Patch API bug fixes and diff --cc suport”
  1. 00/12 Patch API bug fixes and diff --cc suportShawn O. Pearce, Dec 12, 2008
  2. 01/12 Assert the HunkHeader.getFileHeader returns the right fileShawn O. Pearce, Dec 12, 2008
  3. 02/12 Add tests to cover more methods of BinaryHunkShawn O. Pearce, Dec 12, 2008
  4. 03/12 Add a simple toString to FormatError to facilitate debuggingShawn O. Pearce, Dec 12, 2008
  5. 04/12 Allow FileHeader to create its HunkHeader childrenShawn O. Pearce, Dec 12, 2008
  6. 05/12 Refactor the old/pre-image data in HunkHeader to support >1 ancestorShawn O. Pearce, Dec 12, 2008
  7. 06/12 Assert the ChunkHeader.OldImage.getId uses FileHeader.getOldImageShawn O. Pearce, Dec 12, 2008
  8. 07/12 Allow a stray LF at the end of a hunkShawn O. Pearce, Dec 12, 2008
  9. 08/12 Fix HunkHeader start line when parsing "@@ -1 +1 @@" style headersShawn O. Pearce, Dec 12, 2008
  10. 09/12 Add test cases for parsing "\ No newline at end of file" style patchesShawn O. Pearce, Dec 12, 2008
  11. 10/12 Use FileMode.MISSING when a file is added or deleted rather than nullShawn O. Pearce, Dec 12, 2008
  12. 11/12 Add a test for delta binary patch parsing and fix a bug in itShawn O. Pearce, Dec 12, 2008
  13. 12/12 Add support for parsing "diff --cc" style patchesShawn O. Pearce, Dec 12, 2008
  14. 12/12 Add support for parsing "diff --cc" style patchesShawn O. Pearce, Dec 12, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.