-b/-B options to diff

Haakon Riiser haakon.riiser at fys.uio.no
Wed Jun 28 12:30:51 CDT 2006


Hello again,

Just wondering what happened to the patch quoted below.  Did you, or
anyone else, find the time to review it?

> [Bryan O'Sullivan]
> 
> > Also, "hg diff"'s -w is broken :-)
> 
> How?
> 
> > It hasn't been done, and I'm sure a patch would be welcome.
> 
> Here's a first draft: (barely tested, though :)
> 
> Like GNU diff, hg diff -Bb doesn't ignore changes in lines that only
> contain whitespace. That is, hg diff -Bb will show changes in the below
> example:
> 
>   ----- original file -----
>   'foo'
>   'bar'
>   -------------------------
> 
>   ----- new file -----
>   'foo'
>   ' '
>   'bar'
>   --------------------
> 
> Intuitively, this is a bug to me, but if GNU diff is the de facto
> standard, perhaps it's okay? :)
> 
> --- mercurial-0.9/mercurial/mdiff.py.orig	2006-05-10 23:56:42.000000000 +0200
> +++ mercurial-0.9/mercurial/mdiff.py	2006-05-25 10:56:02.000000000 +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,10 @@
>  
>      if showfunc:
>          funcre = re.compile('\w')
> +    if ignorewsamount:
> +        wsamountre = re.compile('[ \t]+')
> +    if ignoreblanklines:
> +        wsblanklinesre = re.compile('\n')
>      if ignorews:
>          wsre = re.compile('[ \t]')
>  
> @@ -149,6 +158,18 @@
>          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))
> +            wsnew = wsamountre.sub(' ', "".join(new))
> +            if wsold == wsnew:
> +                continue
> +
>          if ignorews:
>              wsold = wsre.sub('', "".join(old))
>              wsnew = wsre.sub('', "".join(new))
> --- mercurial-0.9/mercurial/commands.py.orig	2006-05-10 23:56:42.000000000 +0200
> +++ mercurial-0.9/mercurial/commands.py	2006-05-25 10:56:03.000000000 +0200
> @@ -373,23 +373,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, 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, 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, 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"""
> @@ -2882,6 +2892,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]...')),
> --- mercurial-0.9/mercurial/ui.py.orig	2006-05-10 23:56:42.000000000 +0200
> +++ mercurial-0.9/mercurial/ui.py	2006-05-25 10:56:04.000000000 +0200
> @@ -146,7 +146,8 @@
>      def diffopts(self):
>          if self.diffcache:
>              return self.diffcache
> -        ret = { 'showfunc' : True, 'ignorews' : False}
> +        ret = { 'showfunc' : True, 'ignorews' : False,
> +                'ignorewsamount' : False, 'ignoreblanklines' : False}
>          for x in self.configitems("diff"):
>              k = x[0].lower()
>              v = x[1]
> --- mercurial-0.9/mercurial/hgweb.py.orig	2006-05-10 23:56:42.000000000 +0200
> +++ mercurial-0.9/mercurial/hgweb.py	2006-05-25 10:56:04.000000000 +0200
> @@ -206,21 +206,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):
> 
-- 
 Haakon


More information about the Mercurial mailing list