-b/-B options to diff

Haakon Riiser haakon.riiser at fys.uio.no
Thu Jun 29 08:16:25 CDT 2006


[Bryan O'Sullivan]

> On Wed, 2006-06-28 at 19:30 +0200, Haakon Riiser wrote:
> 
>> Just wondering what happened to the patch quoted below.  Did you, or
>> anyone else, find the time to review it?
> 
> Sorry, it fell on the floor.  It has now bit-rotted compared to what's
> in the crew repository, so I can apply it against 0.9, but it no longer
> merges cleanly because some files got moved around.
> 
> If you could redo it against crew, and add tests for the new options you
> added, that would be a big help, I think.

Try this:

diff -r 4716a58c8cd5 mercurial/commands.py
--- a/mercurial/commands.py	Tue Jun 27 11:32:37 2006 -0700
+++ b/mercurial/commands.py	Thu Jun 29 15:15:43 2006 +0200
@@ -405,23 +405,33 @@ def dodiff(fp, ui, repo, node1, node2, f
     diffopts = ui.diffopts()
     showfunc = opts.get('show_function') or diffopts['showfunc']
     ignorews = opts.get('ignore_all_space') or diffopts['ignorews']
+    ignorewsamount = opts.get('ignore_space_change') or \
+                     diffopts['ignorewsamount']
+    ignoreblanklines = opts.get('ignore_blank_lines') or \
+                     diffopts['ignoreblanklines']
     for f in modified:
         to = None
         if f in mmap:
             to = repo.file(f).read(mmap[f])
         tn = read(f)
         fp.write(mdiff.unidiff(to, date1, tn, date2(f), f, r, text=text,
-                               showfunc=showfunc, ignorews=ignorews))
+                               showfunc=showfunc, ignorews=ignorews,
+                               ignorewsamount=ignorewsamount,
+                               ignoreblanklines=ignoreblanklines))
     for f in added:
         to = None
         tn = read(f)
         fp.write(mdiff.unidiff(to, date1, tn, date2(f), f, r, text=text,
-                               showfunc=showfunc, ignorews=ignorews))
+                               showfunc=showfunc, ignorews=ignorews,
+                               ignorewsamount=ignorewsamount,
+                               ignoreblanklines=ignoreblanklines))
     for f in removed:
         to = repo.file(f).read(mmap[f])
         tn = None
         fp.write(mdiff.unidiff(to, date1, tn, date2(f), f, r, text=text,
-                               showfunc=showfunc, ignorews=ignorews))
+                               showfunc=showfunc, ignorews=ignorews,
+                               ignorewsamount=ignorewsamount,
+                               ignoreblanklines=ignoreblanklines))
 
 def trimuser(ui, name, rev, revcache):
     """trim the name of the user who committed a change"""
@@ -2968,6 +2978,10 @@ table = {
            _('show which function each change is in')),
           ('w', 'ignore-all-space', None,
            _('ignore white space when comparing lines')),
+          ('b', 'ignore-space-change', None,
+           _('ignore changes in the amount of white space')),
+          ('B', 'ignore-blank-lines', None,
+           _('ignore changes whose lines are all blank')),
           ('I', 'include', [], _('include names matching the given patterns')),
           ('X', 'exclude', [], _('exclude names matching the given patterns'))],
          _('hg diff [-a] [-I] [-X] [-r REV1 [-r REV2]] [FILE]...')),
diff -r 4716a58c8cd5 mercurial/hgweb/hgweb_mod.py
--- a/mercurial/hgweb/hgweb_mod.py	Tue Jun 27 11:32:37 2006 -0700
+++ b/mercurial/hgweb/hgweb_mod.py	Thu Jun 29 15:15:43 2006 +0200
@@ -133,21 +133,29 @@ class hgweb(object):
         diffopts = self.repo.ui.diffopts()
         showfunc = diffopts['showfunc']
         ignorews = diffopts['ignorews']
+        ignorewsamount = diffopts['ignorewsamount']
+        ignoreblanklines = diffopts['ignoreblanklines']
         for f in modified:
             to = r.file(f).read(mmap1[f])
             tn = r.file(f).read(mmap2[f])
             yield diffblock(mdiff.unidiff(to, date1, tn, date2, f,
-                            showfunc=showfunc, ignorews=ignorews), f, tn)
+                            showfunc=showfunc, ignorews=ignorews,
+                            ignorewsamount=ignorewsamount,
+                            ignoreblanklines=ignoreblanklines), f, tn)
         for f in added:
             to = None
             tn = r.file(f).read(mmap2[f])
             yield diffblock(mdiff.unidiff(to, date1, tn, date2, f,
-                            showfunc=showfunc, ignorews=ignorews), f, tn)
+                            showfunc=showfunc, ignorews=ignorews,
+                            ignorewsamount=ignorewsamount,
+                            ignoreblanklines=ignoreblanklines), f, tn)
         for f in removed:
             to = r.file(f).read(mmap1[f])
             tn = None
             yield diffblock(mdiff.unidiff(to, date1, tn, date2, f,
-                            showfunc=showfunc, ignorews=ignorews), f, tn)
+                            showfunc=showfunc, ignorews=ignorews,
+                            ignorewsamount=ignorewsamount,
+                            ignoreblanklines=ignoreblanklines), f, tn)
 
     def changelog(self, pos):
         def changenav(**map):
diff -r 4716a58c8cd5 mercurial/mdiff.py
--- a/mercurial/mdiff.py	Tue Jun 27 11:32:37 2006 -0700
+++ b/mercurial/mdiff.py	Thu Jun 29 15:15:43 2006 +0200
@@ -20,7 +20,8 @@ def splitnewlines(text):
     return lines
 
 def unidiff(a, ad, b, bd, fn, r=None, text=False,
-            showfunc=False, ignorews=False):
+            showfunc=False, ignorews=False, ignorewsamount=False,
+            ignoreblanklines=False):
 
     if not a and not b: return ""
     epoch = util.datestr((0, 0))
@@ -49,7 +50,9 @@ def unidiff(a, ad, b, bd, fn, r=None, te
         al = splitnewlines(a)
         bl = splitnewlines(b)
         l = list(bunidiff(a, b, al, bl, "a/" + fn, "b/" + fn,
-                          showfunc=showfunc, ignorews=ignorews))
+                          showfunc=showfunc, ignorews=ignorews,
+                          ignorewsamount=ignorewsamount,
+                          ignoreblanklines=ignoreblanklines))
         if not l: return ""
         # difflib uses a space, rather than a tab
         l[0] = "%s\t%s\n" % (l[0][:-2], ad)
@@ -72,8 +75,10 @@ def unidiff(a, ad, b, bd, fn, r=None, te
 # context is the number of context lines
 # showfunc enables diff -p output
 # ignorews ignores all whitespace changes in the diff
+# ignorewsamount ignores changes in the amount of whitespace
+# ignoreblanklines ignores changes whose lines are all blank
 def bunidiff(t1, t2, l1, l2, header1, header2, context=3, showfunc=False,
-             ignorews=False):
+             ignorews=False, ignorewsamount=False, ignoreblanklines=False):
     def contextend(l, len):
         ret = l + context
         if ret > len:
@@ -116,6 +121,11 @@ def bunidiff(t1, t2, l1, l2, header1, he
 
     if showfunc:
         funcre = re.compile('\w')
+    if ignorewsamount:
+        wsamountre = re.compile('[ \t]+')
+        wsappendedre = re.compile(' \n')
+    if ignoreblanklines:
+        wsblanklinesre = re.compile('\n')
     if ignorews:
         wsre = re.compile('[ \t]')
 
@@ -148,6 +158,20 @@ def bunidiff(t1, t2, l1, l2, header1, he
         # and deals with the special first match case described above
         if not old and not new:
             continue
+
+        if ignoreblanklines:
+            wsold = wsblanklinesre.sub('', "".join(old))
+            wsnew = wsblanklinesre.sub('', "".join(new))
+            if wsold == wsnew:
+                continue
+
+        if 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 ignorews:
             wsold = wsre.sub('', "".join(old))
diff -r 4716a58c8cd5 mercurial/ui.py
--- a/mercurial/ui.py	Tue Jun 27 11:32:37 2006 -0700
+++ b/mercurial/ui.py	Thu Jun 29 15:15:43 2006 +0200
@@ -172,7 +172,8 @@ class ui(object):
     def diffopts(self):
         if self.diffcache:
             return self.diffcache
-        result = {'showfunc': True, 'ignorews': False}
+        result = {'showfunc': True, 'ignorews': False,
+                  'ignorewsamount': False, 'ignoreblanklines': False}
         for key, value in self.configitems("diff"):
             if value:
                 result[key.lower()] = (value.lower() == 'true')
diff -r 4716a58c8cd5 tests/test-diff-ignore-whitespace
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-diff-ignore-whitespace	Thu Jun 29 15:15:43 2006 +0200
@@ -0,0 +1,84 @@
+#!/bin/sh
+
+# GNU diff is the reference for all of these results.
+
+hgdiff() {
+    hg diff "$@" | sed "s/\(\(---\|+++\) [a-zA-Z0-9_/.-]*\).*/\1/"
+}
+
+test_added_blank_lines() {
+    printf '\nhello world\n\ngoodbye world\n\n' >foo
+
+    echo '>>> two diffs showing three added lines <<<'
+    hgdiff
+    hgdiff -b
+
+    echo '>>> no diffs <<<'
+    hgdiff -B
+    hgdiff -Bb
+}
+
+test_added_horizontal_space_first_on_a_line() {
+    printf '\t hello world\ngoodbye world\n' >foo
+
+    echo '>>> four diffs showing added space first on the first line <<<'
+    hgdiff
+    hgdiff -b
+    hgdiff -B
+    hgdiff -Bb
+}
+
+test_added_horizontal_space_last_on_a_line() {
+    printf 'hello world\t \ngoodbye world\n' >foo
+
+    echo '>>> two diffs showing space appended to the first line <<<'
+    hgdiff
+    hgdiff -B
+
+    echo '>>> no diffs <<<'
+    hgdiff -b
+    hgdiff -Bb
+}
+
+test_added_horizontal_space_in_the_middle_of_a_word() {
+    printf 'hello world\ngood bye world\n' >foo
+
+    echo '>>> four diffs showing space inserted into "goodbye" <<<'
+    hgdiff
+    hgdiff -B
+    hgdiff -b
+    hgdiff -Bb
+}
+
+test_increased_horizontal_whitespace_amount() {
+    printf 'hello world\ngoodbye\t\t  \tworld\n' >foo
+
+    echo '>>> two diffs showing changed whitespace amount in the last line <<<'
+    hgdiff
+    hgdiff -B
+
+    echo '>>> no diffs <<<'
+    hgdiff -b
+    hgdiff -Bb
+}
+
+test_added_blank_line_with_horizontal_whitespace() {
+    printf 'hello world\n \t\ngoodbye world\n' >foo
+
+    echo '>>> four diffs showing added blank line w/horizontal space <<<'
+    hgdiff
+    hgdiff -B
+    hgdiff -b
+    hgdiff -Bb
+}
+
+hg init
+printf 'hello world\ngoodbye world\n' >foo
+hg ci -Amfoo -ufoo -d '0 0'
+
+test_added_blank_lines
+test_added_horizontal_space_first_on_a_line
+test_added_horizontal_space_last_on_a_line
+test_added_horizontal_space_in_the_middle_of_a_word
+test_increased_horizontal_whitespace_amount
+test_added_blank_line_with_horizontal_whitespace
diff -r 4716a58c8cd5 tests/test-diff-ignore-whitespace.out
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-diff-ignore-whitespace.out	Thu Jun 29 15:15:43 2006 +0200
@@ -0,0 +1,140 @@
+adding foo
+>>> two diffs showing three added lines <<<
+diff -r 540c40a65b78 foo
+--- a/foo
++++ b/foo
+@@ -1,2 +1,5 @@ hello world
++
+ hello world
++
+ goodbye world
++
+diff -r 540c40a65b78 foo
+--- a/foo
++++ b/foo
+@@ -1,2 +1,5 @@ hello world
++
+ hello world
++
+ goodbye world
++
+>>> no diffs <<<
+>>> four diffs showing added space first on the first line <<<
+diff -r 540c40a65b78 foo
+--- a/foo
++++ b/foo
+@@ -1,2 +1,2 @@ hello world
+-hello world
++	 hello world
+ goodbye world
+diff -r 540c40a65b78 foo
+--- a/foo
++++ b/foo
+@@ -1,2 +1,2 @@ hello world
+-hello world
++	 hello world
+ goodbye world
+diff -r 540c40a65b78 foo
+--- a/foo
++++ b/foo
+@@ -1,2 +1,2 @@ hello world
+-hello world
++	 hello world
+ goodbye world
+diff -r 540c40a65b78 foo
+--- a/foo
++++ b/foo
+@@ -1,2 +1,2 @@ hello world
+-hello world
++	 hello world
+ goodbye world
+>>> two diffs showing space appended to the first line <<<
+diff -r 540c40a65b78 foo
+--- a/foo
++++ b/foo
+@@ -1,2 +1,2 @@ hello world
+-hello world
++hello world	 
+ goodbye world
+diff -r 540c40a65b78 foo
+--- a/foo
++++ b/foo
+@@ -1,2 +1,2 @@ hello world
+-hello world
++hello world	 
+ goodbye world
+>>> no diffs <<<
+>>> four diffs showing space inserted into "goodbye" <<<
+diff -r 540c40a65b78 foo
+--- a/foo
++++ b/foo
+@@ -1,2 +1,2 @@ hello world
+ hello world
+-goodbye world
++good bye world
+diff -r 540c40a65b78 foo
+--- a/foo
++++ b/foo
+@@ -1,2 +1,2 @@ hello world
+ hello world
+-goodbye world
++good bye world
+diff -r 540c40a65b78 foo
+--- a/foo
++++ b/foo
+@@ -1,2 +1,2 @@ hello world
+ hello world
+-goodbye world
++good bye world
+diff -r 540c40a65b78 foo
+--- a/foo
++++ b/foo
+@@ -1,2 +1,2 @@ hello world
+ hello world
+-goodbye world
++good bye world
+>>> two diffs showing changed whitespace amount in the last line <<<
+diff -r 540c40a65b78 foo
+--- a/foo
++++ b/foo
+@@ -1,2 +1,2 @@ hello world
+ hello world
+-goodbye world
++goodbye		  	world
+diff -r 540c40a65b78 foo
+--- a/foo
++++ b/foo
+@@ -1,2 +1,2 @@ hello world
+ hello world
+-goodbye world
++goodbye		  	world
+>>> no diffs <<<
+>>> four diffs showing added blank line w/horizontal space <<<
+diff -r 540c40a65b78 foo
+--- a/foo
++++ b/foo
+@@ -1,2 +1,3 @@ hello world
+ hello world
++ 	
+ goodbye world
+diff -r 540c40a65b78 foo
+--- a/foo
++++ b/foo
+@@ -1,2 +1,3 @@ hello world
+ hello world
++ 	
+ goodbye world
+diff -r 540c40a65b78 foo
+--- a/foo
++++ b/foo
+@@ -1,2 +1,3 @@ hello world
+ hello world
++ 	
+ goodbye world
+diff -r 540c40a65b78 foo
+--- a/foo
++++ b/foo
+@@ -1,2 +1,3 @@ hello world
+ hello world
++ 	
+ goodbye world

-- 
 Haakon


More information about the Mercurial mailing list