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
77 lines
= getoutput('git-rev-list HEAD --pretty=format:%at |grep -v ^commit').split('\n')
79 date
= datetime
.datetime
.fromtimestamp(float(stamp
))
83 if hour
in self
.activity_by_hour_of_day
:
84 self
.activity_by_hour_of_day
[hour
] += 1
86 self
.activity_by_hour_of_day
[hour
] = 1
90 if day
in self
.activity_by_day_of_week
:
91 self
.activity_by_day_of_week
[day
] += 1
93 self
.activity_by_day_of_week
[day
] = 1
95 # TODO author of the month
96 self
.author_of_month
= {} # month -> author -> commits
97 self
.author_of_year
= {} # year -> author -> commits
98 self
.commits_by_month
= {} # month -> commits
100 lines
= getoutput('git-rev-list --pretty=format:"%at %an" HEAD |grep -v ^commit').split('\n')
102 parts
= line
.split(' ')
103 stamp
= int(parts
[0])
104 author
= ' '.join(parts
[1:])
106 yymm
= datetime
.datetime
.fromtimestamp(stamp
).strftime('%Y-%m')
107 if yymm
in self
.author_of_month
:
108 if author
in self
.author_of_month
[yymm
]:
109 self
.author_of_month
[yymm
][author
] += 1
111 self
.author_of_month
[yymm
][author
] = 1
113 self
.author_of_month
[yymm
] = {}
114 self
.author_of_month
[yymm
][author
] = 1
115 if yymm
in self
.commits_by_month
:
116 self
.commits_by_month
[yymm
] += 1
118 self
.commits_by_month
[yymm
] = 1
120 yy
= datetime
.datetime
.fromtimestamp(stamp
).year
121 if yy
in self
.author_of_year
:
122 if author
in self
.author_of_year
[yy
]:
123 self
.author_of_year
[yy
][author
] += 1
125 self
.author_of_year
[yy
][author
] = 1
127 self
.author_of_year
[yy
] = {}
128 self
.author_of_year
[yy
][author
] = 1
130 print self
.author_of_month
132 def getActivityByDayOfWeek(self
):
133 return self
.activity_by_day_of_week
135 def getActivityByHourOfDay(self
):
136 return self
.activity_by_hour_of_day
138 def getAuthorInfo(self
, author
):
139 commits
= int(getoutput('git-rev-list HEAD --author="%s" |wc -l' % author
))
140 commits_frac
= (100 * float(commits
)) / self
.getTotalCommits()
141 date_first
= '0000-00-00'
142 date_last
= '0000-00-00'
143 rev_last
= getoutput('git-rev-list --all --author="%s" -n 1' % author
)
144 rev_first
= getoutput('git-rev-list --all --author="%s" |tail -n 1' % author
)
145 date_first
= self
.revToDate(rev_first
)
146 date_last
= self
.revToDate(rev_last
)
148 res
= { 'commits': commits
, 'commits_frac': commits_frac
, 'date_first': date_first
, 'date_last': date_last
}
151 def getAuthors(self
):
152 lines
= getoutput('git-rev-list --all --pretty=format:%an |grep -v ^commit |sort |uniq')
153 return lines
.split('\n')
156 lines
= getoutput('git-show-ref --tags |cut -d/ -f3')
157 return lines
.split('\n')
159 def getTagDate(self
, tag
):
160 return self
.revToDate('tags/' + tag
)
162 def getTotalAuthors(self
):
163 return self
.total_authors
165 def getTotalCommits(self
):
166 return self
.total_commits
168 def getTotalFiles(self
):
169 return self
.total_files
171 def getTotalLOC(self
):
172 return self
.total_lines
174 def revToDate(self
, rev
):
175 stamp
= int(getoutput('git-log --pretty=format:%%at "%s" -n 1' % rev
))
176 return datetime
.datetime
.fromtimestamp(stamp
).strftime('%Y-%m-%d')
182 def create(self
, data
, path
):
186 class HTMLReportCreator(ReportCreator
):
187 def create(self
, data
, path
):
188 ReportCreator
.create(self
, data
, path
)
190 f
= open(path
+ "/index.html", 'w')
191 format
= '%Y-%m-%d %H:%m:%S'
194 f
.write('<h1>StatGit</h1>')
197 f
.write('<dt>Generated</dt><dd>%s</dd>' % datetime
.datetime
.now().strftime(format
));
198 f
.write('<dt>Report Period</dt><dd>%s to %s</dd>' % (data
.getFirstCommitDate().strftime(format
), data
.getLastCommitDate().strftime(format
)))
199 f
.write('<dt>Total Files</dt><dd>%s</dd>' % data
.getTotalFiles())
200 f
.write('<dt>Total Lines of Code</dt><dd>%s</dd>' % data
.getTotalLOC())
201 f
.write('<dt>Total Commits</dt><dd>%s</dd>' % data
.getTotalCommits())
202 f
.write('<dt>Authors</dt><dd>%s</dd>' % data
.getTotalAuthors())
206 <li><a href="activity.html">Activity</a></li>
207 <li><a href="authors.html">Authors</a></li>
208 <li><a href="files.html">Files</a></li>
209 <li><a href="lines.html">Lines</a></li>
213 f
.write('<h2>Tags</h2>')
215 f
.write('<tr><th>Name</th><th>Date</th><th>Developers</th></tr>')
216 for tag
in data
.getTags():
217 f
.write('<tr><td>%s</td><td></td></tr>' % tag
)
220 f
.write('</body>\n</html>');
224 f
= open(path
+ '/activity.html', 'w')
226 f
.write('<h1>Activity</h1>')
228 f
.write('<h2>Last 30 days</h2>')
230 f
.write('<h2>Last 12 months</h2>')
232 f
.write('\n<h2>Hour of Day</h2>\n\n')
233 hour_of_day
= data
.getActivityByHourOfDay()
234 f
.write('<table><tr><th>Hour</th>')
235 for i
in range(1, 25):
236 f
.write('<th>%d</th>' % i
)
237 f
.write('</tr>\n<tr><th>Commits</th>')
238 for i
in range(0, 24):
240 f
.write('<td>%d</td>' % hour_of_day
[i
])
242 f
.write('<td>0</td>')
243 f
.write('</tr>\n<tr><th>%</th>')
244 totalcommits
= data
.getTotalCommits()
245 for i
in range(0, 24):
247 f
.write('<td>%.2f</td>' % ((100.0 * hour_of_day
[i
]) / totalcommits
))
249 f
.write('<td>0.00</td>')
250 f
.write('</tr></table>')
253 # TODO show also by hour of weekday?
254 f
.write('\n<h2>Day of Week</h2>\n\n')
255 day_of_week
= data
.getActivityByDayOfWeek()
257 f
.write('<tr><th>Day</th><th>Total (%)</th></tr>')
258 for d
in range(0, 7):
260 f
.write('<th>%d</th>' % (d
+ 1))
262 f
.write('<td>%d (%.2f%%)</td>' % (day_of_week
[d
], (100.0 * day_of_week
[d
]) / totalcommits
))
264 f
.write('<td>0</td>')
271 f
= open(path
+ '/authors.html', 'w')
274 f
.write('<h1>Authors</h1>')
276 f
.write('\n<h2>List of authors</h2>\n\n')
278 f
.write('<table class="authors">')
279 f
.write('<tr><th>Author</th><th>Commits (%)</th><th>First commit</th><th>Last commit</th></tr>')
280 for author
in data
.getAuthors():
281 info
= data
.getAuthorInfo(author
)
282 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']))
285 f
.write('\n<h2>Author of Month</h2>\n\n')
287 f
.write('<tr><th>Month</th><th>Author</th><th>Commits (%)</th></tr>')
288 for yymm
in reversed(sorted(data
.author_of_month
.keys())):
289 authordict
= data
.author_of_month
[yymm
]
290 authors
= getkeyssortedbyvalues(authordict
)
292 commits
= data
.author_of_month
[yymm
][authors
[0]]
293 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
]))
297 f
.write('\n<h2>Author of Year</h2>\n\n')
298 f
.write('<table><tr><th>Year</th><th>Author</th><th>Commits (%)</th></tr>')
299 for yy
in reversed(sorted(data
.author_of_year
.keys())):
300 authordict
= data
.author_of_year
[yy
]
301 authors
= getkeyssortedbyvalues(authordict
)
303 commits
= data
.author_of_year
[yy
][authors
[0]]
304 f
.write('<tr><td>%s</td><td>%s</td><td>%d</td></tr>' % (yy
, authors
[0], commits
))
307 f
.write('</body></html>')
311 def printHeader(self
, f
):
314 <title>StatGit</title>
315 <link rel="stylesheet" href="statgit.css" type="text/css" />
322 Usage: statgit [options] <gitpath> <outputpath>
328 if len(sys
.argv
) < 3:
332 gitpath
= sys
.argv
[1]
333 outputpath
= sys
.argv
[2]
335 print 'Git path: %s' % gitpath
336 print 'Output path: %s' % outputpath
340 print 'Collecting data...'
341 data
= GitDataCollector()
342 data
.collect(gitpath
)
344 print 'Generating report...'
345 report
= HTMLReportCreator()
346 report
.create(data
, outputpath
)