[PATCH] churn: ability to display added/removed lines separately

Alexander Solovyov piranha at piranha.org.ua
Thu Oct 29 13:50:37 CDT 2009


# HG changeset patch
# User Alexander Solovyov <piranha at piranha.org.ua>
# Date 1256842224 -7200
# Node ID 760c2b7fabd08e2fd1788c79d1d525e7cee5b021
# Parent  f53c549237cadd78299872d77015a15901367bcc
churn: ability to display added/removed lines separately

diff --git a/hgext/churn.py b/hgext/churn.py
--- a/hgext/churn.py
+++ b/hgext/churn.py
@@ -11,7 +11,7 @@
 from mercurial.i18n import _
 from mercurial import patch, cmdutil, util, templater
 import sys, os
-import time, datetime
+import time, datetime, operator
 
 def maketemplater(ui, repo, tmpl):
     tmpl = templater.parsestring(tmpl, quoted=False)
@@ -23,14 +23,15 @@ def maketemplater(ui, repo, tmpl):
     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"""
@@ -74,7 +75,7 @@ def countrate(ui, repo, amap, *pats, **o
 
             ctx1 = parents[0]
             lines = changedlines(ui, repo, ctx1, ctx, fns)
-            rate[key] = rate.get(key, 0) + lines
+            rate[key] = map(operator.add, rate.get(key, (0, 0)), lines)
 
         if opts.get('progress'):
             count += 1
@@ -143,20 +144,34 @@ def churn(ui, repo, *pats, **opts):
     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') % (
+                pad(name, maxname), '+%s/-%s' % (added, removed),
+                '+' * charnum(added), '-' * charnum(removed))
+    else:
+        width -= 6
+        def format(name, count):
+            return "%s %6d %s" % (pad(name, maxname), sum(count),
+                                  '*' * charnum(sum(count)))
+
+    def charnum(count):
+        return int(round(count*width/maxcount))
+
+    for name, count in rate:
+        print format(name, count)
 
 
 cmdtable = {
@@ -169,6 +184,7 @@ cmdtable = {
               _('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]")),
diff --git a/tests/test-churn b/tests/test-churn
--- a/tests/test-churn
+++ b/tests/test-churn
@@ -50,6 +50,11 @@ COLUMNS=40 hg churn
 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
diff --git a/tests/test-churn.out b/tests/test-churn.out
--- a/tests/test-churn.out
+++ b/tests/test-churn.out
@@ -10,7 +10,7 @@ user1      3 ***************************
 user2      2 ******************************************
 % churn up to rev 2
 user2      2 ***************************************************************
-user1      1 *******************************
+user1      1 ********************************
 % churn with aliases
 alias3      3 **************************************************************
 alias1      3 **************************************************************
@@ -24,9 +24,13 @@ user3      3 ***********************
 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 


More information about the Mercurial-devel mailing list