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
88 self
.authors
= {} # name -> {commits, first_commit_stamp, last_commit_stamp}
91 self
.author_of_month
= {} # month -> author -> commits
92 self
.author_of_year
= {} # year -> author -> commits
93 self
.commits_by_month
= {} # month -> commits
94 self
.commits_by_year
= {} # year -> commits
95 self
.first_commit_stamp
= 0
96 self
.last_commit_stamp
= 0
100 lines
= getoutput('git-show-ref --tags').split('\n')
104 (hash, tag
) = line
.split(' ')
105 tag
= tag
.replace('refs/tags/', '')
106 output
= getoutput('git-log "%s" --pretty=format:"%%at %%an" -n 1' % hash)
108 parts
= output
.split(' ')
111 stamp
= int(parts
[0])
114 self
.tags
[tag
] = { 'stamp': stamp
, 'hash' : hash, 'date' : datetime
.datetime
.fromtimestamp(stamp
).strftime('%Y-%m-%d') }
117 # TODO also collect statistics for "last 30 days"/"last 12 months"
118 lines
= getoutput('git-rev-list --pretty=format:"%at %an" HEAD |grep -v ^commit').split('\n')
120 # linux-2.6 says "<unknown>" for one line O_o
121 parts
= line
.split(' ')
124 stamp
= int(parts
[0])
128 author
= ' '.join(parts
[1:])
129 date
= datetime
.datetime
.fromtimestamp(float(stamp
))
131 # First and last commit stamp
132 if self
.last_commit_stamp
== 0:
133 self
.last_commit_stamp
= stamp
134 self
.first_commit_stamp
= stamp
139 if hour
in self
.activity_by_hour_of_day
:
140 self
.activity_by_hour_of_day
[hour
] += 1
142 self
.activity_by_hour_of_day
[hour
] = 1
146 if day
in self
.activity_by_day_of_week
:
147 self
.activity_by_day_of_week
[day
] += 1
149 self
.activity_by_day_of_week
[day
] = 1
153 if month
in self
.activity_by_month_of_year
:
154 self
.activity_by_month_of_year
[month
] += 1
156 self
.activity_by_month_of_year
[month
] = 1
159 if author
not in self
.authors
:
160 self
.authors
[author
] = {}
162 if 'last_commit_stamp' not in self
.authors
[author
]:
163 self
.authors
[author
]['last_commit_stamp'] = stamp
164 self
.authors
[author
]['first_commit_stamp'] = stamp
165 if 'commits' in self
.authors
[author
]:
166 self
.authors
[author
]['commits'] += 1
168 self
.authors
[author
]['commits'] = 1
170 # author of the month/year
171 yymm
= datetime
.datetime
.fromtimestamp(stamp
).strftime('%Y-%m')
172 if yymm
in self
.author_of_month
:
173 if author
in self
.author_of_month
[yymm
]:
174 self
.author_of_month
[yymm
][author
] += 1
176 self
.author_of_month
[yymm
][author
] = 1
178 self
.author_of_month
[yymm
] = {}
179 self
.author_of_month
[yymm
][author
] = 1
180 if yymm
in self
.commits_by_month
:
181 self
.commits_by_month
[yymm
] += 1
183 self
.commits_by_month
[yymm
] = 1
185 yy
= datetime
.datetime
.fromtimestamp(stamp
).year
186 if yy
in self
.author_of_year
:
187 if author
in self
.author_of_year
[yy
]:
188 self
.author_of_year
[yy
][author
] += 1
190 self
.author_of_year
[yy
][author
] = 1
192 self
.author_of_year
[yy
] = {}
193 self
.author_of_year
[yy
][author
] = 1
194 if yy
in self
.commits_by_year
:
195 self
.commits_by_year
[yy
] += 1
197 self
.commits_by_year
[yy
] = 1
199 # outputs "<stamp> <files>" for each revision
200 self
.files_by_stamp
= {} # stamp -> files
201 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')
203 parts
= line
.split(' ')
206 (stamp
, files
) = parts
[0:2]
207 self
.files_by_stamp
[int(stamp
)] = int(files
)
210 self
.extensions
= {} # extension -> files, lines
211 lines
= getoutput('git-ls-files').split('\n')
213 base
= os
.path
.basename(line
)
214 if base
.find('.') == -1:
217 ext
= base
[(base
.rfind('.') + 1):]
219 if ext
not in self
.extensions
:
220 self
.extensions
[ext
] = {'files': 0, 'lines': 0}
222 self
.extensions
[ext
]['files'] += 1
223 self
.extensions
[ext
]['lines'] += int(getoutput('wc -l < %s' % line
, quiet
= True))
225 def getActivityByDayOfWeek(self
):
226 return self
.activity_by_day_of_week
228 def getActivityByHourOfDay(self
):
229 return self
.activity_by_hour_of_day
231 def getAuthorInfo(self
, author
):
232 a
= self
.authors
[author
]
234 commits
= a
['commits']
235 commits_frac
= (100 * float(commits
)) / self
.getTotalCommits()
236 date_first
= datetime
.datetime
.fromtimestamp(a
['first_commit_stamp']).strftime('%Y-%m-%d')
237 date_last
= datetime
.datetime
.fromtimestamp(a
['last_commit_stamp']).strftime('%Y-%m-%d')
239 res
= { 'commits': commits
, 'commits_frac': commits_frac
, 'date_first': date_first
, 'date_last': date_last
}
242 def getAuthors(self
):
243 return self
.authors
.keys()
245 def getFirstCommitDate(self
):
246 return datetime
.datetime
.fromtimestamp(self
.first_commit_stamp
)
248 def getLastCommitDate(self
):
249 return datetime
.datetime
.fromtimestamp(self
.last_commit_stamp
)
252 lines
= getoutput('git-show-ref --tags |cut -d/ -f3')
253 return lines
.split('\n')
255 def getTagDate(self
, tag
):
256 return self
.revToDate('tags/' + tag
)
258 def getTotalAuthors(self
):
259 return self
.total_authors
261 def getTotalCommits(self
):
262 return self
.total_commits
264 def getTotalFiles(self
):
265 return self
.total_files
267 def getTotalLOC(self
):
268 return self
.total_lines
270 def revToDate(self
, rev
):
271 stamp
= int(getoutput('git-log --pretty=format:%%at "%s" -n 1' % rev
))
272 return datetime
.datetime
.fromtimestamp(stamp
).strftime('%Y-%m-%d')
278 def create(self
, data
, path
):
282 class HTMLReportCreator(ReportCreator
):
283 def create(self
, data
, path
):
284 ReportCreator
.create(self
, data
, path
)
286 f
= open(path
+ "/index.html", 'w')
287 format
= '%Y-%m-%d %H:%m:%S'
290 f
.write('<h1>StatGit</h1>')
295 f
.write('<dt>Generated</dt><dd>%s (in %d seconds)</dd>' % (datetime
.datetime
.now().strftime(format
), time
.time() - data
.getStampCreated()));
296 f
.write('<dt>Report Period</dt><dd>%s to %s</dd>' % (data
.getFirstCommitDate().strftime(format
), data
.getLastCommitDate().strftime(format
)))
297 f
.write('<dt>Total Files</dt><dd>%s</dd>' % data
.getTotalFiles())
298 f
.write('<dt>Total Lines of Code</dt><dd>%s</dd>' % data
.getTotalLOC())
299 f
.write('<dt>Total Commits</dt><dd>%s</dd>' % data
.getTotalCommits())
300 f
.write('<dt>Authors</dt><dd>%s</dd>' % data
.getTotalAuthors())
303 f
.write('</body>\n</html>');
308 f
= open(path
+ '/activity.html', 'w')
310 f
.write('<h1>Activity</h1>')
313 f
.write('<h2>Last 30 days</h2>')
315 f
.write('<h2>Last 12 months</h2>')
318 f
.write('\n<h2>Hour of Day</h2>\n\n')
319 hour_of_day
= data
.getActivityByHourOfDay()
320 f
.write('<table><tr><th>Hour</th>')
321 for i
in range(1, 25):
322 f
.write('<th>%d</th>' % i
)
323 f
.write('</tr>\n<tr><th>Commits</th>')
324 fp
= open(path
+ '/hour_of_day.dat', 'w')
325 for i
in range(0, 24):
327 f
.write('<td>%d</td>' % hour_of_day
[i
])
328 fp
.write('%d %d\n' % (i
, hour_of_day
[i
]))
330 f
.write('<td>0</td>')
331 fp
.write('%d 0\n' % i
)
333 f
.write('</tr>\n<tr><th>%</th>')
334 totalcommits
= data
.getTotalCommits()
335 for i
in range(0, 24):
337 f
.write('<td>%.2f</td>' % ((100.0 * hour_of_day
[i
]) / totalcommits
))
339 f
.write('<td>0.00</td>')
340 f
.write('</tr></table>')
341 f
.write('<img src="hour_of_day.png" alt="Hour of Day" />')
342 fg
= open(path
+ '/hour_of_day.dat', 'w')
343 for i
in range(0, 24):
345 fg
.write('%d %d\n' % (i
+ 1, hour_of_day
[i
]))
347 fg
.write('%d 0\n' % (i
+ 1))
351 # TODO show also by hour of weekday?
352 f
.write('\n<h2>Day of Week</h2>\n\n')
353 day_of_week
= data
.getActivityByDayOfWeek()
354 f
.write('<div class="vtable"><table>')
355 f
.write('<tr><th>Day</th><th>Total (%)</th></tr>')
356 fp
= open(path
+ '/day_of_week.dat', 'w')
357 for d
in range(0, 7):
358 fp
.write('%d %d\n' % (d
+ 1, day_of_week
[d
]))
360 f
.write('<th>%d</th>' % (d
+ 1))
362 f
.write('<td>%d (%.2f%%)</td>' % (day_of_week
[d
], (100.0 * day_of_week
[d
]) / totalcommits
))
364 f
.write('<td>0</td>')
366 f
.write('</table></div>')
367 f
.write('<img src="day_of_week.png" alt="Day of Week" />')
371 f
.write('\n<h2>Month of Year</h2>\n\n')
372 f
.write('<div class="vtable"><table>')
373 f
.write('<tr><th>Month</th><th>Commits (%)</th></tr>')
374 fp
= open (path
+ '/month_of_year.dat', 'w')
375 for mm
in range(1, 13):
377 if mm
in data
.activity_by_month_of_year
:
378 commits
= data
.activity_by_month_of_year
[mm
]
379 f
.write('<tr><td>%d</td><td>%d (%.2f %%)</td></tr>' % (mm
, commits
, (100.0 * commits
) / data
.getTotalCommits()))
380 fp
.write('%d %d\n' % (mm
, commits
))
382 f
.write('</table></div>')
383 f
.write('<img src="month_of_year.png" alt="Month of Year" />')
385 # Commits by year/month
386 f
.write('<h2>Commits by year/month</h2>')
387 f
.write('<div class="vtable"><table><tr><th>Month</th><th>Commits</th></tr>')
388 for yymm
in reversed(sorted(data
.commits_by_month
.keys())):
389 f
.write('<tr><td>%s</td><td>%d</td></tr>' % (yymm
, data
.commits_by_month
[yymm
]))
390 f
.write('</table></div>')
391 f
.write('<img src="commits_by_year_month.png" alt="Commits by year/month" />')
392 fg
= open(path
+ '/commits_by_year_month.dat', 'w')
393 for yymm
in sorted(data
.commits_by_month
.keys()):
394 fg
.write('%s %s\n' % (yymm
, data
.commits_by_month
[yymm
]))
398 f
.write('<h2>Commits by year</h2>')
399 f
.write('<div class="vtable"><table><tr><th>Year</th><th>Commits (% of all)</th></tr>')
400 for yy
in reversed(sorted(data
.commits_by_year
.keys())):
401 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()))
402 f
.write('</table></div>')
403 f
.write('<img src="commits_by_year.png" alt="Commits by Year" />')
404 fg
= open(path
+ '/commits_by_year.dat', 'w')
405 for yy
in sorted(data
.commits_by_year
.keys()):
406 fg
.write('%d %d\n' % (yy
, data
.commits_by_year
[yy
]))
409 f
.write('</body></html>')
414 f
= open(path
+ '/authors.html', 'w')
417 f
.write('<h1>Authors</h1>')
420 f
.write('\n<h2>List of authors</h2>\n\n')
422 f
.write('<table class="authors">')
423 f
.write('<tr><th>Author</th><th>Commits (%)</th><th>First commit</th><th>Last commit</th></tr>')
424 for author
in sorted(data
.getAuthors()):
425 info
= data
.getAuthorInfo(author
)
426 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']))
429 f
.write('\n<h2>Author of Month</h2>\n\n')
431 f
.write('<tr><th>Month</th><th>Author</th><th>Commits (%)</th></tr>')
432 for yymm
in reversed(sorted(data
.author_of_month
.keys())):
433 authordict
= data
.author_of_month
[yymm
]
434 authors
= getkeyssortedbyvalues(authordict
)
436 commits
= data
.author_of_month
[yymm
][authors
[0]]
437 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
]))
441 f
.write('\n<h2>Author of Year</h2>\n\n')
442 f
.write('<table><tr><th>Year</th><th>Author</th><th>Commits (%)</th></tr>')
443 for yy
in reversed(sorted(data
.author_of_year
.keys())):
444 authordict
= data
.author_of_year
[yy
]
445 authors
= getkeyssortedbyvalues(authordict
)
447 commits
= data
.author_of_year
[yy
][authors
[0]]
448 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
]))
451 f
.write('</body></html>')
456 f
= open(path
+ '/files.html', 'w')
458 f
.write('<h1>Files</h1>')
462 f
.write('<dt>Total files</dt><dd>%d</dd>' % data
.getTotalFiles())
463 f
.write('<dt>Total lines</dt><dd>%d</dd>' % data
.getTotalLOC())
464 f
.write('<dt>Average file size</dt><dd>%.2f bytes</dd>' % ((100.0 * data
.getTotalLOC()) / data
.getTotalFiles()))
467 # Files :: File count by date
468 f
.write('<h2>File count by date</h2>')
470 fg
= open(path
+ '/files_by_date.dat', 'w')
471 for stamp
in sorted(data
.files_by_stamp
.keys()):
472 fg
.write('%s %d\n' % (datetime
.datetime
.fromtimestamp(stamp
).strftime('%Y-%m-%d'), data
.files_by_stamp
[stamp
]))
475 f
.write('<img src="files_by_date.png" alt="Files by Date" />')
477 #f.write('<h2>Average file size by date</h2>')
479 # Files :: Extensions
480 f
.write('\n<h2>Extensions</h2>\n\n')
481 f
.write('<table><tr><th>Extension</th><th>Files (%)</th><th>Lines (%)</th><th>Lines/file</th></tr>')
482 for ext
in sorted(data
.extensions
.keys()):
483 files
= data
.extensions
[ext
]['files']
484 lines
= data
.extensions
[ext
]['lines']
485 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
))
488 f
.write('</body></html>')
493 f
= open(path
+ '/lines.html', 'w')
495 f
.write('<h1>Lines</h1>')
499 f
.write('<dt>Total lines</dt><dd>%d</dd>' % data
.getTotalLOC())
502 f
.write('</body></html>')
507 f
= open(path
+ '/tags.html', 'w')
509 f
.write('<h1>Tags</h1>')
513 f
.write('<dt>Total tags</dt><dd>%d</dd>' % len(data
.tags
))
514 if len(data
.tags
) > 0:
515 f
.write('<dt>Average commits per tag</dt><dd>%.2f</dd>' % (data
.getTotalCommits() / len(data
.tags
)))
519 f
.write('<tr><th>Name</th><th>Date</th></tr>')
520 # sort the tags by date desc
521 tags_sorted_by_date_desc
= map(lambda el
: el
[1], reversed(sorted(map(lambda el
: (el
[1]['date'], el
[0]), data
.tags
.items()))))
522 for tag
in tags_sorted_by_date_desc
:
523 f
.write('<tr><td>%s</td><td>%s</td></tr>' % (tag
, data
.tags
[tag
]['date']))
526 f
.write('</body></html>')
529 self
.createGraphs(path
)
532 def createGraphs(self
, path
):
533 print 'Generating graphs...'
536 f
= open(path
+ '/hour_of_day.plot', 'w')
537 f
.write(GNUPLOT_COMMON
)
540 set output 'hour_of_day.png'
542 set xrange [0.5:24.5]
545 plot 'hour_of_day.dat' using 1:2:(0.5) w boxes fs solid
550 f
= open(path
+ '/day_of_week.plot', 'w')
551 f
.write(GNUPLOT_COMMON
)
554 set output 'day_of_week.png'
559 plot 'day_of_week.dat' using 1:2:(0.5) w boxes fs solid
564 f
= open(path
+ '/month_of_year.plot', 'w')
565 f
.write(GNUPLOT_COMMON
)
568 set output 'month_of_year.png'
570 set xrange [0.5:12.5]
573 plot 'month_of_year.dat' using 1:2:(0.5) w boxes fs solid
577 # commits_by_year_month
578 f
= open(path
+ '/commits_by_year_month.plot', 'w')
579 f
.write(GNUPLOT_COMMON
)
581 # TODO rotate xtic labels by 90 degrees
583 set output 'commits_by_year_month.png'
590 plot 'commits_by_year_month.dat' using 1:2:(0.5) w boxes fs solid
595 f
= open(path
+ '/commits_by_year.plot', 'w')
596 f
.write(GNUPLOT_COMMON
)
599 set output 'commits_by_year.png'
603 plot 'commits_by_year.dat' using 1:2:(0.5) w boxes fs solid
608 f
= open(path
+ '/files_by_date.plot', 'w')
609 f
.write(GNUPLOT_COMMON
)
612 set output 'files_by_date.png'
615 set timefmt "%Y-%m-%d"
616 set format x "%Y-%m-%d"
618 set xtics rotate by 90
619 plot 'files_by_date.dat' using 1:2 smooth csplines
624 files
= glob
.glob(path
+ '/*.plot')
626 print '>> gnuplot %s' % os
.path
.basename(f
)
627 os
.system('gnuplot %s' % f
)
629 def printHeader(self
, f
):
631 """<?xml version="1.0" encoding="UTF-8"?>
632 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
633 <html xmlns="http://www.w3.org/1999/xhtml">
635 <title>StatGit</title>
636 <link rel="stylesheet" href="statgit.css" type="text/css" />
637 <meta name="generator" content="statgit" />
642 def printNav(self
, f
):
646 <li><a href="index.html">General</a></li>
647 <li><a href="activity.html">Activity</a></li>
648 <li><a href="authors.html">Authors</a></li>
649 <li><a href="files.html">Files</a></li>
650 <li><a href="lines.html">Lines</a></li>
651 <li><a href="tags.html">Tags</a></li>
658 Usage: statgit [options] <gitpath> <outputpath>
663 if len(sys
.argv
) < 3:
667 gitpath
= sys
.argv
[1]
668 outputpath
= os
.path
.abspath(sys
.argv
[2])
670 print 'Git path: %s' % gitpath
671 print 'Output path: %s' % outputpath
675 print 'Collecting data...'
676 data
= GitDataCollector()
677 data
.collect(gitpath
)
679 print 'Generating report...'
680 report
= HTMLReportCreator()
681 report
.create(data
, outputpath
)