churn: ability to display added/removed lines separately
authorAlexander Solovyov <piranha@piranha.org.ua>
Thu, 29 Oct 2009 20:50:24 +0200
changeset 9669 9b127e888640
parent 9668 2c24471d478c
child 9670 7d56b6ffef72
churn: ability to display added/removed lines separately
hgext/churn.py
tests/test-churn
tests/test-churn.out
--- a/hgext/churn.py	Fri Oct 30 09:54:39 2009 +0100
+++ b/hgext/churn.py	Thu Oct 29 20:50:24 2009 +0200
@@ -23,14 +23,15 @@
     return t
 
 def changedlines(ui, repo, ctx1, ctx2, fns):
-    lines = 0
+    added, removed = 0, 0
     fmatch = cmdutil.matchfiles(repo, fns)
     diff = ''.join(patch.diff(repo, ctx1.node(), ctx2.node(), fmatch))
     for l in diff.split('\n'):
-        if (l.startswith("+") and not l.startswith("+++ ") or
-            l.startswith("-") and not l.startswith("--- ")):
-            lines += 1
-    return lines
+        if l.startswith("+") and not l.startswith("+++ "):
+            added += 1
+        elif l.startswith("-") and not l.startswith("--- "):
+            removed += 1
+    return (added, removed)
 
 def countrate(ui, repo, amap, *pats, **opts):
     """Calculate stats"""
@@ -71,7 +72,7 @@
 
             ctx1 = parents[0]
             lines = changedlines(ui, repo, ctx1, ctx, fns)
-            rate[key] = rate.get(key, 0) + lines
+            rate[key] = [r + l for r, l in zip(rate.get(key, (0, 0)), lines)]
 
         if opts.get('progress'):
             count += 1
@@ -143,20 +144,35 @@
     if not rate:
         return
 
-    sortkey = ((not opts.get('sort')) and (lambda x: -x[1]) or None)
+    sortkey = ((not opts.get('sort')) and (lambda x: -sum(x[1])) or None)
     rate.sort(key=sortkey)
 
     # Be careful not to have a zero maxcount (issue833)
-    maxcount = float(max(v for k, v in rate)) or 1.0
+    maxcount = float(max(sum(v) for k, v in rate)) or 1.0
     maxname = max(len(k) for k, v in rate)
 
     ttywidth = util.termwidth()
     ui.debug("assuming %i character terminal\n" % ttywidth)
-    width = ttywidth - maxname - 2 - 6 - 2 - 2
+    width = ttywidth - maxname - 2 - 2 - 2
 
-    for date, count in rate:
-        print "%s %6d %s" % (pad(date, maxname), count,
-                             "*" * int(count * width / maxcount))
+    if opts.get('diffstat'):
+        width -= 15
+        def format(name, (added, removed)):
+            return "%s %15s %s%s\n" % (pad(name, maxname),
+                                       '+%d/-%d' % (added, removed),
+                                       '+' * charnum(added),
+                                       '-' * charnum(removed))
+    else:
+        width -= 6
+        def format(name, count):
+            return "%s %6d %s\n" % (pad(name, maxname), sum(count),
+                                    '*' * charnum(sum(count)))
+
+    def charnum(count):
+        return int(round(count*width/maxcount))
+
+    for name, count in rate:
+        ui.write(format(name, count))
 
 
 cmdtable = {
@@ -169,6 +185,7 @@
               _('strftime-compatible format for grouping by date')),
           ('c', 'changesets', False, _('count rate by number of changesets')),
           ('s', 'sort', False, _('sort by key (default: sort by count)')),
+          ('', 'diffstat', False, _('display added/removed lines separately')),
           ('', 'aliases', '', _('file with email aliases')),
           ('', 'progress', None, _('show progress'))],
          _("hg churn [-d DATE] [-r REV] [--aliases FILE] [--progress] [FILE]")),
--- a/tests/test-churn	Fri Oct 30 09:54:39 2009 +0100
+++ b/tests/test-churn	Thu Oct 29 20:50:24 2009 +0200
@@ -50,6 +50,11 @@
 echo % churn by hour
 hg churn -f '%H' -s
 
+echo % churn with separated added/removed lines
+hg rm d/g/f2.txt
+hg ci -Am "removed d/g/f2.txt" -u user1 -d 14:00 d/g/f2.txt
+hg churn --diffstat
+
 cd ..
 
 # issue 833: ZeroDivisionError
--- a/tests/test-churn.out	Fri Oct 30 09:54:39 2009 +0100
+++ b/tests/test-churn.out	Thu Oct 29 20:50:24 2009 +0200
@@ -10,7 +10,7 @@
 user2      2 ******************************************
 % churn up to rev 2
 user2      2 ***************************************************************
-user1      1 *******************************
+user1      1 ********************************
 % churn with aliases
 alias3      3 **************************************************************
 alias1      3 **************************************************************
@@ -24,9 +24,13 @@
 user1      3 ***********************
 user2      2 ***************
 % churn by hour
-06      1 ****************
+06      1 *****************
 09      2 *********************************
 12      4 ******************************************************************
-13      1 ****************
+13      1 *****************
+% churn with separated added/removed lines
+user1           +3/-1 +++++++++++++++++++++++++++++++++++++++++--------------
+user3           +3/-0 +++++++++++++++++++++++++++++++++++++++++
+user2           +2/-0 +++++++++++++++++++++++++++
 adding foo
 test      0