2 # Copyright (c) 2007 Heikki Hokkanen <hoxu@users.sf.net>
10 GNUPLOT_COMMON
= 'set terminal png\nset size 0.5,0.5\n'
14 output
= commands
.getoutput(cmd
)
17 def getkeyssortedbyvalues(dict):
18 return map(lambda el
: el
[1], sorted(map(lambda el
: (el
[1], el
[0]), dict.items())))
20 # TODO getdictkeyssortedbyvaluekey(dict, key) - eg. dict['author'] = { 'commits' : 512 } - ...key(dict, 'commits')
27 # This should be the main function to extract data from the repository.
28 def collect(self
, dir):
32 # : get a dictionary of author
33 def getAuthorInfo(self
, author
):
36 def getActivityByDayOfWeek(self
):
39 def getActivityByHourOfDay(self
):
43 # Get a list of authors
47 def getFirstCommitDate(self
):
48 return datetime
.datetime
.now()
50 def getLastCommitDate(self
):
51 return datetime
.datetime
.now()
56 def getTotalAuthors(self
):
59 def getTotalCommits(self
):
62 def getTotalFiles(self
):
65 def getTotalLOC(self
):
68 class GitDataCollector(DataCollector
):
69 def collect(self
, dir):
70 DataCollector
.collect(self
, dir)
72 self
.total_authors
= int(getoutput('git-log |git-shortlog -s |wc -l'))
73 self
.total_commits
= int(getoutput('git-rev-list HEAD |wc -l'))
74 self
.total_files
= int(getoutput('git-ls-files |wc -l'))
75 self
.total_lines
= int(getoutput('git-ls-files |xargs cat |wc -l'))
77 self
.activity_by_hour_of_day
= {} # hour -> commits
78 self
.activity_by_day_of_week
= {} # day -> commits
80 self
.authors
= {} # name -> {commits, first_commit_stamp, last_commit_stamp}
83 self
.author_of_month
= {} # month -> author -> commits
84 self
.author_of_year
= {} # year -> author -> commits
85 self
.commits_by_month
= {} # month -> commits
86 self
.commits_by_year
= {} # year -> commits
87 self
.first_commit_stamp
= 0
88 self
.last_commit_stamp
= 0
92 lines
= getoutput('git-show-ref --tags').split('\n')
96 (hash, tag
) = line
.split(' ')
97 tag
= tag
.replace('refs/tags/', '')
98 output
= getoutput('git-log "%s" --pretty=format:"%%at %%an" -n 1' % hash)
100 parts
= output
.split(' ')
103 stamp
= int(parts
[0])
106 self
.tags
[tag
] = { 'stamp': stamp
, 'hash' : hash, 'date' : datetime
.datetime
.fromtimestamp(stamp
).strftime('%Y-%m-%d') }
109 # TODO also collect statistics for "last 30 days"/"last 12 months"
110 lines
= getoutput('git-rev-list --pretty=format:"%at %an" HEAD |grep -v ^commit').split('\n')
112 # linux-2.6 says "<unknown>" for one line O_o
113 parts
= line
.split(' ')
116 stamp
= int(parts
[0])
120 author
= ' '.join(parts
[1:])
121 date
= datetime
.datetime
.fromtimestamp(float(stamp
))
123 # First and last commit stamp
124 if self
.last_commit_stamp
== 0:
125 self
.last_commit_stamp
= stamp
126 self
.first_commit_stamp
= stamp
131 if hour
in self
.activity_by_hour_of_day
:
132 self
.activity_by_hour_of_day
[hour
] += 1
134 self
.activity_by_hour_of_day
[hour
] = 1
138 if day
in self
.activity_by_day_of_week
:
139 self
.activity_by_day_of_week
[day
] += 1
141 self
.activity_by_day_of_week
[day
] = 1
144 if author
not in self
.authors
:
145 self
.authors
[author
] = {}
147 if 'last_commit_stamp' not in self
.authors
[author
]:
148 self
.authors
[author
]['last_commit_stamp'] = stamp
149 self
.authors
[author
]['first_commit_stamp'] = stamp
150 if 'commits' in self
.authors
[author
]:
151 self
.authors
[author
]['commits'] += 1
153 self
.authors
[author
]['commits'] = 1
155 # author of the month/year
156 yymm
= datetime
.datetime
.fromtimestamp(stamp
).strftime('%Y-%m')
157 if yymm
in self
.author_of_month
:
158 if author
in self
.author_of_month
[yymm
]:
159 self
.author_of_month
[yymm
][author
] += 1
161 self
.author_of_month
[yymm
][author
] = 1
163 self
.author_of_month
[yymm
] = {}
164 self
.author_of_month
[yymm
][author
] = 1
165 if yymm
in self
.commits_by_month
:
166 self
.commits_by_month
[yymm
] += 1
168 self
.commits_by_month
[yymm
] = 1
170 yy
= datetime
.datetime
.fromtimestamp(stamp
).year
171 if yy
in self
.author_of_year
:
172 if author
in self
.author_of_year
[yy
]:
173 self
.author_of_year
[yy
][author
] += 1
175 self
.author_of_year
[yy
][author
] = 1
177 self
.author_of_year
[yy
] = {}
178 self
.author_of_year
[yy
][author
] = 1
179 if yy
in self
.commits_by_year
:
180 self
.commits_by_year
[yy
] += 1
182 self
.commits_by_year
[yy
] = 1
184 def getActivityByDayOfWeek(self
):
185 return self
.activity_by_day_of_week
187 def getActivityByHourOfDay(self
):
188 return self
.activity_by_hour_of_day
190 def getAuthorInfo(self
, author
):
191 a
= self
.authors
[author
]
193 commits
= a
['commits']
194 commits_frac
= (100 * float(commits
)) / self
.getTotalCommits()
195 date_first
= datetime
.datetime
.fromtimestamp(a
['first_commit_stamp']).strftime('%Y-%m-%d')
196 date_last
= datetime
.datetime
.fromtimestamp(a
['last_commit_stamp']).strftime('%Y-%m-%d')
198 res
= { 'commits': commits
, 'commits_frac': commits_frac
, 'date_first': date_first
, 'date_last': date_last
}
201 def getAuthors(self
):
202 return self
.authors
.keys()
204 def getFirstCommitDate(self
):
205 return datetime
.datetime
.fromtimestamp(self
.first_commit_stamp
)
207 def getLastCommitDate(self
):
208 return datetime
.datetime
.fromtimestamp(self
.last_commit_stamp
)
211 lines
= getoutput('git-show-ref --tags |cut -d/ -f3')
212 return lines
.split('\n')
214 def getTagDate(self
, tag
):
215 return self
.revToDate('tags/' + tag
)
217 def getTotalAuthors(self
):
218 return self
.total_authors
220 def getTotalCommits(self
):
221 return self
.total_commits
223 def getTotalFiles(self
):
224 return self
.total_files
226 def getTotalLOC(self
):
227 return self
.total_lines
229 def revToDate(self
, rev
):
230 stamp
= int(getoutput('git-log --pretty=format:%%at "%s" -n 1' % rev
))
231 return datetime
.datetime
.fromtimestamp(stamp
).strftime('%Y-%m-%d')
237 def create(self
, data
, path
):
241 class HTMLReportCreator(ReportCreator
):
242 def create(self
, data
, path
):
243 ReportCreator
.create(self
, data
, path
)
245 f
= open(path
+ "/index.html", 'w')
246 format
= '%Y-%m-%d %H:%m:%S'
249 f
.write('<h1>StatGit</h1>')
254 f
.write('<dt>Generated</dt><dd>%s</dd>' % datetime
.datetime
.now().strftime(format
));
255 f
.write('<dt>Report Period</dt><dd>%s to %s</dd>' % (data
.getFirstCommitDate().strftime(format
), data
.getLastCommitDate().strftime(format
)))
256 f
.write('<dt>Total Files</dt><dd>%s</dd>' % data
.getTotalFiles())
257 f
.write('<dt>Total Lines of Code</dt><dd>%s</dd>' % data
.getTotalLOC())
258 f
.write('<dt>Total Commits</dt><dd>%s</dd>' % data
.getTotalCommits())
259 f
.write('<dt>Authors</dt><dd>%s</dd>' % data
.getTotalAuthors())
262 f
.write('</body>\n</html>');
267 f
= open(path
+ '/activity.html', 'w')
269 f
.write('<h1>Activity</h1>')
272 f
.write('<h2>Last 30 days</h2>')
274 f
.write('<h2>Last 12 months</h2>')
277 f
.write('\n<h2>Hour of Day</h2>\n\n')
278 hour_of_day
= data
.getActivityByHourOfDay()
279 f
.write('<table><tr><th>Hour</th>')
280 for i
in range(1, 25):
281 f
.write('<th>%d</th>' % i
)
282 f
.write('</tr>\n<tr><th>Commits</th>')
283 fp
= open(path
+ '/hour_of_day.dat', 'w')
284 for i
in range(0, 24):
286 f
.write('<td>%d</td>' % hour_of_day
[i
])
287 fp
.write('%d %d\n' % (i
, hour_of_day
[i
]))
289 f
.write('<td>0</td>')
290 fp
.write('%d 0\n' % i
)
292 f
.write('</tr>\n<tr><th>%</th>')
293 totalcommits
= data
.getTotalCommits()
294 for i
in range(0, 24):
296 f
.write('<td>%.2f</td>' % ((100.0 * hour_of_day
[i
]) / totalcommits
))
298 f
.write('<td>0.00</td>')
299 f
.write('</tr></table>')
302 # TODO show also by hour of weekday?
303 f
.write('\n<h2>Day of Week</h2>\n\n')
304 day_of_week
= data
.getActivityByDayOfWeek()
306 f
.write('<tr><th>Day</th><th>Total (%)</th></tr>')
307 fp
= open(path
+ '/day_of_week.dat', 'w')
308 for d
in range(0, 7):
309 fp
.write('%d %d\n' % (d
+ 1, day_of_week
[d
]))
311 f
.write('<th>%d</th>' % (d
+ 1))
313 f
.write('<td>%d (%.2f%%)</td>' % (day_of_week
[d
], (100.0 * day_of_week
[d
]) / totalcommits
))
315 f
.write('<td>0</td>')
320 # Commits by year/month
321 f
.write('<h2>Commits by year/month</h2>')
322 f
.write('<table><tr><th>Month</th><th>Commits</th></tr>')
323 for yymm
in reversed(sorted(data
.commits_by_month
.keys())):
324 f
.write('<tr><td>%s</td><td>%d</td></tr>' % (yymm
, data
.commits_by_month
[yymm
]))
326 f
.write('<img src="commits_by_year_month.png" />')
327 fg
= open(path
+ '/commits_by_year_month.dat', 'w')
328 for yymm
in sorted(data
.commits_by_month
.keys()):
329 fg
.write('%s %s\n' % (yymm
, data
.commits_by_month
[yymm
]))
333 f
.write('<h2>Commits by year</h2>')
334 f
.write('<table><tr><th>Year</th><th>Commits (% of all)</th></tr>')
335 for yy
in reversed(sorted(data
.commits_by_year
.keys())):
336 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()))
338 f
.write('<img src="commits_by_year.png" />')
339 fg
= open(path
+ '/commits_by_year.dat', 'w')
340 for yy
in sorted(data
.commits_by_year
.keys()):
341 fg
.write('%d %d\n' % (yy
, data
.commits_by_year
[yy
]))
344 f
.write('</body></html>')
349 f
= open(path
+ '/authors.html', 'w')
352 f
.write('<h1>Authors</h1>')
355 f
.write('\n<h2>List of authors</h2>\n\n')
357 f
.write('<table class="authors">')
358 f
.write('<tr><th>Author</th><th>Commits (%)</th><th>First commit</th><th>Last commit</th></tr>')
359 for author
in sorted(data
.getAuthors()):
360 info
= data
.getAuthorInfo(author
)
361 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']))
364 f
.write('\n<h2>Author of Month</h2>\n\n')
366 f
.write('<tr><th>Month</th><th>Author</th><th>Commits (%)</th></tr>')
367 for yymm
in reversed(sorted(data
.author_of_month
.keys())):
368 authordict
= data
.author_of_month
[yymm
]
369 authors
= getkeyssortedbyvalues(authordict
)
371 commits
= data
.author_of_month
[yymm
][authors
[0]]
372 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
]))
376 f
.write('\n<h2>Author of Year</h2>\n\n')
377 f
.write('<table><tr><th>Year</th><th>Author</th><th>Commits (%)</th></tr>')
378 for yy
in reversed(sorted(data
.author_of_year
.keys())):
379 authordict
= data
.author_of_year
[yy
]
380 authors
= getkeyssortedbyvalues(authordict
)
382 commits
= data
.author_of_year
[yy
][authors
[0]]
383 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
]))
386 f
.write('</body></html>')
391 f
= open(path
+ '/files.html', 'w')
393 f
.write('<h1>Files</h1>')
396 f
.write('<h2>File count by date</h2>')
398 f
.write('</body></html>')
403 f
= open(path
+ '/tags.html', 'w')
405 f
.write('<h1>Tags</h1>')
409 f
.write('<dt>Total tags</dt><dd>%d</dd>' % len(data
.tags
))
410 if len(data
.tags
) > 0:
411 f
.write('<dt>Average commits per tag</dt><dd>%.2f</dd>' % (data
.getTotalCommits() / len(data
.tags
)))
415 f
.write('<tr><th>Name</th><th>Date</th></tr>')
416 # sort the tags by date desc
417 tags_sorted_by_date_desc
= map(lambda el
: el
[1], reversed(sorted(map(lambda el
: (el
[1]['date'], el
[0]), data
.tags
.items()))))
418 for tag
in tags_sorted_by_date_desc
:
419 f
.write('<tr><td>%s</td><td>%s</td></tr>' % (tag
, data
.tags
[tag
]['date']))
422 f
.write('</body></html>')
425 self
.createGraphs(path
)
428 def createGraphs(self
, path
):
429 print 'Generating graphs...'
431 # commits_by_year_month
432 f
= open(path
+ '/commits_by_year_month.plot', 'w')
433 f
.write(GNUPLOT_COMMON
)
435 # TODO rotate xtic labels by 90 degrees
437 set output 'commits_by_year_month.png'
442 plot 'commits_by_year_month.dat' using 1:2:(0.5) w boxes fs solid
447 f
= open(path
+ '/commits_by_year.plot', 'w')
448 f
.write(GNUPLOT_COMMON
)
451 set output 'commits_by_year.png'
454 plot 'commits_by_year.dat' using 1:2:(0.5) w boxes fs solid
459 for i
in ('commits_by_year_month', 'commits_by_year'):
460 os
.system('gnuplot %s.plot' % i
)
464 def printHeader(self
, f
):
467 <title>StatGit</title>
468 <link rel="stylesheet" href="statgit.css" type="text/css" />
473 def printNav(self
, f
):
476 <li><a href="index.html">General</a></li>
477 <li><a href="activity.html">Activity</a></li>
478 <li><a href="authors.html">Authors</a></li>
479 <li><a href="files.html">Files</a></li>
480 <li><a href="lines.html">Lines</a></li>
481 <li><a href="tags.html">Tags</a></li>
488 Usage: statgit [options] <gitpath> <outputpath>
494 if len(sys
.argv
) < 3:
498 gitpath
= sys
.argv
[1]
499 outputpath
= os
.path
.abspath(sys
.argv
[2])
501 print 'Git path: %s' % gitpath
502 print 'Output path: %s' % outputpath
506 print 'Collecting data...'
507 data
= GitDataCollector()
508 data
.collect(gitpath
)
510 print 'Generating report...'
511 report
= HTMLReportCreator()
512 report
.create(data
, outputpath
)