2 # Copyright (c) 2007 Heikki Hokkanen <hoxu@users.sf.net>
10 GNUPLOT_COMMON
= 'set terminal png transparent\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>')
300 f
.write('<img src="hour_of_day.png" />')
301 fg
= open(path
+ '/hour_of_day.dat', 'w')
302 for i
in range(0, 24):
304 fg
.write('%d %d\n' % (i
+ 1, hour_of_day
[i
]))
306 fg
.write('%d 0\n' % (i
+ 1))
310 # TODO show also by hour of weekday?
311 f
.write('\n<h2>Day of Week</h2>\n\n')
312 day_of_week
= data
.getActivityByDayOfWeek()
314 f
.write('<tr><th>Day</th><th>Total (%)</th></tr>')
315 fp
= open(path
+ '/day_of_week.dat', 'w')
316 for d
in range(0, 7):
317 fp
.write('%d %d\n' % (d
+ 1, day_of_week
[d
]))
319 f
.write('<th>%d</th>' % (d
+ 1))
321 f
.write('<td>%d (%.2f%%)</td>' % (day_of_week
[d
], (100.0 * day_of_week
[d
]) / totalcommits
))
323 f
.write('<td>0</td>')
326 f
.write('<img src="day_of_week.png" />')
329 # Commits by year/month
330 f
.write('<h2>Commits by year/month</h2>')
331 f
.write('<table><tr><th>Month</th><th>Commits</th></tr>')
332 for yymm
in reversed(sorted(data
.commits_by_month
.keys())):
333 f
.write('<tr><td>%s</td><td>%d</td></tr>' % (yymm
, data
.commits_by_month
[yymm
]))
335 f
.write('<img src="commits_by_year_month.png" />')
336 fg
= open(path
+ '/commits_by_year_month.dat', 'w')
337 for yymm
in sorted(data
.commits_by_month
.keys()):
338 fg
.write('%s %s\n' % (yymm
, data
.commits_by_month
[yymm
]))
342 f
.write('<h2>Commits by year</h2>')
343 f
.write('<table><tr><th>Year</th><th>Commits (% of all)</th></tr>')
344 for yy
in reversed(sorted(data
.commits_by_year
.keys())):
345 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()))
347 f
.write('<img src="commits_by_year.png" />')
348 fg
= open(path
+ '/commits_by_year.dat', 'w')
349 for yy
in sorted(data
.commits_by_year
.keys()):
350 fg
.write('%d %d\n' % (yy
, data
.commits_by_year
[yy
]))
353 f
.write('</body></html>')
358 f
= open(path
+ '/authors.html', 'w')
361 f
.write('<h1>Authors</h1>')
364 f
.write('\n<h2>List of authors</h2>\n\n')
366 f
.write('<table class="authors">')
367 f
.write('<tr><th>Author</th><th>Commits (%)</th><th>First commit</th><th>Last commit</th></tr>')
368 for author
in sorted(data
.getAuthors()):
369 info
= data
.getAuthorInfo(author
)
370 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']))
373 f
.write('\n<h2>Author of Month</h2>\n\n')
375 f
.write('<tr><th>Month</th><th>Author</th><th>Commits (%)</th></tr>')
376 for yymm
in reversed(sorted(data
.author_of_month
.keys())):
377 authordict
= data
.author_of_month
[yymm
]
378 authors
= getkeyssortedbyvalues(authordict
)
380 commits
= data
.author_of_month
[yymm
][authors
[0]]
381 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
]))
385 f
.write('\n<h2>Author of Year</h2>\n\n')
386 f
.write('<table><tr><th>Year</th><th>Author</th><th>Commits (%)</th></tr>')
387 for yy
in reversed(sorted(data
.author_of_year
.keys())):
388 authordict
= data
.author_of_year
[yy
]
389 authors
= getkeyssortedbyvalues(authordict
)
391 commits
= data
.author_of_year
[yy
][authors
[0]]
392 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
]))
395 f
.write('</body></html>')
400 f
= open(path
+ '/files.html', 'w')
402 f
.write('<h1>Files</h1>')
405 f
.write('<h2>File count by date</h2>')
407 f
.write('</body></html>')
412 f
= open(path
+ '/tags.html', 'w')
414 f
.write('<h1>Tags</h1>')
418 f
.write('<dt>Total tags</dt><dd>%d</dd>' % len(data
.tags
))
419 if len(data
.tags
) > 0:
420 f
.write('<dt>Average commits per tag</dt><dd>%.2f</dd>' % (data
.getTotalCommits() / len(data
.tags
)))
424 f
.write('<tr><th>Name</th><th>Date</th></tr>')
425 # sort the tags by date desc
426 tags_sorted_by_date_desc
= map(lambda el
: el
[1], reversed(sorted(map(lambda el
: (el
[1]['date'], el
[0]), data
.tags
.items()))))
427 for tag
in tags_sorted_by_date_desc
:
428 f
.write('<tr><td>%s</td><td>%s</td></tr>' % (tag
, data
.tags
[tag
]['date']))
431 f
.write('</body></html>')
434 self
.createGraphs(path
)
437 def createGraphs(self
, path
):
438 print 'Generating graphs...'
441 f
= open(path
+ '/hour_of_day.plot', 'w')
442 f
.write(GNUPLOT_COMMON
)
445 set output 'hour_of_day.png'
447 set xrange [0.5:24.5]
450 plot 'hour_of_day.dat' using 1:2:(0.5) w boxes fs solid
455 f
= open(path
+ '/day_of_week.plot', 'w')
456 f
.write(GNUPLOT_COMMON
)
459 set output 'day_of_week.png'
464 plot 'day_of_week.dat' using 1:2:(0.5) w boxes fs solid
468 # commits_by_year_month
469 f
= open(path
+ '/commits_by_year_month.plot', 'w')
470 f
.write(GNUPLOT_COMMON
)
472 # TODO rotate xtic labels by 90 degrees
474 set output 'commits_by_year_month.png'
481 plot 'commits_by_year_month.dat' using 1:2:(0.5) w boxes fs solid
486 f
= open(path
+ '/commits_by_year.plot', 'w')
487 f
.write(GNUPLOT_COMMON
)
490 set output 'commits_by_year.png'
494 plot 'commits_by_year.dat' using 1:2:(0.5) w boxes fs solid
499 for i
in ('hour_of_day', 'day_of_week', 'commits_by_year_month', 'commits_by_year'):
500 os
.system('gnuplot %s.plot' % i
)
504 def printHeader(self
, f
):
507 <title>StatGit</title>
508 <link rel="stylesheet" href="statgit.css" type="text/css" />
513 def printNav(self
, f
):
516 <li><a href="index.html">General</a></li>
517 <li><a href="activity.html">Activity</a></li>
518 <li><a href="authors.html">Authors</a></li>
519 <li><a href="files.html">Files</a></li>
520 <li><a href="lines.html">Lines</a></li>
521 <li><a href="tags.html">Tags</a></li>
528 Usage: statgit [options] <gitpath> <outputpath>
534 if len(sys
.argv
) < 3:
538 gitpath
= sys
.argv
[1]
539 outputpath
= os
.path
.abspath(sys
.argv
[2])
541 print 'Git path: %s' % gitpath
542 print 'Output path: %s' % outputpath
546 print 'Collecting data...'
547 data
= GitDataCollector()
548 data
.collect(gitpath
)
550 print 'Generating report...'
551 report
= HTMLReportCreator()
552 report
.create(data
, outputpath
)