[PATCH 3 of 3] diffstat: mark binary files with Bin in the count column

Brodie Rao dackze at gmail.com
Tue Sep 22 08:05:33 CDT 2009


# HG changeset patch
# User Brodie Rao <me+hg at dackz.net>
# Date 1253624442 14400
# Node ID 15d9e6d13d693c05fef6ccbec78bcc1dc0c3a246
# Parent  52ab347c5cde528ccf1a925894fc0b482327157a
diffstat: mark binary files with Bin in the count column

Normally, binary files simply have no graph data. This can be a little
confusing, so binary files are marked with Bin to let the user know that
something indeed changed, even if the graph doesn't indicate this.

git diff --stat does something similar, though it also includes the old and
new file sizes.

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -1101,11 +1101,12 @@ def diff(ui, repo, *pats, **opts):
 
     if stat:
         opts['unified'] = '0'
+    diffopts = patch.diffopts(ui, opts)
 
     m = cmdutil.match(repo, pats, opts)
-    it = patch.diff(repo, node1, node2, match=m, opts=patch.diffopts(ui, opts))
+    it = patch.diff(repo, node1, node2, match=m, opts=diffopts)
     if stat:
-        statopts = {}
+        statopts = {'git': diffopts.git}
         if ui.interactive():
             statopts['width'] = util.termwidth()
         ui.write(patch.diffstat(util.iterlines(it), **statopts))
diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -1371,7 +1371,8 @@ def diffstatdata(lines):
     for line in lines:
         if line.startswith('diff'):
             if filename:
-                yield (filename, adds, removes)
+                isbinary = adds == 0 and removes == 0
+                yield (filename, adds, removes, isbinary)
             # set numbers to 0 anyway when starting new file
             adds, removes = 0, 0
             if line.startswith('diff --git'):
@@ -1384,21 +1385,27 @@ def diffstatdata(lines):
         elif line.startswith('-') and not line.startswith('---'):
             removes += 1
     if filename:
-        yield (filename, adds, removes)
+        isbinary = adds == 0 and removes == 0
+        yield (filename, adds, removes, isbinary)
 
-def diffstat(lines, width=80):
+def diffstat(lines, width=80, git=False):
     output = []
     stats = list(diffstatdata(lines))
 
     maxtotal, maxname = 0, 0
     totaladds, totalremoves = 0, 0
-    for filename, adds, removes in stats:
+    hasbinary = False
+    for filename, adds, removes, isbinary in stats:
         totaladds += adds
         totalremoves += removes
         maxname = max(maxname, len(filename))
         maxtotal = max(maxtotal, adds+removes)
+        if isbinary:
+            hasbinary = True
 
     countwidth = len(str(maxtotal))
+    if hasbinary and countwidth < 3:
+        countwidth = 3
     graphwidth = width - countwidth - maxname - 6
     if graphwidth < 10:
         graphwidth = 10
@@ -1411,11 +1418,15 @@ def diffstat(lines, width=80):
         # if there were at least some changes.
         return max(i * graphwidth // maxtotal, int(bool(i)))
 
-    for filename, adds, removes in stats:
+    for filename, adds, removes, isbinary in stats:
+        if git and isbinary:
+            count = 'Bin'
+        else:
+            count = adds + removes
         pluses = '+' * scale(adds)
         minuses = '-' * scale(removes)
-        output.append(' %-*s |  %*.d %s%s\n' % (maxname, filename, countwidth,
-                                                adds+removes, pluses, minuses))
+        output.append(' %-*s |  %*s %s%s\n' % (maxname, filename, countwidth,
+                                               count, pluses, minuses))
 
     if stats:
         output.append(_(' %d files changed, %d insertions(+), %d deletions(-)\n')
diff --git a/tests/test-diffstat b/tests/test-diffstat
--- a/tests/test-diffstat
+++ b/tests/test-diffstat
@@ -21,3 +21,11 @@ EOF
 
 echo '% narrow diffstat'
 hg diff --stat
+
+hg ci -m appenda
+
+printf '%b' '\x00' > b
+hg add b
+
+echo '% binary diffstat'
+hg diff --stat
diff --git a/tests/test-diffstat.out b/tests/test-diffstat.out
--- a/tests/test-diffstat.out
+++ b/tests/test-diffstat.out
@@ -7,3 +7,6 @@
 % narrow diffstat
  a |  3 +++
  1 files changed, 3 insertions(+), 0 deletions(-)
+% binary diffstat
+ b |  Bin 
+ 1 files changed, 0 insertions(+), 0 deletions(-)


More information about the Mercurial-devel mailing list