Show More
@@ -1,119 +1,119 | |||
|
1 | 1 | # churn.py - create a graph showing who changed the most lines |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2006 Josef "Jeff" Sipek <jeffpc@josefsipek.net> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms |
|
6 | 6 | # of the GNU General Public License, incorporated herein by reference. |
|
7 | 7 | '''allow graphing the number of lines changed per contributor''' |
|
8 | 8 | |
|
9 | 9 | from mercurial.i18n import gettext as _ |
|
10 | 10 | from mercurial import patch, cmdutil, util, node |
|
11 | 11 | import os, sys |
|
12 | 12 | |
|
13 | 13 | def get_tty_width(): |
|
14 | 14 | if 'COLUMNS' in os.environ: |
|
15 | 15 | try: |
|
16 | 16 | return int(os.environ['COLUMNS']) |
|
17 | 17 | except ValueError: |
|
18 | 18 | pass |
|
19 | 19 | try: |
|
20 | 20 | import termios, array, fcntl |
|
21 | 21 | for dev in (sys.stdout, sys.stdin): |
|
22 | 22 | try: |
|
23 | 23 | fd = dev.fileno() |
|
24 | 24 | if not os.isatty(fd): |
|
25 | 25 | continue |
|
26 | 26 | arri = fcntl.ioctl(fd, termios.TIOCGWINSZ, '\0' * 8) |
|
27 | 27 | return array.array('h', arri)[1] |
|
28 | 28 | except ValueError: |
|
29 | 29 | pass |
|
30 | 30 | except ImportError: |
|
31 | 31 | pass |
|
32 | 32 | return 80 |
|
33 | 33 | |
|
34 | 34 | def countrevs(ui, repo, amap, revs, progress=False): |
|
35 | 35 | stats = {} |
|
36 | 36 | count = pct = 0 |
|
37 | 37 | if not revs: |
|
38 | 38 | revs = range(len(repo)) |
|
39 | 39 | |
|
40 | 40 | for rev in revs: |
|
41 | 41 | ctx2 = repo[rev] |
|
42 | 42 | parents = ctx2.parents() |
|
43 | 43 | if len(parents) > 1: |
|
44 | 44 | ui.note(_('Revision %d is a merge, ignoring...\n') % (rev,)) |
|
45 | 45 | continue |
|
46 | 46 | |
|
47 | 47 | ctx1 = parents[0] |
|
48 | 48 | lines = 0 |
|
49 | 49 | ui.pushbuffer() |
|
50 | 50 | patch.diff(repo, ctx1.node(), ctx2.node()) |
|
51 | 51 | diff = ui.popbuffer() |
|
52 | 52 | |
|
53 | 53 | for l in diff.split('\n'): |
|
54 | 54 | if (l.startswith("+") and not l.startswith("+++ ") or |
|
55 | 55 | l.startswith("-") and not l.startswith("--- ")): |
|
56 | 56 | lines += 1 |
|
57 | 57 | |
|
58 | 58 | user = util.email(ctx2.user()) |
|
59 | 59 | user = amap.get(user, user) # remap |
|
60 | 60 | stats[user] = stats.get(user, 0) + lines |
|
61 | ui.debug("rev %d: %d lines by %s\n" % (rev, lines, user)) | |
|
61 | ui.debug(_("rev %d: %d lines by %s\n") % (rev, lines, user)) | |
|
62 | 62 | |
|
63 | 63 | if progress: |
|
64 | 64 | count += 1 |
|
65 | 65 | newpct = int(100.0 * count / max(len(revs), 1)) |
|
66 | 66 | if pct < newpct: |
|
67 | 67 | pct = newpct |
|
68 | ui.write("\rGenerating stats: %d%%" % pct) | |
|
68 | ui.write(_("\rGenerating stats: %d%%") % pct) | |
|
69 | 69 | sys.stdout.flush() |
|
70 | 70 | |
|
71 | 71 | if progress: |
|
72 | 72 | ui.write("\r") |
|
73 | 73 | sys.stdout.flush() |
|
74 | 74 | |
|
75 | 75 | return stats |
|
76 | 76 | |
|
77 | 77 | def churn(ui, repo, **opts): |
|
78 | 78 | '''graphs the number of lines changed |
|
79 | 79 | |
|
80 | 80 | The map file format used to specify aliases is fairly simple: |
|
81 | 81 | |
|
82 | 82 | <alias email> <actual email>''' |
|
83 | 83 | |
|
84 | 84 | def pad(s, l): |
|
85 | 85 | return (s + " " * l)[:l] |
|
86 | 86 | |
|
87 | 87 | amap = {} |
|
88 | 88 | aliases = opts.get('aliases') |
|
89 | 89 | if aliases: |
|
90 | 90 | for l in open(aliases, "r"): |
|
91 | 91 | l = l.strip() |
|
92 | 92 | alias, actual = l.split() |
|
93 | 93 | amap[alias] = actual |
|
94 | 94 | |
|
95 | 95 | revs = util.sort([int(r) for r in cmdutil.revrange(repo, opts['rev'])]) |
|
96 | 96 | stats = countrevs(ui, repo, amap, revs, opts.get('progress')) |
|
97 | 97 | if not stats: |
|
98 | 98 | return |
|
99 | 99 | |
|
100 | 100 | stats = util.sort([(-l, u, l) for u,l in stats.items()]) |
|
101 | 101 | maxchurn = float(max(1, stats[0][2])) |
|
102 | 102 | maxuser = max([len(u) for k, u, l in stats]) |
|
103 | 103 | |
|
104 | 104 | ttywidth = get_tty_width() |
|
105 | 105 | ui.debug(_("assuming %i character terminal\n") % ttywidth) |
|
106 | 106 | width = ttywidth - maxuser - 2 - 6 - 2 - 2 |
|
107 | 107 | |
|
108 | 108 | for k, user, churn in stats: |
|
109 | 109 | print "%s %6d %s" % (pad(user, maxuser), churn, |
|
110 | 110 | "*" * int(churn * width / maxchurn)) |
|
111 | 111 | |
|
112 | 112 | cmdtable = { |
|
113 | 113 | "churn": |
|
114 | 114 | (churn, |
|
115 | 115 | [('r', 'rev', [], _('limit statistics to the specified revisions')), |
|
116 | 116 | ('', 'aliases', '', _('file with email aliases')), |
|
117 | 117 | ('', 'progress', None, _('show progress'))], |
|
118 | 118 | 'hg churn [-r REVISIONS] [--aliases FILE] [--progress]'), |
|
119 | 119 | } |
General Comments 0
You need to be logged in to leave comments.
Login now