diffhelpers: be more tolerant for stripped empty lines of CRLF ending
authorYuya Nishihara <yuya@tcha.org>
Sat, 07 Apr 2018 01:37:25 +0900
changeset 37575 230eb9594150
parent 37574 a1bcc7ff0eac
child 37576 6ef94f24aa82
diffhelpers: be more tolerant for stripped empty lines of CRLF ending Exchange. It appears to trim lines containing only whitespace as well as converting LF to CRLF.
mercurial/diffhelpers.py
tests/test-import-eol.t
--- a/mercurial/diffhelpers.py	Mon Apr 09 21:08:52 2018 +0900
+++ b/mercurial/diffhelpers.py	Sat Apr 07 01:37:25 2018 +0900
@@ -33,10 +33,10 @@
             if s == "\\ No newline at end of file\n":
                 fixnewline(hunk, a, b)
                 continue
-            if s == "\n":
+            if s == '\n' or s == '\r\n':
                 # Some patches may be missing the control char
                 # on empty lines. Supply a leading space.
-                s = " \n"
+                s = ' ' + s
             hunk.append(s)
             if s.startswith('+'):
                 b.append(s[1:])
--- a/tests/test-import-eol.t	Mon Apr 09 21:08:52 2018 +0900
+++ b/tests/test-import-eol.t	Sat Apr 07 01:37:25 2018 +0900
@@ -1,5 +1,6 @@
   $ cat > makepatch.py <<EOF
-  > f = open('eol.diff', 'wb')
+  > import sys
+  > f = open(sys.argv[2], 'wb')
   > w = f.write
   > w(b'test message\n')
   > w(b'diff --git a/a b/a\n')
@@ -10,7 +11,10 @@
   > w(b'-bbb\r\n')
   > w(b'+yyyy\r\n')
   > w(b' cc\r\n')
-  > w(b' \n')
+  > w({'empty:lf': b' \n',
+  >    'empty:crlf': b' \r\n',
+  >    'empty:stripped-lf': b'\n',
+  >    'empty:stripped-crlf': b'\r\n'}[sys.argv[1]])
   > w(b' d\n')
   > w(b'-e\n')
   > w(b'\ No newline at end of file\n')
@@ -29,8 +33,10 @@
   $ hg ci -Am adda
   adding .hgignore
   adding a
-  $ $PYTHON ../makepatch.py
-
+  $ $PYTHON ../makepatch.py empty:lf eol.diff
+  $ $PYTHON ../makepatch.py empty:crlf eol-empty-crlf.diff
+  $ $PYTHON ../makepatch.py empty:stripped-lf eol-empty-stripped-lf.diff
+  $ $PYTHON ../makepatch.py empty:stripped-crlf eol-empty-stripped-crlf.diff
 
 invalid eol
 
@@ -45,6 +51,8 @@
 
   $ hg --traceback --config patch.eol='LF' import eol.diff
   applying eol.diff
+  $ hg id
+  9e4ef7b3d4af tip
   $ cat a
   a
   yyyy
@@ -54,6 +62,25 @@
   e (no-eol)
   $ hg st
 
+ (test empty-line variants: all of them should generate the same revision)
+
+  $ hg up -qC 0
+  $ hg --config patch.eol='LF' import eol-empty-crlf.diff
+  applying eol-empty-crlf.diff
+  $ hg id
+  9e4ef7b3d4af tip
+
+  $ hg up -qC 0
+  $ hg --config patch.eol='LF' import eol-empty-stripped-lf.diff
+  applying eol-empty-stripped-lf.diff
+  $ hg id
+  9e4ef7b3d4af tip
+
+  $ hg up -qC 0
+  $ hg --config patch.eol='LF' import eol-empty-stripped-crlf.diff
+  applying eol-empty-stripped-crlf.diff
+  $ hg id
+  9e4ef7b3d4af tip
 
 force CRLF