changeset 4878:372d93f03d3a

diff: correctly handle combinations of whitespace options
author Matt Mackall <mpm@selenic.com>
date Sat, 14 Jul 2007 12:44:47 -0500
parents 242026115e6a
children da8640113b5a 6403f948bd6b
files mercurial/mdiff.py tests/test-diff-ignore-whitespace tests/test-diff-ignore-whitespace.out
diffstat 3 files changed, 204 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/mdiff.py	Mon Jul 09 17:41:14 2007 +0300
+++ b/mercurial/mdiff.py	Sat Jul 14 12:44:47 2007 -0500
@@ -49,6 +49,16 @@
 
 defaultopts = diffopts()
 
+def wsclean(opts, text):
+    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:
+        text = re.sub('\n+', '', text)
+    return text
+
 def unidiff(a, ad, b, bd, fn, r=None, opts=defaultopts):
     def datetag(date, addtab=True):
         if not opts.git and not opts.nodates:
@@ -151,13 +161,6 @@
 
     if opts.showfunc:
         funcre = re.compile('\w')
-    if opts.ignorewsamount:
-        wsamountre = re.compile('[ \t]+')
-        wsappendedre = re.compile(' \n')
-    if opts.ignoreblanklines:
-        wsblanklinesre = re.compile('\n')
-    if opts.ignorews:
-        wsre = re.compile('[ \t]')
 
     # bdiff.blocks gives us the matching sequences in the files.  The loop
     # below finds the spaces between those matching sequences and translates
@@ -189,24 +192,8 @@
         if not old and not new:
             continue
 
-        if opts.ignoreblanklines:
-            wsold = wsblanklinesre.sub('', "".join(old))
-            wsnew = wsblanklinesre.sub('', "".join(new))
-            if wsold == wsnew:
-                continue
-
-        if opts.ignorewsamount:
-            wsold = wsamountre.sub(' ', "".join(old))
-            wsold = wsappendedre.sub('\n', wsold)
-            wsnew = wsamountre.sub(' ', "".join(new))
-            wsnew = wsappendedre.sub('\n', wsnew)
-            if wsold == wsnew:
-                continue
-
-        if opts.ignorews:
-            wsold = wsre.sub('', "".join(old))
-            wsnew = wsre.sub('', "".join(new))
-            if wsold == wsnew:
+        if opts.ignorews or opts.ignorewsamount or opts.ignoreblanklines:
+            if wsclean(opts, "".join(old)) == wsclean(opts, "".join(new)):
                 continue
 
         astart = contextstart(a1)
--- a/tests/test-diff-ignore-whitespace	Mon Jul 09 17:41:14 2007 +0300
+++ b/tests/test-diff-ignore-whitespace	Sat Jul 14 12:44:47 2007 -0500
@@ -3,6 +3,7 @@
 # GNU diff is the reference for all of these results.
 
 hgdiff() {
+    echo hg diff $@
     hg diff --nodates "$@"
 }
 
@@ -72,6 +73,39 @@
     hgdiff -Bb
 }
 
+test_added_blank_line_with_other_whitespace() {
+    printf 'hello  world\n \t\ngoodbye world \n' >foo
+
+    echo '>>> three diffs showing added blank line w/other space <<<'
+    hgdiff
+    hgdiff -B
+    hgdiff -b
+    hgdiff -Bb
+}
+
+test_whitespace_changes() {
+    printf 'helloworld\ngoodbye\tworld \n' >foo
+
+    echo '>>> four diffs showing changed whitespace <<<'
+    hgdiff
+    hgdiff -B
+    hgdiff -b
+    hgdiff -Bb
+    hgdiff -w
+}
+
+test_whitespace_changes_and_blank_lines() {
+    printf 'helloworld\n\n\n\ngoodbye\tworld \n' >foo
+
+    echo '>>> five diffs showing changed whitespace <<<'
+    hgdiff
+    hgdiff -B
+    hgdiff -b
+    hgdiff -Bb
+    hgdiff -w
+    hgdiff -wB
+}
+
 hg init
 printf 'hello world\ngoodbye world\n' >foo
 hg ci -Amfoo -ufoo -d '0 0'
@@ -82,3 +116,6 @@
 test_added_horizontal_space_in_the_middle_of_a_word
 test_increased_horizontal_whitespace_amount
 test_added_blank_line_with_horizontal_whitespace
+test_added_blank_line_with_other_whitespace
+test_whitespace_changes
+test_whitespace_changes_and_blank_lines
--- a/tests/test-diff-ignore-whitespace.out	Mon Jul 09 17:41:14 2007 +0300
+++ b/tests/test-diff-ignore-whitespace.out	Sat Jul 14 12:44:47 2007 -0500
@@ -1,5 +1,6 @@
 adding foo
 >>> two diffs showing three added lines <<<
+hg diff
 diff -r 540c40a65b78 foo
 --- a/foo
 +++ b/foo
@@ -9,6 +10,7 @@
 +
  goodbye world
 +
+hg diff -b
 diff -r 540c40a65b78 foo
 --- a/foo
 +++ b/foo
@@ -19,7 +21,10 @@
  goodbye world
 +
 >>> no diffs <<<
+hg diff -B
+hg diff -Bb
 >>> four diffs showing added space first on the first line <<<
+hg diff
 diff -r 540c40a65b78 foo
 --- a/foo
 +++ b/foo
@@ -27,6 +32,7 @@
 -hello world
 +	 hello world
  goodbye world
+hg diff -b
 diff -r 540c40a65b78 foo
 --- a/foo
 +++ b/foo
@@ -34,6 +40,7 @@
 -hello world
 +	 hello world
  goodbye world
+hg diff -B
 diff -r 540c40a65b78 foo
 --- a/foo
 +++ b/foo
@@ -41,6 +48,7 @@
 -hello world
 +	 hello world
  goodbye world
+hg diff -Bb
 diff -r 540c40a65b78 foo
 --- a/foo
 +++ b/foo
@@ -49,6 +57,7 @@
 +	 hello world
  goodbye world
 >>> two diffs showing space appended to the first line <<<
+hg diff
 diff -r 540c40a65b78 foo
 --- a/foo
 +++ b/foo
@@ -56,6 +65,7 @@
 -hello world
 +hello world	 
  goodbye world
+hg diff -B
 diff -r 540c40a65b78 foo
 --- a/foo
 +++ b/foo
@@ -64,7 +74,10 @@
 +hello world	 
  goodbye world
 >>> no diffs <<<
+hg diff -b
+hg diff -Bb
 >>> four diffs showing space inserted into "goodbye" <<<
+hg diff
 diff -r 540c40a65b78 foo
 --- a/foo
 +++ b/foo
@@ -72,6 +85,7 @@
  hello world
 -goodbye world
 +good bye world
+hg diff -B
 diff -r 540c40a65b78 foo
 --- a/foo
 +++ b/foo
@@ -79,6 +93,7 @@
  hello world
 -goodbye world
 +good bye world
+hg diff -b
 diff -r 540c40a65b78 foo
 --- a/foo
 +++ b/foo
@@ -86,6 +101,7 @@
  hello world
 -goodbye world
 +good bye world
+hg diff -Bb
 diff -r 540c40a65b78 foo
 --- a/foo
 +++ b/foo
@@ -94,6 +110,7 @@
 -goodbye world
 +good bye world
 >>> two diffs showing changed whitespace amount in the last line <<<
+hg diff
 diff -r 540c40a65b78 foo
 --- a/foo
 +++ b/foo
@@ -101,6 +118,7 @@
  hello world
 -goodbye world
 +goodbye		  	world
+hg diff -B
 diff -r 540c40a65b78 foo
 --- a/foo
 +++ b/foo
@@ -109,7 +127,10 @@
 -goodbye world
 +goodbye		  	world
 >>> no diffs <<<
+hg diff -b
+hg diff -Bb
 >>> four diffs showing added blank line w/horizontal space <<<
+hg diff
 diff -r 540c40a65b78 foo
 --- a/foo
 +++ b/foo
@@ -117,6 +138,15 @@
  hello world
 + 	
  goodbye world
+hg diff -B
+diff -r 540c40a65b78 foo
+--- a/foo
++++ b/foo
+@@ -1,2 +1,3 @@ hello world
+ hello world
++ 	
+ goodbye world
+hg diff -b
 diff -r 540c40a65b78 foo
 --- a/foo
 +++ b/foo
@@ -124,17 +154,136 @@
  hello world
 + 	
  goodbye world
+hg diff -Bb
+>>> three diffs showing added blank line w/other space <<<
+hg diff
+diff -r 540c40a65b78 foo
+--- a/foo
++++ b/foo
+@@ -1,2 +1,3 @@ hello world
+-hello world
+-goodbye world
++hello  world
++ 	
++goodbye world 
+hg diff -B
+diff -r 540c40a65b78 foo
+--- a/foo
++++ b/foo
+@@ -1,2 +1,3 @@ hello world
+-hello world
+-goodbye world
++hello  world
++ 	
++goodbye world 
+hg diff -b
 diff -r 540c40a65b78 foo
 --- a/foo
 +++ b/foo
 @@ -1,2 +1,3 @@ hello world
- hello world
+-hello world
+-goodbye world
++hello  world
 + 	
- goodbye world
++goodbye world 
+hg diff -Bb
+>>> four diffs showing changed whitespace <<<
+hg diff
+diff -r 540c40a65b78 foo
+--- a/foo
++++ b/foo
+@@ -1,2 +1,2 @@ hello world
+-hello world
+-goodbye world
++helloworld
++goodbye	world 
+hg diff -B
+diff -r 540c40a65b78 foo
+--- a/foo
++++ b/foo
+@@ -1,2 +1,2 @@ hello world
+-hello world
+-goodbye world
++helloworld
++goodbye	world 
+hg diff -b
+diff -r 540c40a65b78 foo
+--- a/foo
++++ b/foo
+@@ -1,2 +1,2 @@ hello world
+-hello world
+-goodbye world
++helloworld
++goodbye	world 
+hg diff -Bb
+diff -r 540c40a65b78 foo
+--- a/foo
++++ b/foo
+@@ -1,2 +1,2 @@ hello world
+-hello world
+-goodbye world
++helloworld
++goodbye	world 
+hg diff -w
+>>> five diffs showing changed whitespace <<<
+hg diff
 diff -r 540c40a65b78 foo
 --- a/foo
 +++ b/foo
-@@ -1,2 +1,3 @@ hello world
- hello world
-+ 	
- goodbye world
+@@ -1,2 +1,5 @@ hello world
+-hello world
+-goodbye world
++helloworld
++
++
++
++goodbye	world 
+hg diff -B
+diff -r 540c40a65b78 foo
+--- a/foo
++++ b/foo
+@@ -1,2 +1,5 @@ hello world
+-hello world
+-goodbye world
++helloworld
++
++
++
++goodbye	world 
+hg diff -b
+diff -r 540c40a65b78 foo
+--- a/foo
++++ b/foo
+@@ -1,2 +1,5 @@ hello world
+-hello world
+-goodbye world
++helloworld
++
++
++
++goodbye	world 
+hg diff -Bb
+diff -r 540c40a65b78 foo
+--- a/foo
++++ b/foo
+@@ -1,2 +1,5 @@ hello world
+-hello world
+-goodbye world
++helloworld
++
++
++
++goodbye	world 
+hg diff -w
+diff -r 540c40a65b78 foo
+--- a/foo
++++ b/foo
+@@ -1,2 +1,5 @@ hello world
+-hello world
+-goodbye world
++helloworld
++
++
++
++goodbye	world 
+hg diff -wB