2 # Copyright (c) 2007-2009 Heikki Hokkanen <hoxu@users.sf.net> & others (see doc/author.txt)
15 GNUPLOT_COMMON
= 'set terminal png transparent\nset size 0.5,0.5\n'
16 MAX_EXT_LENGTH
= 10 # maximum file extension length
18 exectime_internal
= 0.0
19 exectime_external
= 0.0
20 time_start
= time
.time()
22 # By default, gnuplot is searched from path, but can be overridden with the
23 # environment variable "GNUPLOT"
24 gnuplot_cmd
= 'gnuplot'
25 if 'GNUPLOT' in os
.environ
:
26 gnuplot_cmd
= os
.environ
['GNUPLOT']
28 def getpipeoutput(cmds
, quiet
= False):
29 global exectime_external
32 print '>> ' + ' | '.join(cmds
),
34 p0
= subprocess
.Popen(cmds
[0], stdout
= subprocess
.PIPE
, shell
= True)
37 p
= subprocess
.Popen(x
, stdin
= p0
.stdout
, stdout
= subprocess
.PIPE
, shell
= True)
39 output
= p
.communicate()[0]
42 print '\r[%.5f] >> %s' % (end
- start
, ' | '.join(cmds
))
43 exectime_external
+= (end
- start
)
44 return output
.rstrip('\n')
46 def getkeyssortedbyvalues(dict):
47 return map(lambda el
: el
[1], sorted(map(lambda el
: (el
[1], el
[0]), dict.items())))
49 # dict['author'] = { 'commits': 512 } - ...key(dict, 'commits')
50 def getkeyssortedbyvaluekey(d
, key
):
51 return map(lambda el
: el
[1], sorted(map(lambda el
: (d
[el
][key
], el
), d
.keys())))
57 VERSION
= getpipeoutput(["git rev-parse --short HEAD"]).split('\n')[0]
61 """Manages data collection from a revision control repository."""
63 self
.stamp_created
= time
.time()
67 # This should be the main function to extract data from the repository.
68 def collect(self
, dir):
70 self
.projectname
= os
.path
.basename(os
.path
.abspath(dir))
74 def loadCache(self
, cachefile
):
75 if not os
.path
.exists(cachefile
):
77 print 'Loading cache...'
80 self
.cache
= pickle
.loads(zlib
.decompress(f
.read()))
82 # temporary hack to upgrade non-compressed caches
84 self
.cache
= pickle
.load(f
)
88 # Produce any additional statistics from the extracted data.
93 # : get a dictionary of author
94 def getAuthorInfo(self
, author
):
97 def getActivityByDayOfWeek(self
):
100 def getActivityByHourOfDay(self
):
104 # Get a list of authors
105 def getAuthors(self
):
108 def getFirstCommitDate(self
):
109 return datetime
.datetime
.now()
111 def getLastCommitDate(self
):
112 return datetime
.datetime
.now()
114 def getStampCreated(self
):
115 return self
.stamp_created
120 def getTotalAuthors(self
):
123 def getTotalCommits(self
):
126 def getTotalFiles(self
):
129 def getTotalLOC(self
):
133 # Save cacheable data
134 def saveCache(self
, filename
):
135 print 'Saving cache...'
136 f
= open(cachefile
, 'w')
137 #pickle.dump(self.cache, f)
138 data
= zlib
.compress(pickle
.dumps(self
.cache
))
142 class GitDataCollector(DataCollector
):
143 def collect(self
, dir):
144 DataCollector
.collect(self
, dir)
147 self
.total_authors
= int(getpipeoutput(['git log', 'git shortlog -s', 'wc -l']))
149 self
.total_authors
= 0
150 #self.total_lines = int(getoutput('git-ls-files -z |xargs -0 cat |wc -l'))
152 self
.activity_by_hour_of_day
= {} # hour -> commits
153 self
.activity_by_day_of_week
= {} # day -> commits
154 self
.activity_by_month_of_year
= {} # month [1-12] -> commits
155 self
.activity_by_hour_of_week
= {} # weekday -> hour -> commits
156 self
.activity_by_hour_of_day_busiest
= 0
157 self
.activity_by_hour_of_week_busiest
= 0
159 self
.authors
= {} # name -> {commits, first_commit_stamp, last_commit_stamp}
161 # author of the month
162 self
.author_of_month
= {} # month -> author -> commits
163 self
.author_of_year
= {} # year -> author -> commits
164 self
.commits_by_month
= {} # month -> commits
165 self
.commits_by_year
= {} # year -> commits
166 self
.first_commit_stamp
= 0
167 self
.last_commit_stamp
= 0
171 lines
= getpipeoutput(['git show-ref --tags']).split('\n')
175 (hash, tag
) = line
.split(' ')
177 tag
= tag
.replace('refs/tags/', '')
178 output
= getpipeoutput(['git log "%s" --pretty=format:"%%at %%an" -n 1' % hash])
180 parts
= output
.split(' ')
183 stamp
= int(parts
[0])
186 self
.tags
[tag
] = { 'stamp': stamp
, 'hash' : hash, 'date' : datetime
.datetime
.fromtimestamp(stamp
).strftime('%Y-%m-%d'), 'commits': 0, 'authors': {} }
188 # collect info on tags, starting from latest
189 tags_sorted_by_date_desc
= map(lambda el
: el
[1], reversed(sorted(map(lambda el
: (el
[1]['date'], el
[0]), data
.tags
.items()))))
191 for tag
in reversed(tags_sorted_by_date_desc
):
192 cmd
= 'git shortlog -s "%s"' % tag
194 cmd
+= ' "^%s"' % prev
195 output
= getpipeoutput([cmd
])
199 for line
in output
.split('\n'):
200 parts
= re
.split('\s+', line
, 2)
201 commits
= int(parts
[1])
203 self
.tags
[tag
]['commits'] += commits
204 self
.tags
[tag
]['authors'][author
] = commits
206 # Collect revision statistics
207 # Outputs "<stamp> <author>"
208 lines
= getpipeoutput(['git rev-list --pretty=format:"%at %an" HEAD', 'grep -v ^commit']).split('\n')
210 # linux-2.6 says "<unknown>" for one line O_o
211 parts
= line
.split(' ')
214 stamp
= int(parts
[0])
218 author
= ' '.join(parts
[1:])
219 date
= datetime
.datetime
.fromtimestamp(float(stamp
))
221 # First and last commit stamp
222 if self
.last_commit_stamp
== 0:
223 self
.last_commit_stamp
= stamp
224 self
.first_commit_stamp
= stamp
229 if hour
in self
.activity_by_hour_of_day
:
230 self
.activity_by_hour_of_day
[hour
] += 1
232 self
.activity_by_hour_of_day
[hour
] = 1
234 if self
.activity_by_hour_of_day
[hour
] > self
.activity_by_hour_of_day_busiest
:
235 self
.activity_by_hour_of_day_busiest
= self
.activity_by_hour_of_day
[hour
]
239 if day
in self
.activity_by_day_of_week
:
240 self
.activity_by_day_of_week
[day
] += 1
242 self
.activity_by_day_of_week
[day
] = 1
245 if day
not in self
.activity_by_hour_of_week
:
246 self
.activity_by_hour_of_week
[day
] = {}
247 if hour
not in self
.activity_by_hour_of_week
[day
]:
248 self
.activity_by_hour_of_week
[day
][hour
] = 1
250 self
.activity_by_hour_of_week
[day
][hour
] += 1
252 if self
.activity_by_hour_of_week
[day
][hour
] > self
.activity_by_hour_of_week_busiest
:
253 self
.activity_by_hour_of_week_busiest
= self
.activity_by_hour_of_week
[day
][hour
]
257 if month
in self
.activity_by_month_of_year
:
258 self
.activity_by_month_of_year
[month
] += 1
260 self
.activity_by_month_of_year
[month
] = 1
263 if author
not in self
.authors
:
264 self
.authors
[author
] = {}
266 if 'last_commit_stamp' not in self
.authors
[author
]:
267 self
.authors
[author
]['last_commit_stamp'] = stamp
268 self
.authors
[author
]['first_commit_stamp'] = stamp
269 if 'commits' in self
.authors
[author
]:
270 self
.authors
[author
]['commits'] += 1
272 self
.authors
[author
]['commits'] = 1
274 # author of the month/year
275 yymm
= datetime
.datetime
.fromtimestamp(stamp
).strftime('%Y-%m')
276 if yymm
in self
.author_of_month
:
277 if author
in self
.author_of_month
[yymm
]:
278 self
.author_of_month
[yymm
][author
] += 1
280 self
.author_of_month
[yymm
][author
] = 1
282 self
.author_of_month
[yymm
] = {}
283 self
.author_of_month
[yymm
][author
] = 1
284 if yymm
in self
.commits_by_month
:
285 self
.commits_by_month
[yymm
] += 1
287 self
.commits_by_month
[yymm
] = 1
289 yy
= datetime
.datetime
.fromtimestamp(stamp
).year
290 if yy
in self
.author_of_year
:
291 if author
in self
.author_of_year
[yy
]:
292 self
.author_of_year
[yy
][author
] += 1
294 self
.author_of_year
[yy
][author
] = 1
296 self
.author_of_year
[yy
] = {}
297 self
.author_of_year
[yy
][author
] = 1
298 if yy
in self
.commits_by_year
:
299 self
.commits_by_year
[yy
] += 1
301 self
.commits_by_year
[yy
] = 1
303 # TODO Optimize this, it's the worst bottleneck
304 # outputs "<stamp> <files>" for each revision
305 self
.files_by_stamp
= {} # stamp -> files
306 revlines
= getpipeoutput(['git rev-list --pretty=format:"%at %T" HEAD', 'grep -v ^commit']).strip().split('\n')
308 for revline
in revlines
:
309 time
, rev
= revline
.split(' ')
310 linecount
= self
.getFilesInCommit(rev
)
311 lines
.append('%d %d' % (int(time
), linecount
))
313 self
.total_commits
= len(lines
)
315 parts
= line
.split(' ')
318 (stamp
, files
) = parts
[0:2]
320 self
.files_by_stamp
[int(stamp
)] = int(files
)
322 print 'Warning: failed to parse line "%s"' % line
325 self
.extensions
= {} # extension -> files, lines
326 lines
= getpipeoutput(['git ls-files']).split('\n')
327 self
.total_files
= len(lines
)
329 base
= os
.path
.basename(line
)
330 # Ignore extensionless (including .hidden files)
331 if base
.find('.') == -1 or base
.rfind('.') == 0:
334 ext
= base
[(base
.rfind('.') + 1):]
335 if len(ext
) > MAX_EXT_LENGTH
:
338 if ext
not in self
.extensions
:
339 self
.extensions
[ext
] = {'files': 0, 'lines': 0}
341 self
.extensions
[ext
]['files'] += 1
343 # Escaping could probably be improved here
344 self
.extensions
[ext
]['lines'] += int(getpipeoutput(['wc -l "%s"' % line
]).split()[0])
346 print 'Warning: Could not count lines for file "%s"' % line
350 # N files changed, N insertions (+), N deletions(-)
352 self
.changes_by_date
= {} # stamp -> { files, ins, del }
353 lines
= getpipeoutput(['git log --shortstat --pretty=format:"%at %an"']).split('\n')
355 files
= 0; inserted
= 0; deleted
= 0; total_lines
= 0
361 if line
.find('files changed,') == -1:
365 (stamp
, author
) = (int(line
[:pos
]), line
[pos
+1:])
366 self
.changes_by_date
[stamp
] = { 'files': files
, 'ins': inserted
, 'del': deleted
, 'lines': total_lines
}
368 print 'Warning: unexpected line "%s"' % line
370 print 'Warning: unexpected line "%s"' % line
372 numbers
= re
.findall('\d+', line
)
373 if len(numbers
) == 3:
374 (files
, inserted
, deleted
) = map(lambda el
: int(el
), numbers
)
375 total_lines
+= inserted
376 total_lines
-= deleted
378 print 'Warning: failed to handle line "%s"' % line
379 (files
, inserted
, deleted
) = (0, 0, 0)
380 #self.changes_by_date[stamp] = { 'files': files, 'ins': inserted, 'del': deleted }
381 self
.total_lines
= total_lines
385 # name -> {place_by_commits, commits_frac, date_first, date_last, timedelta}
386 authors_by_commits
= getkeyssortedbyvaluekey(self
.authors
, 'commits')
387 authors_by_commits
.reverse() # most first
388 for i
, name
in enumerate(authors_by_commits
):
389 self
.authors
[name
]['place_by_commits'] = i
+ 1
391 for name
in self
.authors
.keys():
392 a
= self
.authors
[name
]
393 a
['commits_frac'] = (100 * float(a
['commits'])) / self
.getTotalCommits()
394 date_first
= datetime
.datetime
.fromtimestamp(a
['first_commit_stamp'])
395 date_last
= datetime
.datetime
.fromtimestamp(a
['last_commit_stamp'])
396 delta
= date_last
- date_first
397 a
['date_first'] = date_first
.strftime('%Y-%m-%d')
398 a
['date_last'] = date_last
.strftime('%Y-%m-%d')
399 a
['timedelta'] = delta
401 def getActivityByDayOfWeek(self
):
402 return self
.activity_by_day_of_week
404 def getActivityByHourOfDay(self
):
405 return self
.activity_by_hour_of_day
407 def getAuthorInfo(self
, author
):
408 return self
.authors
[author
]
410 def getAuthors(self
):
411 return self
.authors
.keys()
413 def getCommitDeltaDays(self
):
414 return (self
.last_commit_stamp
- self
.first_commit_stamp
) / 86400
416 def getFilesInCommit(self
, rev
):
418 res
= self
.cache
['files_in_tree'][rev
]
420 res
= int(getpipeoutput(['git ls-tree -r --name-only "%s"' % rev
, 'wc -l']).split('\n')[0])
421 if 'files_in_tree' not in self
.cache
:
422 self
.cache
['files_in_tree'] = {}
423 self
.cache
['files_in_tree'][rev
] = res
427 def getFirstCommitDate(self
):
428 return datetime
.datetime
.fromtimestamp(self
.first_commit_stamp
)
430 def getLastCommitDate(self
):
431 return datetime
.datetime
.fromtimestamp(self
.last_commit_stamp
)
434 lines
= getpipeoutput(['git show-ref --tags', 'cut -d/ -f3'])
435 return lines
.split('\n')
437 def getTagDate(self
, tag
):
438 return self
.revToDate('tags/' + tag
)
440 def getTotalAuthors(self
):
441 return self
.total_authors
443 def getTotalCommits(self
):
444 return self
.total_commits
446 def getTotalFiles(self
):
447 return self
.total_files
449 def getTotalLOC(self
):
450 return self
.total_lines
452 def revToDate(self
, rev
):
453 stamp
= int(getpipeoutput(['git log --pretty=format:%%at "%s" -n 1' % rev
]))
454 return datetime
.datetime
.fromtimestamp(stamp
).strftime('%Y-%m-%d')
457 """Creates the actual report based on given data."""
461 def create(self
, data
, path
):
465 def html_linkify(text
):
466 return text
.lower().replace(' ', '_')
468 def html_header(level
, text
):
469 name
= html_linkify(text
)
470 return '\n<h%d><a href="#%s" name="%s">%s</a></h%d>\n\n' % (level
, name
, name
, text
, level
)
472 class HTMLReportCreator(ReportCreator
):
473 def create(self
, data
, path
):
474 ReportCreator
.create(self
, data
, path
)
475 self
.title
= data
.projectname
477 # copy static files if they do not exist
478 for file in ('gitstats.css', 'sortable.js', 'arrow-up.gif', 'arrow-down.gif', 'arrow-none.gif'):
479 basedir
= os
.path
.dirname(os
.path
.abspath(__file__
))
480 shutil
.copyfile(basedir
+ '/' + file, path
+ '/' + file)
482 f
= open(path
+ "/index.html", 'w')
483 format
= '%Y-%m-%d %H:%M:%S'
486 f
.write('<h1>GitStats - %s</h1>' % data
.projectname
)
491 f
.write('<dt>Project name</dt><dd>%s</dd>' % (data
.projectname
))
492 f
.write('<dt>Generated</dt><dd>%s (in %d seconds)</dd>' % (datetime
.datetime
.now().strftime(format
), time
.time() - data
.getStampCreated()))
493 f
.write('<dt>Generator</dt><dd><a href="http://gitstats.sourceforge.net/">GitStats</a> (version %s)</dd>' % getversion())
494 f
.write('<dt>Report Period</dt><dd>%s to %s (%d days)</dd>' % (data
.getFirstCommitDate().strftime(format
), data
.getLastCommitDate().strftime(format
), data
.getCommitDeltaDays()))
495 f
.write('<dt>Total Files</dt><dd>%s</dd>' % data
.getTotalFiles())
496 f
.write('<dt>Total Lines of Code</dt><dd>%s</dd>' % data
.getTotalLOC())
497 f
.write('<dt>Total Commits</dt><dd>%s</dd>' % data
.getTotalCommits())
498 f
.write('<dt>Authors</dt><dd>%s</dd>' % data
.getTotalAuthors())
501 f
.write('</body>\n</html>')
506 f
= open(path
+ '/activity.html', 'w')
508 f
.write('<h1>Activity</h1>')
511 #f.write('<h2>Last 30 days</h2>')
513 #f.write('<h2>Last 12 months</h2>')
516 f
.write(html_header(2, 'Hour of Day'))
517 hour_of_day
= data
.getActivityByHourOfDay()
518 f
.write('<table><tr><th>Hour</th>')
519 for i
in range(0, 24):
520 f
.write('<th>%d</th>' % i
)
521 f
.write('</tr>\n<tr><th>Commits</th>')
522 fp
= open(path
+ '/hour_of_day.dat', 'w')
523 for i
in range(0, 24):
525 r
= 127 + int((float(hour_of_day
[i
]) / data
.activity_by_hour_of_day_busiest
) * 128)
526 f
.write('<td style="background-color: rgb(%d, 0, 0)">%d</td>' % (r
, hour_of_day
[i
]))
527 fp
.write('%d %d\n' % (i
, hour_of_day
[i
]))
529 f
.write('<td>0</td>')
530 fp
.write('%d 0\n' % i
)
532 f
.write('</tr>\n<tr><th>%</th>')
533 totalcommits
= data
.getTotalCommits()
534 for i
in range(0, 24):
536 r
= 127 + int((float(hour_of_day
[i
]) / data
.activity_by_hour_of_day_busiest
) * 128)
537 f
.write('<td style="background-color: rgb(%d, 0, 0)">%.2f</td>' % (r
, (100.0 * hour_of_day
[i
]) / totalcommits
))
539 f
.write('<td>0.00</td>')
540 f
.write('</tr></table>')
541 f
.write('<img src="hour_of_day.png" alt="Hour of Day" />')
542 fg
= open(path
+ '/hour_of_day.dat', 'w')
543 for i
in range(0, 24):
545 fg
.write('%d %d\n' % (i
+ 1, hour_of_day
[i
]))
547 fg
.write('%d 0\n' % (i
+ 1))
551 f
.write(html_header(2, 'Day of Week'))
552 day_of_week
= data
.getActivityByDayOfWeek()
553 f
.write('<div class="vtable"><table>')
554 f
.write('<tr><th>Day</th><th>Total (%)</th></tr>')
555 fp
= open(path
+ '/day_of_week.dat', 'w')
556 for d
in range(0, 7):
559 commits
= day_of_week
[d
]
560 fp
.write('%d %d\n' % (d
+ 1, commits
))
562 f
.write('<th>%d</th>' % (d
+ 1))
564 f
.write('<td>%d (%.2f%%)</td>' % (day_of_week
[d
], (100.0 * day_of_week
[d
]) / totalcommits
))
566 f
.write('<td>0</td>')
568 f
.write('</table></div>')
569 f
.write('<img src="day_of_week.png" alt="Day of Week" />')
573 f
.write(html_header(2, 'Hour of Week'))
576 f
.write('<tr><th>Weekday</th>')
577 for hour
in range(0, 24):
578 f
.write('<th>%d</th>' % (hour
))
581 for weekday
in range(0, 7):
582 f
.write('<tr><th>%d</th>' % (weekday
+ 1))
583 for hour
in range(0, 24):
585 commits
= data
.activity_by_hour_of_week
[weekday
][hour
]
590 r
= 127 + int((float(commits
) / data
.activity_by_hour_of_week_busiest
) * 128)
591 f
.write(' style="background-color: rgb(%d, 0, 0)"' % r
)
592 f
.write('>%d</td>' % commits
)
600 f
.write(html_header(2, 'Month of Year'))
601 f
.write('<div class="vtable"><table>')
602 f
.write('<tr><th>Month</th><th>Commits (%)</th></tr>')
603 fp
= open (path
+ '/month_of_year.dat', 'w')
604 for mm
in range(1, 13):
606 if mm
in data
.activity_by_month_of_year
:
607 commits
= data
.activity_by_month_of_year
[mm
]
608 f
.write('<tr><td>%d</td><td>%d (%.2f %%)</td></tr>' % (mm
, commits
, (100.0 * commits
) / data
.getTotalCommits()))
609 fp
.write('%d %d\n' % (mm
, commits
))
611 f
.write('</table></div>')
612 f
.write('<img src="month_of_year.png" alt="Month of Year" />')
614 # Commits by year/month
615 f
.write(html_header(2, 'Commits by year/month'))
616 f
.write('<div class="vtable"><table><tr><th>Month</th><th>Commits</th></tr>')
617 for yymm
in reversed(sorted(data
.commits_by_month
.keys())):
618 f
.write('<tr><td>%s</td><td>%d</td></tr>' % (yymm
, data
.commits_by_month
[yymm
]))
619 f
.write('</table></div>')
620 f
.write('<img src="commits_by_year_month.png" alt="Commits by year/month" />')
621 fg
= open(path
+ '/commits_by_year_month.dat', 'w')
622 for yymm
in sorted(data
.commits_by_month
.keys()):
623 fg
.write('%s %s\n' % (yymm
, data
.commits_by_month
[yymm
]))
627 f
.write(html_header(2, 'Commits by Year'))
628 f
.write('<div class="vtable"><table><tr><th>Year</th><th>Commits (% of all)</th></tr>')
629 for yy
in reversed(sorted(data
.commits_by_year
.keys())):
630 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()))
631 f
.write('</table></div>')
632 f
.write('<img src="commits_by_year.png" alt="Commits by Year" />')
633 fg
= open(path
+ '/commits_by_year.dat', 'w')
634 for yy
in sorted(data
.commits_by_year
.keys()):
635 fg
.write('%d %d\n' % (yy
, data
.commits_by_year
[yy
]))
638 f
.write('</body></html>')
643 f
= open(path
+ '/authors.html', 'w')
646 f
.write('<h1>Authors</h1>')
649 # Authors :: List of authors
650 f
.write(html_header(2, 'List of Authors'))
652 f
.write('<table class="authors sortable" id="authors">')
653 f
.write('<tr><th>Author</th><th>Commits (%)</th><th>First commit</th><th>Last commit</th><th class="unsortable">Age</th><th># by commits</th></tr>')
654 for author
in sorted(data
.getAuthors()):
655 info
= data
.getAuthorInfo(author
)
656 f
.write('<tr><td>%s</td><td>%d (%.2f%%)</td><td>%s</td><td>%s</td><td>%s</td><td>%d</td></tr>' % (author
, info
['commits'], info
['commits_frac'], info
['date_first'], info
['date_last'], info
['timedelta'], info
['place_by_commits']))
659 # Authors :: Author of Month
660 f
.write(html_header(2, 'Author of Month'))
661 f
.write('<table class="sortable" id="aom">')
662 f
.write('<tr><th>Month</th><th>Author</th><th>Commits (%)</th><th class="unsortable">Next top 5</th></tr>')
663 for yymm
in reversed(sorted(data
.author_of_month
.keys())):
664 authordict
= data
.author_of_month
[yymm
]
665 authors
= getkeyssortedbyvalues(authordict
)
667 commits
= data
.author_of_month
[yymm
][authors
[0]]
668 next
= ', '.join(authors
[1:5])
669 f
.write('<tr><td>%s</td><td>%s</td><td>%d (%.2f%% of %d)</td><td>%s</td></tr>' % (yymm
, authors
[0], commits
, (100 * commits
) / data
.commits_by_month
[yymm
], data
.commits_by_month
[yymm
], next
))
673 f
.write(html_header(2, 'Author of Year'))
674 f
.write('<table class="sortable" id="aoy"><tr><th>Year</th><th>Author</th><th>Commits (%)</th><th class="unsortable">Next top 5</th></tr>')
675 for yy
in reversed(sorted(data
.author_of_year
.keys())):
676 authordict
= data
.author_of_year
[yy
]
677 authors
= getkeyssortedbyvalues(authordict
)
679 commits
= data
.author_of_year
[yy
][authors
[0]]
680 next
= ', '.join(authors
[1:5])
681 f
.write('<tr><td>%s</td><td>%s</td><td>%d (%.2f%% of %d)</td><td>%s</td></tr>' % (yy
, authors
[0], commits
, (100 * commits
) / data
.commits_by_year
[yy
], data
.commits_by_year
[yy
], next
))
684 f
.write('</body></html>')
689 f
= open(path
+ '/files.html', 'w')
691 f
.write('<h1>Files</h1>')
695 f
.write('<dt>Total files</dt><dd>%d</dd>' % data
.getTotalFiles())
696 f
.write('<dt>Total lines</dt><dd>%d</dd>' % data
.getTotalLOC())
697 f
.write('<dt>Average file size</dt><dd>%.2f bytes</dd>' % ((100.0 * data
.getTotalLOC()) / data
.getTotalFiles()))
700 # Files :: File count by date
701 f
.write(html_header(2, 'File count by date'))
703 fg
= open(path
+ '/files_by_date.dat', 'w')
704 for stamp
in sorted(data
.files_by_stamp
.keys()):
705 fg
.write('%s %d\n' % (datetime
.datetime
.fromtimestamp(stamp
).strftime('%Y-%m-%d'), data
.files_by_stamp
[stamp
]))
708 f
.write('<img src="files_by_date.png" alt="Files by Date" />')
710 #f.write('<h2>Average file size by date</h2>')
712 # Files :: Extensions
713 f
.write(html_header(2, 'Extensions'))
714 f
.write('<table class="sortable" id="ext"><tr><th>Extension</th><th>Files (%)</th><th>Lines (%)</th><th>Lines/file</th></tr>')
715 for ext
in sorted(data
.extensions
.keys()):
716 files
= data
.extensions
[ext
]['files']
717 lines
= data
.extensions
[ext
]['lines']
718 f
.write('<tr><td>%s</td><td>%d (%.2f%%)</td><td>%d (%.2f%%)</td><td>%d</td></tr>' % (ext
, files
, (100.0 * files
) / data
.getTotalFiles(), lines
, (100.0 * lines
) / data
.getTotalLOC(), lines
/ files
))
721 f
.write('</body></html>')
726 f
= open(path
+ '/lines.html', 'w')
728 f
.write('<h1>Lines</h1>')
732 f
.write('<dt>Total lines</dt><dd>%d</dd>' % data
.getTotalLOC())
735 f
.write(html_header(2, 'Lines of Code'))
736 f
.write('<img src="lines_of_code.png" />')
738 fg
= open(path
+ '/lines_of_code.dat', 'w')
739 for stamp
in sorted(data
.changes_by_date
.keys()):
740 fg
.write('%d %d\n' % (stamp
, data
.changes_by_date
[stamp
]['lines']))
743 f
.write('</body></html>')
748 f
= open(path
+ '/tags.html', 'w')
750 f
.write('<h1>Tags</h1>')
754 f
.write('<dt>Total tags</dt><dd>%d</dd>' % len(data
.tags
))
755 if len(data
.tags
) > 0:
756 f
.write('<dt>Average commits per tag</dt><dd>%.2f</dd>' % (data
.getTotalCommits() / len(data
.tags
)))
760 f
.write('<tr><th>Name</th><th>Date</th><th>Commits</th><th>Authors</th></tr>')
761 # sort the tags by date desc
762 tags_sorted_by_date_desc
= map(lambda el
: el
[1], reversed(sorted(map(lambda el
: (el
[1]['date'], el
[0]), data
.tags
.items()))))
763 for tag
in tags_sorted_by_date_desc
:
765 authors_by_commits
= getkeyssortedbyvalues(data
.tags
[tag
]['authors'])
766 for i
in reversed(authors_by_commits
):
767 authorinfo
.append('%s (%d)' % (i
, data
.tags
[tag
]['authors'][i
]))
768 f
.write('<tr><td>%s</td><td>%s</td><td>%d</td><td>%s</td></tr>' % (tag
, data
.tags
[tag
]['date'], data
.tags
[tag
]['commits'], ', '.join(authorinfo
)))
771 f
.write('</body></html>')
774 self
.createGraphs(path
)
776 def createGraphs(self
, path
):
777 print 'Generating graphs...'
780 f
= open(path
+ '/hour_of_day.plot', 'w')
781 f
.write(GNUPLOT_COMMON
)
784 set output 'hour_of_day.png'
786 set xrange [0.5:24.5]
789 plot 'hour_of_day.dat' using 1:2:(0.5) w boxes fs solid
794 f
= open(path
+ '/day_of_week.plot', 'w')
795 f
.write(GNUPLOT_COMMON
)
798 set output 'day_of_week.png'
803 plot 'day_of_week.dat' using 1:2:(0.5) w boxes fs solid
808 f
= open(path
+ '/month_of_year.plot', 'w')
809 f
.write(GNUPLOT_COMMON
)
812 set output 'month_of_year.png'
814 set xrange [0.5:12.5]
817 plot 'month_of_year.dat' using 1:2:(0.5) w boxes fs solid
821 # commits_by_year_month
822 f
= open(path
+ '/commits_by_year_month.plot', 'w')
823 f
.write(GNUPLOT_COMMON
)
826 set output 'commits_by_year_month.png'
831 set xtics rotate by 90 15768000
834 plot 'commits_by_year_month.dat' using 1:2:(0.5) w boxes fs solid
839 f
= open(path
+ '/commits_by_year.plot', 'w')
840 f
.write(GNUPLOT_COMMON
)
843 set output 'commits_by_year.png'
848 plot 'commits_by_year.dat' using 1:2:(0.5) w boxes fs solid
853 f
= open(path
+ '/files_by_date.plot', 'w')
854 f
.write(GNUPLOT_COMMON
)
857 set output 'files_by_date.png'
860 set timefmt "%Y-%m-%d"
861 set format x "%Y-%m-%d"
863 set xtics rotate by 90
865 plot 'files_by_date.dat' using 1:2 w steps
870 f
= open(path
+ '/lines_of_code.plot', 'w')
871 f
.write(GNUPLOT_COMMON
)
874 set output 'lines_of_code.png'
878 set format x "%Y-%m-%d"
880 set xtics rotate by 90
882 plot 'lines_of_code.dat' using 1:2 w lines
887 files
= glob
.glob(path
+ '/*.plot')
889 out
= getpipeoutput([gnuplot_cmd
+ ' "%s"' % f
])
893 def printHeader(self
, f
, title
= ''):
895 """<?xml version="1.0" encoding="UTF-8"?>
896 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
897 <html xmlns="http://www.w3.org/1999/xhtml">
899 <title>GitStats - %s</title>
900 <link rel="stylesheet" href="gitstats.css" type="text/css" />
901 <meta name="generator" content="GitStats %s" />
902 <script type="text/javascript" src="sortable.js"></script>
905 """ % (self
.title
, getversion()))
907 def printNav(self
, f
):
911 <li><a href="index.html">General</a></li>
912 <li><a href="activity.html">Activity</a></li>
913 <li><a href="authors.html">Authors</a></li>
914 <li><a href="files.html">Files</a></li>
915 <li><a href="lines.html">Lines</a></li>
916 <li><a href="tags.html">Tags</a></li>
923 Usage: gitstats [options] <gitpath> <outputpath>
928 if len(sys
.argv
) < 3:
932 gitpath
= sys
.argv
[1]
933 outputpath
= os
.path
.abspath(sys
.argv
[2])
937 os
.makedirs(outputpath
)
940 if not os
.path
.isdir(outputpath
):
941 print 'FATAL: Output path is not a directory or does not exist'
944 print 'Git path: %s' % gitpath
945 print 'Output path: %s' % outputpath
949 cachefile
= os
.path
.join(outputpath
, 'gitstats.cache')
951 print 'Collecting data...'
952 data
= GitDataCollector()
953 data
.loadCache(cachefile
)
954 data
.collect(gitpath
)
955 print 'Refining data...'
956 data
.saveCache(cachefile
)
961 print 'Generating report...'
962 report
= HTMLReportCreator()
963 report
.create(data
, outputpath
)
965 time_end
= time
.time()
966 exectime_internal
= time_end
- time_start
967 print 'Execution time %.5f secs, %.5f secs (%.2f %%) in external commands)' % (exectime_internal
, exectime_external
, (100.0 * exectime_external
) / exectime_internal
)