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