From 3d18f65af1341bc1ebc8c5c1e92726d701e5a55e Mon Sep 17 00:00:00 2001 From: Markus Duft Date: Tue, 12 Jun 2012 07:02:58 +0200 Subject: [PATCH] Fix patch application WRT windows line endings. Previously the result of an application would have been \r\r\n in the case of windows line endings, as RawText does not touch the \r, and ApplyCommand adds "\r\n" if this is the ending of the first line in the target file. Only always adding \n should be ok, since \r\n would be the result if the file and the patch include windows line endings. Also add according test. Change-Id: Ibd4c4948d81bd1c511ecf5fd6c906444930d236e --- .../tst-rsrc/org/eclipse/jgit/diff/NL1.patch | Bin 0 -> 108 bytes .../tst-rsrc/org/eclipse/jgit/diff/NL1_PostImage | Bin 0 -> 11 bytes .../tst-rsrc/org/eclipse/jgit/diff/NL1_PreImage | Bin 0 -> 11 bytes .../org/eclipse/jgit/api/ApplyCommandTest.java | 10 ++++++++++ .../src/org/eclipse/jgit/api/ApplyCommand.java | 11 ++++------- 5 files changed, 14 insertions(+), 7 deletions(-) create mode 100644 org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NL1.patch create mode 100644 org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NL1_PostImage create mode 100644 org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NL1_PreImage diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NL1.patch b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NL1.patch new file mode 100644 index 0000000000000000000000000000000000000000..ba5a4fc25dcac173f5f3e9676801b9a647505993 GIT binary patch literal 108 zcmXYoI}UC!{9xWnt$MMwxzascBU_=usgvhesyw3w-8YHtl9N57EfZMDe5)X%!# r@FC??7p=2(CWHcOjCYPi5MnX~~SrJ!7C@+d0*8dmj literal 0 HcmV?d00001 diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NL1_PostImage b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NL1_PostImage new file mode 100644 index 0000000000000000000000000000000000000000..b14088c045694df68b60086a15edb04e0480c23f GIT binary patch literal 11 ScmYe~@ literal 0 HcmV?d00001 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 5513b4460..a0a4ddcbf 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 @@ -178,6 +178,16 @@ public void testModifyZ() throws Exception { b.getString(0, b.size(), false)); } + @Test + public void testModifyNL1() throws Exception { + ApplyResult result = init("NL1"); + assertEquals(1, result.getUpdatedFiles().size()); + assertEquals(new File(db.getWorkTree(), "NL1"), result + .getUpdatedFiles().get(0)); + checkFile(new File(db.getWorkTree(), "NL1"), + b.getString(0, b.size(), false)); + } + private static byte[] readFile(final String patchFile) throws IOException { final InputStream in = DiffFormatterReflowTest.class .getResourceAsStream(patchFile); 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 2147a2e5c..6a945e4d3 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/ApplyCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/ApplyCommand.java @@ -238,13 +238,10 @@ private void apply(File f, FileHeader fh) if (!isChanged(oldLines, newLines)) return; // don't touch the file StringBuilder sb = new StringBuilder(); - final String eol = rt.size() == 0 - || (rt.size() == 1 && rt.isMissingNewlineAtEnd()) ? "\n" : rt //$NON-NLS-1$ - .getLineDelimiter(); for (String l : newLines) { - sb.append(l); - if (eol != null) - sb.append(eol); + // don't bother handling line endings - if it was windows, the \r is + // still there! + sb.append(l).append('\n'); } sb.deleteCharAt(sb.length() - 1); FileWriter fw = new FileWriter(f); @@ -252,7 +249,7 @@ private void apply(File f, FileHeader fh) fw.close(); } - private boolean isChanged(List ol, List nl) { + private static boolean isChanged(List ol, List nl) { if (ol.size() != nl.size()) return true; for (int i = 0; i < ol.size(); i++)