mdiff: add a --ignore-space-at-eol option
authorDavid Soria Parra <davidsp@fb.com>
Tue, 29 Aug 2017 18:20:50 -0700
changeset 34013 da07367d683b
parent 34012 dcfdf4d09663
child 34014 47e52f079a57
mdiff: add a --ignore-space-at-eol option Add an option that only ignores whitespaces at EOL. The name of the option is the same as Git. .. feature:: Added `--ignore-space-at-eol` diff option to ignore whitespace differences at line endings. Differential Revision: https://phab.mercurial-scm.org/D422
mercurial/cmdutil.py
mercurial/help/config.txt
mercurial/mdiff.py
mercurial/patch.py
tests/test-completion.t
tests/test-diff-ignore-whitespace.t
tests/test-help.t
tests/test-qrecord.t
tests/test-record.t
--- a/mercurial/cmdutil.py	Fri Aug 25 11:20:34 2017 -0700
+++ b/mercurial/cmdutil.py	Tue Aug 29 18:20:50 2017 -0700
@@ -123,6 +123,8 @@
      _('ignore changes in the amount of white space')),
     ('B', 'ignore-blank-lines', None,
      _('ignore changes whose lines are all blank')),
+    ('Z', 'ignore-space-at-eol', None,
+     _('ignore changes in whitespace at EOL')),
 ]
 
 diffopts2 = [
--- a/mercurial/help/config.txt	Fri Aug 25 11:20:34 2017 -0700
+++ b/mercurial/help/config.txt	Tue Aug 29 18:20:50 2017 -0700
@@ -313,6 +313,9 @@
 ``ignorews``
     Ignore white space when comparing lines.
 
+``ignorewseol``
+    Ignore white space at the end of a line when comparing lines.
+
 ``ignorewsamount``
     Ignore changes in the amount of white space.
 
--- a/mercurial/mdiff.py	Fri Aug 25 11:20:34 2017 -0700
+++ b/mercurial/mdiff.py	Tue Aug 29 18:20:50 2017 -0700
@@ -63,6 +63,7 @@
         'index': 0,
         'ignorews': False,
         'ignorewsamount': False,
+        'ignorewseol': False,
         'ignoreblanklines': False,
         'upgrade': False,
         'showsimilarity': False,
@@ -97,6 +98,8 @@
         text = bdiff.fixws(text, 0)
     if blank and opts.ignoreblanklines:
         text = re.sub('\n+', '\n', text).strip('\n')
+    if opts.ignorewseol:
+        text = re.sub(r'[ \t\r\f]+\n', r'\n', text)
     return text
 
 def splitblock(base1, lines1, base2, lines2, opts):
@@ -199,7 +202,7 @@
     """
     if opts is None:
         opts = defaultopts
-    if opts.ignorews or opts.ignorewsamount:
+    if opts.ignorews or opts.ignorewsamount or opts.ignorewseol:
         text1 = wsclean(opts, text1, False)
         text2 = wsclean(opts, text2, False)
     diff = bdiff.blocks(text1, text2)
--- a/mercurial/patch.py	Fri Aug 25 11:20:34 2017 -0700
+++ b/mercurial/patch.py	Tue Aug 29 18:20:50 2017 -0700
@@ -2282,6 +2282,7 @@
                                           'ignorewsamount')
         buildopts['ignoreblanklines'] = get('ignore_blank_lines',
                                             'ignoreblanklines')
+        buildopts['ignorewseol'] = get('ignore_space_at_eol', 'ignorewseol')
     if formatchanging:
         buildopts['text'] = opts and opts.get('text')
         binary = None if opts is None else opts.get('binary')
--- a/tests/test-completion.t	Fri Aug 25 11:20:34 2017 -0700
+++ b/tests/test-completion.t	Tue Aug 29 18:20:50 2017 -0700
@@ -218,10 +218,10 @@
 Show all commands + options
   $ hg debugcommands
   add: include, exclude, subrepos, dry-run
-  annotate: rev, follow, no-follow, text, user, file, date, number, changeset, line-number, skip, ignore-all-space, ignore-space-change, ignore-blank-lines, include, exclude, template
+  annotate: rev, follow, no-follow, text, user, file, date, number, changeset, line-number, skip, ignore-all-space, ignore-space-change, ignore-blank-lines, ignore-space-at-eol, include, exclude, template
   clone: noupdate, updaterev, rev, branch, pull, uncompressed, ssh, remotecmd, insecure
   commit: addremove, close-branch, amend, secret, edit, interactive, include, exclude, message, logfile, date, user, subrepos
-  diff: rev, change, text, git, binary, nodates, noprefix, show-function, reverse, ignore-all-space, ignore-space-change, ignore-blank-lines, unified, stat, root, include, exclude, subrepos
+  diff: rev, change, text, git, binary, nodates, noprefix, show-function, reverse, ignore-all-space, ignore-space-change, ignore-blank-lines, ignore-space-at-eol, unified, stat, root, include, exclude, subrepos
   export: output, switch-parent, rev, text, git, binary, nodates
   forget: include, exclude
   init: ssh, remotecmd, insecure
--- a/tests/test-diff-ignore-whitespace.t	Fri Aug 25 11:20:34 2017 -0700
+++ b/tests/test-diff-ignore-whitespace.t	Tue Aug 29 18:20:50 2017 -0700
@@ -407,8 +407,23 @@
   +goodbye\r (no-eol) (esc)
   world
 
+Test \r (carriage return) as used in "DOS" line endings:
+
+  $ printf 'hello world    \r\n\t\ngoodbye world\n' >foo
+
+  $ hg ndiff --ignore-space-at-eol
+  diff -r 540c40a65b78 foo
+  --- a/foo
+  +++ b/foo
+  @@ -1,2 +1,3 @@
+   hello world
+  +\t (esc)
+   goodbye world
+
 No completely blank lines to ignore:
 
+  $ printf 'hello world\r\n\r\ngoodbye\rworld\n' >foo
+
   $ hg ndiff --ignore-blank-lines
   diff -r 540c40a65b78 foo
   --- a/foo
--- a/tests/test-help.t	Fri Aug 25 11:20:34 2017 -0700
+++ b/tests/test-help.t	Tue Aug 29 18:20:50 2017 -0700
@@ -553,6 +553,7 @@
    -w --ignore-all-space    ignore white space when comparing lines
    -b --ignore-space-change ignore changes in the amount of white space
    -B --ignore-blank-lines  ignore changes whose lines are all blank
+   -Z --ignore-space-at-eol ignore changes in whitespace at EOL
    -U --unified NUM         number of lines of context to show
       --stat                output diffstat-style summary of changes
       --root DIR            produce diffs relative to subdirectory
--- a/tests/test-qrecord.t	Fri Aug 25 11:20:34 2017 -0700
+++ b/tests/test-qrecord.t	Tue Aug 29 18:20:50 2017 -0700
@@ -79,6 +79,7 @@
    -w --ignore-all-space    ignore white space when comparing lines
    -b --ignore-space-change ignore changes in the amount of white space
    -B --ignore-blank-lines  ignore changes whose lines are all blank
+   -Z --ignore-space-at-eol ignore changes in whitespace at EOL
   
   (some details hidden, use --verbose to show complete help)
 
@@ -152,6 +153,7 @@
    -w --ignore-all-space    ignore white space when comparing lines
    -b --ignore-space-change ignore changes in the amount of white space
    -B --ignore-blank-lines  ignore changes whose lines are all blank
+   -Z --ignore-space-at-eol ignore changes in whitespace at EOL
       --mq                  operate on patch repository
   
   (some details hidden, use --verbose to show complete help)
--- a/tests/test-record.t	Fri Aug 25 11:20:34 2017 -0700
+++ b/tests/test-record.t	Tue Aug 29 18:20:50 2017 -0700
@@ -62,6 +62,7 @@
    -w --ignore-all-space    ignore white space when comparing lines
    -b --ignore-space-change ignore changes in the amount of white space
    -B --ignore-blank-lines  ignore changes whose lines are all blank
+   -Z --ignore-space-at-eol ignore changes in whitespace at EOL
   
   (some details hidden, use --verbose to show complete help)