diff: add --stat for diffstat output
authorBrodie Rao <me+hg@dackz.net>
Sun, 25 Oct 2009 02:52:35 +0200
changeset 9640 9e76232fbfbe
parent 9639 5384a22ab698
child 9641 9b99f158348a
diff: add --stat for diffstat output diff/qdiff --stat invokes patch.diffstat() on the diff output. When in interactive mode, the output's maximum width is determined by the terminal's width.
hgext/mq.py
mercurial/commands.py
tests/test-debugcomplete.out
tests/test-diffstat
tests/test-diffstat.out
tests/test-help.out
--- a/hgext/mq.py	Sun Oct 25 02:35:35 2009 +0200
+++ b/hgext/mq.py	Sun Oct 25 02:52:35 2009 +0200
@@ -420,11 +420,19 @@
 
     def printdiff(self, repo, node1, node2=None, files=None,
                   fp=None, changes=None, opts={}):
+        stat = opts.get('stat')
+        if stat:
+            opts['unified'] = '0'
+
         m = cmdutil.match(repo, files, opts)
         chunks = patch.diff(repo, node1, node2, m, changes, self.diffopts())
         write = fp is None and repo.ui.write or fp.write
-        for chunk in chunks:
-            write(chunk)
+        if stat:
+            width = self.ui.interactive() and util.termwidth() or 80
+            write(patch.diffstat(util.iterlines(chunks), width=width))
+        else:
+            for chunk in chunks:
+                write(chunk)
 
     def mergeone(self, repo, mergeq, head, patch, rev):
         # first try just applying the patch
--- a/mercurial/commands.py	Sun Oct 25 02:35:35 2009 +0200
+++ b/mercurial/commands.py	Sun Oct 25 02:52:35 2009 +0200
@@ -1085,6 +1085,7 @@
 
     revs = opts.get('rev')
     change = opts.get('change')
+    stat = opts.get('stat')
 
     if revs and change:
         msg = _('cannot specify --rev and --change at the same time')
@@ -1095,10 +1096,17 @@
     else:
         node1, node2 = cmdutil.revpair(repo, revs)
 
+    if stat:
+        opts['unified'] = '0'
+
     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 stat:
+        width = ui.interactive() and util.termwidth() or 80
+        ui.write(patch.diffstat(util.iterlines(it), width=width))
+    else:
+        for chunk in it:
+            ui.write(chunk)
 
 def export(ui, repo, *changesets, **opts):
     """dump the header and diffs for one or more changesets
@@ -3260,7 +3268,8 @@
      _('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')),
+    ('', 'stat', None, _('output diffstat-style summary of changes')),
 ]
 
 similarityopts = [
--- a/tests/test-debugcomplete.out	Sun Oct 25 02:35:35 2009 +0200
+++ b/tests/test-debugcomplete.out	Sun Oct 25 02:52:35 2009 +0200
@@ -167,7 +167,7 @@
 annotate: rev, follow, text, user, date, number, changeset, line-number, include, exclude
 clone: noupdate, rev, pull, uncompressed, ssh, remotecmd
 commit: addremove, close-branch, include, exclude, message, logfile, date, user
-diff: rev, change, text, git, nodates, show-function, ignore-all-space, ignore-space-change, ignore-blank-lines, unified, include, exclude
+diff: rev, change, text, git, nodates, show-function, ignore-all-space, ignore-space-change, ignore-blank-lines, unified, stat, include, exclude
 export: output, switch-parent, text, git, nodates
 forget: include, exclude
 init: ssh, remotecmd
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-diffstat	Sun Oct 25 02:52:35 2009 +0200
@@ -0,0 +1,31 @@
+#!/bin/sh
+
+hg init repo
+cd repo
+i=0; while (( $i < 213 )); do echo a >> a; i=$(($i + 1)); done
+hg add a
+
+echo '% wide diffstat'
+hg diff --stat
+
+echo '% diffstat width'
+COLUMNS=24 hg diff --config ui.interactive=true --stat
+
+hg ci -m adda
+
+cat >> a <<EOF
+a
+a
+a
+EOF
+
+echo '% narrow diffstat'
+hg diff --stat
+
+hg ci -m appenda
+
+printf '%b' '\x00' > b
+hg add b
+
+echo '% binary diffstat'
+hg diff --stat
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-diffstat.out	Sun Oct 25 02:52:35 2009 +0200
@@ -0,0 +1,12 @@
+% wide diffstat
+ a |  213 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ 1 files changed, 213 insertions(+), 0 deletions(-)
+% diffstat width
+ a |  213 ++++++++++++++
+ 1 files changed, 213 insertions(+), 0 deletions(-)
+% narrow diffstat
+ a |  3 +++
+ 1 files changed, 3 insertions(+), 0 deletions(-)
+% binary diffstat
+ b |  0 
+ 1 files changed, 0 insertions(+), 0 deletions(-)
--- a/tests/test-help.out	Sun Oct 25 02:35:35 2009 +0200
+++ b/tests/test-help.out	Sun Oct 25 02:52:35 2009 +0200
@@ -241,6 +241,7 @@
  -b --ignore-space-change  ignore changes in the amount of white space
  -B --ignore-blank-lines   ignore changes whose lines are all blank
  -U --unified              number of lines of context to show
+    --stat                 output diffstat-style summary of changes
  -I --include              include names matching the given patterns
  -X --exclude              exclude names matching the given patterns