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