]> source.dussan.org Git - jgit.git/commitdiff
ApplyCommand: fix "no newline at end" detection 98/177598/6
authorThomas Wolf <thomas.wolf@paranor.ch>
Wed, 10 Mar 2021 18:26:39 +0000 (19:26 +0100)
committerMatthias Sohn <matthias.sohn@sap.com>
Tue, 25 May 2021 22:38:00 +0000 (00:38 +0200)
Check the last line of the last hunk of a file, not the last line of
the whole patch.

Note that C git only checks that this line starts with "\ " and is at
least 12 characters long because of possible different texts when non-
English messages are used.

Change-Id: I0db81699eb3e99ed7b536a3e2b8dc97df1f58a89
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/hello.patch [new file with mode: 0644]
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/hello_PostImage [new file with mode: 0644]
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/hello_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/hello.patch b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/hello.patch
new file mode 100644 (file)
index 0000000..f015a38
--- /dev/null
@@ -0,0 +1,16 @@
+diff --git a/hello b/hello
+index b6fc4c6..0abaeaa 100644
+--- a/hello
++++ b/hello
+@@ -1 +1 @@
+-hello
+\ No newline at end of file
++bye
+\ No newline at end of file
+diff --git a/yello b/yello
+index 391a8cb..d1ed081 100644
+--- a/yello
++++ b/yello
+@@ -1 +1 @@
+-yello
++yellow
diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/hello_PostImage b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/hello_PostImage
new file mode 100644 (file)
index 0000000..0abaeaa
--- /dev/null
@@ -0,0 +1 @@
+bye
\ No newline at end of file
diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/hello_PreImage b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/hello_PreImage
new file mode 100644 (file)
index 0000000..b6fc4c6
--- /dev/null
@@ -0,0 +1 @@
+hello
\ No newline at end of file
index 807b96130008e52a9064ab35714baa7fa506e576..867310b60c8fd3ee27e9efdc40a3111f124added 100644 (file)
@@ -20,6 +20,7 @@ import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 
 import org.eclipse.jgit.api.errors.PatchApplyException;
@@ -251,6 +252,11 @@ public class ApplyCommandTest extends RepositoryTestCase {
 
        private void checkBinary(String name, boolean hasPreImage)
                        throws Exception {
+               checkBinary(name, hasPreImage, 1);
+       }
+
+       private void checkBinary(String name, boolean hasPreImage,
+                       int numberOfFiles) throws Exception {
                try (Git git = new Git(db)) {
                        byte[] post = IO
                                        .readWholeStream(getTestResource(name + "_PostImage"), 0)
@@ -266,7 +272,7 @@ public class ApplyCommandTest extends RepositoryTestCase {
                        }
                        ApplyResult result = git.apply()
                                        .setPatch(getTestResource(name + ".patch")).call();
-                       assertEquals(1, result.getUpdatedFiles().size());
+                       assertEquals(numberOfFiles, result.getUpdatedFiles().size());
                        assertEquals(f, result.getUpdatedFiles().get(0));
                        assertArrayEquals(post, Files.readAllBytes(f.toPath()));
                }
@@ -303,6 +309,18 @@ public class ApplyCommandTest extends RepositoryTestCase {
                checkBinary("emptyLine", true);
        }
 
+       @Test
+       public void testMultiFileNoNewline() throws Exception {
+               // This test needs two files. One is in the test resources.
+               try (Git git = new Git(db)) {
+                       Files.write(db.getWorkTree().toPath().resolve("yello"),
+                                       "yello".getBytes(StandardCharsets.US_ASCII));
+                       git.add().addFilepattern("yello").call();
+                       git.commit().setMessage("yello").call();
+               }
+               checkBinary("hello", true, 2);
+       }
+
        @Test
        public void testAddA1() throws Exception {
                ApplyResult result = init("A1", false, true);
index f649c5fdebe60c8429ef02404971ba08bf4d189e..583767af3f7d98dcc63f5e8f586dcdbebf534646 100644 (file)
@@ -742,7 +742,11 @@ public class ApplyCommand extends GitCommand<ApplyResult> {
                        return false;
                }
                HunkHeader lastHunk = hunks.get(hunks.size() - 1);
-               RawText lhrt = new RawText(lastHunk.getBuffer());
+               byte[] buf = new byte[lastHunk.getEndOffset()
+                               - lastHunk.getStartOffset()];
+               System.arraycopy(lastHunk.getBuffer(), lastHunk.getStartOffset(), buf,
+                               0, buf.length);
+               RawText lhrt = new RawText(buf);
                return lhrt.getString(lhrt.size() - 1)
                                .equals("\\ No newline at end of file"); //$NON-NLS-1$
        }