churn.py
217 lines
| 7.1 KiB
| text/x-python
|
PythonLexer
/ hgext / churn.py
|
r7065 | # churn.py - create a graph of revisions count grouped by template | ||
|
r6348 | # | ||
# Copyright 2006 Josef "Jeff" Sipek <jeffpc@josefsipek.net> | ||||
|
r7065 | # Copyright 2008 Alexander Solovyov <piranha@piranha.org.ua> | ||
|
r6348 | # | ||
|
r8225 | # This software may be used and distributed according to the terms of the | ||
|
r10263 | # GNU General Public License version 2 or any later version. | ||
|
r8228 | |||
|
r8934 | '''command to display statistics about repository history''' | ||
|
r6348 | |||
|
r28094 | from __future__ import absolute_import | ||
import datetime | ||||
import os | ||||
import time | ||||
|
r7051 | from mercurial.i18n import _ | ||
|
r28094 | from mercurial import ( | ||
cmdutil, | ||||
commands, | ||||
encoding, | ||||
error, | ||||
patch, | ||||
scmutil, | ||||
util, | ||||
) | ||||
|
r6348 | |||
|
r21245 | cmdtable = {} | ||
command = cmdutil.command(cmdtable) | ||||
|
r25186 | # Note for extension authors: ONLY specify testedwith = 'internal' for | ||
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should | ||||
# be specifying the version(s) of Mercurial they are tested with, or | ||||
# leave the attribute unspecified. | ||||
|
r16743 | testedwith = 'internal' | ||
|
r7065 | def maketemplater(ui, repo, tmpl): | ||
try: | ||||
|
r20667 | t = cmdutil.changeset_templater(ui, repo, False, None, tmpl, | ||
None, False) | ||||
|
r25660 | except SyntaxError as inst: | ||
|
r26587 | raise error.Abort(inst.args[0]) | ||
|
r7065 | return t | ||
|
r7870 | def changedlines(ui, repo, ctx1, ctx2, fns): | ||
|
r9669 | added, removed = 0, 0 | ||
|
r14322 | fmatch = scmutil.matchfiles(repo, fns) | ||
|
r7870 | diff = ''.join(patch.diff(repo, ctx1.node(), ctx2.node(), fmatch)) | ||
|
r7065 | for l in diff.split('\n'): | ||
|
r9669 | if l.startswith("+") and not l.startswith("+++ "): | ||
added += 1 | ||||
elif l.startswith("-") and not l.startswith("--- "): | ||||
removed += 1 | ||||
return (added, removed) | ||||
|
r6348 | |||
|
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: | ||||
|
r24139 | tmpl = opts.get('oldtemplate') or opts.get('template') | ||
|
r7065 | tmpl = maketemplater(ui, repo, tmpl) | ||
def getkey(ctx): | ||||
ui.pushbuffer() | ||||
|
r7369 | tmpl.show(ctx) | ||
|
r7065 | return ui.popbuffer() | ||
|
r10647 | state = {'count': 0} | ||
|
r7065 | rate = {} | ||
df = False | ||||
if opts.get('date'): | ||||
df = util.matchdate(opts['date']) | ||||
|
r14671 | m = scmutil.match(repo[None], pats, opts) | ||
|
r9662 | def prep(ctx, fns): | ||
|
r9654 | rev = ctx.rev() | ||
|
r9367 | if df and not df(ctx.date()[0]): # doesn't match date format | ||
|
r9662 | return | ||
|
r6348 | |||
|
r14040 | key = getkey(ctx).strip() | ||
|
r7065 | key = amap.get(key, key) # alias remap | ||
|
r7070 | if opts.get('changesets'): | ||
|
r9670 | rate[key] = (rate.get(key, (0,))[0] + 1, 0) | ||
|
r7070 | else: | ||
|
r7065 | parents = ctx.parents() | ||
if len(parents) > 1: | ||||
|
r16924 | ui.note(_('revision %d is a merge, ignoring...\n') % (rev,)) | ||
|
r9662 | return | ||
|
r6348 | |||
|
r7065 | ctx1 = parents[0] | ||
|
r7870 | lines = changedlines(ui, repo, ctx1, ctx, fns) | ||
|
r9669 | rate[key] = [r + l for r, l in zip(rate.get(key, (0, 0)), lines)] | ||
|
r6348 | |||
|
r10647 | state['count'] += 1 | ||
r28472 | ui.progress(_('analyzing'), state['count'], total=len(repo), | |||
unit=_('revisions')) | ||||
|
r6348 | |||
|
r9665 | for ctx in cmdutil.walkchangerevs(repo, m, opts, prep): | ||
|
r9662 | continue | ||
|
r10700 | ui.progress(_('analyzing'), None) | ||
|
r6348 | |||
|
r7065 | return rate | ||
|
r21245 | @command('churn', | ||
[('r', 'rev', [], | ||||
|
r23091 | _('count rate for the specified revision or revset'), _('REV')), | ||
|
r21245 | ('d', 'date', '', | ||
_('count rate for revisions matching date spec'), _('DATE')), | ||||
|
r24139 | ('t', 'oldtemplate', '', | ||
_('template to group changesets (DEPRECATED)'), _('TEMPLATE')), | ||||
('T', 'template', '{author|email}', | ||||
|
r21245 | _('template to group changesets'), _('TEMPLATE')), | ||
('f', 'dateformat', '', | ||||
_('strftime-compatible format for grouping by date'), _('FORMAT')), | ||||
('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'), _('FILE')), | ||||
] + commands.walkopts, | ||||
|
r21779 | _("hg churn [-d DATE] [-r REV] [--aliases FILE] [FILE]"), | ||
inferrepo=True) | ||||
|
r7070 | def churn(ui, repo, *pats, **opts): | ||
|
r8823 | '''histogram of changes to the repository | ||
|
r6348 | |||
|
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. | ||||
|
r7065 | |||
|
r8823 | Statistics are based on the number of changed lines, or | ||
alternatively the number of matching revisions if the | ||||
--changesets option is specified. | ||||
|
r7065 | |||
|
r9205 | Examples:: | ||
|
r6666 | |||
|
r7070 | # display count of changed lines for every committer | ||
|
r19959 | hg churn -t "{author|email}" | ||
|
r7065 | |||
# display daily activity graph | ||||
|
r19959 | hg churn -f "%H" -s -c | ||
|
r6666 | |||
|
r7065 | # display activity of developers by month | ||
|
r19959 | hg churn -f "%Y-%m" -s -c | ||
|
r6348 | |||
|
r7065 | # display count of lines changed in every year | ||
|
r19959 | hg churn -f "%Y" -s | ||
|
r7070 | |||
|
r8823 | It is possible to map alternate email addresses to a main address | ||
|
r9254 | by providing a file using the following format:: | ||
|
r8843 | |||
|
r11264 | <alias email> = <actual email> | ||
|
r8254 | |||
|
r9254 | Such a file may be specified with the --aliases option, otherwise | ||
a .hgchurn file will be looked for in the working directory root. | ||||
|
r19464 | Aliases will be split from the rightmost "=". | ||
|
r8254 | ''' | ||
|
r6348 | def pad(s, l): | ||
|
r21163 | return s + " " * (l - encoding.colwidth(s)) | ||
|
r6348 | |||
amap = {} | ||||
aliases = opts.get('aliases') | ||||
|
r8254 | if not aliases and os.path.exists(repo.wjoin('.hgchurn')): | ||
aliases = repo.wjoin('.hgchurn') | ||||
|
r6348 | if aliases: | ||
|
r6759 | for l in open(aliases, "r"): | ||
|
r12069 | try: | ||
|
r19464 | alias, actual = l.rsplit('=' in l and '=' or None, 1) | ||
|
r12069 | amap[alias.strip()] = actual.strip() | ||
except ValueError: | ||||
l = l.strip() | ||||
if l: | ||||
|
r16231 | ui.warn(_("skipping malformed alias: %s\n") % l) | ||
|
r12068 | continue | ||
|
r6348 | |||
|
r7065 | rate = countrate(ui, repo, amap, *pats, **opts).items() | ||
if not rate: | ||||
|
r6348 | return | ||
|
r18369 | if opts.get('sort'): | ||
rate.sort() | ||||
else: | ||||
rate.sort(key=lambda x: (-sum(x[1]), x)) | ||||
|
r7065 | |||
|
r9388 | # Be careful not to have a zero maxcount (issue833) | ||
|
r9669 | maxcount = float(max(sum(v) for k, v in rate)) or 1.0 | ||
|
r9390 | maxname = max(len(k) for k, v in rate) | ||
|
r6348 | |||
|
r12689 | ttywidth = ui.termwidth() | ||
|
r9467 | ui.debug("assuming %i character terminal\n" % ttywidth) | ||
|
r9669 | width = ttywidth - maxname - 2 - 2 - 2 | ||
|
r7065 | |||
|
r9669 | if opts.get('diffstat'): | ||
width -= 15 | ||||
|
r11501 | def format(name, diffstat): | ||
added, removed = diffstat | ||||
|
r11310 | return "%s %15s %s%s\n" % (pad(name, maxname), | ||
'+%d/-%d' % (added, removed), | ||||
|
r10821 | ui.label('+' * charnum(added), | ||
'diffstat.inserted'), | ||||
ui.label('-' * charnum(removed), | ||||
'diffstat.deleted')) | ||||
|
r9669 | else: | ||
width -= 6 | ||||
def format(name, count): | ||||
|
r11310 | return "%s %6d %s\n" % (pad(name, maxname), sum(count), | ||
'*' * charnum(sum(count))) | ||||
|
r9669 | |||
def charnum(count): | ||||
|
r10282 | return int(round(count * width / maxcount)) | ||
|
r9669 | |||
for name, count in rate: | ||||
|
r11310 | ui.write(format(name, count)) | ||