[PATCH 3 of 4] diff: add --stat option for diffstat output

Brodie Rao dackze at gmail.com
Thu Jul 30 10:06:26 CDT 2009


# HG changeset patch
# User Brodie Rao <me+hg at dackz.net>
# Date 1248966179 14400
# Node ID 5862bde342a377fa66d789d25d37626dc779626a
# Parent  4e4d554a609b50494411693f6a390ffd711f4501
diff: add --stat option for diffstat output

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -98,6 +98,21 @@ def loglimit(opts):
         limit = sys.maxint
     return limit
 
+def diffstatwidth(opts):
+    '''get the value of --stat-width'''
+    width = opts.get('stat_width')
+    if width == 'auto':
+        width = util.termwidth()
+    elif width:
+        try:
+            width = int(width)
+        except ValueError:
+            raise util.Abort(_('width must be a positive integer or "auto"'))
+        if width <= 0: raise util.Abort(_('width must be positive'))
+    else:
+        width = 80
+    return width
+
 def remoteui(src, opts):
     'build a remote ui from ui or repo and opts'
     if hasattr(src, 'baseui'): # looks like a repository
diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -1088,6 +1088,7 @@ def diff(ui, repo, *pats, **opts):
 
     revs = opts.get('rev')
     change = opts.get('change')
+    diffstat = opts.get('stat')
 
     if revs and change:
         msg = _('cannot specify --rev and --change at the same time')
@@ -1098,10 +1099,18 @@ def diff(ui, repo, *pats, **opts):
     else:
         node1, node2 = cmdutil.revpair(repo, revs)
 
+    if diffstat:
+        opts['unified'] = '0'
+        opts['git'] = None
+
     m = cmdutil.match(repo, pats, opts)
     it = patch.diff(repo, node1, node2, match=m, opts=patch.diffopts(ui, opts))
-    for chunk in it:
-        ui.write(chunk)
+    if diffstat:
+        ui.write(patch.diffstat(util.iterlines(it),
+                                cmdutil.diffstatwidth(opts)))
+    else:
+        for chunk in it:
+            ui.write(chunk)
 
 def export(ui, repo, *changesets, **opts):
     """dump the header and diffs for one or more changesets
@@ -3160,7 +3169,10 @@ diffopts2 = [
      _('ignore changes in the amount of white space')),
     ('B', 'ignore-blank-lines', None,
      _('ignore changes whose lines are all blank')),
-    ('U', 'unified', '', _('number of lines of context to show'))
+    ('U', 'unified', '', _('number of lines of context to show')),
+    ('s', 'stat', None, _('output diffstat-style summary of changes')),
+    ('', 'stat-width', '80', _('diffstat output width (number of columns '
+                               'or auto)')),
 ]
 
 similarityopts = [


More information about the Mercurial-devel mailing list