hgext/churn.py
author Dirkjan Ochtman <dirkjan@ochtman.nl>
Thu, 21 Aug 2008 19:05:18 +0200
changeset 6930 a58a611c320f
parent 6843 b114a8c7998f
child 6955 a9411e29773f
permissions -rw-r--r--
merge with crew-stable
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3040
f74077473b36 Churn extension
Josef "Jeff" Sipek <jeff@josefsipek.net>
parents:
diff changeset
     1
# churn.py - create a graph showing who changed the most lines
f74077473b36 Churn extension
Josef "Jeff" Sipek <jeff@josefsipek.net>
parents:
diff changeset
     2
#
f74077473b36 Churn extension
Josef "Jeff" Sipek <jeff@josefsipek.net>
parents:
diff changeset
     3
# Copyright 2006 Josef "Jeff" Sipek <jeffpc@josefsipek.net>
f74077473b36 Churn extension
Josef "Jeff" Sipek <jeff@josefsipek.net>
parents:
diff changeset
     4
#
f74077473b36 Churn extension
Josef "Jeff" Sipek <jeff@josefsipek.net>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms
f74077473b36 Churn extension
Josef "Jeff" Sipek <jeff@josefsipek.net>
parents:
diff changeset
     6
# of the GNU General Public License, incorporated herein by reference.
6666
53465a7464e2 convert comments to docstrings in a bunch of extensions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6598
diff changeset
     7
'''allow graphing the number of lines changed per contributor'''
3040
f74077473b36 Churn extension
Josef "Jeff" Sipek <jeff@josefsipek.net>
parents:
diff changeset
     8
3045
c0be8990e819 Add revision range support
Brendan Cully <brendan@kublai.com>
parents: 3043
diff changeset
     9
from mercurial.i18n import gettext as _
6759
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
    10
from mercurial import patch, cmdutil, util, node
4955
9bbc0217209b churn: get current terminal width if possible
Christian Ebert <blacktrash@gmx.net>
parents: 3963
diff changeset
    11
import os, sys
9bbc0217209b churn: get current terminal width if possible
Christian Ebert <blacktrash@gmx.net>
parents: 3963
diff changeset
    12
9bbc0217209b churn: get current terminal width if possible
Christian Ebert <blacktrash@gmx.net>
parents: 3963
diff changeset
    13
def get_tty_width():
9bbc0217209b churn: get current terminal width if possible
Christian Ebert <blacktrash@gmx.net>
parents: 3963
diff changeset
    14
    if 'COLUMNS' in os.environ:
9bbc0217209b churn: get current terminal width if possible
Christian Ebert <blacktrash@gmx.net>
parents: 3963
diff changeset
    15
        try:
9bbc0217209b churn: get current terminal width if possible
Christian Ebert <blacktrash@gmx.net>
parents: 3963
diff changeset
    16
            return int(os.environ['COLUMNS'])
9bbc0217209b churn: get current terminal width if possible
Christian Ebert <blacktrash@gmx.net>
parents: 3963
diff changeset
    17
        except ValueError:
9bbc0217209b churn: get current terminal width if possible
Christian Ebert <blacktrash@gmx.net>
parents: 3963
diff changeset
    18
            pass
9bbc0217209b churn: get current terminal width if possible
Christian Ebert <blacktrash@gmx.net>
parents: 3963
diff changeset
    19
    try:
5419
041bd297f01e churn: simplify code to get terminal width
Christian Ebert <blacktrash@gmx.net>
parents: 4955
diff changeset
    20
        import termios, array, fcntl
4955
9bbc0217209b churn: get current terminal width if possible
Christian Ebert <blacktrash@gmx.net>
parents: 3963
diff changeset
    21
        for dev in (sys.stdout, sys.stdin):
9bbc0217209b churn: get current terminal width if possible
Christian Ebert <blacktrash@gmx.net>
parents: 3963
diff changeset
    22
            try:
9bbc0217209b churn: get current terminal width if possible
Christian Ebert <blacktrash@gmx.net>
parents: 3963
diff changeset
    23
                fd = dev.fileno()
9bbc0217209b churn: get current terminal width if possible
Christian Ebert <blacktrash@gmx.net>
parents: 3963
diff changeset
    24
                if not os.isatty(fd):
9bbc0217209b churn: get current terminal width if possible
Christian Ebert <blacktrash@gmx.net>
parents: 3963
diff changeset
    25
                    continue
5419
041bd297f01e churn: simplify code to get terminal width
Christian Ebert <blacktrash@gmx.net>
parents: 4955
diff changeset
    26
                arri = fcntl.ioctl(fd, termios.TIOCGWINSZ, '\0' * 8)
041bd297f01e churn: simplify code to get terminal width
Christian Ebert <blacktrash@gmx.net>
parents: 4955
diff changeset
    27
                return array.array('h', arri)[1]
4955
9bbc0217209b churn: get current terminal width if possible
Christian Ebert <blacktrash@gmx.net>
parents: 3963
diff changeset
    28
            except ValueError:
9bbc0217209b churn: get current terminal width if possible
Christian Ebert <blacktrash@gmx.net>
parents: 3963
diff changeset
    29
                pass
9bbc0217209b churn: get current terminal width if possible
Christian Ebert <blacktrash@gmx.net>
parents: 3963
diff changeset
    30
    except ImportError:
9bbc0217209b churn: get current terminal width if possible
Christian Ebert <blacktrash@gmx.net>
parents: 3963
diff changeset
    31
        pass
9bbc0217209b churn: get current terminal width if possible
Christian Ebert <blacktrash@gmx.net>
parents: 3963
diff changeset
    32
    return 80
3040
f74077473b36 Churn extension
Josef "Jeff" Sipek <jeff@josefsipek.net>
parents:
diff changeset
    33
6759
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
    34
def countrevs(ui, repo, amap, revs, progress=False):
3040
f74077473b36 Churn extension
Josef "Jeff" Sipek <jeff@josefsipek.net>
parents:
diff changeset
    35
    stats = {}
6759
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
    36
    count = pct = 0
3045
c0be8990e819 Add revision range support
Brendan Cully <brendan@kublai.com>
parents: 3043
diff changeset
    37
    if not revs:
6759
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
    38
        revs = range(len(repo))
3050
dd1a142988d3 [churn] progress meter
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 3049
diff changeset
    39
3045
c0be8990e819 Add revision range support
Brendan Cully <brendan@kublai.com>
parents: 3043
diff changeset
    40
    for rev in revs:
6759
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
    41
        ctx2 = repo[rev]
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
    42
        parents = ctx2.parents()
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
    43
        if len(parents) > 1:
3049
461573aa02ef [churn] Ignore merge csets
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 3048
diff changeset
    44
            ui.note(_('Revision %d is a merge, ignoring...\n') % (rev,))
3050
dd1a142988d3 [churn] progress meter
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 3049
diff changeset
    45
            continue
3049
461573aa02ef [churn] Ignore merge csets
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 3048
diff changeset
    46
6759
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
    47
        ctx1 = parents[0]
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
    48
        lines = 0
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
    49
        ui.pushbuffer()
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
    50
        patch.diff(repo, ctx1.node(), ctx2.node())
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
    51
        diff = ui.popbuffer()
3040
f74077473b36 Churn extension
Josef "Jeff" Sipek <jeff@josefsipek.net>
parents:
diff changeset
    52
6759
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
    53
        for l in diff.split('\n'):
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
    54
            if (l.startswith("+") and not l.startswith("+++ ") or
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
    55
                l.startswith("-") and not l.startswith("--- ")):
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
    56
                lines += 1
3040
f74077473b36 Churn extension
Josef "Jeff" Sipek <jeff@josefsipek.net>
parents:
diff changeset
    57
6759
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
    58
        user = util.email(ctx2.user())
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
    59
        user = amap.get(user, user) # remap
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
    60
        stats[user] = stats.get(user, 0) + lines
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
    61
        ui.debug("rev %d: %d lines by %s\n" % (rev, lines, user))
3040
f74077473b36 Churn extension
Josef "Jeff" Sipek <jeff@josefsipek.net>
parents:
diff changeset
    62
3050
dd1a142988d3 [churn] progress meter
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 3049
diff changeset
    63
        if progress:
6759
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
    64
            count += 1
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
    65
            newpct = int(100.0 * count / max(len(revs), 1))
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
    66
            if pct < newpct:
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
    67
                pct = newpct
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
    68
                ui.write("\rGenerating stats: %d%%" % pct)
3050
dd1a142988d3 [churn] progress meter
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 3049
diff changeset
    69
                sys.stdout.flush()
dd1a142988d3 [churn] progress meter
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 3049
diff changeset
    70
dd1a142988d3 [churn] progress meter
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 3049
diff changeset
    71
    if progress:
5989
a7817ad608ea added \r for progress counting in churn extension
Armin Ronacher <armin.ronacher@active-4.com>
parents: 5976
diff changeset
    72
        ui.write("\r")
3050
dd1a142988d3 [churn] progress meter
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 3049
diff changeset
    73
        sys.stdout.flush()
dd1a142988d3 [churn] progress meter
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 3049
diff changeset
    74
3040
f74077473b36 Churn extension
Josef "Jeff" Sipek <jeff@josefsipek.net>
parents:
diff changeset
    75
    return stats
f74077473b36 Churn extension
Josef "Jeff" Sipek <jeff@josefsipek.net>
parents:
diff changeset
    76
3045
c0be8990e819 Add revision range support
Brendan Cully <brendan@kublai.com>
parents: 3043
diff changeset
    77
def churn(ui, repo, **opts):
6666
53465a7464e2 convert comments to docstrings in a bunch of extensions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6598
diff changeset
    78
    '''graphs the number of lines changed
53465a7464e2 convert comments to docstrings in a bunch of extensions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6598
diff changeset
    79
53465a7464e2 convert comments to docstrings in a bunch of extensions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6598
diff changeset
    80
    The map file format used to specify aliases is fairly simple:
53465a7464e2 convert comments to docstrings in a bunch of extensions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6598
diff changeset
    81
53465a7464e2 convert comments to docstrings in a bunch of extensions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6598
diff changeset
    82
    <alias email> <actual email>'''
3223
53e843840349 Whitespace/Tab cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3090
diff changeset
    83
3040
f74077473b36 Churn extension
Josef "Jeff" Sipek <jeff@josefsipek.net>
parents:
diff changeset
    84
    def pad(s, l):
6759
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
    85
        return (s + " " * l)[:l]
3223
53e843840349 Whitespace/Tab cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3090
diff changeset
    86
3040
f74077473b36 Churn extension
Josef "Jeff" Sipek <jeff@josefsipek.net>
parents:
diff changeset
    87
    amap = {}
3045
c0be8990e819 Add revision range support
Brendan Cully <brendan@kublai.com>
parents: 3043
diff changeset
    88
    aliases = opts.get('aliases')
3040
f74077473b36 Churn extension
Josef "Jeff" Sipek <jeff@josefsipek.net>
parents:
diff changeset
    89
    if aliases:
6759
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
    90
        for l in open(aliases, "r"):
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
    91
            l = l.strip()
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
    92
            alias, actual = l.split()
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
    93
            amap[alias] = actual
3040
f74077473b36 Churn extension
Josef "Jeff" Sipek <jeff@josefsipek.net>
parents:
diff changeset
    94
6762
f67d1468ac50 util: add sort helper
Matt Mackall <mpm@selenic.com>
parents: 6759
diff changeset
    95
    revs = util.sort([int(r) for r in cmdutil.revrange(repo, opts['rev'])])
6759
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
    96
    stats = countrevs(ui, repo, amap, revs, opts.get('progress'))
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
    97
    if not stats:
5588
083b6e3142a2 churn: avoid division by zero
Matt Mackall <mpm@selenic.com>
parents: 5482
diff changeset
    98
        return
3040
f74077473b36 Churn extension
Josef "Jeff" Sipek <jeff@josefsipek.net>
parents:
diff changeset
    99
6762
f67d1468ac50 util: add sort helper
Matt Mackall <mpm@selenic.com>
parents: 6759
diff changeset
   100
    stats = util.sort([(-l, u, l) for u,l in stats.items()])
6759
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
   101
    maxchurn = float(max(1, stats[0][2]))
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
   102
    maxuser = max([len(u) for k, u, l in stats])
3040
f74077473b36 Churn extension
Josef "Jeff" Sipek <jeff@josefsipek.net>
parents:
diff changeset
   103
6759
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
   104
    ttywidth = get_tty_width()
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
   105
    ui.debug(_("assuming %i character terminal\n") % ttywidth)
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
   106
    width = ttywidth - maxuser - 2 - 6 - 2 - 2
6223
bab6c8f2bb1a churn: show comitter email addresses unclipped (bug 1023)
Stephen Deasey <sdeasey@gmail.com>
parents: 6212
diff changeset
   107
6759
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
   108
    for k, user, churn in stats:
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
   109
        print "%s %6d %s" % (pad(user, maxuser), churn,
9d2ab50803e9 churn: major refactor
Matt Mackall <mpm@selenic.com>
parents: 6750
diff changeset
   110
                             "*" * int(churn * width / maxchurn))
3040
f74077473b36 Churn extension
Josef "Jeff" Sipek <jeff@josefsipek.net>
parents:
diff changeset
   111
f74077473b36 Churn extension
Josef "Jeff" Sipek <jeff@josefsipek.net>
parents:
diff changeset
   112
cmdtable = {
f74077473b36 Churn extension
Josef "Jeff" Sipek <jeff@josefsipek.net>
parents:
diff changeset
   113
    "churn":
f74077473b36 Churn extension
Josef "Jeff" Sipek <jeff@josefsipek.net>
parents:
diff changeset
   114
    (churn,
3045
c0be8990e819 Add revision range support
Brendan Cully <brendan@kublai.com>
parents: 3043
diff changeset
   115
     [('r', 'rev', [], _('limit statistics to the specified revisions')),
3050
dd1a142988d3 [churn] progress meter
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 3049
diff changeset
   116
      ('', 'aliases', '', _('file with email aliases')),
dd1a142988d3 [churn] progress meter
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 3049
diff changeset
   117
      ('', 'progress', None, _('show progress'))],
6841
05a62e8ddf3d churn: fix documentation for churn options
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6421
diff changeset
   118
    'hg churn [-r REVISIONS] [--aliases FILE] [--progress]'),
3040
f74077473b36 Churn extension
Josef "Jeff" Sipek <jeff@josefsipek.net>
parents:
diff changeset
   119
}