2 # Copyright (c) 2007 Heikki Hokkanen <hoxu@users.sf.net>
12 GNUPLOT_COMMON
= 'set terminal png transparent\nset size 0.5,0.5\n'
14 def getoutput(cmd
, quiet
= False):
17 output
= commands
.getoutput(cmd
)
20 def getkeyssortedbyvalues(dict):
21 return map(lambda el
: el
[1], sorted(map(lambda el
: (el
[1], el
[0]), dict.items())))
23 # TODO getdictkeyssortedbyvaluekey(dict, key) - eg. dict['author'] = { 'commits' : 512 } - ...key(dict, 'commits')
27 self
.stamp_created
= time
.time()
31 # This should be the main function to extract data from the repository.
32 def collect(self
, dir):
36 # : get a dictionary of author
37 def getAuthorInfo(self
, author
):
40 def getActivityByDayOfWeek(self
):
43 def getActivityByHourOfDay(self
):
47 # Get a list of authors
51 def getFirstCommitDate(self
):
52 return datetime
.datetime
.now()
54 def getLastCommitDate(self
):
55 return datetime
.datetime
.now()
57 def getStampCreated(self
):
58 return self
.stamp_created
63 def getTotalAuthors(self
):
66 def getTotalCommits(self
):
69 def getTotalFiles(self
):
72 def getTotalLOC(self
):
75 class GitDataCollector(DataCollector
):
76 def collect(self
, dir):
77 DataCollector
.collect(self
, dir)
79 self
.total_authors
= int(getoutput('git-log |git-shortlog -s |wc -l'))
80 self
.total_commits
= int(getoutput('git-rev-list HEAD |wc -l'))
81 self
.total_files
= int(getoutput('git-ls-files |wc -l'))
82 self
.total_lines
= int(getoutput('git-ls-files -z |xargs -0 cat |wc -l'))
84 self
.activity_by_hour_of_day
= {} # hour -> commits
85 self
.activity_by_day_of_week
= {} # day -> commits
86 self
.activity_by_month_of_year
= {} # month [1-12] -> commits
87 self
.activity_by_hour_of_week
= {} # weekday -> hour -> commits
89 self
.authors
= {} # name -> {commits, first_commit_stamp, last_commit_stamp}
92 self
.author_of_month
= {} # month -> author -> commits
93 self
.author_of_year
= {} # year -> author -> commits
94 self
.commits_by_month
= {} # month -> commits
95 self
.commits_by_year
= {} # year -> commits
96 self
.first_commit_stamp
= 0
97 self
.last_commit_stamp
= 0
101 lines
= getoutput('git-show-ref --tags').split('\n')
105 (hash, tag
) = line
.split(' ')
106 tag
= tag
.replace('refs/tags/', '')
107 output
= getoutput('git-log "%s" --pretty=format:"%%at %%an" -n 1' % hash)
109 parts
= output
.split(' ')
112 stamp
= int(parts
[0])
115 self
.tags
[tag
] = { 'stamp': stamp
, 'hash' : hash, 'date' : datetime
.datetime
.fromtimestamp(stamp
).strftime('%Y-%m-%d') }
118 # Collect revision statistics
119 # Outputs "<stamp> <author>"
120 lines
= getoutput('git-rev-list --pretty=format:"%at %an" HEAD |grep -v ^commit').split('\n')
122 # linux-2.6 says "<unknown>" for one line O_o
123 parts
= line
.split(' ')
126 stamp
= int(parts
[0])
130 author
= ' '.join(parts
[1:])
131 date
= datetime
.datetime
.fromtimestamp(float(stamp
))
133 # First and last commit stamp
134 if self
.last_commit_stamp
== 0:
135 self
.last_commit_stamp
= stamp
136 self
.first_commit_stamp
= stamp
141 if hour
in self
.activity_by_hour_of_day
:
142 self
.activity_by_hour_of_day
[hour
] += 1
144 self
.activity_by_hour_of_day
[hour
] = 1
148 if day
in self
.activity_by_day_of_week
:
149 self
.activity_by_day_of_week
[day
] += 1
151 self
.activity_by_day_of_week
[day
] = 1
154 if day
not in self
.activity_by_hour_of_week
:
155 self
.activity_by_hour_of_week
[day
] = {}
156 if hour
not in self
.activity_by_hour_of_week
[day
]:
157 self
.activity_by_hour_of_week
[day
][hour
] = 1
159 self
.activity_by_hour_of_week
[day
][hour
] += 1
163 if month
in self
.activity_by_month_of_year
:
164 self
.activity_by_month_of_year
[month
] += 1
166 self
.activity_by_month_of_year
[month
] = 1
169 if author
not in self
.authors
:
170 self
.authors
[author
] = {}
172 if 'last_commit_stamp' not in self
.authors
[author
]:
173 self
.authors
[author
]['last_commit_stamp'] = stamp
174 self
.authors
[author
]['first_commit_stamp'] = stamp
175 if 'commits' in self
.authors
[author
]:
176 self
.authors
[author
]['commits'] += 1
178 self
.authors
[author
]['commits'] = 1
180 # author of the month/year
181 yymm
= datetime
.datetime
.fromtimestamp(stamp
).strftime('%Y-%m')
182 if yymm
in self
.author_of_month
:
183 if author
in self
.author_of_month
[yymm
]:
184 self
.author_of_month
[yymm
][author
] += 1
186 self
.author_of_month
[yymm
][author
] = 1
188 self
.author_of_month
[yymm
] = {}
189 self
.author_of_month
[yymm
][author
] = 1
190 if yymm
in self
.commits_by_month
:
191 self
.commits_by_month
[yymm
] += 1
193 self
.commits_by_month
[yymm
] = 1
195 yy
= datetime
.datetime
.fromtimestamp(stamp
).year
196 if yy
in self
.author_of_year
:
197 if author
in self
.author_of_year
[yy
]:
198 self
.author_of_year
[yy
][author
] += 1
200 self
.author_of_year
[yy
][author
] = 1
202 self
.author_of_year
[yy
] = {}
203 self
.author_of_year
[yy
][author
] = 1
204 if yy
in self
.commits_by_year
:
205 self
.commits_by_year
[yy
] += 1
207 self
.commits_by_year
[yy
] = 1
209 # outputs "<stamp> <files>" for each revision
210 self
.files_by_stamp
= {} # stamp -> files
211 lines
= getoutput('git-rev-list --pretty=format:"%at %H" HEAD |grep -v ^commit |while read line; do set $line; echo "$1 $(git-ls-tree -r "$2" |wc -l)"; done').split('\n')
213 parts
= line
.split(' ')
216 (stamp
, files
) = parts
[0:2]
217 self
.files_by_stamp
[int(stamp
)] = int(files
)
220 self
.extensions
= {} # extension -> files, lines
221 lines
= getoutput('git-ls-files').split('\n')
223 base
= os
.path
.basename(line
)
224 if base
.find('.') == -1:
227 ext
= base
[(base
.rfind('.') + 1):]
229 if ext
not in self
.extensions
:
230 self
.extensions
[ext
] = {'files': 0, 'lines': 0}
232 self
.extensions
[ext
]['files'] += 1
233 self
.extensions
[ext
]['lines'] += int(getoutput('wc -l < %s' % line
, quiet
= True))
235 def getActivityByDayOfWeek(self
):
236 return self
.activity_by_day_of_week
238 def getActivityByHourOfDay(self
):
239 return self
.activity_by_hour_of_day
241 def getAuthorInfo(self
, author
):
242 a
= self
.authors
[author
]
244 commits
= a
['commits']
245 commits_frac
= (100 * float(commits
)) / self
.getTotalCommits()
246 date_first
= datetime
.datetime
.fromtimestamp(a
['first_commit_stamp'])
247 date_last
= datetime
.datetime
.fromtimestamp(a
['last_commit_stamp'])
248 delta
= date_last
- date_first
250 res
= { 'commits': commits
, 'commits_frac': commits_frac
, 'date_first': date_first
.strftime('%Y-%m-%d'), 'date_last': date_last
.strftime('%Y-%m-%d'), 'timedelta' : delta
}
253 def getAuthors(self
):
254 return self
.authors
.keys()
256 def getFirstCommitDate(self
):
257 return datetime
.datetime
.fromtimestamp(self
.first_commit_stamp
)
259 def getLastCommitDate(self
):
260 return datetime
.datetime
.fromtimestamp(self
.last_commit_stamp
)
263 lines
= getoutput('git-show-ref --tags |cut -d/ -f3')
264 return lines
.split('\n')
266 def getTagDate(self
, tag
):
267 return self
.revToDate('tags/' + tag
)
269 def getTotalAuthors(self
):
270 return self
.total_authors
272 def getTotalCommits(self
):
273 return self
.total_commits
275 def getTotalFiles(self
):
276 return self
.total_files
278 def getTotalLOC(self
):
279 return self
.total_lines
281 def revToDate(self
, rev
):
282 stamp
= int(getoutput('git-log --pretty=format:%%at "%s" -n 1' % rev
))
283 return datetime
.datetime
.fromtimestamp(stamp
).strftime('%Y-%m-%d')
289 def create(self
, data
, path
):
293 class HTMLReportCreator(ReportCreator
):
294 def create(self
, data
, path
):
295 ReportCreator
.create(self
, data
, path
)
297 f
= open(path
+ "/index.html", 'w')
298 format
= '%Y-%m-%d %H:%m:%S'
301 f
.write('<h1>StatGit</h1>')
306 f
.write('<dt>Generated</dt><dd>%s (in %d seconds)</dd>' % (datetime
.datetime
.now().strftime(format
), time
.time() - data
.getStampCreated()));
307 f
.write('<dt>Report Period</dt><dd>%s to %s</dd>' % (data
.getFirstCommitDate().strftime(format
), data
.getLastCommitDate().strftime(format
)))
308 f
.write('<dt>Total Files</dt><dd>%s</dd>' % data
.getTotalFiles())
309 f
.write('<dt>Total Lines of Code</dt><dd>%s</dd>' % data
.getTotalLOC())
310 f
.write('<dt>Total Commits</dt><dd>%s</dd>' % data
.getTotalCommits())
311 f
.write('<dt>Authors</dt><dd>%s</dd>' % data
.getTotalAuthors())
314 f
.write('</body>\n</html>');
319 f
= open(path
+ '/activity.html', 'w')
321 f
.write('<h1>Activity</h1>')
324 f
.write('<h2>Last 30 days</h2>')
326 f
.write('<h2>Last 12 months</h2>')
329 f
.write('\n<h2>Hour of Day</h2>\n\n')
330 hour_of_day
= data
.getActivityByHourOfDay()
331 f
.write('<table><tr><th>Hour</th>')
332 for i
in range(1, 25):
333 f
.write('<th>%d</th>' % i
)
334 f
.write('</tr>\n<tr><th>Commits</th>')
335 fp
= open(path
+ '/hour_of_day.dat', 'w')
336 for i
in range(0, 24):
338 f
.write('<td>%d</td>' % hour_of_day
[i
])
339 fp
.write('%d %d\n' % (i
, hour_of_day
[i
]))
341 f
.write('<td>0</td>')
342 fp
.write('%d 0\n' % i
)
344 f
.write('</tr>\n<tr><th>%</th>')
345 totalcommits
= data
.getTotalCommits()
346 for i
in range(0, 24):
348 f
.write('<td>%.2f</td>' % ((100.0 * hour_of_day
[i
]) / totalcommits
))
350 f
.write('<td>0.00</td>')
351 f
.write('</tr></table>')
352 f
.write('<img src="hour_of_day.png" alt="Hour of Day" />')
353 fg
= open(path
+ '/hour_of_day.dat', 'w')
354 for i
in range(0, 24):
356 fg
.write('%d %d\n' % (i
+ 1, hour_of_day
[i
]))
358 fg
.write('%d 0\n' % (i
+ 1))
362 # TODO show also by hour of weekday?
363 f
.write('\n<h2>Day of Week</h2>\n\n')
364 day_of_week
= data
.getActivityByDayOfWeek()
365 f
.write('<div class="vtable"><table>')
366 f
.write('<tr><th>Day</th><th>Total (%)</th></tr>')
367 fp
= open(path
+ '/day_of_week.dat', 'w')
368 for d
in range(0, 7):
369 fp
.write('%d %d\n' % (d
+ 1, day_of_week
[d
]))
371 f
.write('<th>%d</th>' % (d
+ 1))
373 f
.write('<td>%d (%.2f%%)</td>' % (day_of_week
[d
], (100.0 * day_of_week
[d
]) / totalcommits
))
375 f
.write('<td>0</td>')
377 f
.write('</table></div>')
378 f
.write('<img src="day_of_week.png" alt="Day of Week" />')
382 f
.write('\n<h2>Hour of Week</h2>\n\n')
385 f
.write('<tr><th>Weekday</th>')
386 for hour
in range(0, 24):
387 f
.write('<th>%d</th>' % (hour
+ 1))
390 for weekday
in range(0, 7):
391 f
.write('<tr><th>%d</th>' % (weekday
+ 1))
392 for hour
in range(0, 24):
394 commits
= data
.activity_by_hour_of_week
[weekday
][hour
]
398 f
.write('<td>%d</td>' % commits
)
406 f
.write('\n<h2>Month of Year</h2>\n\n')
407 f
.write('<div class="vtable"><table>')
408 f
.write('<tr><th>Month</th><th>Commits (%)</th></tr>')
409 fp
= open (path
+ '/month_of_year.dat', 'w')
410 for mm
in range(1, 13):
412 if mm
in data
.activity_by_month_of_year
:
413 commits
= data
.activity_by_month_of_year
[mm
]
414 f
.write('<tr><td>%d</td><td>%d (%.2f %%)</td></tr>' % (mm
, commits
, (100.0 * commits
) / data
.getTotalCommits()))
415 fp
.write('%d %d\n' % (mm
, commits
))
417 f
.write('</table></div>')
418 f
.write('<img src="month_of_year.png" alt="Month of Year" />')
420 # Commits by year/month
421 f
.write('<h2>Commits by year/month</h2>')
422 f
.write('<div class="vtable"><table><tr><th>Month</th><th>Commits</th></tr>')
423 for yymm
in reversed(sorted(data
.commits_by_month
.keys())):
424 f
.write('<tr><td>%s</td><td>%d</td></tr>' % (yymm
, data
.commits_by_month
[yymm
]))
425 f
.write('</table></div>')
426 f
.write('<img src="commits_by_year_month.png" alt="Commits by year/month" />')
427 fg
= open(path
+ '/commits_by_year_month.dat', 'w')
428 for yymm
in sorted(data
.commits_by_month
.keys()):
429 fg
.write('%s %s\n' % (yymm
, data
.commits_by_month
[yymm
]))
433 f
.write('<h2>Commits by year</h2>')
434 f
.write('<div class="vtable"><table><tr><th>Year</th><th>Commits (% of all)</th></tr>')
435 for yy
in reversed(sorted(data
.commits_by_year
.keys())):
436 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()))
437 f
.write('</table></div>')
438 f
.write('<img src="commits_by_year.png" alt="Commits by Year" />')
439 fg
= open(path
+ '/commits_by_year.dat', 'w')
440 for yy
in sorted(data
.commits_by_year
.keys()):
441 fg
.write('%d %d\n' % (yy
, data
.commits_by_year
[yy
]))
444 f
.write('</body></html>')
449 f
= open(path
+ '/authors.html', 'w')
452 f
.write('<h1>Authors</h1>')
455 # Authors :: List of authors
456 f
.write('\n<h2>List of authors</h2>\n\n')
458 f
.write('<table class="authors">')
459 f
.write('<tr><th>Author</th><th>Commits (%)</th><th>First commit</th><th>Last commit</th><th>Age</th></tr>')
460 for author
in sorted(data
.getAuthors()):
461 info
= data
.getAuthorInfo(author
)
462 f
.write('<tr><td>%s</td><td>%d (%.2f%%)</td><td>%s</td><td>%s</td><td>%s</td></tr>' % (author
, info
['commits'], info
['commits_frac'], info
['date_first'], info
['date_last'], info
['timedelta']))
465 # Authors :: Author of Month
466 f
.write('\n<h2>Author of Month</h2>\n\n')
468 f
.write('<tr><th>Month</th><th>Author</th><th>Commits (%)</th></tr>')
469 for yymm
in reversed(sorted(data
.author_of_month
.keys())):
470 authordict
= data
.author_of_month
[yymm
]
471 authors
= getkeyssortedbyvalues(authordict
)
473 commits
= data
.author_of_month
[yymm
][authors
[0]]
474 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
]))
478 f
.write('\n<h2>Author of Year</h2>\n\n')
479 f
.write('<table><tr><th>Year</th><th>Author</th><th>Commits (%)</th></tr>')
480 for yy
in reversed(sorted(data
.author_of_year
.keys())):
481 authordict
= data
.author_of_year
[yy
]
482 authors
= getkeyssortedbyvalues(authordict
)
484 commits
= data
.author_of_year
[yy
][authors
[0]]
485 f
.write('<tr><td>%s</td><td>%s</td><td>%d (%.2f%% of %d)</td></tr>' % (yy
, authors
[0], commits
, (100 * commits
) / data
.commits_by_year
[yy
], data
.commits_by_year
[yy
]))
488 f
.write('</body></html>')
493 f
= open(path
+ '/files.html', 'w')
495 f
.write('<h1>Files</h1>')
499 f
.write('<dt>Total files</dt><dd>%d</dd>' % data
.getTotalFiles())
500 f
.write('<dt>Total lines</dt><dd>%d</dd>' % data
.getTotalLOC())
501 f
.write('<dt>Average file size</dt><dd>%.2f bytes</dd>' % ((100.0 * data
.getTotalLOC()) / data
.getTotalFiles()))
504 # Files :: File count by date
505 f
.write('<h2>File count by date</h2>')
507 fg
= open(path
+ '/files_by_date.dat', 'w')
508 for stamp
in sorted(data
.files_by_stamp
.keys()):
509 fg
.write('%s %d\n' % (datetime
.datetime
.fromtimestamp(stamp
).strftime('%Y-%m-%d'), data
.files_by_stamp
[stamp
]))
512 f
.write('<img src="files_by_date.png" alt="Files by Date" />')
514 #f.write('<h2>Average file size by date</h2>')
516 # Files :: Extensions
517 f
.write('\n<h2>Extensions</h2>\n\n')
518 f
.write('<table><tr><th>Extension</th><th>Files (%)</th><th>Lines (%)</th><th>Lines/file</th></tr>')
519 for ext
in sorted(data
.extensions
.keys()):
520 files
= data
.extensions
[ext
]['files']
521 lines
= data
.extensions
[ext
]['lines']
522 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
))
525 f
.write('</body></html>')
530 f
= open(path
+ '/lines.html', 'w')
532 f
.write('<h1>Lines</h1>')
536 f
.write('<dt>Total lines</dt><dd>%d</dd>' % data
.getTotalLOC())
539 f
.write('</body></html>')
544 f
= open(path
+ '/tags.html', 'w')
546 f
.write('<h1>Tags</h1>')
550 f
.write('<dt>Total tags</dt><dd>%d</dd>' % len(data
.tags
))
551 if len(data
.tags
) > 0:
552 f
.write('<dt>Average commits per tag</dt><dd>%.2f</dd>' % (data
.getTotalCommits() / len(data
.tags
)))
556 f
.write('<tr><th>Name</th><th>Date</th></tr>')
557 # sort the tags by date desc
558 tags_sorted_by_date_desc
= map(lambda el
: el
[1], reversed(sorted(map(lambda el
: (el
[1]['date'], el
[0]), data
.tags
.items()))))
559 for tag
in tags_sorted_by_date_desc
:
560 f
.write('<tr><td>%s</td><td>%s</td></tr>' % (tag
, data
.tags
[tag
]['date']))
563 f
.write('</body></html>')
566 self
.createGraphs(path
)
569 def createGraphs(self
, path
):
570 print 'Generating graphs...'
573 f
= open(path
+ '/hour_of_day.plot', 'w')
574 f
.write(GNUPLOT_COMMON
)
577 set output 'hour_of_day.png'
579 set xrange [0.5:24.5]
582 plot 'hour_of_day.dat' using 1:2:(0.5) w boxes fs solid
587 f
= open(path
+ '/day_of_week.plot', 'w')
588 f
.write(GNUPLOT_COMMON
)
591 set output 'day_of_week.png'
596 plot 'day_of_week.dat' using 1:2:(0.5) w boxes fs solid
601 f
= open(path
+ '/month_of_year.plot', 'w')
602 f
.write(GNUPLOT_COMMON
)
605 set output 'month_of_year.png'
607 set xrange [0.5:12.5]
610 plot 'month_of_year.dat' using 1:2:(0.5) w boxes fs solid
614 # commits_by_year_month
615 f
= open(path
+ '/commits_by_year_month.plot', 'w')
616 f
.write(GNUPLOT_COMMON
)
618 # TODO rotate xtic labels by 90 degrees
620 set output 'commits_by_year_month.png'
627 plot 'commits_by_year_month.dat' using 1:2:(0.5) w boxes fs solid
632 f
= open(path
+ '/commits_by_year.plot', 'w')
633 f
.write(GNUPLOT_COMMON
)
636 set output 'commits_by_year.png'
640 plot 'commits_by_year.dat' using 1:2:(0.5) w boxes fs solid
645 f
= open(path
+ '/files_by_date.plot', 'w')
646 f
.write(GNUPLOT_COMMON
)
649 set output 'files_by_date.png'
652 set timefmt "%Y-%m-%d"
653 set format x "%Y-%m-%d"
655 set xtics rotate by 90
656 plot 'files_by_date.dat' using 1:2 smooth csplines
661 files
= glob
.glob(path
+ '/*.plot')
663 print '>> gnuplot %s' % os
.path
.basename(f
)
664 os
.system('gnuplot %s' % f
)
666 def printHeader(self
, f
):
668 """<?xml version="1.0" encoding="UTF-8"?>
669 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
670 <html xmlns="http://www.w3.org/1999/xhtml">
672 <title>StatGit</title>
673 <link rel="stylesheet" href="statgit.css" type="text/css" />
674 <meta name="generator" content="statgit" />
679 def printNav(self
, f
):
683 <li><a href="index.html">General</a></li>
684 <li><a href="activity.html">Activity</a></li>
685 <li><a href="authors.html">Authors</a></li>
686 <li><a href="files.html">Files</a></li>
687 <li><a href="lines.html">Lines</a></li>
688 <li><a href="tags.html">Tags</a></li>
695 Usage: statgit [options] <gitpath> <outputpath>
700 if len(sys
.argv
) < 3:
704 gitpath
= sys
.argv
[1]
705 outputpath
= os
.path
.abspath(sys
.argv
[2])
707 print 'Git path: %s' % gitpath
708 print 'Output path: %s' % outputpath
712 print 'Collecting data...'
713 data
= GitDataCollector()
714 data
.collect(gitpath
)
716 print 'Generating report...'
717 report
= HTMLReportCreator()
718 report
.create(data
, outputpath
)