Mercurial > public > mercurial-scm > hg
annotate hgext/churn.py @ 3044:a10adb6a9c9c
[churn] Fixed churn ignoring the first commit
author | Josef "Jeff" Sipek <jeff@josefsipek.net> |
---|---|
date | Sat, 19 Aug 2006 14:47:31 -0400 |
parents | fe0e3508ec6e |
children | 2a4d4aecb2b4 |
rev | line source |
---|---|
3040 | 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 | |
3041
45942bb49194
[churn] Cleanup suggestions from tonfa
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
3040
diff
changeset
|
14 from mercurial.demandload import * |
45942bb49194
[churn] Cleanup suggestions from tonfa
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
3040
diff
changeset
|
15 demandload(globals(), 'time sys signal os') |
45942bb49194
[churn] Cleanup suggestions from tonfa
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
3040
diff
changeset
|
16 demandload(globals(), 'mercurial:hg,mdiff,fancyopts,commands,ui,util,templater') |
3040 | 17 |
18 def __gather(ui, repo, node1, node2): | |
19 def dirtywork(f, mmap1, mmap2): | |
20 lines = 0 | |
21 | |
3042
2d35d7c6f251
[churn] Trivial cleanup suggested by Thomas
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
3041
diff
changeset
|
22 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:
3041
diff
changeset
|
23 tn = mmap2 and repo.file(f).read(mmap2[f]) or None |
3040 | 24 |
25 diff = mdiff.unidiff(to, "", tn, "", f).split("\n") | |
26 | |
27 for line in diff: | |
3043
fe0e3508ec6e
[churn] Trivial cleanup
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
3042
diff
changeset
|
28 if not line: |
3040 | 29 continue # skip EOF |
3042
2d35d7c6f251
[churn] Trivial cleanup suggested by Thomas
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
3041
diff
changeset
|
30 if line.startswith(" "): |
3040 | 31 continue # context line |
3042
2d35d7c6f251
[churn] Trivial cleanup suggested by Thomas
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
3041
diff
changeset
|
32 if line.startswith("--- ") or line.startswith("+++ "): |
3040 | 33 continue # begining of diff |
3042
2d35d7c6f251
[churn] Trivial cleanup suggested by Thomas
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
3041
diff
changeset
|
34 if line.startswith("@@ "): |
3040 | 35 continue # info line |
36 | |
37 # changed lines | |
38 lines += 1 | |
39 | |
40 return lines | |
41 | |
42 ## | |
43 | |
44 lines = 0 | |
45 | |
46 changes = repo.changes(node1, node2, None, util.always) | |
47 | |
48 modified, added, removed, deleted, unknown = changes | |
49 | |
50 who = repo.changelog.read(node2)[1] | |
51 who = templater.email(who) # get the email of the person | |
52 | |
53 mmap1 = repo.manifest.read(repo.changelog.read(node1)[0]) | |
54 mmap2 = repo.manifest.read(repo.changelog.read(node2)[0]) | |
55 for f in modified: | |
56 lines += dirtywork(f, mmap1, mmap2) | |
57 | |
58 for f in added: | |
59 lines += dirtywork(f, None, mmap2) | |
60 | |
61 for f in removed: | |
62 lines += dirtywork(f, mmap1, None) | |
63 | |
64 for f in deleted: | |
65 lines += dirtywork(f, mmap1, mmap2) | |
66 | |
67 for f in unknown: | |
68 lines += dirtywork(f, mmap1, mmap2) | |
69 | |
70 return (who, lines) | |
71 | |
72 def gather_stats(ui, repo, amap): | |
73 stats = {} | |
74 | |
75 cl = repo.changelog | |
76 | |
3044
a10adb6a9c9c
[churn] Fixed churn ignoring the first commit
Josef "Jeff" Sipek <jeff@josefsipek.net>
parents:
3043
diff
changeset
|
77 for rev in range(0,cl.count()): |
3040 | 78 node2 = cl.node(rev) |
79 node1 = cl.parents(node2)[0] | |
80 | |
81 who, lines = __gather(ui, repo, node1, node2) | |
82 | |
83 # remap the owner if possible | |
84 if amap.has_key(who): | |
85 ui.note("using '%s' alias for '%s'\n" % (amap[who], who)) | |
86 who = amap[who] | |
87 | |
88 if not stats.has_key(who): | |
89 stats[who] = 0 | |
90 stats[who] += lines | |
91 | |
92 ui.note("rev %d: %d lines by %s\n" % (rev, lines, who)) | |
93 | |
94 return stats | |
95 | |
96 def churn(ui, repo, aliases): | |
97 "Graphs the number of lines changed" | |
98 | |
99 def pad(s, l): | |
100 if len(s) < l: | |
101 return s + " " * (l-len(s)) | |
102 return s[0:l] | |
103 | |
104 def graph(n, maximum, width, char): | |
105 n = int(n * width / float(maximum)) | |
106 | |
107 return char * (n) | |
108 | |
109 def get_aliases(f): | |
110 aliases = {} | |
111 | |
112 for l in f.readlines(): | |
113 l = l.strip() | |
114 alias, actual = l.split(" ") | |
115 aliases[alias] = actual | |
116 | |
117 return aliases | |
118 | |
119 amap = {} | |
120 if aliases: | |
121 try: | |
122 f = open(aliases,"r") | |
123 except OSError, e: | |
124 print "Error: " + e | |
125 return | |
126 | |
127 amap = get_aliases(f) | |
128 f.close() | |
129 | |
130 stats = gather_stats(ui, repo, amap) | |
131 | |
132 # make a list of tuples (name, lines) and sort it in descending order | |
133 ordered = stats.items() | |
3042
2d35d7c6f251
[churn] Trivial cleanup suggested by Thomas
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
3041
diff
changeset
|
134 ordered.sort(cmp=lambda x, y: cmp(y[1], x[1])) |
3040 | 135 |
136 maximum = ordered[0][1] | |
137 | |
138 ui.note("Assuming 80 character terminal\n") | |
139 width = 80 - 1 | |
140 | |
141 for i in ordered: | |
142 person = i[0] | |
143 lines = i[1] | |
144 print "%s %6d %s" % (pad(person, 20), lines, | |
145 graph(lines, maximum, width - 20 - 1 - 6 - 2 - 2, '*')) | |
146 | |
147 cmdtable = { | |
148 "churn": | |
149 (churn, | |
150 [('', 'aliases', '', 'file with email aliases')], | |
151 'hg churn [-a file]'), | |
152 } | |
153 | |
154 def reposetup(ui, repo): | |
155 pass | |
156 |