churn.py
211 lines
| 7.0 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 | ||
Matt Mackall
|
r10263 | # GNU General Public License version 2 or any later version. | ||
Martin Geisler
|
r8228 | |||
Dirkjan Ochtman
|
r8934 | '''command to display statistics about repository history''' | ||
Patrick Mezard
|
r6348 | |||
Gregory Szorc
|
r28094 | from __future__ import absolute_import | ||
import datetime | ||||
import os | ||||
import time | ||||
Martin Geisler
|
r7051 | from mercurial.i18n import _ | ||
Gregory Szorc
|
r28094 | from mercurial import ( | ||
cmdutil, | ||||
encoding, | ||||
Yuya Nishihara
|
r35906 | logcmdutil, | ||
Gregory Szorc
|
r28094 | patch, | ||
Pulkit Goyal
|
r34975 | pycompat, | ||
Yuya Nishihara
|
r32337 | registrar, | ||
Gregory Szorc
|
r28094 | scmutil, | ||
) | ||||
Boris Feld
|
r36625 | from mercurial.utils import dateutil | ||
Patrick Mezard
|
r6348 | |||
Gregory Szorc
|
r21245 | cmdtable = {} | ||
Yuya Nishihara
|
r32337 | command = registrar.command(cmdtable) | ||
Augie Fackler
|
r29841 | # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for | ||
Augie Fackler
|
r25186 | # 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. | ||||
Augie Fackler
|
r29841 | testedwith = 'ships-with-hg-core' | ||
Augie Fackler
|
r16743 | |||
madhu@madhu
|
r7870 | def changedlines(ui, repo, ctx1, ctx2, fns): | ||
Alexander Solovyov
|
r9669 | added, removed = 0, 0 | ||
Matt Mackall
|
r14322 | fmatch = scmutil.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'): | ||
Alexander Solovyov
|
r9669 | if l.startswith("+") and not l.startswith("+++ "): | ||
added += 1 | ||||
elif l.startswith("-") and not l.startswith("--- "): | ||||
removed += 1 | ||||
return (added, removed) | ||||
Patrick Mezard
|
r6348 | |||
Alexander Solovyov
|
r7065 | def countrate(ui, repo, amap, *pats, **opts): | ||
"""Calculate stats""" | ||||
Pulkit Goyal
|
r34975 | opts = pycompat.byteskwargs(opts) | ||
Alexander Solovyov
|
r7065 | if opts.get('dateformat'): | ||
def getkey(ctx): | ||||
t, tz = ctx.date() | ||||
date = datetime.datetime(*time.gmtime(float(t) - tz)[:6]) | ||||
Pulkit Goyal
|
r38077 | return date.strftime(pycompat.sysstr(opts['dateformat'])) | ||
Alexander Solovyov
|
r7065 | else: | ||
Jordi Gutiérrez Hermoso
|
r24139 | tmpl = opts.get('oldtemplate') or opts.get('template') | ||
Yuya Nishihara
|
r35906 | tmpl = logcmdutil.maketemplater(ui, repo, tmpl) | ||
Alexander Solovyov
|
r7065 | def getkey(ctx): | ||
ui.pushbuffer() | ||||
Dirkjan Ochtman
|
r7369 | tmpl.show(ctx) | ||
Alexander Solovyov
|
r7065 | return ui.popbuffer() | ||
Eric Eisner
|
r10647 | state = {'count': 0} | ||
Alexander Solovyov
|
r7065 | rate = {} | ||
df = False | ||||
if opts.get('date'): | ||||
Boris Feld
|
r36625 | df = dateutil.matchdate(opts['date']) | ||
Alexander Solovyov
|
r7065 | |||
Matt Mackall
|
r14671 | m = scmutil.match(repo[None], pats, opts) | ||
Matt Mackall
|
r9662 | def prep(ctx, fns): | ||
Matt Mackall
|
r9654 | rev = ctx.rev() | ||
Dirkjan Ochtman
|
r9367 | if df and not df(ctx.date()[0]): # doesn't match date format | ||
Matt Mackall
|
r9662 | return | ||
Patrick Mezard
|
r6348 | |||
Alexander Solovyov
|
r14040 | key = getkey(ctx).strip() | ||
Alexander Solovyov
|
r7065 | key = amap.get(key, key) # alias remap | ||
Alexander Solovyov
|
r7070 | if opts.get('changesets'): | ||
Alexander Solovyov
|
r9670 | rate[key] = (rate.get(key, (0,))[0] + 1, 0) | ||
Alexander Solovyov
|
r7070 | else: | ||
Alexander Solovyov
|
r7065 | parents = ctx.parents() | ||
if len(parents) > 1: | ||||
Martin Geisler
|
r16924 | ui.note(_('revision %d is a merge, ignoring...\n') % (rev,)) | ||
Matt Mackall
|
r9662 | return | ||
Patrick Mezard
|
r6348 | |||
Alexander Solovyov
|
r7065 | ctx1 = parents[0] | ||
madhu@madhu
|
r7870 | lines = changedlines(ui, repo, ctx1, ctx, fns) | ||
Alexander Solovyov
|
r9669 | rate[key] = [r + l for r, l in zip(rate.get(key, (0, 0)), lines)] | ||
Patrick Mezard
|
r6348 | |||
Eric Eisner
|
r10647 | state['count'] += 1 | ||
r28472 | ui.progress(_('analyzing'), state['count'], total=len(repo), | |||
unit=_('revisions')) | ||||
Patrick Mezard
|
r6348 | |||
Matt Mackall
|
r9665 | for ctx in cmdutil.walkchangerevs(repo, m, opts, prep): | ||
Matt Mackall
|
r9662 | continue | ||
Martin Geisler
|
r10700 | ui.progress(_('analyzing'), None) | ||
Patrick Mezard
|
r6348 | |||
Alexander Solovyov
|
r7065 | return rate | ||
Gregory Szorc
|
r21245 | @command('churn', | ||
[('r', 'rev', [], | ||||
Jordi Gutiérrez Hermoso
|
r23091 | _('count rate for the specified revision or revset'), _('REV')), | ||
Gregory Szorc
|
r21245 | ('d', 'date', '', | ||
_('count rate for revisions matching date spec'), _('DATE')), | ||||
Jordi Gutiérrez Hermoso
|
r24139 | ('t', 'oldtemplate', '', | ||
_('template to group changesets (DEPRECATED)'), _('TEMPLATE')), | ||||
('T', 'template', '{author|email}', | ||||
Gregory Szorc
|
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')), | ||||
Yuya Nishihara
|
r32375 | ] + cmdutil.walkopts, | ||
Gregory Szorc
|
r21779 | _("hg churn [-d DATE] [-r REV] [--aliases FILE] [FILE]"), | ||
inferrepo=True) | ||||
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 | ||
Matt Harbison
|
r32229 | hg churn -T "{author|email}" | ||
Alexander Solovyov
|
r7065 | |||
# display daily activity graph | ||||
FUJIWARA Katsunori
|
r19959 | hg churn -f "%H" -s -c | ||
Dirkjan Ochtman
|
r6666 | |||
Alexander Solovyov
|
r7065 | # display activity of developers by month | ||
FUJIWARA Katsunori
|
r19959 | hg churn -f "%Y-%m" -s -c | ||
Patrick Mezard
|
r6348 | |||
Alexander Solovyov
|
r7065 | # display count of lines changed in every year | ||
FUJIWARA Katsunori
|
r19959 | hg churn -f "%Y" -s | ||
Alexander Solovyov
|
r7070 | |||
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 | |||
Alexander Solovyov
|
r11264 | <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. | ||||
Matthew Turk
|
r19464 | Aliases will be split from the rightmost "=". | ||
Martin Geisler
|
r8254 | ''' | ||
Patrick Mezard
|
r6348 | def pad(s, l): | ||
Isaac Jurado
|
r21163 | return s + " " * (l - encoding.colwidth(s)) | ||
Patrick Mezard
|
r6348 | |||
amap = {} | ||||
Pulkit Goyal
|
r34975 | aliases = opts.get(r'aliases') | ||
Martin Geisler
|
r8254 | if not aliases and os.path.exists(repo.wjoin('.hgchurn')): | ||
aliases = repo.wjoin('.hgchurn') | ||||
Patrick Mezard
|
r6348 | if aliases: | ||
Pulkit Goyal
|
r38093 | for l in open(aliases, "rb"): | ||
Martin Geisler
|
r12069 | try: | ||
Matthew Turk
|
r19464 | alias, actual = l.rsplit('=' in l and '=' or None, 1) | ||
Martin Geisler
|
r12069 | amap[alias.strip()] = actual.strip() | ||
except ValueError: | ||||
l = l.strip() | ||||
if l: | ||||
Matt Mackall
|
r16231 | ui.warn(_("skipping malformed alias: %s\n") % l) | ||
Ronny Pfannschmidt
|
r12068 | continue | ||
Patrick Mezard
|
r6348 | |||
Pulkit Goyal
|
r36410 | rate = list(countrate(ui, repo, amap, *pats, **opts).items()) | ||
Alexander Solovyov
|
r7065 | if not rate: | ||
Patrick Mezard
|
r6348 | return | ||
Pulkit Goyal
|
r34975 | if opts.get(r'sort'): | ||
Mads Kiilerich
|
r18369 | rate.sort() | ||
else: | ||||
rate.sort(key=lambda x: (-sum(x[1]), x)) | ||||
Alexander Solovyov
|
r7065 | |||
Nicolas Dumazet
|
r9388 | # Be careful not to have a zero maxcount (issue833) | ||
Alexander Solovyov
|
r9669 | maxcount = float(max(sum(v) for k, v in rate)) or 1.0 | ||
Nicolas Dumazet
|
r9390 | maxname = max(len(k) for k, v in rate) | ||
Patrick Mezard
|
r6348 | |||
Augie Fackler
|
r12689 | ttywidth = ui.termwidth() | ||
Martin Geisler
|
r9467 | ui.debug("assuming %i character terminal\n" % ttywidth) | ||
Alexander Solovyov
|
r9669 | width = ttywidth - maxname - 2 - 2 - 2 | ||
Alexander Solovyov
|
r7065 | |||
Pulkit Goyal
|
r34975 | if opts.get(r'diffstat'): | ||
Alexander Solovyov
|
r9669 | width -= 15 | ||
Renato Cunha
|
r11501 | def format(name, diffstat): | ||
added, removed = diffstat | ||||
Steve Borho
|
r11310 | return "%s %15s %s%s\n" % (pad(name, maxname), | ||
'+%d/-%d' % (added, removed), | ||||
Brodie Rao
|
r10821 | ui.label('+' * charnum(added), | ||
'diffstat.inserted'), | ||||
ui.label('-' * charnum(removed), | ||||
'diffstat.deleted')) | ||||
Alexander Solovyov
|
r9669 | else: | ||
width -= 6 | ||||
def format(name, count): | ||||
Steve Borho
|
r11310 | return "%s %6d %s\n" % (pad(name, maxname), sum(count), | ||
'*' * charnum(sum(count))) | ||||
Alexander Solovyov
|
r9669 | |||
def charnum(count): | ||||
Matt Mackall
|
r10282 | return int(round(count * width / maxcount)) | ||
Alexander Solovyov
|
r9669 | |||
for name, count in rate: | ||||
Steve Borho
|
r11310 | ui.write(format(name, count)) | ||