diffstat: with --git, mark binary files with Bin
authorBrodie Rao <me+hg@dackz.net>
Sun, 25 Oct 2009 02:53:33 +0200
changeset 9642 7d17794f08a9
parent 9641 9b99f158348a
child 9643 013cc052a926
diffstat: with --git, mark binary files with Bin Normally, diffs without any text insertions or deletions are reported as having 0 lines changed by stock diffstat. Compatibility is preserved with stock diffstat in this case, but when using --git, binary files are marked with Bin as a means of clarification. git diff --stat does something similar, though it also includes the old and new file sizes.
hgext/mq.py
mercurial/commands.py
mercurial/patch.py
tests/test-diffstat
tests/test-diffstat.out
--- a/hgext/mq.py	Sun Oct 25 02:52:36 2009 +0200
+++ b/hgext/mq.py	Sun Oct 25 02:53:33 2009 +0200
@@ -429,7 +429,8 @@
         write = fp is None and repo.ui.write or fp.write
         if stat:
             width = self.ui.interactive() and util.termwidth() or 80
-            write(patch.diffstat(util.iterlines(chunks), width=width))
+            write(patch.diffstat(util.iterlines(chunks), width=width,
+                                 git=self.diffopts().git))
         else:
             for chunk in chunks:
                 write(chunk)
--- a/mercurial/commands.py	Sun Oct 25 02:52:36 2009 +0200
+++ b/mercurial/commands.py	Sun Oct 25 02:53:33 2009 +0200
@@ -1098,12 +1098,14 @@
 
     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:
         width = ui.interactive() and util.termwidth() or 80
-        ui.write(patch.diffstat(util.iterlines(it), width=width))
+        ui.write(patch.diffstat(util.iterlines(it), width=width,
+                                git=diffopts.git))
     else:
         for chunk in it:
             ui.write(chunk)
--- a/mercurial/patch.py	Sun Oct 25 02:52:36 2009 +0200
+++ b/mercurial/patch.py	Sun Oct 25 02:53:33 2009 +0200
@@ -1364,7 +1364,8 @@
     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'):
@@ -1377,21 +1378,27 @@
         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
@@ -1404,11 +1411,15 @@
         # 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 |  %*s %s%s\n' % (maxname, filename, countwidth,
-                                               adds+removes, pluses, minuses))
+                                               count, pluses, minuses))
 
     if stats:
         output.append(_(' %d files changed, %d insertions(+), %d deletions(-)\n')
--- a/tests/test-diffstat	Sun Oct 25 02:52:36 2009 +0200
+++ b/tests/test-diffstat	Sun Oct 25 02:53:33 2009 +0200
@@ -29,3 +29,6 @@
 
 echo '% binary diffstat'
 hg diff --stat
+
+echo '% binary git diffstat'
+hg diff --stat --git
--- a/tests/test-diffstat.out	Sun Oct 25 02:52:36 2009 +0200
+++ b/tests/test-diffstat.out	Sun Oct 25 02:53:33 2009 +0200
@@ -8,5 +8,8 @@
  a |  3 +++
  1 files changed, 3 insertions(+), 0 deletions(-)
 % binary diffstat
- b |  0 
+ b |    0 
  1 files changed, 0 insertions(+), 0 deletions(-)
+% binary git diffstat
+ b |  Bin 
+ 1 files changed, 0 insertions(+), 0 deletions(-)