Browse Source

Fix ApplyCommand when result of patch is an empty file

Such hunks are identifiable by a zero value for "new start line". Prior
to the fix, JGit throws and ArrayIndexOutOfBoundsException on such
patches.

Change-Id: I4f3deb5e5f41a08af965fcc178d678c77270cddb
Signed-off-by: Jonathan Schneider <jkschneider@gmail.com>
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
stable-4.3
Jon Schneider 9 years ago committed by Matthias Sohn
parent
commit
80fa5f39f9
  1. 7
      org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/W.patch
  2. 0
      org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/W_PostImage
  3. 1
      org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/W_PreImage
  4. 10
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java
  5. 18
      org.eclipse.jgit/src/org/eclipse/jgit/api/ApplyCommand.java

7
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/W.patch

@ -0,0 +1,7 @@
diff --git a/W b/W
index a3648a1..2d44096 100644
--- a/W
+++ b/W
@@ -1 +0,0 @@
-a
\ No newline at end of file

0
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/W_PostImage

1
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/W_PreImage

@ -0,0 +1 @@
a

10
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java

@ -146,6 +146,16 @@ public class ApplyCommandTest extends RepositoryTestCase {
b.getString(0, b.size(), false)); b.getString(0, b.size(), false));
} }
@Test
public void testModifyW() throws Exception {
ApplyResult result = init("W");
assertEquals(1, result.getUpdatedFiles().size());
assertEquals(new File(db.getWorkTree(), "W"),
result.getUpdatedFiles().get(0));
checkFile(new File(db.getWorkTree(), "W"),
b.getString(0, b.size(), false));
}
@Test @Test
public void testModifyX() throws Exception { public void testModifyX() throws Exception {
ApplyResult result = init("X"); ApplyResult result = init("X");

18
org.eclipse.jgit/src/org/eclipse/jgit/api/ApplyCommand.java

@ -223,12 +223,16 @@ public class ApplyCommand extends GitCommand<ApplyResult> {
pos++; pos++;
break; break;
case '-': case '-':
if (!newLines.get(hh.getNewStartLine() - 1 + pos).equals( if (hh.getNewStartLine() == 0) {
hunkLine.substring(1))) { newLines.clear();
throw new PatchApplyException(MessageFormat.format( } else {
JGitText.get().patchApplyException, hh)); if (!newLines.get(hh.getNewStartLine() - 1 + pos)
.equals(hunkLine.substring(1))) {
throw new PatchApplyException(MessageFormat.format(
JGitText.get().patchApplyException, hh));
}
newLines.remove(hh.getNewStartLine() - 1 + pos);
} }
newLines.remove(hh.getNewStartLine() - 1 + pos);
break; break;
case '+': case '+':
newLines.add(hh.getNewStartLine() - 1 + pos, newLines.add(hh.getNewStartLine() - 1 + pos,
@ -250,7 +254,9 @@ public class ApplyCommand extends GitCommand<ApplyResult> {
// still there! // still there!
sb.append(l).append('\n'); sb.append(l).append('\n');
} }
sb.deleteCharAt(sb.length() - 1); if (sb.length() > 0) {
sb.deleteCharAt(sb.length() - 1);
}
FileWriter fw = new FileWriter(f); FileWriter fw = new FileWriter(f);
fw.write(sb.toString()); fw.write(sb.toString());
fw.close(); fw.close();

Loading…
Cancel
Save