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

Brodie Rao dackze at gmail.com
Mon Aug 10 09:24:10 CDT 2009


# HG changeset patch
# User Brodie Rao <me+hg at dackz.net>
# Date 1249913326 14400
# Node ID d0eed20ff768443c57c1cbe007f9aa4b97b6aaab
# Parent  ea68b90e36f6794d001f983ef32f19a1a30e4a92
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/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -1401,7 +1401,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'):
@@ -1414,7 +1415,8 @@ 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):
     output = []
@@ -1422,13 +1424,18 @@ def diffstat(lines, width=80):
 
     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
@@ -1438,14 +1445,18 @@ def diffstat(lines, width=80):
             return i
         return ((i - 1) * (graphwidth - 1) + maxtotal - 1) / (maxtotal - 1)
 
-    for filename, adds, removes in stats:
+    for filename, adds, removes, isbinary in stats:
         # If diffstat runs out of room it doesn't print anything, which
         # isn't very useful, so always print at least one + or - if there
         # were at least some changes
+        if isbinary:
+            count = 'Bin'
+        else:
+            count = adds + removes
         pluses = '+' * max(scale(adds), int(bool(adds)))
         minuses = '-' * max(scale(removes), int(bool(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'


More information about the Mercurial-devel mailing list