[PATCH 2 of 2 STABLE] patch: pass canstripcr to fix_newline (issue4042)

Phil Cohen phillco at fb.com
Mon Apr 24 16:10:01 EDT 2017


# HG changeset patch
# User Phil Cohen <phillco at fb.com>
# Date 1493055385 25200
#      Mon Apr 24 10:36:25 2017 -0700
# Branch stable
# Node ID 7147efa0ca8da2d66056d2728ded7ba823a3d996
# Parent  7f413064263ff1b3cf98d1b23ae35e4c098da482
patch: pass canstripcr to fix_newline (issue4042)

As part of fixing issue4042, we more accurately decide whether to strip a CRLF
(instead of just a LF) from the last line of patch, by checking the endings of
the "\ No newline" marker line itself.

diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -1317,7 +1317,8 @@
     def _fixnewline(self, lr):
         l = lr.readline()
         if l.startswith('\ '):
-            diffhelpers.fix_newline(self.hunk, self.a, self.b)
+            diffhelpers.fix_newline(self.hunk, self.a, self.b,
+                                    l.endswith('\r\n'))
         else:
             lr.push(l)
 
diff --git a/mercurial/pure/diffhelpers.py b/mercurial/pure/diffhelpers.py
--- a/mercurial/pure/diffhelpers.py
+++ b/mercurial/pure/diffhelpers.py
@@ -17,8 +17,8 @@
         for i in xrange(num):
             s = fp.readline()
             c = s[0]
-            if s == "\\ No newline at end of file\n":
-                fix_newline(hunk, a, b)
+            if s.startswith("\\ No newline at end of file"):
+                fix_newline(hunk, a, b, s.endswith("\r\n"))
                 continue
             if c == "\n":
                 # Some patches may be missing the control char
diff --git a/tests/test-import-eol.t b/tests/test-import-eol.t
--- a/tests/test-import-eol.t
+++ b/tests/test-import-eol.t
@@ -144,3 +144,15 @@
   d (no-eol)
   $ hg st
   $ cd ..
+
+Ensure trailing CRs are left in the patch (issue4042)
+  $ hg init repo1
+  $ printf $'test\r' > repo1/test
+  $ cat repo1/test
+  test\r (no-eol) (esc)
+  $ hg -R repo1 add
+  adding repo1/test
+  $ hg -R repo1 commit -m.
+  $ hg init repo2
+  $ hg -R repo1 export tip | hg -R repo2 import --exact -
+  applying patch from stdin


More information about the Mercurial-devel mailing list