Mercurial > hg > mercurial-crew-with-dirclash
annotate hgext/churn.py @ 3046:461573aa02ef
[churn] Ignore merge csets
Merge changesets attributed a large number of lines to people that didn't
actually develop it, but simply merged the code. By ignoring merges, we get
a more accurate picture of how much code everyone changed.
author | Josef "Jeff" Sipek <jeffpc@josefsipek.net> |
---|---|
date | Sun, 03 Sep 2006 16:25:41 -0400 |
parents | 8d344bc72e68 |
children | dd1a142988d3 |
rev | line source |
---|---|
3037 | 1 # churn.py - create a graph showing who changed the most lines |
2 # | |
3 # Copyright 2006 Josef "Jeff" Sipek <jeffpc@josefsipek.net> | |
4 # | |
5 # This software may be used and distributed according to the terms | |
6 # of the GNU General Public License, incorporated herein by reference. | |
7 # | |
8 # | |
9 # Aliases map file format is simple one alias per line in the following | |
10 # format: | |
11 # | |
12 # <alias email> <actual email> | |
13 | |
3038
45942bb49194
[churn] Cleanup suggestions from tonfa
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
3037
diff
changeset
|
14 from mercurial.demandload import * |
3042
c0be8990e819
Add revision range support
Brendan Cully <brendan@kublai.com>
parents:
3040
diff
changeset
|
15 from mercurial.i18n import gettext as _ |
3038
45942bb49194
[churn] Cleanup suggestions from tonfa
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
3037
diff
changeset
|
16 demandload(globals(), 'time sys signal os') |
3046
461573aa02ef
[churn] Ignore merge csets
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
3045
diff
changeset
|
17 demandload(globals(), 'mercurial:hg,mdiff,fancyopts,commands,ui,util,templater,node') |
3037 | 18 |
19 def __gather(ui, repo, node1, node2): | |
20 def dirtywork(f, mmap1, mmap2): | |
21 lines = 0 | |
22 | |
3039
2d35d7c6f251
[churn] Trivial cleanup suggested by Thomas
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
3038
diff
changeset
|
23 to = mmap1 and repo.file(f).read(mmap1[f]) or None |
2d35d7c6f251
[churn] Trivial cleanup suggested by Thomas
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
3038
diff
changeset
|
24 tn = mmap2 and repo.file(f).read(mmap2[f]) or None |
3037 | 25 |
26 diff = mdiff.unidiff(to, "", tn, "", f).split("\n") | |
27 | |
28 for line in diff: | |
3040
fe0e3508ec6e
[churn] Trivial cleanup
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
3039
diff
changeset
|
29 if not line: |
3037 | 30 continue # skip EOF |
3039
2d35d7c6f251
[churn] Trivial cleanup suggested by Thomas
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
3038
diff
changeset
|
31 if line.startswith(" "): |
3037 | 32 continue # context line |
3039
2d35d7c6f251
[churn] Trivial cleanup suggested by Thomas
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
3038
diff
changeset
|
33 if line.startswith("--- ") or line.startswith("+++ "): |
3037 | 34 continue # begining of diff |
3039
2d35d7c6f251
[churn] Trivial cleanup suggested by Thomas
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
3038
diff
changeset
|
35 if line.startswith("@@ "): |
3037 | 36 continue # info line |
37 | |
38 # changed lines | |
39 lines += 1 | |
40 | |
41 return lines | |
42 | |
43 ## | |
44 | |
45 lines = 0 | |
46 | |
3045
8d344bc72e68
[churn] repo.changes was renamed to repo.status
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
3043
diff
changeset
|
47 changes = repo.status(node1, node2, None, util.always)[:5] |
3037 | 48 |
49 modified, added, removed, deleted, unknown = changes | |
50 | |
51 who = repo.changelog.read(node2)[1] | |
52 who = templater.email(who) # get the email of the person | |
53 | |
54 mmap1 = repo.manifest.read(repo.changelog.read(node1)[0]) | |
55 mmap2 = repo.manifest.read(repo.changelog.read(node2)[0]) | |
56 for f in modified: | |
57 lines += dirtywork(f, mmap1, mmap2) | |
58 | |
59 for f in added: | |
60 lines += dirtywork(f, None, mmap2) | |
61 | |
62 for f in removed: | |
63 lines += dirtywork(f, mmap1, None) | |
64 | |
65 for f in deleted: | |
66 lines += dirtywork(f, mmap1, mmap2) | |
67 | |
68 for f in unknown: | |
69 lines += dirtywork(f, mmap1, mmap2) | |
70 | |
71 return (who, lines) | |
72 | |
3042
c0be8990e819
Add revision range support
Brendan Cully <brendan@kublai.com>
parents:
3040
diff
changeset
|
73 def gather_stats(ui, repo, amap, revs=None): |
3037 | 74 stats = {} |
75 | |
76 cl = repo.changelog | |
77 | |
3042
c0be8990e819
Add revision range support
Brendan Cully <brendan@kublai.com>
parents:
3040
diff
changeset
|
78 if not revs: |
3043 | 79 revs = range(0, cl.count()) |
3042
c0be8990e819
Add revision range support
Brendan Cully <brendan@kublai.com>
parents:
3040
diff
changeset
|
80 |
c0be8990e819
Add revision range support
Brendan Cully <brendan@kublai.com>
parents:
3040
diff
changeset
|
81 for rev in revs: |
3037 | 82 node2 = cl.node(rev) |
83 node1 = cl.parents(node2)[0] | |
84 | |
3046
461573aa02ef
[churn] Ignore merge csets
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
3045
diff
changeset
|
85 if cl.parents(node2)[1] != node.nullid: |
461573aa02ef
[churn] Ignore merge csets
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
3045
diff
changeset
|
86 ui.note(_('Revision %d is a merge, ignoring...\n') % (rev,)) |
461573aa02ef
[churn] Ignore merge csets
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
3045
diff
changeset
|
87 continue |
461573aa02ef
[churn] Ignore merge csets
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
3045
diff
changeset
|
88 |
3037 | 89 who, lines = __gather(ui, repo, node1, node2) |
90 | |
91 # remap the owner if possible | |
92 if amap.has_key(who): | |
93 ui.note("using '%s' alias for '%s'\n" % (amap[who], who)) | |
94 who = amap[who] | |
95 | |
96 if not stats.has_key(who): | |
97 stats[who] = 0 | |
98 stats[who] += lines | |
99 | |
100 ui.note("rev %d: %d lines by %s\n" % (rev, lines, who)) | |
101 | |
102 return stats | |
103 | |
3042
c0be8990e819
Add revision range support
Brendan Cully <brendan@kublai.com>
parents:
3040
diff
changeset
|
104 def churn(ui, repo, **opts): |
3037 | 105 "Graphs the number of lines changed" |
106 | |
107 def pad(s, l): | |
108 if len(s) < l: | |
109 return s + " " * (l-len(s)) | |
110 return s[0:l] | |
111 | |
112 def graph(n, maximum, width, char): | |
113 n = int(n * width / float(maximum)) | |
114 | |
115 return char * (n) | |
116 | |
117 def get_aliases(f): | |
118 aliases = {} | |
119 | |
120 for l in f.readlines(): | |
121 l = l.strip() | |
122 alias, actual = l.split(" ") | |
123 aliases[alias] = actual | |
124 | |
125 return aliases | |
126 | |
127 amap = {} | |
3042
c0be8990e819
Add revision range support
Brendan Cully <brendan@kublai.com>
parents:
3040
diff
changeset
|
128 aliases = opts.get('aliases') |
3037 | 129 if aliases: |
130 try: | |
131 f = open(aliases,"r") | |
132 except OSError, e: | |
133 print "Error: " + e | |
134 return | |
135 | |
136 amap = get_aliases(f) | |
137 f.close() | |
3042
c0be8990e819
Add revision range support
Brendan Cully <brendan@kublai.com>
parents:
3040
diff
changeset
|
138 |
c0be8990e819
Add revision range support
Brendan Cully <brendan@kublai.com>
parents:
3040
diff
changeset
|
139 revs = [int(r) for r in commands.revrange(ui, repo, opts['rev'])] |
c0be8990e819
Add revision range support
Brendan Cully <brendan@kublai.com>
parents:
3040
diff
changeset
|
140 revs.sort() |
c0be8990e819
Add revision range support
Brendan Cully <brendan@kublai.com>
parents:
3040
diff
changeset
|
141 stats = gather_stats(ui, repo, amap, revs) |
3037 | 142 |
143 # make a list of tuples (name, lines) and sort it in descending order | |
144 ordered = stats.items() | |
3039
2d35d7c6f251
[churn] Trivial cleanup suggested by Thomas
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
3038
diff
changeset
|
145 ordered.sort(cmp=lambda x, y: cmp(y[1], x[1])) |
3037 | 146 |
147 maximum = ordered[0][1] | |
148 | |
149 ui.note("Assuming 80 character terminal\n") | |
150 width = 80 - 1 | |
151 | |
152 for i in ordered: | |
153 person = i[0] | |
154 lines = i[1] | |
155 print "%s %6d %s" % (pad(person, 20), lines, | |
156 graph(lines, maximum, width - 20 - 1 - 6 - 2 - 2, '*')) | |
157 | |
158 cmdtable = { | |
159 "churn": | |
160 (churn, | |
3042
c0be8990e819
Add revision range support
Brendan Cully <brendan@kublai.com>
parents:
3040
diff
changeset
|
161 [('r', 'rev', [], _('limit statistics to the specified revisions')), |
c0be8990e819
Add revision range support
Brendan Cully <brendan@kublai.com>
parents:
3040
diff
changeset
|
162 ('', 'aliases', '', _('file with email aliases'))], |
c0be8990e819
Add revision range support
Brendan Cully <brendan@kublai.com>
parents:
3040
diff
changeset
|
163 'hg churn [-r revision range] [-a file]'), |
3037 | 164 } |