diff: add -b/-B options
authorHaakon Riiser <haakon.riiser@fys.uio.no>
Thu, 29 Jun 2006 15:16:25 +0200
changeset 2580 a20a1bb0c396
parent 2579 0875cda033fd
child 2581 54b152379589
diff: add -b/-B options
mercurial/commands.py
mercurial/hgweb/hgweb_mod.py
mercurial/mdiff.py
mercurial/ui.py
tests/test-help.out
--- a/mercurial/commands.py	Sun Jul 09 01:30:30 2006 +0200
+++ b/mercurial/commands.py	Thu Jun 29 15:16:25 2006 +0200
@@ -405,23 +405,33 @@
     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"""
@@ -3018,6 +3028,10 @@
            _('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]...')),
--- a/mercurial/hgweb/hgweb_mod.py	Sun Jul 09 01:30:30 2006 +0200
+++ b/mercurial/hgweb/hgweb_mod.py	Thu Jun 29 15:16:25 2006 +0200
@@ -133,21 +133,29 @@
         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):
--- a/mercurial/mdiff.py	Sun Jul 09 01:30:30 2006 +0200
+++ b/mercurial/mdiff.py	Thu Jun 29 15:16:25 2006 +0200
@@ -20,7 +20,8 @@
     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 @@
         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 @@
 # 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 @@
 
     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]')
 
@@ -149,6 +159,20 @@
         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))
             wsnew = wsre.sub('', "".join(new))
--- a/mercurial/ui.py	Sun Jul 09 01:30:30 2006 +0200
+++ b/mercurial/ui.py	Thu Jun 29 15:16:25 2006 +0200
@@ -172,7 +172,8 @@
     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')
--- a/tests/test-help.out	Sun Jul 09 01:30:30 2006 +0200
+++ b/tests/test-help.out	Thu Jun 29 15:16:25 2006 +0200
@@ -173,12 +173,14 @@
 
 options:
 
- -r --rev               revision
- -a --text              treat all files as text
- -p --show-function     show which function each change is in
- -w --ignore-all-space  ignore white space when comparing lines
- -I --include           include names matching the given patterns
- -X --exclude           exclude names matching the given patterns
+ -r --rev                  revision
+ -a --text                 treat all files as text
+ -p --show-function        show which function each change is in
+ -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
+ -I --include              include names matching the given patterns
+ -X --exclude              exclude names matching the given patterns
 hg status [OPTION]... [FILE]...
 
 show changed files in the working directory