mdiff: fix diff -b/B/w on mixed whitespace hunks (issue127)
authorPatrick Mezard <pmezard@gmail.com>
Wed, 11 Nov 2009 18:31:42 +0100
changeset 9827 4fe9ca519637
parent 9826 d768614578dd
child 9828 4761e6203b77
mdiff: fix diff -b/B/w on mixed whitespace hunks (issue127) Previous code was computing hunks then checking if these hunks could be ignored when taking whitespace/blank-lines options in accounts. This approach is simple but fails with hunks containing both whitespace and non-whitespace changes, the whole hunk is emitted while it can be mostly made of whitespace. The new version normalize the whitespaces before hunk generation, and test for blank-lines afterwards.
mercurial/mdiff.py
tests/test-diff-ignore-whitespace.out
tests/test-mq-qdiff.out
--- a/mercurial/mdiff.py	Wed Nov 11 17:02:00 2009 +0100
+++ b/mercurial/mdiff.py	Wed Nov 11 18:31:42 2009 +0100
@@ -57,13 +57,13 @@
 
 defaultopts = diffopts()
 
-def wsclean(opts, text):
+def wsclean(opts, text, blank=True):
     if opts.ignorews:
         text = re.sub('[ \t]+', '', text)
     elif opts.ignorewsamount:
         text = re.sub('[ \t]+', ' ', text)
         text = re.sub('[ \t]+\n', '\n', text)
-    if opts.ignoreblanklines:
+    if blank and opts.ignoreblanklines:
         text = re.sub('\n+', '', text)
     return text
 
@@ -183,6 +183,10 @@
     # below finds the spaces between those matching sequences and translates
     # them into diff output.
     #
+    if opts.ignorews or opts.ignorewsamount:
+        t1 = wsclean(opts, t1, False)
+        t2 = wsclean(opts, t2, False)
+
     diff = bdiff.blocks(t1, t2)
     hunk = None
     for i, s1 in enumerate(diff):
@@ -208,7 +212,7 @@
         if not old and not new:
             continue
 
-        if opts.ignorews or opts.ignorewsamount or opts.ignoreblanklines:
+        if opts.ignoreblanklines:
             if wsclean(opts, "".join(old)) == wsclean(opts, "".join(new)):
                 continue
 
--- a/tests/test-diff-ignore-whitespace.out	Wed Nov 11 17:02:00 2009 +0100
+++ b/tests/test-diff-ignore-whitespace.out	Wed Nov 11 18:31:42 2009 +0100
@@ -181,11 +181,9 @@
 --- a/foo
 +++ b/foo
 @@ -1,2 +1,3 @@
--hello world
--goodbye world
-+hello  world
+ hello world
 + 	
-+goodbye world 
+ goodbye world
 hg diff -Bb
 >>> four diffs showing changed whitespace <<<
 hg diff
@@ -212,18 +210,16 @@
 +++ b/foo
 @@ -1,2 +1,2 @@
 -hello world
--goodbye world
 +helloworld
-+goodbye	world 
+ goodbye world
 hg diff -Bb
 diff -r 540c40a65b78 foo
 --- a/foo
 +++ b/foo
 @@ -1,2 +1,2 @@
 -hello world
--goodbye world
 +helloworld
-+goodbye	world 
+ goodbye world
 hg diff -w
 >>> five diffs showing changed whitespace <<<
 hg diff
@@ -256,34 +252,30 @@
 +++ b/foo
 @@ -1,2 +1,5 @@
 -hello world
--goodbye world
 +helloworld
 +
 +
 +
-+goodbye	world 
+ goodbye world
 hg diff -Bb
 diff -r 540c40a65b78 foo
 --- a/foo
 +++ b/foo
 @@ -1,2 +1,5 @@
 -hello world
--goodbye world
 +helloworld
 +
 +
 +
-+goodbye	world 
+ goodbye world
 hg diff -w
 diff -r 540c40a65b78 foo
 --- a/foo
 +++ b/foo
 @@ -1,2 +1,5 @@
--hello world
--goodbye world
-+helloworld
+ hello world
 +
 +
 +
-+goodbye	world 
+ goodbye world
 hg diff -wB
--- a/tests/test-mq-qdiff.out	Wed Nov 11 17:02:00 2009 +0100
+++ b/tests/test-mq-qdiff.out	Wed Nov 11 18:31:42 2009 +0100
@@ -58,9 +58,8 @@
  2
  3
  4
--hello world
+ hello world
 -goodbye world
-+hello  world
 +     goodbye world
  7
  8