From 5e44bfa3ad462e1220426492c53606c6a643a970 Mon Sep 17 00:00:00 2001 From: Anton Khodos Date: Fri, 14 Jun 2019 17:16:15 +0200 Subject: [PATCH] Fix ApplyCommand which doesn't work if patch adds empty file Bug: 548219 Change-Id: Ibb32132a38e54508a24489322da58ddfd80a1d9a Signed-off-by: Anton Khodos Signed-off-by: Matthias Sohn --- .../tst-rsrc/org/eclipse/jgit/diff/A3.patch | 3 +++ .../tst-rsrc/org/eclipse/jgit/diff/A3_PostImage | 0 .../tst/org/eclipse/jgit/api/ApplyCommandTest.java | 10 ++++++++++ .../src/org/eclipse/jgit/api/ApplyCommand.java | 10 +++++++--- 4 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/A3.patch create mode 100644 org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/A3_PostImage diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/A3.patch b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/A3.patch new file mode 100644 index 000000000..ac63d9113 --- /dev/null +++ b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/A3.patch @@ -0,0 +1,3 @@ +diff --git a/A3 b/A3 +new file mode 100644 +index 0000000..de98044 \ No newline at end of file diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/A3_PostImage b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/A3_PostImage new file mode 100644 index 000000000..e69de29bb 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 1dd329a9e..714a54c90 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 @@ -110,6 +110,16 @@ public class ApplyCommandTest extends RepositoryTestCase { b.getString(0, b.size(), false)); } + @Test + public void testAddA3() throws Exception { + ApplyResult result = init("A3", false, true); + assertEquals(1, result.getUpdatedFiles().size()); + assertEquals(new File(db.getWorkTree(), "A3"), + result.getUpdatedFiles().get(0)); + checkFile(new File(db.getWorkTree(), "A3"), + b.getString(0, b.size(), false)); + } + @Test public void testAddA1Sub() throws Exception { ApplyResult result = init("A1_sub", false, false); 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 c6f3c671a..38e544452 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/ApplyCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/ApplyCommand.java @@ -279,9 +279,13 @@ public class ApplyCommand extends GitCommand { } private boolean isNoNewlineAtEndOfFile(FileHeader fh) { - HunkHeader lastHunk = fh.getHunks().get(fh.getHunks().size() - 1); + List hunks = fh.getHunks(); + if (hunks == null || hunks.isEmpty()) { + return false; + } + HunkHeader lastHunk = hunks.get(hunks.size() - 1); RawText lhrt = new RawText(lastHunk.getBuffer()); - return lhrt.getString(lhrt.size() - 1).equals( - "\\ No newline at end of file"); //$NON-NLS-1$ + return lhrt.getString(lhrt.size() - 1) + .equals("\\ No newline at end of file"); //$NON-NLS-1$ } }