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

[JGIT PATCH 10/12] Use FileMode.MISSING when a file is added or deleted rather than null

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

Null is better used to indicate "no mode information at all" in the patch, while FileMode.MISSING is already commonly used within the TreeWalk code to mean "this path doesn't exist in this tree". In the context of a patch to create or delete a file, MISSING makes a lot more sense for the application to work with.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 .../tst/org/spearce/jgit/patch/FileHeaderTest.java |    4 ++--
 .../tst/org/spearce/jgit/patch/PatchTest.java      |    1 +
 .../src/org/spearce/jgit/patch/FileHeader.java     |    2 ++
 3 files changed, 5 insertions(+), 2 deletions(-)
diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/patch/FileHeaderTest.java b/org.spearce.jgit.test/tst/org/spearce/jgit/patch/FileHeaderTest.java
index 36c528e..69e06ab 100644
--- a/org.spearce.jgit.test/tst/org/spearce/jgit/patch/FileHeaderTest.java
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/patch/FileHeaderTest.java
@@ -153,7 +153,7 @@ public void testParseUnicodeName_NewFile() {
 		assertSame(FileHeader.PatchType.UNIFIED, fh.getPatchType());
 		assertTrue(fh.hasMetaDataChanges());
 
-		assertNull(fh.getOldMode());
+		assertSame(FileMode.MISSING, fh.getOldMode());
 		assertSame(FileMode.REGULAR_FILE, fh.getNewMode());
 
 		assertEquals("0000000", fh.getOldId().name());
@@ -179,7 +179,7 @@ public void testParseUnicodeName_DeleteFile() {
 		assertTrue(fh.hasMetaDataChanges());
 
 		assertSame(FileMode.REGULAR_FILE, fh.getOldMode());
-		assertNull(fh.getNewMode());
+		assertSame(FileMode.MISSING, fh.getNewMode());
 
 		assertEquals("7898192", fh.getOldId().name());
 		assertEquals("0000000", fh.getNewId().name());
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 13eab5f..2c617d3 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
@@ -160,6 +160,7 @@ public void testParse_NoBinary() throws IOException {
 			assertNotNull(fh.getOldId());
 			assertNotNull(fh.getNewId());
 			assertEquals("0000000", fh.getOldId().name());
+			assertSame(FileMode.MISSING, fh.getOldMode());
 			assertSame(FileMode.REGULAR_FILE, fh.getNewMode());
 			assertTrue(fh.getNewName().startsWith(
 					"org.spearce.egit.ui/icons/toolbar/"));
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 f93129d..48d7623 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/patch/FileHeader.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/patch/FileHeader.java
@@ -386,9 +386,11 @@ int parseGitHeaders(int ptr, final int end) {
 
 			} else if (match(buf, ptr, DELETED_FILE_MODE) >= 0) {
 				oldMode = parseFileMode(ptr + DELETED_FILE_MODE.length, eol);
+				newMode = FileMode.MISSING;
 				changeType = ChangeType.DELETE;
 
 			} else if (match(buf, ptr, NEW_FILE_MODE) >= 0) {
+				oldMode = FileMode.MISSING;
 				newMode = parseFileMode(ptr + NEW_FILE_MODE.length, eol);
 				changeType = ChangeType.ADD;
 
-- 
1.6.1.rc2.306.ge5d5e
Previous: Shawn O. PearceNext: Shawn O. Pearce
Message 11 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.