2 # Copyright (c) 2007 Heikki Hokkanen <hoxu@users.sf.net>
12 output
= commands
.getoutput(cmd
)
15 def getkeyssortedbyvalues(dict):
16 return map(lambda el
: el
[1], sorted(map(lambda el
: (el
[1], el
[0]), dict.items())))
23 # This should be the main function to extract data from the repository.
24 def collect(self
, dir):
28 # : get a dictionary of author
29 def getAuthorInfo(self
, author
):
32 def getActivityByDayOfWeek(self
):
35 def getActivityByHourOfDay(self
):
39 # Get a list of authors
43 def getFirstCommitDate(self
):
44 return datetime
.datetime
.now()
46 def getLastCommitDate(self
):
47 return datetime
.datetime
.now()
52 def getTotalAuthors(self
):
55 def getTotalCommits(self
):
58 def getTotalFiles(self
):
61 def getTotalLOC(self
):
64 class GitDataCollector(DataCollector
):
65 def collect(self
, dir):
66 DataCollector
.collect(self
, dir)
68 self
.total_authors
= int(getoutput('git-log |git-shortlog -s |wc -l'))
69 self
.total_commits
= int(getoutput('git-rev-list HEAD |wc -l'))
70 self
.total_files
= int(getoutput('git-ls-files |wc -l'))
71 self
.total_lines
= int(getoutput('git-ls-files |xargs cat |wc -l'))
73 self
.activity_by_hour_of_day
= {} # hour -> commits
74 self
.activity_by_day_of_week
= {} # day -> commits
76 self
.authors
= {} # name -> {commits, first_commit_stamp, last_commit_stamp}
79 self
.author_of_month
= {} # month -> author -> commits
80 self
.author_of_year
= {} # year -> author -> commits
81 self
.commits_by_month
= {} # month -> commits
82 self
.first_commit_stamp
= 0
83 self
.last_commit_stamp
= 0
87 lines
= getoutput('git-show-ref --tags').split('\n')
89 (hash, tag
) = line
.split(' ')
90 tag
= tag
.replace('refs/tags/', '')
91 output
= getoutput('git-log "%s" --pretty=format:"%%at %%an" -n 1' % hash)
93 parts
= output
.split(' ')
99 self
.tags
[tag
] = { 'stamp': stamp
, 'hash' : hash, 'date' : datetime
.datetime
.fromtimestamp(stamp
).strftime('%Y-%m-%d') }
102 # TODO also collect statistics for "last 30 days"/"last 12 months"
103 lines
= getoutput('git-rev-list --pretty=format:"%at %an" HEAD |grep -v ^commit').split('\n')
105 # linux-2.6 says "<unknown>" for one line O_o
106 parts
= line
.split(' ')
109 stamp
= int(parts
[0])
113 author
= ' '.join(parts
[1:])
114 date
= datetime
.datetime
.fromtimestamp(float(stamp
))
116 # First and last commit stamp
117 if self
.last_commit_stamp
== 0:
118 self
.last_commit_stamp
= stamp
119 self
.first_commit_stamp
= stamp
124 if hour
in self
.activity_by_hour_of_day
:
125 self
.activity_by_hour_of_day
[hour
] += 1
127 self
.activity_by_hour_of_day
[hour
] = 1
131 if day
in self
.activity_by_day_of_week
:
132 self
.activity_by_day_of_week
[day
] += 1
134 self
.activity_by_day_of_week
[day
] = 1
137 if author
not in self
.authors
:
138 self
.authors
[author
] = {}
140 if 'last_commit_stamp' not in self
.authors
[author
]:
141 self
.authors
[author
]['last_commit_stamp'] = stamp
142 self
.authors
[author
]['first_commit_stamp'] = stamp
143 if 'commits' in self
.authors
[author
]:
144 self
.authors
[author
]['commits'] += 1
146 self
.authors
[author
]['commits'] = 1
148 # author of the month/year
149 yymm
= datetime
.datetime
.fromtimestamp(stamp
).strftime('%Y-%m')
150 if yymm
in self
.author_of_month
:
151 if author
in self
.author_of_month
[yymm
]:
152 self
.author_of_month
[yymm
][author
] += 1
154 self
.author_of_month
[yymm
][author
] = 1
156 self
.author_of_month
[yymm
] = {}
157 self
.author_of_month
[yymm
][author
] = 1
158 if yymm
in self
.commits_by_month
:
159 self
.commits_by_month
[yymm
] += 1
161 self
.commits_by_month
[yymm
] = 1
163 yy
= datetime
.datetime
.fromtimestamp(stamp
).year
164 if yy
in self
.author_of_year
:
165 if author
in self
.author_of_year
[yy
]:
166 self
.author_of_year
[yy
][author
] += 1
168 self
.author_of_year
[yy
][author
] = 1
170 self
.author_of_year
[yy
] = {}
171 self
.author_of_year
[yy
][author
] = 1
173 def getActivityByDayOfWeek(self
):
174 return self
.activity_by_day_of_week
176 def getActivityByHourOfDay(self
):
177 return self
.activity_by_hour_of_day
179 def getAuthorInfo(self
, author
):
180 a
= self
.authors
[author
]
182 commits
= a
['commits']
183 commits_frac
= (100 * float(commits
)) / self
.getTotalCommits()
184 date_first
= datetime
.datetime
.fromtimestamp(a
['first_commit_stamp']).strftime('%Y-%m-%d')
185 date_last
= datetime
.datetime
.fromtimestamp(a
['last_commit_stamp']).strftime('%Y-%m-%d')
187 res
= { 'commits': commits
, 'commits_frac': commits_frac
, 'date_first': date_first
, 'date_last': date_last
}
190 def getAuthors(self
):
191 return self
.authors
.keys()
193 def getFirstCommitDate(self
):
194 return datetime
.datetime
.fromtimestamp(self
.first_commit_stamp
)
196 def getLastCommitDate(self
):
197 return datetime
.datetime
.fromtimestamp(self
.last_commit_stamp
)
200 lines
= getoutput('git-show-ref --tags |cut -d/ -f3')
201 return lines
.split('\n')
203 def getTagDate(self
, tag
):
204 return self
.revToDate('tags/' + tag
)
206 def getTotalAuthors(self
):
207 return self
.total_authors
209 def getTotalCommits(self
):
210 return self
.total_commits
212 def getTotalFiles(self
):
213 return self
.total_files
215 def getTotalLOC(self
):
216 return self
.total_lines
218 def revToDate(self
, rev
):
219 stamp
= int(getoutput('git-log --pretty=format:%%at "%s" -n 1' % rev
))
220 return datetime
.datetime
.fromtimestamp(stamp
).strftime('%Y-%m-%d')
226 def create(self
, data
, path
):
230 class HTMLReportCreator(ReportCreator
):
231 def create(self
, data
, path
):
232 ReportCreator
.create(self
, data
, path
)
234 f
= open(path
+ "/index.html", 'w')
235 format
= '%Y-%m-%d %H:%m:%S'
238 f
.write('<h1>StatGit</h1>')
243 f
.write('<dt>Generated</dt><dd>%s</dd>' % datetime
.datetime
.now().strftime(format
));
244 f
.write('<dt>Report Period</dt><dd>%s to %s</dd>' % (data
.getFirstCommitDate().strftime(format
), data
.getLastCommitDate().strftime(format
)))
245 f
.write('<dt>Total Files</dt><dd>%s</dd>' % data
.getTotalFiles())
246 f
.write('<dt>Total Lines of Code</dt><dd>%s</dd>' % data
.getTotalLOC())
247 f
.write('<dt>Total Commits</dt><dd>%s</dd>' % data
.getTotalCommits())
248 f
.write('<dt>Authors</dt><dd>%s</dd>' % data
.getTotalAuthors())
251 f
.write('</body>\n</html>');
256 f
= open(path
+ '/activity.html', 'w')
258 f
.write('<h1>Activity</h1>')
261 f
.write('<h2>Last 30 days</h2>')
263 f
.write('<h2>Last 12 months</h2>')
266 f
.write('\n<h2>Hour of Day</h2>\n\n')
267 hour_of_day
= data
.getActivityByHourOfDay()
268 f
.write('<table><tr><th>Hour</th>')
269 for i
in range(1, 25):
270 f
.write('<th>%d</th>' % i
)
271 f
.write('</tr>\n<tr><th>Commits</th>')
272 fp
= open(path
+ '/hour_of_day.dat', 'w')
273 for i
in range(0, 24):
275 f
.write('<td>%d</td>' % hour_of_day
[i
])
276 fp
.write('%d %d\n' % (i
, hour_of_day
[i
]))
278 f
.write('<td>0</td>')
279 fp
.write('%d 0\n' % i
)
281 f
.write('</tr>\n<tr><th>%</th>')
282 totalcommits
= data
.getTotalCommits()
283 for i
in range(0, 24):
285 f
.write('<td>%.2f</td>' % ((100.0 * hour_of_day
[i
]) / totalcommits
))
287 f
.write('<td>0.00</td>')
288 f
.write('</tr></table>')
291 # TODO show also by hour of weekday?
292 f
.write('\n<h2>Day of Week</h2>\n\n')
293 day_of_week
= data
.getActivityByDayOfWeek()
295 f
.write('<tr><th>Day</th><th>Total (%)</th></tr>')
296 fp
= open(path
+ '/day_of_week.dat', 'w')
297 for d
in range(0, 7):
298 fp
.write('%d %d\n' % (d
+ 1, day_of_week
[d
]))
300 f
.write('<th>%d</th>' % (d
+ 1))
302 f
.write('<td>%d (%.2f%%)</td>' % (day_of_week
[d
], (100.0 * day_of_week
[d
]) / totalcommits
))
304 f
.write('<td>0</td>')
312 f
= open(path
+ '/authors.html', 'w')
315 f
.write('<h1>Authors</h1>')
318 f
.write('\n<h2>List of authors</h2>\n\n')
320 f
.write('<table class="authors">')
321 f
.write('<tr><th>Author</th><th>Commits (%)</th><th>First commit</th><th>Last commit</th></tr>')
322 for author
in data
.getAuthors():
323 info
= data
.getAuthorInfo(author
)
324 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']))
327 f
.write('\n<h2>Author of Month</h2>\n\n')
329 f
.write('<tr><th>Month</th><th>Author</th><th>Commits (%)</th></tr>')
330 for yymm
in reversed(sorted(data
.author_of_month
.keys())):
331 authordict
= data
.author_of_month
[yymm
]
332 authors
= getkeyssortedbyvalues(authordict
)
334 commits
= data
.author_of_month
[yymm
][authors
[0]]
335 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
]))
339 f
.write('\n<h2>Author of Year</h2>\n\n')
340 f
.write('<table><tr><th>Year</th><th>Author</th><th>Commits (%)</th></tr>')
341 for yy
in reversed(sorted(data
.author_of_year
.keys())):
342 authordict
= data
.author_of_year
[yy
]
343 authors
= getkeyssortedbyvalues(authordict
)
345 commits
= data
.author_of_year
[yy
][authors
[0]]
346 f
.write('<tr><td>%s</td><td>%s</td><td>%d</td></tr>' % (yy
, authors
[0], commits
))
349 f
.write('</body></html>')
354 f
= open(path
+ '/tags.html', 'w')
356 f
.write('<h1>Tags</h1>')
360 f
.write('<tr><th>Name</th><th>Date</th></tr>')
361 for tag
in data
.tags
.keys():
362 f
.write('<tr><td>%s</td><td>%s</td></tr>' % (tag
, data
.tags
[tag
]['date']))
365 f
.write('</body></html>')
369 def printHeader(self
, f
):
372 <title>StatGit</title>
373 <link rel="stylesheet" href="statgit.css" type="text/css" />
378 def printNav(self
, f
):
381 <li><a href="index.html">General</a></li>
382 <li><a href="activity.html">Activity</a></li>
383 <li><a href="authors.html">Authors</a></li>
384 <li><a href="files.html">Files</a></li>
385 <li><a href="lines.html">Lines</a></li>
386 <li><a href="tags.html">Tags</a></li>
393 Usage: statgit [options] <gitpath> <outputpath>
399 if len(sys
.argv
) < 3:
403 gitpath
= sys
.argv
[1]
404 outputpath
= sys
.argv
[2]
406 print 'Git path: %s' % gitpath
407 print 'Output path: %s' % outputpath
411 print 'Collecting data...'
412 data
= GitDataCollector()
413 data
.collect(gitpath
)
415 print 'Generating report...'
416 report
= HTMLReportCreator()
417 report
.create(data
, outputpath
)