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

[JGIT PATCH 11/12] Add a test for delta binary patch parsing and fix a bug in it

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

We had the wrong header code in this case, so we didn't parse the length correctly for delta style binary hunks. Without a test case for it we never noticed the problem.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 .../tst/org/spearce/jgit/patch/PatchTest.java      |   39 +++++++++++++++++++-
 .../jgit/patch/testParse_GitBinaryDelta.patch      |   21 +++++++++++
 ...nary.patch => testParse_GitBinaryLiteral.patch} |    0
 .../src/org/spearce/jgit/patch/BinaryHunk.java     |    2 +-
 4 files changed, 60 insertions(+), 2 deletions(-)
 create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/patch/testParse_GitBinaryDelta.patch
 rename org.spearce.jgit.test/tst/org/spearce/jgit/patch/{testParse_GitBinary.patch => testParse_GitBinaryLiteral.patch} (100%)
diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/patch/PatchTest.java b/org.spearce.jgit.test/tst/org/spearce/jgit/patch/PatchTest.java
index 2c617d3..8309951 100644
--- a/org.spearce.jgit.test/tst/org/spearce/jgit/patch/PatchTest.java
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/patch/PatchTest.java
@@ -184,7 +184,7 @@ assertTrue(fh.getNewName().startsWith(
 		assertEquals(272, fh.getHunks().get(0).getOldImage().getStartLine());
 	}
 
-	public void testParse_GitBinary() throws IOException {
+	public void testParse_GitBinaryLiteral() throws IOException {
 		final Patch p = parseTestPatchFile();
 		final int[] binsizes = { 359, 393, 372, 404 };
 		assertEquals(5, p.getFiles().size());
@@ -229,6 +229,43 @@ assertTrue(fh.getNewName().startsWith(
 		assertEquals(272, fh.getHunks().get(0).getOldImage().getStartLine());
 	}
 
+	public void testParse_GitBinaryDelta() throws IOException {
+		final Patch p = parseTestPatchFile();
+		assertEquals(1, p.getFiles().size());
+		assertTrue(p.getErrors().isEmpty());
+
+		final FileHeader fh = p.getFiles().get(0);
+		assertTrue(fh.getNewName().startsWith("zero.bin"));
+		assertSame(FileHeader.ChangeType.MODIFY, fh.getChangeType());
+		assertSame(FileHeader.PatchType.GIT_BINARY, fh.getPatchType());
+		assertSame(FileMode.REGULAR_FILE, fh.getNewMode());
+
+		assertNotNull(fh.getOldId());
+		assertNotNull(fh.getNewId());
+		assertEquals("08e7df176454f3ee5eeda13efa0adaa54828dfd8", fh.getOldId()
+				.name());
+		assertEquals("d70d8710b6d32ff844af0ee7c247e4b4b051867f", fh.getNewId()
+				.name());
+
+		assertTrue(fh.getHunks().isEmpty());
+		assertFalse(fh.hasMetaDataChanges());
+
+		final BinaryHunk fwd = fh.getForwardBinaryHunk();
+		final BinaryHunk rev = fh.getReverseBinaryHunk();
+		assertNotNull(fwd);
+		assertNotNull(rev);
+		assertEquals(12, fwd.getSize());
+		assertEquals(11, rev.getSize());
+
+		assertSame(fh, fwd.getFileHeader());
+		assertSame(fh, rev.getFileHeader());
+
+		assertSame(BinaryHunk.Type.DELTA_DEFLATED, fwd.getType());
+		assertSame(BinaryHunk.Type.DELTA_DEFLATED, rev.getType());
+
+		assertEquals(496, fh.endOffset);
+	}
+
 	public void testParse_FixNoNewline() throws IOException {
 		final Patch p = parseTestPatchFile();
 		assertEquals(1, p.getFiles().size());
diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/patch/testParse_GitBinaryDelta.patch b/org.spearce.jgit.test/tst/org/spearce/jgit/patch/testParse_GitBinaryDelta.patch
new file mode 100644
index 0000000..5b2c9c6
--- /dev/null
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/patch/testParse_GitBinaryDelta.patch
@@ -0,0 +1,21 @@
+From 7e49721ad0efdec3a81e20bc58e385ea5d2b87b7 Mon Sep 17 00:00:00 2001
+From: Shawn O. Pearce <sop@google.com>
+Date: Fri, 12 Dec 2008 12:45:17 -0800
+Subject: [PATCH] make zero have a 3
+
+---
+ zero.bin |  Bin 4096 -> 4096 bytes
+ 1 files changed, 0 insertions(+), 0 deletions(-)
+
+diff --git a/zero.bin b/zero.bin
+index 08e7df176454f3ee5eeda13efa0adaa54828dfd8..d70d8710b6d32ff844af0ee7c247e4b4b051867f 100644
+GIT binary patch
+delta 12
+TcmZorXi%6C%4ociaTPxR8IA+R
+
+delta 11
+ScmZorXi(Uguz-JJK>`37u>@iO
+
+-- 
+1.6.1.rc2.306.ge5d5e
+
diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/patch/testParse_GitBinary.patch b/org.spearce.jgit.test/tst/org/spearce/jgit/patch/testParse_GitBinaryLiteral.patch
similarity index 100%
rename from org.spearce.jgit.test/tst/org/spearce/jgit/patch/testParse_GitBinary.patch
rename to org.spearce.jgit.test/tst/org/spearce/jgit/patch/testParse_GitBinaryLiteral.patch
diff --git a/org.spearce.jgit/src/org/spearce/jgit/patch/BinaryHunk.java b/org.spearce.jgit/src/org/spearce/jgit/patch/BinaryHunk.java
index 3e07ec4..92eab86 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/patch/BinaryHunk.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/patch/BinaryHunk.java
@@ -100,7 +100,7 @@ int parseHunk(int ptr, final int end) {
 
 		} else if (match(buf, ptr, DELTA) >= 0) {
 			type = Type.DELTA_DEFLATED;
-			length = parseBase10(buf, ptr + LITERAL.length, null);
+			length = parseBase10(buf, ptr + DELTA.length, null);
 
 		} else {
 			// Not a valid binary hunk. Signal to the caller that
-- 
1.6.1.rc2.306.ge5d5e
Previous: Shawn O. PearceNext: Shawn O. Pearce
Message 12 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.