| 1 |
# churn.py - create a graph of revisions count grouped by template |
| 2 |
# |
| 3 |
# Copyright 2006 Josef "Jeff" Sipek <[email protected]> |
| 4 |
# Copyright 2008 Alexander Solovyov <[email protected]> |
| 5 |
# |
| 6 |
# This software may be used and distributed according to the terms of the |
| 7 |
# GNU General Public License version 2 or any later version. |
| 8 |
|
| 9 |
'''command to display statistics about repository history''' |
| 10 |
|
| 11 |
from __future__ import annotations |
| 12 |
|
| 13 |
import datetime |
| 14 |
import os |
| 15 |
import time |
| 16 |
|
| 17 |
from mercurial.i18n import _ |
| 18 |
from mercurial import ( |
| 19 |
cmdutil, |
| 20 |
encoding, |
| 21 |
logcmdutil, |
| 22 |
patch, |
| 23 |
pycompat, |
| 24 |
registrar, |
| 25 |
scmutil, |
| 26 |
) |
| 27 |
|
| 28 |
cmdtable = {} |
| 29 |
command = registrar.command(cmdtable) |
| 30 |
# Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for |
| 31 |
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should |
| 32 |
# be specifying the version(s) of Mercurial they are tested with, or |
| 33 |
# leave the attribute unspecified. |
| 34 |
testedwith = b'ships-with-hg-core' |
| 35 |
|
| 36 |
|
| 37 |
def changedlines(ui, repo, ctx1, ctx2, fmatch): |
| 38 |
added, removed = 0, 0 |
| 39 |
diff = b''.join(patch.diff(repo, ctx1.node(), ctx2.node(), fmatch)) |
| 40 |
inhunk = False |
| 41 |
for l in diff.split(b'\n'): |
| 42 |
if inhunk and l.startswith(b"+"): |
| 43 |
added += 1 |
| 44 |
elif inhunk and l.startswith(b"-"): |
| 45 |
removed += 1 |
| 46 |
elif l.startswith(b"@"): |
| 47 |
inhunk = True |
| 48 |
elif l.startswith(b"d"): |
| 49 |
inhunk = False |
| 50 |
return (added, removed) |
| 51 |
|
| 52 |
|
| 53 |
def countrate(ui, repo, amap, *pats, **opts): |
| 54 |
"""Calculate stats""" |
| 55 |
if opts.get('dateformat'): |
| 56 |
|
| 57 |
def getkey(ctx): |
| 58 |
t, tz = ctx.date() |
| 59 |
date = datetime.datetime(*time.gmtime(float(t) - tz)[:6]) |
| 60 |
return encoding.strtolocal( |
| 61 |
date.strftime(encoding.strfromlocal(opts['dateformat'])) |
| 62 |
) |
| 63 |
|
| 64 |
else: |
| 65 |
tmpl = opts.get('oldtemplate') or opts.get('template') |
| 66 |
tmpl = logcmdutil.maketemplater(ui, repo, tmpl) |
| 67 |
|
| 68 |
def getkey(ctx): |
| 69 |
ui.pushbuffer() |
| 70 |
tmpl.show(ctx) |
| 71 |
return ui.popbuffer() |
| 72 |
|
| 73 |
progress = ui.makeprogress( |
| 74 |
_(b'analyzing'), unit=_(b'revisions'), total=len(repo) |
| 75 |
) |
| 76 |
rate = {} |
| 77 |
|
| 78 |
def prep(ctx, fmatch): |
| 79 |
rev = ctx.rev() |
| 80 |
key = getkey(ctx).strip() |
| 81 |
key = amap.get(key, key) # alias remap |
| 82 |
if opts.get('changesets'): |
| 83 |
rate[key] = (rate.get(key, (0,))[0] + 1, 0) |
| 84 |
else: |
| 85 |
parents = ctx.parents() |
| 86 |
if len(parents) > 1: |
| 87 |
ui.note(_(b'revision %d is a merge, ignoring...\n') % (rev,)) |
| 88 |
return |
| 89 |
|
| 90 |
ctx1 = parents[0] |
| 91 |
lines = changedlines(ui, repo, ctx1, ctx, fmatch) |
| 92 |
rate[key] = [r + l for r, l in zip(rate.get(key, (0, 0)), lines)] |
| 93 |
|
| 94 |
progress.increment() |
| 95 |
|
| 96 |
wopts = logcmdutil.walkopts( |
| 97 |
pats=pats, |
| 98 |
opts=pycompat.byteskwargs(opts), |
| 99 |
revspec=opts['rev'], |
| 100 |
date=opts['date'], |
| 101 |
include_pats=opts['include'], |
| 102 |
exclude_pats=opts['exclude'], |
| 103 |
) |
| 104 |
revs, makefilematcher = logcmdutil.makewalker(repo, wopts) |
| 105 |
for ctx in scmutil.walkchangerevs(repo, revs, makefilematcher, prep): |
| 106 |
continue |
| 107 |
|
| 108 |
progress.complete() |
| 109 |
|
| 110 |
return rate |
| 111 |
|
| 112 |
|
| 113 |
@command( |
| 114 |
b'churn', |
| 115 |
[ |
| 116 |
( |
| 117 |
b'r', |
| 118 |
b'rev', |
| 119 |
[], |
| 120 |
_(b'count rate for the specified revision or revset'), |
| 121 |
_(b'REV'), |
| 122 |
), |
| 123 |
( |
| 124 |
b'd', |
| 125 |
b'date', |
| 126 |
b'', |
| 127 |
_(b'count rate for revisions matching date spec'), |
| 128 |
_(b'DATE'), |
| 129 |
), |
| 130 |
( |
| 131 |
b't', |
| 132 |
b'oldtemplate', |
| 133 |
b'', |
| 134 |
_(b'template to group changesets (DEPRECATED)'), |
| 135 |
_(b'TEMPLATE'), |
| 136 |
), |
| 137 |
( |
| 138 |
b'T', |
| 139 |
b'template', |
| 140 |
b'{author|email}', |
| 141 |
_(b'template to group changesets'), |
| 142 |
_(b'TEMPLATE'), |
| 143 |
), |
| 144 |
( |
| 145 |
b'f', |
| 146 |
b'dateformat', |
| 147 |
b'', |
| 148 |
_(b'strftime-compatible format for grouping by date'), |
| 149 |
_(b'FORMAT'), |
| 150 |
), |
| 151 |
(b'c', b'changesets', False, _(b'count rate by number of changesets')), |
| 152 |
(b's', b'sort', False, _(b'sort by key (default: sort by count)')), |
| 153 |
(b'', b'diffstat', False, _(b'display added/removed lines separately')), |
| 154 |
(b'', b'aliases', b'', _(b'file with email aliases'), _(b'FILE')), |
| 155 |
] |
| 156 |
+ cmdutil.walkopts, |
| 157 |
_(b"hg churn [-d DATE] [-r REV] [--aliases FILE] [FILE]"), |
| 158 |
helpcategory=command.CATEGORY_MAINTENANCE, |
| 159 |
inferrepo=True, |
| 160 |
) |
| 161 |
def churn(ui, repo, *pats, **opts): |
| 162 |
"""histogram of changes to the repository |
| 163 |
|
| 164 |
This command will display a histogram representing the number |
| 165 |
of changed lines or revisions, grouped according to the given |
| 166 |
template. The default template will group changes by author. |
| 167 |
The --dateformat option may be used to group the results by |
| 168 |
date instead. |
| 169 |
|
| 170 |
Statistics are based on the number of changed lines, or |
| 171 |
alternatively the number of matching revisions if the |
| 172 |
--changesets option is specified. |
| 173 |
|
| 174 |
Examples:: |
| 175 |
|
| 176 |
# display count of changed lines for every committer |
| 177 |
hg churn -T "{author|email}" |
| 178 |
|
| 179 |
# display daily activity graph |
| 180 |
hg churn -f "%H" -s -c |
| 181 |
|
| 182 |
# display activity of developers by month |
| 183 |
hg churn -f "%Y-%m" -s -c |
| 184 |
|
| 185 |
# display count of lines changed in every year |
| 186 |
hg churn -f "%Y" -s |
| 187 |
|
| 188 |
# display count of lines changed in a time range |
| 189 |
hg churn -d "2020-04 to 2020-09" |
| 190 |
|
| 191 |
It is possible to map alternate email addresses to a main address |
| 192 |
by providing a file using the following format:: |
| 193 |
|
| 194 |
<alias email> = <actual email> |
| 195 |
|
| 196 |
Such a file may be specified with the --aliases option, otherwise |
| 197 |
a .hgchurn file will be looked for in the working directory root. |
| 198 |
Aliases will be split from the rightmost "=". |
| 199 |
""" |
| 200 |
|
| 201 |
def pad(s, l): |
| 202 |
return s + b" " * (l - encoding.colwidth(s)) |
| 203 |
|
| 204 |
amap = {} |
| 205 |
aliases = opts.get('aliases') |
| 206 |
if not aliases and os.path.exists(repo.wjoin(b'.hgchurn')): |
| 207 |
aliases = repo.wjoin(b'.hgchurn') |
| 208 |
if aliases: |
| 209 |
with open(aliases, "rb") as fp: |
| 210 |
for l in fp: |
| 211 |
try: |
| 212 |
alias, actual = l.rsplit(b'=' in l and b'=' or None, 1) |
| 213 |
amap[alias.strip()] = actual.strip() |
| 214 |
except ValueError: |
| 215 |
l = l.strip() |
| 216 |
if l: |
| 217 |
ui.warn(_(b"skipping malformed alias: %s\n") % l) |
| 218 |
continue |
| 219 |
|
| 220 |
rate = list(countrate(ui, repo, amap, *pats, **opts).items()) |
| 221 |
if not rate: |
| 222 |
return |
| 223 |
|
| 224 |
if opts.get('sort'): |
| 225 |
rate.sort() |
| 226 |
else: |
| 227 |
rate.sort(key=lambda x: (-sum(x[1]), x)) |
| 228 |
|
| 229 |
# Be careful not to have a zero maxcount (issue833) |
| 230 |
maxcount = float(max(sum(v) for k, v in rate)) or 1.0 |
| 231 |
maxname = max(len(k) for k, v in rate) |
| 232 |
|
| 233 |
ttywidth = ui.termwidth() |
| 234 |
ui.debug(b"assuming %i character terminal\n" % ttywidth) |
| 235 |
width = ttywidth - maxname - 2 - 2 - 2 |
| 236 |
|
| 237 |
if opts.get('diffstat'): |
| 238 |
width -= 15 |
| 239 |
|
| 240 |
def format(name, diffstat): |
| 241 |
added, removed = diffstat |
| 242 |
return b"%s %15s %s%s\n" % ( |
| 243 |
pad(name, maxname), |
| 244 |
b'+%d/-%d' % (added, removed), |
| 245 |
ui.label(b'+' * charnum(added), b'diffstat.inserted'), |
| 246 |
ui.label(b'-' * charnum(removed), b'diffstat.deleted'), |
| 247 |
) |
| 248 |
|
| 249 |
else: |
| 250 |
width -= 6 |
| 251 |
|
| 252 |
def format(name, count): |
| 253 |
return b"%s %6d %s\n" % ( |
| 254 |
pad(name, maxname), |
| 255 |
sum(count), |
| 256 |
b'*' * charnum(sum(count)), |
| 257 |
) |
| 258 |
|
| 259 |
def charnum(count): |
| 260 |
return int(count * width // maxcount) |
| 261 |
|
| 262 |
for name, count in rate: |
| 263 |
ui.write(format(name, count)) |