]> source.dussan.org Git - jgit.git/commitdiff
Fix ApplyCommand when result of patch is an empty file 19/70819/1
authorJon Schneider <jkschneider@gmail.com>
Fri, 15 Apr 2016 21:37:32 +0000 (14:37 -0700)
committerMatthias Sohn <matthias.sohn@sap.com>
Sat, 16 Apr 2016 22:28:06 +0000 (17:28 -0500)
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>
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/W.patch [new file with mode: 0644]
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/W_PostImage [new file with mode: 0644]
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/W_PreImage [new file with mode: 0644]
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java
org.eclipse.jgit/src/org/eclipse/jgit/api/ApplyCommand.java

diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/W.patch b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/W.patch
new file mode 100644 (file)
index 0000000..cfecb8c
--- /dev/null
@@ -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
\ No newline at end of file
diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/W_PostImage b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/W_PostImage
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/W_PreImage b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/W_PreImage
new file mode 100644 (file)
index 0000000..2e65efe
--- /dev/null
@@ -0,0 +1 @@
+a
\ No newline at end of file
index 239c844c332d1ce0dc6a78287abda75d7da0bef4..f2b5b3ba95dff776c099ead95ab6c1bbbbdd925f 100644 (file)
@@ -146,6 +146,16 @@ public class ApplyCommandTest extends RepositoryTestCase {
                                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
        public void testModifyX() throws Exception {
                ApplyResult result = init("X");
index bde450f99d7077a29511a8c353c99f7c1333a16b..8fbf83954cb441098a318490d7a8bf6054abb1f5 100644 (file)
@@ -223,12 +223,16 @@ public class ApplyCommand extends GitCommand<ApplyResult> {
                                        pos++;
                                        break;
                                case '-':
-                                       if (!newLines.get(hh.getNewStartLine() - 1 + pos).equals(
-                                                       hunkLine.substring(1))) {
-                                               throw new PatchApplyException(MessageFormat.format(
-                                                               JGitText.get().patchApplyException, hh));
+                                       if (hh.getNewStartLine() == 0) {
+                                               newLines.clear();
+                                       } else {
+                                               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;
                                case '+':
                                        newLines.add(hh.getNewStartLine() - 1 + pos,
@@ -250,7 +254,9 @@ public class ApplyCommand extends GitCommand<ApplyResult> {
                        // still there!
                        sb.append(l).append('\n');
                }
-               sb.deleteCharAt(sb.length() - 1);
+               if (sb.length() > 0) {
+                       sb.deleteCharAt(sb.length() - 1);
+               }
                FileWriter fw = new FileWriter(f);
                fw.write(sb.toString());
                fw.close();