Cache file count for each commit.
[gitstats.git] / gitstats
blobc134513e00a4fa5e2b257b23e6a3e4eebe782134
1 #!/usr/bin/env python
2 # Copyright (c) 2007-2008 Heikki Hokkanen <hoxu@users.sf.net>
3 # GPLv2 / GPLv3
4 import subprocess
5 import datetime
6 import glob
7 import os
8 import pickle
9 import re
10 import shutil
11 import sys
12 import time
14 GNUPLOT_COMMON = 'set terminal png transparent\nset size 0.5,0.5\n'
16 exectime_internal = 0.0
17 exectime_external = 0.0
18 time_start = time.time()
20 # By default, gnuplot is searched from path, but can be overridden with the
21 # environment variable "GNUPLOT"
22 gnuplot_cmd = 'gnuplot'
23 if 'GNUPLOT' in os.environ:
24 gnuplot_cmd = os.environ['GNUPLOT']
26 def getpipeoutput(cmds, quiet = False):
27 global exectime_external
28 start = time.time()
29 if not quiet:
30 print '>> ' + ' | '.join(cmds),
31 sys.stdout.flush()
32 p0 = subprocess.Popen(cmds[0], stdout = subprocess.PIPE, shell = True)
33 p = p0
34 for x in cmds[1:]:
35 p = subprocess.Popen(x, stdin = p0.stdout, stdout = subprocess.PIPE, shell = True)
36 p0 = p
37 output = p.communicate()[0]
38 end = time.time()
39 if not quiet:
40 print '\r[%.5f] >> %s' % (end - start, ' | '.join(cmds))
41 exectime_external += (end - start)
42 return output.rstrip('\n')
44 def getkeyssortedbyvalues(dict):
45 return map(lambda el : el[1], sorted(map(lambda el : (el[1], el[0]), dict.items())))
47 # dict['author'] = { 'commits': 512 } - ...key(dict, 'commits')
48 def getkeyssortedbyvaluekey(d, key):
49 return map(lambda el : el[1], sorted(map(lambda el : (d[el][key], el), d.keys())))
51 class DataCollector:
52 """Manages data collection from a revision control repository."""
53 def __init__(self):
54 self.stamp_created = time.time()
55 self.cache = {}
58 # This should be the main function to extract data from the repository.
59 def collect(self, dir):
60 self.dir = dir
61 self.projectname = os.path.basename(os.path.abspath(dir))
64 # Load cacheable data
65 def loadCache(self, dir):
66 cachefile = os.path.join(dir, '.git', 'gitstats.cache')
67 if not os.path.exists(cachefile):
68 return
69 print 'Loading cache...'
70 f = open(cachefile)
71 self.cache = pickle.load(f)
72 f.close()
75 # Produce any additional statistics from the extracted data.
76 def refine(self):
77 pass
80 # : get a dictionary of author
81 def getAuthorInfo(self, author):
82 return None
84 def getActivityByDayOfWeek(self):
85 return {}
87 def getActivityByHourOfDay(self):
88 return {}
91 # Get a list of authors
92 def getAuthors(self):
93 return []
95 def getFirstCommitDate(self):
96 return datetime.datetime.now()
98 def getLastCommitDate(self):
99 return datetime.datetime.now()
101 def getStampCreated(self):
102 return self.stamp_created
104 def getTags(self):
105 return []
107 def getTotalAuthors(self):
108 return -1
110 def getTotalCommits(self):
111 return -1
113 def getTotalFiles(self):
114 return -1
116 def getTotalLOC(self):
117 return -1
120 # Save cacheable data
121 def saveCache(self, dir):
122 print 'Saving cache...'
123 f = open(os.path.join(dir, '.git', 'gitstats.cache'), 'w')
124 pickle.dump(self.cache, f)
125 f.close()
127 class GitDataCollector(DataCollector):
128 def collect(self, dir):
129 DataCollector.collect(self, dir)
131 try:
132 self.total_authors = int(getpipeoutput(['git-log', 'git-shortlog -s', 'wc -l']))
133 except:
134 self.total_authors = 0
135 #self.total_lines = int(getoutput('git-ls-files -z |xargs -0 cat |wc -l'))
137 self.activity_by_hour_of_day = {} # hour -> commits
138 self.activity_by_day_of_week = {} # day -> commits
139 self.activity_by_month_of_year = {} # month [1-12] -> commits
140 self.activity_by_hour_of_week = {} # weekday -> hour -> commits
142 self.authors = {} # name -> {commits, first_commit_stamp, last_commit_stamp}
144 # author of the month
145 self.author_of_month = {} # month -> author -> commits
146 self.author_of_year = {} # year -> author -> commits
147 self.commits_by_month = {} # month -> commits
148 self.commits_by_year = {} # year -> commits
149 self.first_commit_stamp = 0
150 self.last_commit_stamp = 0
152 # tags
153 self.tags = {}
154 lines = getpipeoutput(['git-show-ref --tags']).split('\n')
155 for line in lines:
156 if len(line) == 0:
157 continue
158 (hash, tag) = line.split(' ')
160 tag = tag.replace('refs/tags/', '')
161 output = getpipeoutput(['git-log "%s" --pretty=format:"%%at %%an" -n 1' % hash])
162 if len(output) > 0:
163 parts = output.split(' ')
164 stamp = 0
165 try:
166 stamp = int(parts[0])
167 except ValueError:
168 stamp = 0
169 self.tags[tag] = { 'stamp': stamp, 'hash' : hash, 'date' : datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m-%d') }
171 # Collect revision statistics
172 # Outputs "<stamp> <author>"
173 lines = getpipeoutput(['git-rev-list --pretty=format:"%at %an" HEAD', 'grep -v ^commit']).split('\n')
174 for line in lines:
175 # linux-2.6 says "<unknown>" for one line O_o
176 parts = line.split(' ')
177 author = ''
178 try:
179 stamp = int(parts[0])
180 except ValueError:
181 stamp = 0
182 if len(parts) > 1:
183 author = ' '.join(parts[1:])
184 date = datetime.datetime.fromtimestamp(float(stamp))
186 # First and last commit stamp
187 if self.last_commit_stamp == 0:
188 self.last_commit_stamp = stamp
189 self.first_commit_stamp = stamp
191 # activity
192 # hour
193 hour = date.hour
194 if hour in self.activity_by_hour_of_day:
195 self.activity_by_hour_of_day[hour] += 1
196 else:
197 self.activity_by_hour_of_day[hour] = 1
199 # day of week
200 day = date.weekday()
201 if day in self.activity_by_day_of_week:
202 self.activity_by_day_of_week[day] += 1
203 else:
204 self.activity_by_day_of_week[day] = 1
206 # hour of week
207 if day not in self.activity_by_hour_of_week:
208 self.activity_by_hour_of_week[day] = {}
209 if hour not in self.activity_by_hour_of_week[day]:
210 self.activity_by_hour_of_week[day][hour] = 1
211 else:
212 self.activity_by_hour_of_week[day][hour] += 1
214 # month of year
215 month = date.month
216 if month in self.activity_by_month_of_year:
217 self.activity_by_month_of_year[month] += 1
218 else:
219 self.activity_by_month_of_year[month] = 1
221 # author stats
222 if author not in self.authors:
223 self.authors[author] = {}
224 # commits
225 if 'last_commit_stamp' not in self.authors[author]:
226 self.authors[author]['last_commit_stamp'] = stamp
227 self.authors[author]['first_commit_stamp'] = stamp
228 if 'commits' in self.authors[author]:
229 self.authors[author]['commits'] += 1
230 else:
231 self.authors[author]['commits'] = 1
233 # author of the month/year
234 yymm = datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m')
235 if yymm in self.author_of_month:
236 if author in self.author_of_month[yymm]:
237 self.author_of_month[yymm][author] += 1
238 else:
239 self.author_of_month[yymm][author] = 1
240 else:
241 self.author_of_month[yymm] = {}
242 self.author_of_month[yymm][author] = 1
243 if yymm in self.commits_by_month:
244 self.commits_by_month[yymm] += 1
245 else:
246 self.commits_by_month[yymm] = 1
248 yy = datetime.datetime.fromtimestamp(stamp).year
249 if yy in self.author_of_year:
250 if author in self.author_of_year[yy]:
251 self.author_of_year[yy][author] += 1
252 else:
253 self.author_of_year[yy][author] = 1
254 else:
255 self.author_of_year[yy] = {}
256 self.author_of_year[yy][author] = 1
257 if yy in self.commits_by_year:
258 self.commits_by_year[yy] += 1
259 else:
260 self.commits_by_year[yy] = 1
262 # TODO Optimize this, it's the worst bottleneck
263 # outputs "<stamp> <files>" for each revision
264 self.files_by_stamp = {} # stamp -> files
265 revlines = getpipeoutput(['git-rev-list --pretty=format:"%at %T" HEAD', 'grep -v ^commit']).strip().split('\n')
266 lines = []
267 for revline in revlines:
268 time, rev = revline.split(' ')
269 #linecount = int(getpipeoutput(['git-ls-tree -r --name-only "%s"' % rev, 'wc -l']).split('\n')[0])
270 linecount = self.getFilesInCommit(rev)
271 lines.append('%d %d' % (int(time), linecount))
273 self.total_commits = len(lines)
274 for line in lines:
275 parts = line.split(' ')
276 if len(parts) != 2:
277 continue
278 (stamp, files) = parts[0:2]
279 try:
280 self.files_by_stamp[int(stamp)] = int(files)
281 except ValueError:
282 print 'Warning: failed to parse line "%s"' % line
284 # extensions
285 self.extensions = {} # extension -> files, lines
286 lines = getpipeoutput(['git-ls-files']).split('\n')
287 self.total_files = len(lines)
288 for line in lines:
289 base = os.path.basename(line)
290 if base.find('.') == -1:
291 ext = ''
292 else:
293 ext = base[(base.rfind('.') + 1):]
295 if ext not in self.extensions:
296 self.extensions[ext] = {'files': 0, 'lines': 0}
298 self.extensions[ext]['files'] += 1
299 try:
300 # Escaping could probably be improved here
301 self.extensions[ext]['lines'] += int(getpipeoutput(['wc -l "%s"' % line]).split()[0])
302 except:
303 print 'Warning: Could not count lines for file "%s"' % line
305 # line statistics
306 # outputs:
307 # N files changed, N insertions (+), N deletions(-)
308 # <stamp> <author>
309 self.changes_by_date = {} # stamp -> { files, ins, del }
310 lines = getpipeoutput(['git-log --shortstat --pretty=format:"%at %an"']).split('\n')
311 lines.reverse()
312 files = 0; inserted = 0; deleted = 0; total_lines = 0
313 for line in lines:
314 if len(line) == 0:
315 continue
317 # <stamp> <author>
318 if line.find('files changed,') == -1:
319 pos = line.find(' ')
320 if pos != -1:
321 try:
322 (stamp, author) = (int(line[:pos]), line[pos+1:])
323 self.changes_by_date[stamp] = { 'files': files, 'ins': inserted, 'del': deleted, 'lines': total_lines }
324 except ValueError:
325 print 'Warning: unexpected line "%s"' % line
326 else:
327 print 'Warning: unexpected line "%s"' % line
328 else:
329 numbers = re.findall('\d+', line)
330 if len(numbers) == 3:
331 (files, inserted, deleted) = map(lambda el : int(el), numbers)
332 total_lines += inserted
333 total_lines -= deleted
334 else:
335 print 'Warning: failed to handle line "%s"' % line
336 (files, inserted, deleted) = (0, 0, 0)
337 #self.changes_by_date[stamp] = { 'files': files, 'ins': inserted, 'del': deleted }
338 self.total_lines = total_lines
340 def refine(self):
341 # authors
342 # name -> {place_by_commits, commits_frac, date_first, date_last, timedelta}
343 authors_by_commits = getkeyssortedbyvaluekey(self.authors, 'commits')
344 authors_by_commits.reverse() # most first
345 for i, name in enumerate(authors_by_commits):
346 self.authors[name]['place_by_commits'] = i + 1
348 for name in self.authors.keys():
349 a = self.authors[name]
350 a['commits_frac'] = (100 * float(a['commits'])) / self.getTotalCommits()
351 date_first = datetime.datetime.fromtimestamp(a['first_commit_stamp'])
352 date_last = datetime.datetime.fromtimestamp(a['last_commit_stamp'])
353 delta = date_last - date_first
354 a['date_first'] = date_first.strftime('%Y-%m-%d')
355 a['date_last'] = date_last.strftime('%Y-%m-%d')
356 a['timedelta'] = delta
358 def getActivityByDayOfWeek(self):
359 return self.activity_by_day_of_week
361 def getActivityByHourOfDay(self):
362 return self.activity_by_hour_of_day
364 def getAuthorInfo(self, author):
365 return self.authors[author]
367 def getAuthors(self):
368 return self.authors.keys()
370 def getFilesInCommit(self, rev):
371 try:
372 res = self.cache['files_in_tree'][rev]
373 except:
374 res = int(getpipeoutput(['git-ls-tree -r --name-only "%s"' % rev, 'wc -l']).split('\n')[0])
375 if 'files_in_tree' not in self.cache:
376 self.cache['files_in_tree'] = {}
377 self.cache['files_in_tree'][rev] = res
379 return res
381 def getFirstCommitDate(self):
382 return datetime.datetime.fromtimestamp(self.first_commit_stamp)
384 def getLastCommitDate(self):
385 return datetime.datetime.fromtimestamp(self.last_commit_stamp)
387 def getTags(self):
388 lines = getpipeoutput(['git-show-ref --tags', 'cut -d/ -f3'])
389 return lines.split('\n')
391 def getTagDate(self, tag):
392 return self.revToDate('tags/' + tag)
394 def getTotalAuthors(self):
395 return self.total_authors
397 def getTotalCommits(self):
398 return self.total_commits
400 def getTotalFiles(self):
401 return self.total_files
403 def getTotalLOC(self):
404 return self.total_lines
406 def revToDate(self, rev):
407 stamp = int(getpipeoutput(['git-log --pretty=format:%%at "%s" -n 1' % rev]))
408 return datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m-%d')
410 class ReportCreator:
411 """Creates the actual report based on given data."""
412 def __init__(self):
413 pass
415 def create(self, data, path):
416 self.data = data
417 self.path = path
419 def html_linkify(text):
420 return text.lower().replace(' ', '_')
422 def html_header(level, text):
423 name = html_linkify(text)
424 return '\n<h%d><a href="#%s" name="%s">%s</a></h%d>\n\n' % (level, name, name, text, level)
426 class HTMLReportCreator(ReportCreator):
427 def create(self, data, path):
428 ReportCreator.create(self, data, path)
429 self.title = data.projectname
431 # copy the CSS if it does not exist
432 if not os.path.exists(path + '/gitstats.css'):
433 basedir = os.path.dirname(os.path.abspath(__file__))
434 shutil.copyfile(basedir + '/gitstats.css', path + '/gitstats.css')
436 f = open(path + "/index.html", 'w')
437 format = '%Y-%m-%d %H:%m:%S'
438 self.printHeader(f)
440 f.write('<h1>GitStats - %s</h1>' % data.projectname)
442 self.printNav(f)
444 f.write('<dl>');
445 f.write('<dt>Project name</dt><dd>%s</dd>' % (data.projectname))
446 f.write('<dt>Generated</dt><dd>%s (in %d seconds)</dd>' % (datetime.datetime.now().strftime(format), time.time() - data.getStampCreated()));
447 f.write('<dt>Report Period</dt><dd>%s to %s</dd>' % (data.getFirstCommitDate().strftime(format), data.getLastCommitDate().strftime(format)))
448 f.write('<dt>Total Files</dt><dd>%s</dd>' % data.getTotalFiles())
449 f.write('<dt>Total Lines of Code</dt><dd>%s</dd>' % data.getTotalLOC())
450 f.write('<dt>Total Commits</dt><dd>%s</dd>' % data.getTotalCommits())
451 f.write('<dt>Authors</dt><dd>%s</dd>' % data.getTotalAuthors())
452 f.write('</dl>');
454 f.write('</body>\n</html>');
455 f.close()
458 # Activity
459 f = open(path + '/activity.html', 'w')
460 self.printHeader(f)
461 f.write('<h1>Activity</h1>')
462 self.printNav(f)
464 #f.write('<h2>Last 30 days</h2>')
466 #f.write('<h2>Last 12 months</h2>')
468 # Hour of Day
469 f.write(html_header(2, 'Hour of Day'))
470 hour_of_day = data.getActivityByHourOfDay()
471 f.write('<table><tr><th>Hour</th>')
472 for i in range(1, 25):
473 f.write('<th>%d</th>' % i)
474 f.write('</tr>\n<tr><th>Commits</th>')
475 fp = open(path + '/hour_of_day.dat', 'w')
476 for i in range(0, 24):
477 if i in hour_of_day:
478 f.write('<td>%d</td>' % hour_of_day[i])
479 fp.write('%d %d\n' % (i, hour_of_day[i]))
480 else:
481 f.write('<td>0</td>')
482 fp.write('%d 0\n' % i)
483 fp.close()
484 f.write('</tr>\n<tr><th>%</th>')
485 totalcommits = data.getTotalCommits()
486 for i in range(0, 24):
487 if i in hour_of_day:
488 f.write('<td>%.2f</td>' % ((100.0 * hour_of_day[i]) / totalcommits))
489 else:
490 f.write('<td>0.00</td>')
491 f.write('</tr></table>')
492 f.write('<img src="hour_of_day.png" alt="Hour of Day" />')
493 fg = open(path + '/hour_of_day.dat', 'w')
494 for i in range(0, 24):
495 if i in hour_of_day:
496 fg.write('%d %d\n' % (i + 1, hour_of_day[i]))
497 else:
498 fg.write('%d 0\n' % (i + 1))
499 fg.close()
501 # Day of Week
502 f.write(html_header(2, 'Day of Week'))
503 day_of_week = data.getActivityByDayOfWeek()
504 f.write('<div class="vtable"><table>')
505 f.write('<tr><th>Day</th><th>Total (%)</th></tr>')
506 fp = open(path + '/day_of_week.dat', 'w')
507 for d in range(0, 7):
508 commits = 0
509 if d in day_of_week:
510 commits = day_of_week[d]
511 fp.write('%d %d\n' % (d + 1, commits))
512 f.write('<tr>')
513 f.write('<th>%d</th>' % (d + 1))
514 if d in day_of_week:
515 f.write('<td>%d (%.2f%%)</td>' % (day_of_week[d], (100.0 * day_of_week[d]) / totalcommits))
516 else:
517 f.write('<td>0</td>')
518 f.write('</tr>')
519 f.write('</table></div>')
520 f.write('<img src="day_of_week.png" alt="Day of Week" />')
521 fp.close()
523 # Hour of Week
524 f.write(html_header(2, 'Hour of Week'))
525 f.write('<table>')
527 f.write('<tr><th>Weekday</th>')
528 for hour in range(0, 24):
529 f.write('<th>%d</th>' % (hour + 1))
530 f.write('</tr>')
532 for weekday in range(0, 7):
533 f.write('<tr><th>%d</th>' % (weekday + 1))
534 for hour in range(0, 24):
535 try:
536 commits = data.activity_by_hour_of_week[weekday][hour]
537 except KeyError:
538 commits = 0
539 if commits != 0:
540 f.write('<td>%d</td>' % commits)
541 else:
542 f.write('<td></td>')
543 f.write('</tr>')
545 f.write('</table>')
547 # Month of Year
548 f.write(html_header(2, 'Month of Year'))
549 f.write('<div class="vtable"><table>')
550 f.write('<tr><th>Month</th><th>Commits (%)</th></tr>')
551 fp = open (path + '/month_of_year.dat', 'w')
552 for mm in range(1, 13):
553 commits = 0
554 if mm in data.activity_by_month_of_year:
555 commits = data.activity_by_month_of_year[mm]
556 f.write('<tr><td>%d</td><td>%d (%.2f %%)</td></tr>' % (mm, commits, (100.0 * commits) / data.getTotalCommits()))
557 fp.write('%d %d\n' % (mm, commits))
558 fp.close()
559 f.write('</table></div>')
560 f.write('<img src="month_of_year.png" alt="Month of Year" />')
562 # Commits by year/month
563 f.write(html_header(2, 'Commits by year/month'))
564 f.write('<div class="vtable"><table><tr><th>Month</th><th>Commits</th></tr>')
565 for yymm in reversed(sorted(data.commits_by_month.keys())):
566 f.write('<tr><td>%s</td><td>%d</td></tr>' % (yymm, data.commits_by_month[yymm]))
567 f.write('</table></div>')
568 f.write('<img src="commits_by_year_month.png" alt="Commits by year/month" />')
569 fg = open(path + '/commits_by_year_month.dat', 'w')
570 for yymm in sorted(data.commits_by_month.keys()):
571 fg.write('%s %s\n' % (yymm, data.commits_by_month[yymm]))
572 fg.close()
574 # Commits by year
575 f.write(html_header(2, 'Commits by Year'))
576 f.write('<div class="vtable"><table><tr><th>Year</th><th>Commits (% of all)</th></tr>')
577 for yy in reversed(sorted(data.commits_by_year.keys())):
578 f.write('<tr><td>%s</td><td>%d (%.2f%%)</td></tr>' % (yy, data.commits_by_year[yy], (100.0 * data.commits_by_year[yy]) / data.getTotalCommits()))
579 f.write('</table></div>')
580 f.write('<img src="commits_by_year.png" alt="Commits by Year" />')
581 fg = open(path + '/commits_by_year.dat', 'w')
582 for yy in sorted(data.commits_by_year.keys()):
583 fg.write('%d %d\n' % (yy, data.commits_by_year[yy]))
584 fg.close()
586 f.write('</body></html>')
587 f.close()
590 # Authors
591 f = open(path + '/authors.html', 'w')
592 self.printHeader(f)
594 f.write('<h1>Authors</h1>')
595 self.printNav(f)
597 # Authors :: List of authors
598 f.write(html_header(2, 'List of Authors'))
600 f.write('<table class="authors">')
601 f.write('<tr><th>Author</th><th>Commits (%)</th><th>First commit</th><th>Last commit</th><th>Age</th><th># by commits</th></tr>')
602 for author in sorted(data.getAuthors()):
603 info = data.getAuthorInfo(author)
604 f.write('<tr><td>%s</td><td>%d (%.2f%%)</td><td>%s</td><td>%s</td><td>%s</td><td>%d</td></tr>' % (author, info['commits'], info['commits_frac'], info['date_first'], info['date_last'], info['timedelta'], info['place_by_commits']))
605 f.write('</table>')
607 # Authors :: Author of Month
608 f.write(html_header(2, 'Author of Month'))
609 f.write('<table>')
610 f.write('<tr><th>Month</th><th>Author</th><th>Commits (%)</th><th>Next top 5</th></tr>')
611 for yymm in reversed(sorted(data.author_of_month.keys())):
612 authordict = data.author_of_month[yymm]
613 authors = getkeyssortedbyvalues(authordict)
614 authors.reverse()
615 commits = data.author_of_month[yymm][authors[0]]
616 next = ', '.join(authors[1:5])
617 f.write('<tr><td>%s</td><td>%s</td><td>%d (%.2f%% of %d)</td><td>%s</td></tr>' % (yymm, authors[0], commits, (100 * commits) / data.commits_by_month[yymm], data.commits_by_month[yymm], next))
619 f.write('</table>')
621 f.write(html_header(2, 'Author of Year'))
622 f.write('<table><tr><th>Year</th><th>Author</th><th>Commits (%)</th><th>Next top 5</th></tr>')
623 for yy in reversed(sorted(data.author_of_year.keys())):
624 authordict = data.author_of_year[yy]
625 authors = getkeyssortedbyvalues(authordict)
626 authors.reverse()
627 commits = data.author_of_year[yy][authors[0]]
628 next = ', '.join(authors[1:5])
629 f.write('<tr><td>%s</td><td>%s</td><td>%d (%.2f%% of %d)</td><td>%s</td></tr>' % (yy, authors[0], commits, (100 * commits) / data.commits_by_year[yy], data.commits_by_year[yy], next))
630 f.write('</table>')
632 f.write('</body></html>')
633 f.close()
636 # Files
637 f = open(path + '/files.html', 'w')
638 self.printHeader(f)
639 f.write('<h1>Files</h1>')
640 self.printNav(f)
642 f.write('<dl>\n')
643 f.write('<dt>Total files</dt><dd>%d</dd>' % data.getTotalFiles())
644 f.write('<dt>Total lines</dt><dd>%d</dd>' % data.getTotalLOC())
645 f.write('<dt>Average file size</dt><dd>%.2f bytes</dd>' % ((100.0 * data.getTotalLOC()) / data.getTotalFiles()))
646 f.write('</dl>\n')
648 # Files :: File count by date
649 f.write(html_header(2, 'File count by date'))
651 fg = open(path + '/files_by_date.dat', 'w')
652 for stamp in sorted(data.files_by_stamp.keys()):
653 fg.write('%s %d\n' % (datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m-%d'), data.files_by_stamp[stamp]))
654 fg.close()
656 f.write('<img src="files_by_date.png" alt="Files by Date" />')
658 #f.write('<h2>Average file size by date</h2>')
660 # Files :: Extensions
661 f.write(html_header(2, 'Extensions'))
662 f.write('<table><tr><th>Extension</th><th>Files (%)</th><th>Lines (%)</th><th>Lines/file</th></tr>')
663 for ext in sorted(data.extensions.keys()):
664 files = data.extensions[ext]['files']
665 lines = data.extensions[ext]['lines']
666 f.write('<tr><td>%s</td><td>%d (%.2f%%)</td><td>%d (%.2f%%)</td><td>%d</td></tr>' % (ext, files, (100.0 * files) / data.getTotalFiles(), lines, (100.0 * lines) / data.getTotalLOC(), lines / files))
667 f.write('</table>')
669 f.write('</body></html>')
670 f.close()
673 # Lines
674 f = open(path + '/lines.html', 'w')
675 self.printHeader(f)
676 f.write('<h1>Lines</h1>')
677 self.printNav(f)
679 f.write('<dl>\n')
680 f.write('<dt>Total lines</dt><dd>%d</dd>' % data.getTotalLOC())
681 f.write('</dl>\n')
683 f.write(html_header(2, 'Lines of Code'))
684 f.write('<img src="lines_of_code.png" />')
686 fg = open(path + '/lines_of_code.dat', 'w')
687 for stamp in sorted(data.changes_by_date.keys()):
688 fg.write('%d %d\n' % (stamp, data.changes_by_date[stamp]['lines']))
689 fg.close()
691 f.write('</body></html>')
692 f.close()
695 # tags.html
696 f = open(path + '/tags.html', 'w')
697 self.printHeader(f)
698 f.write('<h1>Tags</h1>')
699 self.printNav(f)
701 f.write('<dl>')
702 f.write('<dt>Total tags</dt><dd>%d</dd>' % len(data.tags))
703 if len(data.tags) > 0:
704 f.write('<dt>Average commits per tag</dt><dd>%.2f</dd>' % (data.getTotalCommits() / len(data.tags)))
705 f.write('</dl>')
707 f.write('<table>')
708 f.write('<tr><th>Name</th><th>Date</th></tr>')
709 # sort the tags by date desc
710 tags_sorted_by_date_desc = map(lambda el : el[1], reversed(sorted(map(lambda el : (el[1]['date'], el[0]), data.tags.items()))))
711 for tag in tags_sorted_by_date_desc:
712 f.write('<tr><td>%s</td><td>%s</td></tr>' % (tag, data.tags[tag]['date']))
713 f.write('</table>')
715 f.write('</body></html>')
716 f.close()
718 self.createGraphs(path)
720 def createGraphs(self, path):
721 print 'Generating graphs...'
723 # hour of day
724 f = open(path + '/hour_of_day.plot', 'w')
725 f.write(GNUPLOT_COMMON)
726 f.write(
728 set output 'hour_of_day.png'
729 unset key
730 set xrange [0.5:24.5]
731 set xtics 4
732 set ylabel "Commits"
733 plot 'hour_of_day.dat' using 1:2:(0.5) w boxes fs solid
734 """)
735 f.close()
737 # day of week
738 f = open(path + '/day_of_week.plot', 'w')
739 f.write(GNUPLOT_COMMON)
740 f.write(
742 set output 'day_of_week.png'
743 unset key
744 set xrange [0.5:7.5]
745 set xtics 1
746 set ylabel "Commits"
747 plot 'day_of_week.dat' using 1:2:(0.5) w boxes fs solid
748 """)
749 f.close()
751 # Month of Year
752 f = open(path + '/month_of_year.plot', 'w')
753 f.write(GNUPLOT_COMMON)
754 f.write(
756 set output 'month_of_year.png'
757 unset key
758 set xrange [0.5:12.5]
759 set xtics 1
760 set ylabel "Commits"
761 plot 'month_of_year.dat' using 1:2:(0.5) w boxes fs solid
762 """)
763 f.close()
765 # commits_by_year_month
766 f = open(path + '/commits_by_year_month.plot', 'w')
767 f.write(GNUPLOT_COMMON)
768 f.write(
770 set output 'commits_by_year_month.png'
771 unset key
772 set xdata time
773 set timefmt "%Y-%m"
774 set format x "%Y-%m"
775 set xtics rotate by 90 15768000
776 set bmargin 5
777 set ylabel "Commits"
778 plot 'commits_by_year_month.dat' using 1:2:(0.5) w boxes fs solid
779 """)
780 f.close()
782 # commits_by_year
783 f = open(path + '/commits_by_year.plot', 'w')
784 f.write(GNUPLOT_COMMON)
785 f.write(
787 set output 'commits_by_year.png'
788 unset key
789 set xtics 1
790 set ylabel "Commits"
791 plot 'commits_by_year.dat' using 1:2:(0.5) w boxes fs solid
792 """)
793 f.close()
795 # Files by date
796 f = open(path + '/files_by_date.plot', 'w')
797 f.write(GNUPLOT_COMMON)
798 f.write(
800 set output 'files_by_date.png'
801 unset key
802 set xdata time
803 set timefmt "%Y-%m-%d"
804 set format x "%Y-%m-%d"
805 set ylabel "Files"
806 set xtics rotate by 90
807 set bmargin 6
808 plot 'files_by_date.dat' using 1:2 smooth csplines
809 """)
810 f.close()
812 # Lines of Code
813 f = open(path + '/lines_of_code.plot', 'w')
814 f.write(GNUPLOT_COMMON)
815 f.write(
817 set output 'lines_of_code.png'
818 unset key
819 set xdata time
820 set timefmt "%s"
821 set format x "%Y-%m-%d"
822 set ylabel "Lines"
823 set xtics rotate by 90
824 set bmargin 6
825 plot 'lines_of_code.dat' using 1:2 w lines
826 """)
827 f.close()
829 os.chdir(path)
830 files = glob.glob(path + '/*.plot')
831 for f in files:
832 out = getpipeoutput([gnuplot_cmd + ' "%s"' % f])
833 if len(out) > 0:
834 print out
836 def printHeader(self, f, title = ''):
837 f.write(
838 """<?xml version="1.0" encoding="UTF-8"?>
839 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
840 <html xmlns="http://www.w3.org/1999/xhtml">
841 <head>
842 <title>GitStats - %s</title>
843 <link rel="stylesheet" href="gitstats.css" type="text/css" />
844 <meta name="generator" content="GitStats" />
845 </head>
846 <body>
847 """ % self.title)
849 def printNav(self, f):
850 f.write("""
851 <div class="nav">
852 <ul>
853 <li><a href="index.html">General</a></li>
854 <li><a href="activity.html">Activity</a></li>
855 <li><a href="authors.html">Authors</a></li>
856 <li><a href="files.html">Files</a></li>
857 <li><a href="lines.html">Lines</a></li>
858 <li><a href="tags.html">Tags</a></li>
859 </ul>
860 </div>
861 """)
864 usage = """
865 Usage: gitstats [options] <gitpath> <outputpath>
867 Options:
870 if len(sys.argv) < 3:
871 print usage
872 sys.exit(0)
874 gitpath = sys.argv[1]
875 outputpath = os.path.abspath(sys.argv[2])
876 rundir = os.getcwd()
878 try:
879 os.makedirs(outputpath)
880 except OSError:
881 pass
882 if not os.path.isdir(outputpath):
883 print 'FATAL: Output path is not a directory or does not exist'
884 sys.exit(1)
886 print 'Git path: %s' % gitpath
887 print 'Output path: %s' % outputpath
889 os.chdir(gitpath)
891 print 'Collecting data...'
892 data = GitDataCollector()
893 data.loadCache(gitpath)
894 data.collect(gitpath)
895 print 'Refining data...'
896 data.saveCache(gitpath)
897 data.refine()
899 os.chdir(rundir)
901 print 'Generating report...'
902 report = HTMLReportCreator()
903 report.create(data, outputpath)
905 time_end = time.time()
906 exectime_internal = time_end - time_start
907 print 'Execution time %.5f secs, %.5f secs (%.2f %%) in external commands)' % (exectime_internal, exectime_external, (100.0 * exectime_external) / exectime_internal)