##// END OF EJS Templates
churn: use genexps now that we dropped 2.3 compatibility
Nicolas Dumazet -
r9390:637f2726 default
parent child Browse files
Show More
@@ -1,177 +1,177
1 # churn.py - create a graph of revisions count grouped by template
1 # churn.py - create a graph of revisions count grouped by template
2 #
2 #
3 # Copyright 2006 Josef "Jeff" Sipek <jeffpc@josefsipek.net>
3 # Copyright 2006 Josef "Jeff" Sipek <jeffpc@josefsipek.net>
4 # Copyright 2008 Alexander Solovyov <piranha@piranha.org.ua>
4 # Copyright 2008 Alexander Solovyov <piranha@piranha.org.ua>
5 #
5 #
6 # This software may be used and distributed according to the terms of the
6 # This software may be used and distributed according to the terms of the
7 # GNU General Public License version 2, incorporated herein by reference.
7 # GNU General Public License version 2, incorporated herein by reference.
8
8
9 '''command to display statistics about repository history'''
9 '''command to display statistics about repository history'''
10
10
11 from mercurial.i18n import _
11 from mercurial.i18n import _
12 from mercurial import patch, cmdutil, util, templater
12 from mercurial import patch, cmdutil, util, templater
13 import sys, os
13 import sys, os
14 import time, datetime
14 import time, datetime
15
15
16 def maketemplater(ui, repo, tmpl):
16 def maketemplater(ui, repo, tmpl):
17 tmpl = templater.parsestring(tmpl, quoted=False)
17 tmpl = templater.parsestring(tmpl, quoted=False)
18 try:
18 try:
19 t = cmdutil.changeset_templater(ui, repo, False, None, None, False)
19 t = cmdutil.changeset_templater(ui, repo, False, None, None, False)
20 except SyntaxError, inst:
20 except SyntaxError, inst:
21 raise util.Abort(inst.args[0])
21 raise util.Abort(inst.args[0])
22 t.use_template(tmpl)
22 t.use_template(tmpl)
23 return t
23 return t
24
24
25 def changedlines(ui, repo, ctx1, ctx2, fns):
25 def changedlines(ui, repo, ctx1, ctx2, fns):
26 lines = 0
26 lines = 0
27 fmatch = cmdutil.matchfiles(repo, fns)
27 fmatch = cmdutil.matchfiles(repo, fns)
28 diff = ''.join(patch.diff(repo, ctx1.node(), ctx2.node(), fmatch))
28 diff = ''.join(patch.diff(repo, ctx1.node(), ctx2.node(), fmatch))
29 for l in diff.split('\n'):
29 for l in diff.split('\n'):
30 if (l.startswith("+") and not l.startswith("+++ ") or
30 if (l.startswith("+") and not l.startswith("+++ ") or
31 l.startswith("-") and not l.startswith("--- ")):
31 l.startswith("-") and not l.startswith("--- ")):
32 lines += 1
32 lines += 1
33 return lines
33 return lines
34
34
35 def countrate(ui, repo, amap, *pats, **opts):
35 def countrate(ui, repo, amap, *pats, **opts):
36 """Calculate stats"""
36 """Calculate stats"""
37 if opts.get('dateformat'):
37 if opts.get('dateformat'):
38 def getkey(ctx):
38 def getkey(ctx):
39 t, tz = ctx.date()
39 t, tz = ctx.date()
40 date = datetime.datetime(*time.gmtime(float(t) - tz)[:6])
40 date = datetime.datetime(*time.gmtime(float(t) - tz)[:6])
41 return date.strftime(opts['dateformat'])
41 return date.strftime(opts['dateformat'])
42 else:
42 else:
43 tmpl = opts.get('template', '{author|email}')
43 tmpl = opts.get('template', '{author|email}')
44 tmpl = maketemplater(ui, repo, tmpl)
44 tmpl = maketemplater(ui, repo, tmpl)
45 def getkey(ctx):
45 def getkey(ctx):
46 ui.pushbuffer()
46 ui.pushbuffer()
47 tmpl.show(ctx)
47 tmpl.show(ctx)
48 return ui.popbuffer()
48 return ui.popbuffer()
49
49
50 count = pct = 0
50 count = pct = 0
51 rate = {}
51 rate = {}
52 df = False
52 df = False
53 if opts.get('date'):
53 if opts.get('date'):
54 df = util.matchdate(opts['date'])
54 df = util.matchdate(opts['date'])
55
55
56 get = util.cachefunc(lambda r: repo[r])
56 get = util.cachefunc(lambda r: repo[r])
57 changeiter, matchfn = cmdutil.walkchangerevs(ui, repo, pats, get, opts)
57 changeiter, matchfn = cmdutil.walkchangerevs(ui, repo, pats, get, opts)
58 for st, rev, fns in changeiter:
58 for st, rev, fns in changeiter:
59
59
60 if not st == 'add':
60 if not st == 'add':
61 continue
61 continue
62
62
63 ctx = get(rev)
63 ctx = get(rev)
64 if df and not df(ctx.date()[0]): # doesn't match date format
64 if df and not df(ctx.date()[0]): # doesn't match date format
65 continue
65 continue
66
66
67 key = getkey(ctx)
67 key = getkey(ctx)
68 key = amap.get(key, key) # alias remap
68 key = amap.get(key, key) # alias remap
69 if opts.get('changesets'):
69 if opts.get('changesets'):
70 rate[key] = rate.get(key, 0) + 1
70 rate[key] = rate.get(key, 0) + 1
71 else:
71 else:
72 parents = ctx.parents()
72 parents = ctx.parents()
73 if len(parents) > 1:
73 if len(parents) > 1:
74 ui.note(_('Revision %d is a merge, ignoring...\n') % (rev,))
74 ui.note(_('Revision %d is a merge, ignoring...\n') % (rev,))
75 continue
75 continue
76
76
77 ctx1 = parents[0]
77 ctx1 = parents[0]
78 lines = changedlines(ui, repo, ctx1, ctx, fns)
78 lines = changedlines(ui, repo, ctx1, ctx, fns)
79 rate[key] = rate.get(key, 0) + lines
79 rate[key] = rate.get(key, 0) + lines
80
80
81 if opts.get('progress'):
81 if opts.get('progress'):
82 count += 1
82 count += 1
83 newpct = int(100.0 * count / max(len(repo), 1))
83 newpct = int(100.0 * count / max(len(repo), 1))
84 if pct < newpct:
84 if pct < newpct:
85 pct = newpct
85 pct = newpct
86 ui.write("\r" + _("generating stats: %d%%") % pct)
86 ui.write("\r" + _("generating stats: %d%%") % pct)
87 sys.stdout.flush()
87 sys.stdout.flush()
88
88
89 if opts.get('progress'):
89 if opts.get('progress'):
90 ui.write("\r")
90 ui.write("\r")
91 sys.stdout.flush()
91 sys.stdout.flush()
92
92
93 return rate
93 return rate
94
94
95
95
96 def churn(ui, repo, *pats, **opts):
96 def churn(ui, repo, *pats, **opts):
97 '''histogram of changes to the repository
97 '''histogram of changes to the repository
98
98
99 This command will display a histogram representing the number
99 This command will display a histogram representing the number
100 of changed lines or revisions, grouped according to the given
100 of changed lines or revisions, grouped according to the given
101 template. The default template will group changes by author.
101 template. The default template will group changes by author.
102 The --dateformat option may be used to group the results by
102 The --dateformat option may be used to group the results by
103 date instead.
103 date instead.
104
104
105 Statistics are based on the number of changed lines, or
105 Statistics are based on the number of changed lines, or
106 alternatively the number of matching revisions if the
106 alternatively the number of matching revisions if the
107 --changesets option is specified.
107 --changesets option is specified.
108
108
109 Examples::
109 Examples::
110
110
111 # display count of changed lines for every committer
111 # display count of changed lines for every committer
112 hg churn -t '{author|email}'
112 hg churn -t '{author|email}'
113
113
114 # display daily activity graph
114 # display daily activity graph
115 hg churn -f '%H' -s -c
115 hg churn -f '%H' -s -c
116
116
117 # display activity of developers by month
117 # display activity of developers by month
118 hg churn -f '%Y-%m' -s -c
118 hg churn -f '%Y-%m' -s -c
119
119
120 # display count of lines changed in every year
120 # display count of lines changed in every year
121 hg churn -f '%Y' -s
121 hg churn -f '%Y' -s
122
122
123 It is possible to map alternate email addresses to a main address
123 It is possible to map alternate email addresses to a main address
124 by providing a file using the following format::
124 by providing a file using the following format::
125
125
126 <alias email> <actual email>
126 <alias email> <actual email>
127
127
128 Such a file may be specified with the --aliases option, otherwise
128 Such a file may be specified with the --aliases option, otherwise
129 a .hgchurn file will be looked for in the working directory root.
129 a .hgchurn file will be looked for in the working directory root.
130 '''
130 '''
131 def pad(s, l):
131 def pad(s, l):
132 return (s + " " * l)[:l]
132 return (s + " " * l)[:l]
133
133
134 amap = {}
134 amap = {}
135 aliases = opts.get('aliases')
135 aliases = opts.get('aliases')
136 if not aliases and os.path.exists(repo.wjoin('.hgchurn')):
136 if not aliases and os.path.exists(repo.wjoin('.hgchurn')):
137 aliases = repo.wjoin('.hgchurn')
137 aliases = repo.wjoin('.hgchurn')
138 if aliases:
138 if aliases:
139 for l in open(aliases, "r"):
139 for l in open(aliases, "r"):
140 l = l.strip()
140 l = l.strip()
141 alias, actual = l.split()
141 alias, actual = l.split()
142 amap[alias] = actual
142 amap[alias] = actual
143
143
144 rate = countrate(ui, repo, amap, *pats, **opts).items()
144 rate = countrate(ui, repo, amap, *pats, **opts).items()
145 if not rate:
145 if not rate:
146 return
146 return
147
147
148 sortkey = ((not opts.get('sort')) and (lambda x: -x[1]) or None)
148 sortkey = ((not opts.get('sort')) and (lambda x: -x[1]) or None)
149 rate.sort(key=sortkey)
149 rate.sort(key=sortkey)
150
150
151 # Be careful not to have a zero maxcount (issue833)
151 # Be careful not to have a zero maxcount (issue833)
152 maxcount = float(max([v for k, v in rate])) or 1.0
152 maxcount = float(max(v for k, v in rate)) or 1.0
153 maxname = max([len(k) for k, v in rate])
153 maxname = max(len(k) for k, v in rate)
154
154
155 ttywidth = util.termwidth()
155 ttywidth = util.termwidth()
156 ui.debug(_("assuming %i character terminal\n") % ttywidth)
156 ui.debug(_("assuming %i character terminal\n") % ttywidth)
157 width = ttywidth - maxname - 2 - 6 - 2 - 2
157 width = ttywidth - maxname - 2 - 6 - 2 - 2
158
158
159 for date, count in rate:
159 for date, count in rate:
160 print "%s %6d %s" % (pad(date, maxname), count,
160 print "%s %6d %s" % (pad(date, maxname), count,
161 "*" * int(count * width / maxcount))
161 "*" * int(count * width / maxcount))
162
162
163
163
164 cmdtable = {
164 cmdtable = {
165 "churn":
165 "churn":
166 (churn,
166 (churn,
167 [('r', 'rev', [], _('count rate for the specified revision or range')),
167 [('r', 'rev', [], _('count rate for the specified revision or range')),
168 ('d', 'date', '', _('count rate for revisions matching date spec')),
168 ('d', 'date', '', _('count rate for revisions matching date spec')),
169 ('t', 'template', '{author|email}', _('template to group changesets')),
169 ('t', 'template', '{author|email}', _('template to group changesets')),
170 ('f', 'dateformat', '',
170 ('f', 'dateformat', '',
171 _('strftime-compatible format for grouping by date')),
171 _('strftime-compatible format for grouping by date')),
172 ('c', 'changesets', False, _('count rate by number of changesets')),
172 ('c', 'changesets', False, _('count rate by number of changesets')),
173 ('s', 'sort', False, _('sort by key (default: sort by count)')),
173 ('s', 'sort', False, _('sort by key (default: sort by count)')),
174 ('', 'aliases', '', _('file with email aliases')),
174 ('', 'aliases', '', _('file with email aliases')),
175 ('', 'progress', None, _('show progress'))],
175 ('', 'progress', None, _('show progress'))],
176 _("hg churn [-d DATE] [-r REV] [--aliases FILE] [--progress] [FILE]")),
176 _("hg churn [-d DATE] [-r REV] [--aliases FILE] [--progress] [FILE]")),
177 }
177 }
General Comments 0
You need to be logged in to leave comments. Login now