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
85 # TODO also collect statistics for "last 30 days"/"last 12 months"
86 lines
= getoutput('git-rev-list --pretty=format:"%at %an" HEAD |grep -v ^commit').split('\n')
88 # linux-2.6 says "<unknown>" for one line O_o
89 parts
= line
.split(' ')
96 author
= ' '.join(parts
[1:])
97 date
= datetime
.datetime
.fromtimestamp(float(stamp
))
99 # First and last commit stamp
100 if self
.last_commit_stamp
== 0:
101 self
.last_commit_stamp
= stamp
102 self
.first_commit_stamp
= stamp
107 if hour
in self
.activity_by_hour_of_day
:
108 self
.activity_by_hour_of_day
[hour
] += 1
110 self
.activity_by_hour_of_day
[hour
] = 1
114 if day
in self
.activity_by_day_of_week
:
115 self
.activity_by_day_of_week
[day
] += 1
117 self
.activity_by_day_of_week
[day
] = 1
120 if author
not in self
.authors
:
121 self
.authors
[author
] = {}
123 if 'last_commit_stamp' not in self
.authors
[author
]:
124 self
.authors
[author
]['last_commit_stamp'] = stamp
125 self
.authors
[author
]['first_commit_stamp'] = stamp
126 if 'commits' in self
.authors
[author
]:
127 self
.authors
[author
]['commits'] += 1
129 self
.authors
[author
]['commits'] = 1
131 # author of the month/year
132 yymm
= datetime
.datetime
.fromtimestamp(stamp
).strftime('%Y-%m')
133 if yymm
in self
.author_of_month
:
134 if author
in self
.author_of_month
[yymm
]:
135 self
.author_of_month
[yymm
][author
] += 1
137 self
.author_of_month
[yymm
][author
] = 1
139 self
.author_of_month
[yymm
] = {}
140 self
.author_of_month
[yymm
][author
] = 1
141 if yymm
in self
.commits_by_month
:
142 self
.commits_by_month
[yymm
] += 1
144 self
.commits_by_month
[yymm
] = 1
146 yy
= datetime
.datetime
.fromtimestamp(stamp
).year
147 if yy
in self
.author_of_year
:
148 if author
in self
.author_of_year
[yy
]:
149 self
.author_of_year
[yy
][author
] += 1
151 self
.author_of_year
[yy
][author
] = 1
153 self
.author_of_year
[yy
] = {}
154 self
.author_of_year
[yy
][author
] = 1
156 def getActivityByDayOfWeek(self
):
157 return self
.activity_by_day_of_week
159 def getActivityByHourOfDay(self
):
160 return self
.activity_by_hour_of_day
162 def getAuthorInfo(self
, author
):
163 a
= self
.authors
[author
]
165 commits
= a
['commits']
166 commits_frac
= (100 * float(commits
)) / self
.getTotalCommits()
167 date_first
= datetime
.datetime
.fromtimestamp(a
['first_commit_stamp']).strftime('%Y-%m-%d')
168 date_last
= datetime
.datetime
.fromtimestamp(a
['last_commit_stamp']).strftime('%Y-%m-%d')
170 res
= { 'commits': commits
, 'commits_frac': commits_frac
, 'date_first': date_first
, 'date_last': date_last
}
173 def getAuthors(self
):
174 return self
.authors
.keys()
176 def getFirstCommitDate(self
):
177 return datetime
.datetime
.fromtimestamp(self
.first_commit_stamp
)
179 def getLastCommitDate(self
):
180 return datetime
.datetime
.fromtimestamp(self
.last_commit_stamp
)
183 lines
= getoutput('git-show-ref --tags |cut -d/ -f3')
184 return lines
.split('\n')
186 def getTagDate(self
, tag
):
187 return self
.revToDate('tags/' + tag
)
189 def getTotalAuthors(self
):
190 return self
.total_authors
192 def getTotalCommits(self
):
193 return self
.total_commits
195 def getTotalFiles(self
):
196 return self
.total_files
198 def getTotalLOC(self
):
199 return self
.total_lines
201 def revToDate(self
, rev
):
202 stamp
= int(getoutput('git-log --pretty=format:%%at "%s" -n 1' % rev
))
203 return datetime
.datetime
.fromtimestamp(stamp
).strftime('%Y-%m-%d')
209 def create(self
, data
, path
):
213 class HTMLReportCreator(ReportCreator
):
214 def create(self
, data
, path
):
215 ReportCreator
.create(self
, data
, path
)
217 f
= open(path
+ "/index.html", 'w')
218 format
= '%Y-%m-%d %H:%m:%S'
221 f
.write('<h1>StatGit</h1>')
226 f
.write('<dt>Generated</dt><dd>%s</dd>' % datetime
.datetime
.now().strftime(format
));
227 f
.write('<dt>Report Period</dt><dd>%s to %s</dd>' % (data
.getFirstCommitDate().strftime(format
), data
.getLastCommitDate().strftime(format
)))
228 f
.write('<dt>Total Files</dt><dd>%s</dd>' % data
.getTotalFiles())
229 f
.write('<dt>Total Lines of Code</dt><dd>%s</dd>' % data
.getTotalLOC())
230 f
.write('<dt>Total Commits</dt><dd>%s</dd>' % data
.getTotalCommits())
231 f
.write('<dt>Authors</dt><dd>%s</dd>' % data
.getTotalAuthors())
235 #f.write('<h2>Tags</h2>')
237 #f.write('<tr><th>Name</th><th>Date</th><th>Developers</th></tr>')
238 #for tag in data.getTags():
239 # f.write('<tr><td>%s</td><td></td></tr>' % tag)
242 f
.write('</body>\n</html>');
246 f
= open(path
+ '/activity.html', 'w')
248 f
.write('<h1>Activity</h1>')
251 f
.write('<h2>Last 30 days</h2>')
253 f
.write('<h2>Last 12 months</h2>')
255 f
.write('\n<h2>Hour of Day</h2>\n\n')
256 hour_of_day
= data
.getActivityByHourOfDay()
257 f
.write('<table><tr><th>Hour</th>')
258 for i
in range(1, 25):
259 f
.write('<th>%d</th>' % i
)
260 f
.write('</tr>\n<tr><th>Commits</th>')
261 for i
in range(0, 24):
263 f
.write('<td>%d</td>' % hour_of_day
[i
])
265 f
.write('<td>0</td>')
266 f
.write('</tr>\n<tr><th>%</th>')
267 totalcommits
= data
.getTotalCommits()
268 for i
in range(0, 24):
270 f
.write('<td>%.2f</td>' % ((100.0 * hour_of_day
[i
]) / totalcommits
))
272 f
.write('<td>0.00</td>')
273 f
.write('</tr></table>')
276 # TODO show also by hour of weekday?
277 f
.write('\n<h2>Day of Week</h2>\n\n')
278 day_of_week
= data
.getActivityByDayOfWeek()
280 f
.write('<tr><th>Day</th><th>Total (%)</th></tr>')
281 for d
in range(0, 7):
283 f
.write('<th>%d</th>' % (d
+ 1))
285 f
.write('<td>%d (%.2f%%)</td>' % (day_of_week
[d
], (100.0 * day_of_week
[d
]) / totalcommits
))
287 f
.write('<td>0</td>')
294 f
= open(path
+ '/authors.html', 'w')
297 f
.write('<h1>Authors</h1>')
300 f
.write('\n<h2>List of authors</h2>\n\n')
302 f
.write('<table class="authors">')
303 f
.write('<tr><th>Author</th><th>Commits (%)</th><th>First commit</th><th>Last commit</th></tr>')
304 for author
in data
.getAuthors():
305 info
= data
.getAuthorInfo(author
)
306 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']))
309 f
.write('\n<h2>Author of Month</h2>\n\n')
311 f
.write('<tr><th>Month</th><th>Author</th><th>Commits (%)</th></tr>')
312 for yymm
in reversed(sorted(data
.author_of_month
.keys())):
313 authordict
= data
.author_of_month
[yymm
]
314 authors
= getkeyssortedbyvalues(authordict
)
316 commits
= data
.author_of_month
[yymm
][authors
[0]]
317 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
]))
321 f
.write('\n<h2>Author of Year</h2>\n\n')
322 f
.write('<table><tr><th>Year</th><th>Author</th><th>Commits (%)</th></tr>')
323 for yy
in reversed(sorted(data
.author_of_year
.keys())):
324 authordict
= data
.author_of_year
[yy
]
325 authors
= getkeyssortedbyvalues(authordict
)
327 commits
= data
.author_of_year
[yy
][authors
[0]]
328 f
.write('<tr><td>%s</td><td>%s</td><td>%d</td></tr>' % (yy
, authors
[0], commits
))
331 f
.write('</body></html>')
335 def printHeader(self
, f
):
338 <title>StatGit</title>
339 <link rel="stylesheet" href="statgit.css" type="text/css" />
344 def printNav(self
, f
):
347 <li><a href="index.html">General</a></li>
348 <li><a href="activity.html">Activity</a></li>
349 <li><a href="authors.html">Authors</a></li>
350 <li><a href="files.html">Files</a></li>
351 <li><a href="lines.html">Lines</a></li>
358 Usage: statgit [options] <gitpath> <outputpath>
364 if len(sys
.argv
) < 3:
368 gitpath
= sys
.argv
[1]
369 outputpath
= sys
.argv
[2]
371 print 'Git path: %s' % gitpath
372 print 'Output path: %s' % outputpath
376 print 'Collecting data...'
377 data
= GitDataCollector()
378 data
.collect(gitpath
)
380 print 'Generating report...'
381 report
= HTMLReportCreator()
382 report
.create(data
, outputpath
)