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 index 000000000..f015a3806 Binary files /dev/null and b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/hello.patch differ 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 index 000000000..0abaeaa99 Binary files /dev/null and b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/hello_PostImage differ 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 index 000000000..b6fc4c620 Binary files /dev/null and b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/hello_PreImage differ diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java index 807b96130..867310b60 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java @@ -20,6 +20,7 @@ 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 void testFiltering() throws Exception { 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 @@ private void checkBinary(String name, boolean hasPreImage) } 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 void testEmptyLine() throws Exception { 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); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/ApplyCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/ApplyCommand.java index f649c5fde..583767af3 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/ApplyCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/ApplyCommand.java @@ -742,7 +742,11 @@ private boolean isNoNewlineAtEndOfFile(FileHeader fh) { 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$ }