Added tags page.
[gitstats.git] / statgit
blob2dc54d577780d7422401b5cea64059e5f896d84a
1 #!/usr/bin/python
2 # Copyright (c) 2007 Heikki Hokkanen <hoxu@users.sf.net>
3 # GPLv2
4 import commands
5 import datetime
6 import os
7 import re
8 import sys
10 def getoutput(cmd):
11 print '>> %s' % cmd
12 output = commands.getoutput(cmd)
13 return output
15 def getkeyssortedbyvalues(dict):
16 return map(lambda el : el[1], sorted(map(lambda el : (el[1], el[0]), dict.items())))
18 class DataCollector:
19 def __init__(self):
20 pass
23 # This should be the main function to extract data from the repository.
24 def collect(self, dir):
25 self.dir = dir
28 # : get a dictionary of author
29 def getAuthorInfo(self, author):
30 return None
32 def getActivityByDayOfWeek(self):
33 return {}
35 def getActivityByHourOfDay(self):
36 return {}
39 # Get a list of authors
40 def getAuthors(self):
41 return []
43 def getFirstCommitDate(self):
44 return datetime.datetime.now()
46 def getLastCommitDate(self):
47 return datetime.datetime.now()
49 def getTags(self):
50 return []
52 def getTotalAuthors(self):
53 return -1
55 def getTotalCommits(self):
56 return -1
58 def getTotalFiles(self):
59 return -1
61 def getTotalLOC(self):
62 return -1
64 class GitDataCollector(DataCollector):
65 def collect(self, dir):
66 DataCollector.collect(self, dir)
68 self.total_authors = int(getoutput('git-log |git-shortlog -s |wc -l'))
69 self.total_commits = int(getoutput('git-rev-list HEAD |wc -l'))
70 self.total_files = int(getoutput('git-ls-files |wc -l'))
71 self.total_lines = int(getoutput('git-ls-files |xargs cat |wc -l'))
73 self.activity_by_hour_of_day = {} # hour -> commits
74 self.activity_by_day_of_week = {} # day -> commits
76 self.authors = {} # name -> {commits, first_commit_stamp, last_commit_stamp}
78 # author of the month
79 self.author_of_month = {} # month -> author -> commits
80 self.author_of_year = {} # year -> author -> commits
81 self.commits_by_month = {} # month -> commits
82 self.first_commit_stamp = 0
83 self.last_commit_stamp = 0
85 # tags
86 self.tags = {}
87 lines = getoutput('git-show-ref --tags').split('\n')
88 for line in lines:
89 (hash, tag) = line.split(' ')
90 tag = tag.replace('refs/tags/', '')
91 output = getoutput('git-log "%s" --pretty=format:"%%at %%an" -n 1' % hash)
92 if len(output) > 0:
93 parts = output.split(' ')
94 stamp = 0
95 try:
96 stamp = int(parts[0])
97 except ValueError:
98 stamp = 0
99 self.tags[tag] = { 'stamp': stamp, 'hash' : hash, 'date' : datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m-%d') }
100 pass
102 # TODO also collect statistics for "last 30 days"/"last 12 months"
103 lines = getoutput('git-rev-list --pretty=format:"%at %an" HEAD |grep -v ^commit').split('\n')
104 for line in lines:
105 # linux-2.6 says "<unknown>" for one line O_o
106 parts = line.split(' ')
107 author = ''
108 try:
109 stamp = int(parts[0])
110 except ValueError:
111 stamp = 0
112 if len(parts) > 1:
113 author = ' '.join(parts[1:])
114 date = datetime.datetime.fromtimestamp(float(stamp))
116 # First and last commit stamp
117 if self.last_commit_stamp == 0:
118 self.last_commit_stamp = stamp
119 self.first_commit_stamp = stamp
121 # activity
122 # hour
123 hour = date.hour
124 if hour in self.activity_by_hour_of_day:
125 self.activity_by_hour_of_day[hour] += 1
126 else:
127 self.activity_by_hour_of_day[hour] = 1
129 # day
130 day = date.weekday()
131 if day in self.activity_by_day_of_week:
132 self.activity_by_day_of_week[day] += 1
133 else:
134 self.activity_by_day_of_week[day] = 1
136 # author stats
137 if author not in self.authors:
138 self.authors[author] = {}
139 # TODO commits
140 if 'last_commit_stamp' not in self.authors[author]:
141 self.authors[author]['last_commit_stamp'] = stamp
142 self.authors[author]['first_commit_stamp'] = stamp
143 if 'commits' in self.authors[author]:
144 self.authors[author]['commits'] += 1
145 else:
146 self.authors[author]['commits'] = 1
148 # author of the month/year
149 yymm = datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m')
150 if yymm in self.author_of_month:
151 if author in self.author_of_month[yymm]:
152 self.author_of_month[yymm][author] += 1
153 else:
154 self.author_of_month[yymm][author] = 1
155 else:
156 self.author_of_month[yymm] = {}
157 self.author_of_month[yymm][author] = 1
158 if yymm in self.commits_by_month:
159 self.commits_by_month[yymm] += 1
160 else:
161 self.commits_by_month[yymm] = 1
163 yy = datetime.datetime.fromtimestamp(stamp).year
164 if yy in self.author_of_year:
165 if author in self.author_of_year[yy]:
166 self.author_of_year[yy][author] += 1
167 else:
168 self.author_of_year[yy][author] = 1
169 else:
170 self.author_of_year[yy] = {}
171 self.author_of_year[yy][author] = 1
173 def getActivityByDayOfWeek(self):
174 return self.activity_by_day_of_week
176 def getActivityByHourOfDay(self):
177 return self.activity_by_hour_of_day
179 def getAuthorInfo(self, author):
180 a = self.authors[author]
182 commits = a['commits']
183 commits_frac = (100 * float(commits)) / self.getTotalCommits()
184 date_first = datetime.datetime.fromtimestamp(a['first_commit_stamp']).strftime('%Y-%m-%d')
185 date_last = datetime.datetime.fromtimestamp(a['last_commit_stamp']).strftime('%Y-%m-%d')
187 res = { 'commits': commits, 'commits_frac': commits_frac, 'date_first': date_first, 'date_last': date_last }
188 return res
190 def getAuthors(self):
191 return self.authors.keys()
193 def getFirstCommitDate(self):
194 return datetime.datetime.fromtimestamp(self.first_commit_stamp)
196 def getLastCommitDate(self):
197 return datetime.datetime.fromtimestamp(self.last_commit_stamp)
199 def getTags(self):
200 lines = getoutput('git-show-ref --tags |cut -d/ -f3')
201 return lines.split('\n')
203 def getTagDate(self, tag):
204 return self.revToDate('tags/' + tag)
206 def getTotalAuthors(self):
207 return self.total_authors
209 def getTotalCommits(self):
210 return self.total_commits
212 def getTotalFiles(self):
213 return self.total_files
215 def getTotalLOC(self):
216 return self.total_lines
218 def revToDate(self, rev):
219 stamp = int(getoutput('git-log --pretty=format:%%at "%s" -n 1' % rev))
220 return datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m-%d')
222 class ReportCreator:
223 def __init__(self):
224 pass
226 def create(self, data, path):
227 self.data = data
228 self.path = path
230 class HTMLReportCreator(ReportCreator):
231 def create(self, data, path):
232 ReportCreator.create(self, data, path)
234 f = open(path + "/index.html", 'w')
235 format = '%Y-%m-%d %H:%m:%S'
236 self.printHeader(f)
238 f.write('<h1>StatGit</h1>')
240 self.printNav(f)
242 f.write('<dl>');
243 f.write('<dt>Generated</dt><dd>%s</dd>' % datetime.datetime.now().strftime(format));
244 f.write('<dt>Report Period</dt><dd>%s to %s</dd>' % (data.getFirstCommitDate().strftime(format), data.getLastCommitDate().strftime(format)))
245 f.write('<dt>Total Files</dt><dd>%s</dd>' % data.getTotalFiles())
246 f.write('<dt>Total Lines of Code</dt><dd>%s</dd>' % data.getTotalLOC())
247 f.write('<dt>Total Commits</dt><dd>%s</dd>' % data.getTotalCommits())
248 f.write('<dt>Authors</dt><dd>%s</dd>' % data.getTotalAuthors())
249 f.write('</dl>');
251 f.write('</body>\n</html>');
252 f.close()
255 # Activity
256 f = open(path + '/activity.html', 'w')
257 self.printHeader(f)
258 f.write('<h1>Activity</h1>')
259 self.printNav(f)
261 f.write('<h2>Last 30 days</h2>')
263 f.write('<h2>Last 12 months</h2>')
265 # Hour of Day
266 f.write('\n<h2>Hour of Day</h2>\n\n')
267 hour_of_day = data.getActivityByHourOfDay()
268 f.write('<table><tr><th>Hour</th>')
269 for i in range(1, 25):
270 f.write('<th>%d</th>' % i)
271 f.write('</tr>\n<tr><th>Commits</th>')
272 fp = open(path + '/hour_of_day.dat', 'w')
273 for i in range(0, 24):
274 if i in hour_of_day:
275 f.write('<td>%d</td>' % hour_of_day[i])
276 fp.write('%d %d\n' % (i, hour_of_day[i]))
277 else:
278 f.write('<td>0</td>')
279 fp.write('%d 0\n' % i)
280 fp.close()
281 f.write('</tr>\n<tr><th>%</th>')
282 totalcommits = data.getTotalCommits()
283 for i in range(0, 24):
284 if i in hour_of_day:
285 f.write('<td>%.2f</td>' % ((100.0 * hour_of_day[i]) / totalcommits))
286 else:
287 f.write('<td>0.00</td>')
288 f.write('</tr></table>')
290 # Day of Week
291 # TODO show also by hour of weekday?
292 f.write('\n<h2>Day of Week</h2>\n\n')
293 day_of_week = data.getActivityByDayOfWeek()
294 f.write('<table>')
295 f.write('<tr><th>Day</th><th>Total (%)</th></tr>')
296 fp = open(path + '/day_of_week.dat', 'w')
297 for d in range(0, 7):
298 fp.write('%d %d\n' % (d + 1, day_of_week[d]))
299 f.write('<tr>')
300 f.write('<th>%d</th>' % (d + 1))
301 if d in day_of_week:
302 f.write('<td>%d (%.2f%%)</td>' % (day_of_week[d], (100.0 * day_of_week[d]) / totalcommits))
303 else:
304 f.write('<td>0</td>')
305 f.write('</tr>')
306 f.write('</table>')
307 fp.close()
309 f.close()
311 # authors.html
312 f = open(path + '/authors.html', 'w')
313 self.printHeader(f)
315 f.write('<h1>Authors</h1>')
316 self.printNav(f)
318 f.write('\n<h2>List of authors</h2>\n\n')
320 f.write('<table class="authors">')
321 f.write('<tr><th>Author</th><th>Commits (%)</th><th>First commit</th><th>Last commit</th></tr>')
322 for author in data.getAuthors():
323 info = data.getAuthorInfo(author)
324 f.write('<tr><td>%s</td><td>%d (%.2f%%)</td><td>%s</td><td>%s</td></tr>' % (author, info['commits'], info['commits_frac'], info['date_first'], info['date_last']))
325 f.write('</table>')
327 f.write('\n<h2>Author of Month</h2>\n\n')
328 f.write('<table>')
329 f.write('<tr><th>Month</th><th>Author</th><th>Commits (%)</th></tr>')
330 for yymm in reversed(sorted(data.author_of_month.keys())):
331 authordict = data.author_of_month[yymm]
332 authors = getkeyssortedbyvalues(authordict)
333 authors.reverse()
334 commits = data.author_of_month[yymm][authors[0]]
335 f.write('<tr><td>%s</td><td>%s</td><td>%d (%.2f%% of %d)</td></tr>' % (yymm, authors[0], commits, (100 * commits) / data.commits_by_month[yymm], data.commits_by_month[yymm]))
337 f.write('</table>')
339 f.write('\n<h2>Author of Year</h2>\n\n')
340 f.write('<table><tr><th>Year</th><th>Author</th><th>Commits (%)</th></tr>')
341 for yy in reversed(sorted(data.author_of_year.keys())):
342 authordict = data.author_of_year[yy]
343 authors = getkeyssortedbyvalues(authordict)
344 authors.reverse()
345 commits = data.author_of_year[yy][authors[0]]
346 f.write('<tr><td>%s</td><td>%s</td><td>%d</td></tr>' % (yy, authors[0], commits))
347 f.write('</table>')
349 f.write('</body></html>')
350 f.close()
353 # tags.html
354 f = open(path + '/tags.html', 'w')
355 self.printHeader(f)
356 f.write('<h1>Tags</h1>')
357 self.printNav(f)
359 f.write('<table>')
360 f.write('<tr><th>Name</th><th>Date</th></tr>')
361 for tag in data.tags.keys():
362 f.write('<tr><td>%s</td><td>%s</td></tr>' % (tag, data.tags[tag]['date']))
363 f.write('</table>')
365 f.write('</body></html>')
366 f.close()
367 pass
369 def printHeader(self, f):
370 f.write("""<html>
371 <head>
372 <title>StatGit</title>
373 <link rel="stylesheet" href="statgit.css" type="text/css" />
374 </head>
375 <body>
376 """)
378 def printNav(self, f):
379 f.write("""
380 <div class="nav">
381 <li><a href="index.html">General</a></li>
382 <li><a href="activity.html">Activity</a></li>
383 <li><a href="authors.html">Authors</a></li>
384 <li><a href="files.html">Files</a></li>
385 <li><a href="lines.html">Lines</a></li>
386 <li><a href="tags.html">Tags</a></li>
387 </ul>
388 </div>
389 """)
392 usage = """
393 Usage: statgit [options] <gitpath> <outputpath>
395 Options:
396 -o html
399 if len(sys.argv) < 3:
400 print usage
401 sys.exit(0)
403 gitpath = sys.argv[1]
404 outputpath = sys.argv[2]
406 print 'Git path: %s' % gitpath
407 print 'Output path: %s' % outputpath
409 os.chdir(gitpath)
411 print 'Collecting data...'
412 data = GitDataCollector()
413 data.collect(gitpath)
415 print 'Generating report...'
416 report = HTMLReportCreator()
417 report.create(data, outputpath)