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
.commits_by_month
= {} # month -> commits
99 lines
= getoutput('git-rev-list --pretty=format:"%at %an" HEAD |grep -v ^commit').split('\n')
101 parts
= line
.split(' ')
102 stamp
= int(parts
[0])
103 author
= ' '.join(parts
[1:])
105 yymm
= datetime
.datetime
.fromtimestamp(stamp
).strftime('%Y-%m')
106 if yymm
in self
.author_of_month
and author
in self
.author_of_month
[yymm
]:
107 if author
in self
.author_of_month
[yymm
]:
108 self
.author_of_month
[yymm
][author
] += 1
110 self
.author_of_month
[yymm
][author
] = 1
112 self
.author_of_month
[yymm
] = {}
113 self
.author_of_month
[yymm
][author
] = 1
114 if yymm
in self
.commits_by_month
:
115 self
.commits_by_month
[yymm
] += 1
117 self
.commits_by_month
[yymm
] = 1
119 print self
.author_of_month
121 def getActivityByDayOfWeek(self
):
122 return self
.activity_by_day_of_week
124 def getActivityByHourOfDay(self
):
125 return self
.activity_by_hour_of_day
127 def getAuthorInfo(self
, author
):
128 commits
= int(getoutput('git-rev-list HEAD --author="%s" |wc -l' % author
))
129 commits_frac
= (100 * float(commits
)) / self
.getTotalCommits()
130 date_first
= '0000-00-00'
131 date_last
= '0000-00-00'
132 rev_last
= getoutput('git-rev-list --all --author="%s" -n 1' % author
)
133 rev_first
= getoutput('git-rev-list --all --author="%s" |tail -n 1' % author
)
134 date_first
= self
.revToDate(rev_first
)
135 date_last
= self
.revToDate(rev_last
)
137 res
= { 'commits': commits
, 'commits_frac': commits_frac
, 'date_first': date_first
, 'date_last': date_last
}
140 def getAuthors(self
):
141 lines
= getoutput('git-rev-list --all --pretty=format:%an |grep -v ^commit |sort |uniq')
142 return lines
.split('\n')
145 lines
= getoutput('git-show-ref --tags |cut -d/ -f3')
146 return lines
.split('\n')
148 def getTagDate(self
, tag
):
149 return self
.revToDate('tags/' + tag
)
151 def getTotalAuthors(self
):
152 return self
.total_authors
154 def getTotalCommits(self
):
155 return self
.total_commits
157 def getTotalFiles(self
):
158 return self
.total_files
160 def getTotalLOC(self
):
161 return self
.total_lines
163 def revToDate(self
, rev
):
164 stamp
= int(getoutput('git-log --pretty=format:%%at "%s" -n 1' % rev
))
165 return datetime
.datetime
.fromtimestamp(stamp
).strftime('%Y-%m-%d')
171 def create(self
, data
, path
):
175 class HTMLReportCreator(ReportCreator
):
176 def create(self
, data
, path
):
177 ReportCreator
.create(self
, data
, path
)
179 f
= open(path
+ "/index.html", 'w')
180 format
= '%Y-%m-%d %H:%m:%S'
183 f
.write('<h1>StatGit</h1>')
186 f
.write('<dt>Generated</dt><dd>%s</dd>' % datetime
.datetime
.now().strftime(format
));
187 f
.write('<dt>Report Period</dt><dd>%s to %s</dd>' % (data
.getFirstCommitDate().strftime(format
), data
.getLastCommitDate().strftime(format
)))
188 f
.write('<dt>Total Files</dt><dd>%s</dd>' % data
.getTotalFiles())
189 f
.write('<dt>Total Lines of Code</dt><dd>%s</dd>' % data
.getTotalLOC())
190 f
.write('<dt>Total Commits</dt><dd>%s</dd>' % data
.getTotalCommits())
191 f
.write('<dt>Authors</dt><dd>%s</dd>' % data
.getTotalAuthors())
195 <li><a href="activity.html">Activity</a></li>
196 <li><a href="authors.html">Authors</a></li>
197 <li><a href="files.html">Files</a></li>
198 <li><a href="lines.html">Lines</a></li>
202 f
.write('<h2>Tags</h2>')
204 f
.write('<tr><th>Name</th><th>Date</th><th>Developers</th></tr>')
205 for tag
in data
.getTags():
206 f
.write('<tr><td>%s</td><td></td></tr>' % tag
)
209 f
.write('</body>\n</html>');
213 f
= open(path
+ '/activity.html', 'w')
215 f
.write('<h1>Activity</h1>')
217 f
.write('<h2>Last 30 days</h2>')
219 f
.write('<h2>Last 12 months</h2>')
221 f
.write('\n<h2>Hour of Day</h2>\n\n')
222 hour_of_day
= data
.getActivityByHourOfDay()
223 f
.write('<table><tr><th>Hour</th>')
224 for i
in range(1, 25):
225 f
.write('<th>%d</th>' % i
)
226 f
.write('</tr>\n<tr><th>Commits</th>')
227 for i
in range(0, 24):
229 f
.write('<td>%d</td>' % hour_of_day
[i
])
231 f
.write('<td>0</td>')
232 f
.write('</tr>\n<tr><th>%</th>')
233 totalcommits
= data
.getTotalCommits()
234 for i
in range(0, 24):
236 f
.write('<td>%.2f</td>' % ((100.0 * hour_of_day
[i
]) / totalcommits
))
238 f
.write('<td>0.00</td>')
239 f
.write('</tr></table>')
242 # TODO show also by hour of weekday?
243 f
.write('\n<h2>Day of Week</h2>\n\n')
244 day_of_week
= data
.getActivityByDayOfWeek()
246 f
.write('<tr><th>Day</th><th>Total (%)</th></tr>')
247 for d
in range(0, 7):
249 f
.write('<th>%d</th>' % (d
+ 1))
251 f
.write('<td>%d (%.2f%%)</td>' % (day_of_week
[d
], (100.0 * day_of_week
[d
]) / totalcommits
))
253 f
.write('<td>0</td>')
260 f
= open(path
+ '/authors.html', 'w')
263 f
.write('<h1>Authors</h1>')
265 f
.write('\n<h2>List of authors</h2>\n\n')
267 f
.write('<table class="authors">')
268 f
.write('<tr><th>Author</th><th>Commits (%)</th><th>First commit</th><th>Last commit</th></tr>')
269 for author
in data
.getAuthors():
270 info
= data
.getAuthorInfo(author
)
271 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']))
274 f
.write('\n<h2>Author of Month</h2>\n\n')
276 f
.write('<tr><th>Month</th><th>Author</th><th>Commits (%)</th></tr>')
277 for yymm
in reversed(sorted(data
.author_of_month
.keys())):
278 authordict
= data
.author_of_month
[yymm
]
279 authors
= getkeyssortedbyvalues(authordict
)
281 commits
= data
.author_of_month
[yymm
][authors
[0]]
282 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
]))
286 f
.write('\n<h2>Author of Year</h2>\n\n')
289 f
.write('</body></html>')
293 def printHeader(self
, f
):
296 <title>StatGit</title>
297 <link rel="stylesheet" href="statgit.css" type="text/css" />
304 Usage: statgit [options] <gitpath> <outputpath>
310 if len(sys
.argv
) < 3:
314 gitpath
= sys
.argv
[1]
315 outputpath
= sys
.argv
[2]
317 print 'Git path: %s' % gitpath
318 print 'Output path: %s' % outputpath
322 print 'Collecting data...'
323 data
= GitDataCollector()
324 data
.collect(gitpath
)
326 print 'Generating report...'
327 report
= HTMLReportCreator()
328 report
.create(data
, outputpath
)