Patchwork [10,of,10] diffhelpers: be more tolerant for stripped empty lines of CRLF ending

login
register
mail settings
Submitter Yuya Nishihara
Date April 11, 2018, 3:34 p.m.
Message ID <209a355d60e0d4996b55.1523460840@mimosa>
Download mbox | patch
Permalink /patch/30704/
State Accepted
Headers show

Comments

Yuya Nishihara - April 11, 2018, 3:34 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1523032645 -32400
#      Sat Apr 07 01:37:25 2018 +0900
# Node ID 209a355d60e0d4996b5587d0e017380d3484670b
# Parent  c487b1b45eabcbe4cfd38a22a66e2fe6a71176b3
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.

Patch

diff --git a/mercurial/diffhelpers.py b/mercurial/diffhelpers.py
--- a/mercurial/diffhelpers.py
+++ b/mercurial/diffhelpers.py
@@ -33,10 +33,10 @@  def addlines(fp, hunk, lena, lenb, a, b)
             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:])
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
@@ -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 @@  Test different --eol values
   $ 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 @@  force LF
 
   $ hg --traceback --config patch.eol='LF' import eol.diff
   applying eol.diff
+  $ hg id
+  9e4ef7b3d4af tip
   $ cat a
   a
   yyyy
@@ -54,6 +62,25 @@  force LF
   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