]> source.dussan.org Git - jgit.git/commitdiff
Fix patch application WRT windows line endings. 14/6314/5
authorMarkus Duft <markus.duft@salomon.at>
Tue, 12 Jun 2012 05:02:58 +0000 (07:02 +0200)
committerRobin Rosenberg <robin.rosenberg@dewire.com>
Sat, 19 Jan 2013 12:29:02 +0000 (13:29 +0100)
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

org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NL1.patch [new file with mode: 0644]
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NL1_PostImage [new file with mode: 0644]
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NL1_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/NL1.patch b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NL1.patch
new file mode 100644 (file)
index 0000000..ba5a4fc
--- /dev/null
@@ -0,0 +1,10 @@
+diff --git a/NL1 b/NL1
+index 68abad7..b14088c 100644
+--- a/NL1
++++ b/NL1
+@@ -1,4 +1,4 @@
+ a\r
+-b\r
++d\r
+ c\r
\r
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 (file)
index 0000000..b14088c
--- /dev/null
@@ -0,0 +1,4 @@
+a\r
+d\r
+c\r
+\r
diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NL1_PreImage b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NL1_PreImage
new file mode 100644 (file)
index 0000000..68abad7
--- /dev/null
@@ -0,0 +1,4 @@
+a\r
+b\r
+c\r
+\r
index 5513b446031ee35cae831f4e27e8090215c7a649..a0a4ddcbf7396d12774a9c8f172f14c1d0b56987 100644 (file)
@@ -178,6 +178,16 @@ public class ApplyCommandTest extends RepositoryTestCase {
                                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);
index 2147a2e5c4084edc3e4c2a22ff33b242ec3aead0..6a945e4d39e482e07fef5162a1446c75aeb98aee 100644 (file)
@@ -238,13 +238,10 @@ public class ApplyCommand extends GitCommand<ApplyResult> {
                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 @@ public class ApplyCommand extends GitCommand<ApplyResult> {
                fw.close();
        }
 
-       private boolean isChanged(List<String> ol, List<String> nl) {
+       private static boolean isChanged(List<String> ol, List<String> nl) {
                if (ol.size() != nl.size())
                        return true;
                for (int i = 0; i < ol.size(); i++)