churn.py
177 lines
| 5.7 KiB
| text/x-python
|
PythonLexer
/ hgext / churn.py
Alexander Solovyov
|
r7065 | # churn.py - create a graph of revisions count grouped by template | ||
Patrick Mezard
|
r6348 | # | ||
# Copyright 2006 Josef "Jeff" Sipek <jeffpc@josefsipek.net> | ||||
Alexander Solovyov
|
r7065 | # Copyright 2008 Alexander Solovyov <piranha@piranha.org.ua> | ||
Patrick Mezard
|
r6348 | # | ||
Martin Geisler
|
r8225 | # This software may be used and distributed according to the terms of the | ||
# GNU General Public License version 2, incorporated herein by reference. | ||||
Martin Geisler
|
r8228 | |||
Dirkjan Ochtman
|
r8934 | '''command to display statistics about repository history''' | ||
Patrick Mezard
|
r6348 | |||
Martin Geisler
|
r7051 | from mercurial.i18n import _ | ||
Alexander Solovyov
|
r7065 | from mercurial import patch, cmdutil, util, templater | ||
Martin Geisler
|
r8254 | import sys, os | ||
Alexander Solovyov
|
r7065 | import time, datetime | ||
Patrick Mezard
|
r6348 | |||
Alexander Solovyov
|
r7065 | def maketemplater(ui, repo, tmpl): | ||
tmpl = templater.parsestring(tmpl, quoted=False) | ||||
try: | ||||
Jim Correia
|
r7762 | t = cmdutil.changeset_templater(ui, repo, False, None, None, False) | ||
Alexander Solovyov
|
r7065 | except SyntaxError, inst: | ||
raise util.Abort(inst.args[0]) | ||||
t.use_template(tmpl) | ||||
return t | ||||
madhu@madhu
|
r7870 | def changedlines(ui, repo, ctx1, ctx2, fns): | ||
Alexander Solovyov
|
r7065 | lines = 0 | ||
Brendan Cully
|
r9321 | fmatch = cmdutil.matchfiles(repo, fns) | ||
madhu@madhu
|
r7870 | diff = ''.join(patch.diff(repo, ctx1.node(), ctx2.node(), fmatch)) | ||
Alexander Solovyov
|
r7065 | for l in diff.split('\n'): | ||
if (l.startswith("+") and not l.startswith("+++ ") or | ||||
l.startswith("-") and not l.startswith("--- ")): | ||||
lines += 1 | ||||
return lines | ||||
Patrick Mezard
|
r6348 | |||
Alexander Solovyov
|
r7065 | def countrate(ui, repo, amap, *pats, **opts): | ||
"""Calculate stats""" | ||||
if opts.get('dateformat'): | ||||
def getkey(ctx): | ||||
t, tz = ctx.date() | ||||
date = datetime.datetime(*time.gmtime(float(t) - tz)[:6]) | ||||
return date.strftime(opts['dateformat']) | ||||
else: | ||||
tmpl = opts.get('template', '{author|email}') | ||||
tmpl = maketemplater(ui, repo, tmpl) | ||||
def getkey(ctx): | ||||
ui.pushbuffer() | ||||
Dirkjan Ochtman
|
r7369 | tmpl.show(ctx) | ||
Alexander Solovyov
|
r7065 | return ui.popbuffer() | ||
count = pct = 0 | ||||
rate = {} | ||||
df = False | ||||
if opts.get('date'): | ||||
df = util.matchdate(opts['date']) | ||||
Dirkjan Ochtman
|
r9367 | get = util.cachefunc(lambda r: repo[r]) | ||
Alexander Solovyov
|
r7065 | changeiter, matchfn = cmdutil.walkchangerevs(ui, repo, pats, get, opts) | ||
for st, rev, fns in changeiter: | ||||
Dirkjan Ochtman
|
r9367 | |||
Alexander Solovyov
|
r7065 | if not st == 'add': | ||
continue | ||||
Dirkjan Ochtman
|
r9367 | |||
ctx = get(rev) | ||||
if df and not df(ctx.date()[0]): # doesn't match date format | ||||
Patrick Mezard
|
r6348 | continue | ||
Alexander Solovyov
|
r7065 | key = getkey(ctx) | ||
key = amap.get(key, key) # alias remap | ||||
Alexander Solovyov
|
r7070 | if opts.get('changesets'): | ||
rate[key] = rate.get(key, 0) + 1 | ||||
else: | ||||
Alexander Solovyov
|
r7065 | parents = ctx.parents() | ||
if len(parents) > 1: | ||||
ui.note(_('Revision %d is a merge, ignoring...\n') % (rev,)) | ||||
continue | ||||
Patrick Mezard
|
r6348 | |||
Alexander Solovyov
|
r7065 | ctx1 = parents[0] | ||
madhu@madhu
|
r7870 | lines = changedlines(ui, repo, ctx1, ctx, fns) | ||
Alexander Solovyov
|
r7065 | rate[key] = rate.get(key, 0) + lines | ||
Patrick Mezard
|
r6348 | |||
Alexander Solovyov
|
r7065 | if opts.get('progress'): | ||
Matt Mackall
|
r6759 | count += 1 | ||
Alexander Solovyov
|
r7065 | newpct = int(100.0 * count / max(len(repo), 1)) | ||
Matt Mackall
|
r6759 | if pct < newpct: | ||
pct = newpct | ||||
Benoit Boissinot
|
r8085 | ui.write("\r" + _("generating stats: %d%%") % pct) | ||
Patrick Mezard
|
r6348 | sys.stdout.flush() | ||
Alexander Solovyov
|
r7065 | if opts.get('progress'): | ||
Patrick Mezard
|
r6348 | ui.write("\r") | ||
sys.stdout.flush() | ||||
Alexander Solovyov
|
r7065 | return rate | ||
Alexander Solovyov
|
r7070 | def churn(ui, repo, *pats, **opts): | ||
Cédric Duval
|
r8823 | '''histogram of changes to the repository | ||
Patrick Mezard
|
r6348 | |||
Cédric Duval
|
r8823 | This command will display a histogram representing the number | ||
of changed lines or revisions, grouped according to the given | ||||
template. The default template will group changes by author. | ||||
The --dateformat option may be used to group the results by | ||||
date instead. | ||||
Alexander Solovyov
|
r7065 | |||
Cédric Duval
|
r8823 | Statistics are based on the number of changed lines, or | ||
alternatively the number of matching revisions if the | ||||
--changesets option is specified. | ||||
Alexander Solovyov
|
r7065 | |||
Martin Geisler
|
r9205 | Examples:: | ||
Dirkjan Ochtman
|
r6666 | |||
Alexander Solovyov
|
r7070 | # display count of changed lines for every committer | ||
hg churn -t '{author|email}' | ||||
Alexander Solovyov
|
r7065 | |||
# display daily activity graph | ||||
Alexander Solovyov
|
r7070 | hg churn -f '%H' -s -c | ||
Dirkjan Ochtman
|
r6666 | |||
Alexander Solovyov
|
r7065 | # display activity of developers by month | ||
Alexander Solovyov
|
r7070 | hg churn -f '%Y-%m' -s -c | ||
Patrick Mezard
|
r6348 | |||
Alexander Solovyov
|
r7065 | # display count of lines changed in every year | ||
Alexander Solovyov
|
r7070 | hg churn -f '%Y' -s | ||
Cédric Duval
|
r8823 | It is possible to map alternate email addresses to a main address | ||
Martin Geisler
|
r9254 | by providing a file using the following format:: | ||
Dirkjan Ochtman
|
r8843 | |||
Martin Geisler
|
r9205 | <alias email> <actual email> | ||
Martin Geisler
|
r8254 | |||
Martin Geisler
|
r9254 | Such a file may be specified with the --aliases option, otherwise | ||
a .hgchurn file will be looked for in the working directory root. | ||||
Martin Geisler
|
r8254 | ''' | ||
Patrick Mezard
|
r6348 | def pad(s, l): | ||
Matt Mackall
|
r6759 | return (s + " " * l)[:l] | ||
Patrick Mezard
|
r6348 | |||
amap = {} | ||||
aliases = opts.get('aliases') | ||||
Martin Geisler
|
r8254 | if not aliases and os.path.exists(repo.wjoin('.hgchurn')): | ||
aliases = repo.wjoin('.hgchurn') | ||||
Patrick Mezard
|
r6348 | if aliases: | ||
Matt Mackall
|
r6759 | for l in open(aliases, "r"): | ||
l = l.strip() | ||||
alias, actual = l.split() | ||||
amap[alias] = actual | ||||
Patrick Mezard
|
r6348 | |||
Alexander Solovyov
|
r7065 | rate = countrate(ui, repo, amap, *pats, **opts).items() | ||
if not rate: | ||||
Patrick Mezard
|
r6348 | return | ||
Alejandro Santos
|
r9032 | sortkey = ((not opts.get('sort')) and (lambda x: -x[1]) or None) | ||
rate.sort(key=sortkey) | ||||
Alexander Solovyov
|
r7065 | |||
Nicolas Dumazet
|
r9388 | # Be careful not to have a zero maxcount (issue833) | ||
maxcount = float(max([v for k, v in rate])) or 1.0 | ||||
Alexander Solovyov
|
r7076 | maxname = max([len(k) for k, v in rate]) | ||
Patrick Mezard
|
r6348 | |||
Alexander Solovyov
|
r7547 | ttywidth = util.termwidth() | ||
Matt Mackall
|
r6759 | ui.debug(_("assuming %i character terminal\n") % ttywidth) | ||
Alexander Solovyov
|
r7065 | width = ttywidth - maxname - 2 - 6 - 2 - 2 | ||
for date, count in rate: | ||||
print "%s %6d %s" % (pad(date, maxname), count, | ||||
"*" * int(count * width / maxcount)) | ||||
Patrick Mezard
|
r6348 | |||
cmdtable = { | ||||
Alexander Solovyov
|
r7070 | "churn": | ||
(churn, | ||||
Alexander Solovyov
|
r7065 | [('r', 'rev', [], _('count rate for the specified revision or range')), | ||
Martin Geisler
|
r8028 | ('d', 'date', '', _('count rate for revisions matching date spec')), | ||
Alexander Solovyov
|
r7065 | ('t', 'template', '{author|email}', _('template to group changesets')), | ||
('f', 'dateformat', '', | ||||
_('strftime-compatible format for grouping by date')), | ||||
Alexander Solovyov
|
r7070 | ('c', 'changesets', False, _('count rate by number of changesets')), | ||
Alexander Solovyov
|
r7065 | ('s', 'sort', False, _('sort by key (default: sort by count)')), | ||
('', 'aliases', '', _('file with email aliases')), | ||||
('', 'progress', None, _('show progress'))], | ||||
Martin Geisler
|
r7129 | _("hg churn [-d DATE] [-r REV] [--aliases FILE] [--progress] [FILE]")), | ||
Patrick Mezard
|
r6348 | } | ||