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

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


# HG changeset patch
# User Brodie Rao <me+hg at dackz.net>
# Date 1248966173 14400
# Node ID 4e4d554a609b50494411693f6a390ffd711f4501
# Parent  4076e49a0bf9555116bf4b6e5bb1fe0d420d4814
diffstat: mark binary files with Bin in the count column

diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -1390,7 +1390,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'):
@@ -1403,7 +1404,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 = []
@@ -1411,13 +1413,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
@@ -1427,14 +1434,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