##// END OF EJS Templates
churn: simplify hg status call
Matt Mackall -
r6598:7fc87fa4 default
parent child Browse files
Show More
@@ -1,206 +1,206 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 #
7 #
8 #
8 #
9 # Aliases map file format is simple one alias per line in the following
9 # Aliases map file format is simple one alias per line in the following
10 # format:
10 # format:
11 #
11 #
12 # <alias email> <actual email>
12 # <alias email> <actual email>
13
13
14 from mercurial.i18n import gettext as _
14 from mercurial.i18n import gettext as _
15 from mercurial import mdiff, cmdutil, util, node
15 from mercurial import mdiff, cmdutil, util, node
16 import os, sys
16 import os, sys
17
17
18 def get_tty_width():
18 def get_tty_width():
19 if 'COLUMNS' in os.environ:
19 if 'COLUMNS' in os.environ:
20 try:
20 try:
21 return int(os.environ['COLUMNS'])
21 return int(os.environ['COLUMNS'])
22 except ValueError:
22 except ValueError:
23 pass
23 pass
24 try:
24 try:
25 import termios, array, fcntl
25 import termios, array, fcntl
26 for dev in (sys.stdout, sys.stdin):
26 for dev in (sys.stdout, sys.stdin):
27 try:
27 try:
28 fd = dev.fileno()
28 fd = dev.fileno()
29 if not os.isatty(fd):
29 if not os.isatty(fd):
30 continue
30 continue
31 arri = fcntl.ioctl(fd, termios.TIOCGWINSZ, '\0' * 8)
31 arri = fcntl.ioctl(fd, termios.TIOCGWINSZ, '\0' * 8)
32 return array.array('h', arri)[1]
32 return array.array('h', arri)[1]
33 except ValueError:
33 except ValueError:
34 pass
34 pass
35 except ImportError:
35 except ImportError:
36 pass
36 pass
37 return 80
37 return 80
38
38
39 def __gather(ui, repo, node1, node2):
39 def __gather(ui, repo, node1, node2):
40 def dirtywork(f, mmap1, mmap2):
40 def dirtywork(f, mmap1, mmap2):
41 lines = 0
41 lines = 0
42
42
43 to = mmap1 and repo.file(f).read(mmap1[f]) or None
43 to = mmap1 and repo.file(f).read(mmap1[f]) or None
44 tn = mmap2 and repo.file(f).read(mmap2[f]) or None
44 tn = mmap2 and repo.file(f).read(mmap2[f]) or None
45
45
46 diff = mdiff.unidiff(to, "", tn, "", f, f).split("\n")
46 diff = mdiff.unidiff(to, "", tn, "", f, f).split("\n")
47
47
48 for line in diff:
48 for line in diff:
49 if not line:
49 if not line:
50 continue # skip EOF
50 continue # skip EOF
51 if line.startswith(" "):
51 if line.startswith(" "):
52 continue # context line
52 continue # context line
53 if line.startswith("--- ") or line.startswith("+++ "):
53 if line.startswith("--- ") or line.startswith("+++ "):
54 continue # begining of diff
54 continue # begining of diff
55 if line.startswith("@@ "):
55 if line.startswith("@@ "):
56 continue # info line
56 continue # info line
57
57
58 # changed lines
58 # changed lines
59 lines += 1
59 lines += 1
60
60
61 return lines
61 return lines
62
62
63 ##
63 ##
64
64
65 lines = 0
65 lines = 0
66
66
67 changes = repo.status(node1, node2, None, util.always)[:5]
67 changes = repo.status(node1, node2)[:5]
68
68
69 modified, added, removed, deleted, unknown = changes
69 modified, added, removed, deleted, unknown = changes
70
70
71 who = repo.changelog.read(node2)[1]
71 who = repo.changelog.read(node2)[1]
72 who = util.email(who) # get the email of the person
72 who = util.email(who) # get the email of the person
73
73
74 mmap1 = repo.manifest.read(repo.changelog.read(node1)[0])
74 mmap1 = repo.manifest.read(repo.changelog.read(node1)[0])
75 mmap2 = repo.manifest.read(repo.changelog.read(node2)[0])
75 mmap2 = repo.manifest.read(repo.changelog.read(node2)[0])
76 for f in modified:
76 for f in modified:
77 lines += dirtywork(f, mmap1, mmap2)
77 lines += dirtywork(f, mmap1, mmap2)
78
78
79 for f in added:
79 for f in added:
80 lines += dirtywork(f, None, mmap2)
80 lines += dirtywork(f, None, mmap2)
81
81
82 for f in removed:
82 for f in removed:
83 lines += dirtywork(f, mmap1, None)
83 lines += dirtywork(f, mmap1, None)
84
84
85 for f in deleted:
85 for f in deleted:
86 lines += dirtywork(f, mmap1, mmap2)
86 lines += dirtywork(f, mmap1, mmap2)
87
87
88 for f in unknown:
88 for f in unknown:
89 lines += dirtywork(f, mmap1, mmap2)
89 lines += dirtywork(f, mmap1, mmap2)
90
90
91 return (who, lines)
91 return (who, lines)
92
92
93 def gather_stats(ui, repo, amap, revs=None, progress=False):
93 def gather_stats(ui, repo, amap, revs=None, progress=False):
94 stats = {}
94 stats = {}
95
95
96 cl = repo.changelog
96 cl = repo.changelog
97
97
98 if not revs:
98 if not revs:
99 revs = range(0, cl.count())
99 revs = range(0, cl.count())
100
100
101 nr_revs = len(revs)
101 nr_revs = len(revs)
102 cur_rev = 0
102 cur_rev = 0
103
103
104 for rev in revs:
104 for rev in revs:
105 cur_rev += 1 # next revision
105 cur_rev += 1 # next revision
106
106
107 node2 = cl.node(rev)
107 node2 = cl.node(rev)
108 node1 = cl.parents(node2)[0]
108 node1 = cl.parents(node2)[0]
109
109
110 if cl.parents(node2)[1] != node.nullid:
110 if cl.parents(node2)[1] != node.nullid:
111 ui.note(_('Revision %d is a merge, ignoring...\n') % (rev,))
111 ui.note(_('Revision %d is a merge, ignoring...\n') % (rev,))
112 continue
112 continue
113
113
114 who, lines = __gather(ui, repo, node1, node2)
114 who, lines = __gather(ui, repo, node1, node2)
115
115
116 # remap the owner if possible
116 # remap the owner if possible
117 if who in amap:
117 if who in amap:
118 ui.note("using '%s' alias for '%s'\n" % (amap[who], who))
118 ui.note("using '%s' alias for '%s'\n" % (amap[who], who))
119 who = amap[who]
119 who = amap[who]
120
120
121 if not who in stats:
121 if not who in stats:
122 stats[who] = 0
122 stats[who] = 0
123 stats[who] += lines
123 stats[who] += lines
124
124
125 ui.note("rev %d: %d lines by %s\n" % (rev, lines, who))
125 ui.note("rev %d: %d lines by %s\n" % (rev, lines, who))
126
126
127 if progress:
127 if progress:
128 nr_revs = max(nr_revs, 1)
128 nr_revs = max(nr_revs, 1)
129 if int(100.0*(cur_rev - 1)/nr_revs) < int(100.0*cur_rev/nr_revs):
129 if int(100.0*(cur_rev - 1)/nr_revs) < int(100.0*cur_rev/nr_revs):
130 ui.write("\rGenerating stats: %d%%" % (int(100.0*cur_rev/nr_revs),))
130 ui.write("\rGenerating stats: %d%%" % (int(100.0*cur_rev/nr_revs),))
131 sys.stdout.flush()
131 sys.stdout.flush()
132
132
133 if progress:
133 if progress:
134 ui.write("\r")
134 ui.write("\r")
135 sys.stdout.flush()
135 sys.stdout.flush()
136
136
137 return stats
137 return stats
138
138
139 def churn(ui, repo, **opts):
139 def churn(ui, repo, **opts):
140 "Graphs the number of lines changed"
140 "Graphs the number of lines changed"
141
141
142 def pad(s, l):
142 def pad(s, l):
143 if len(s) < l:
143 if len(s) < l:
144 return s + " " * (l-len(s))
144 return s + " " * (l-len(s))
145 return s[0:l]
145 return s[0:l]
146
146
147 def graph(n, maximum, width, char):
147 def graph(n, maximum, width, char):
148 maximum = max(1, maximum)
148 maximum = max(1, maximum)
149 n = int(n * width / float(maximum))
149 n = int(n * width / float(maximum))
150
150
151 return char * (n)
151 return char * (n)
152
152
153 def get_aliases(f):
153 def get_aliases(f):
154 aliases = {}
154 aliases = {}
155
155
156 for l in f.readlines():
156 for l in f.readlines():
157 l = l.strip()
157 l = l.strip()
158 alias, actual = l.split()
158 alias, actual = l.split()
159 aliases[alias] = actual
159 aliases[alias] = actual
160
160
161 return aliases
161 return aliases
162
162
163 amap = {}
163 amap = {}
164 aliases = opts.get('aliases')
164 aliases = opts.get('aliases')
165 if aliases:
165 if aliases:
166 try:
166 try:
167 f = open(aliases,"r")
167 f = open(aliases,"r")
168 except OSError, e:
168 except OSError, e:
169 print "Error: " + e
169 print "Error: " + e
170 return
170 return
171
171
172 amap = get_aliases(f)
172 amap = get_aliases(f)
173 f.close()
173 f.close()
174
174
175 revs = [int(r) for r in cmdutil.revrange(repo, opts['rev'])]
175 revs = [int(r) for r in cmdutil.revrange(repo, opts['rev'])]
176 revs.sort()
176 revs.sort()
177 stats = gather_stats(ui, repo, amap, revs, opts.get('progress'))
177 stats = gather_stats(ui, repo, amap, revs, opts.get('progress'))
178
178
179 # make a list of tuples (name, lines) and sort it in descending order
179 # make a list of tuples (name, lines) and sort it in descending order
180 ordered = stats.items()
180 ordered = stats.items()
181 if not ordered:
181 if not ordered:
182 return
182 return
183 ordered.sort(lambda x, y: cmp(y[1], x[1]))
183 ordered.sort(lambda x, y: cmp(y[1], x[1]))
184 max_churn = ordered[0][1]
184 max_churn = ordered[0][1]
185
185
186 tty_width = get_tty_width()
186 tty_width = get_tty_width()
187 ui.note(_("assuming %i character terminal\n") % tty_width)
187 ui.note(_("assuming %i character terminal\n") % tty_width)
188 tty_width -= 1
188 tty_width -= 1
189
189
190 max_user_width = max([len(user) for user, churn in ordered])
190 max_user_width = max([len(user) for user, churn in ordered])
191
191
192 graph_width = tty_width - max_user_width - 1 - 6 - 2 - 2
192 graph_width = tty_width - max_user_width - 1 - 6 - 2 - 2
193
193
194 for user, churn in ordered:
194 for user, churn in ordered:
195 print "%s %6d %s" % (pad(user, max_user_width),
195 print "%s %6d %s" % (pad(user, max_user_width),
196 churn,
196 churn,
197 graph(churn, max_churn, graph_width, '*'))
197 graph(churn, max_churn, graph_width, '*'))
198
198
199 cmdtable = {
199 cmdtable = {
200 "churn":
200 "churn":
201 (churn,
201 (churn,
202 [('r', 'rev', [], _('limit statistics to the specified revisions')),
202 [('r', 'rev', [], _('limit statistics to the specified revisions')),
203 ('', 'aliases', '', _('file with email aliases')),
203 ('', 'aliases', '', _('file with email aliases')),
204 ('', 'progress', None, _('show progress'))],
204 ('', 'progress', None, _('show progress'))],
205 'hg churn [-r revision range] [-a file] [--progress]'),
205 'hg churn [-r revision range] [-a file] [--progress]'),
206 }
206 }
General Comments 0
You need to be logged in to leave comments. Login now