2 # Copyright (c) 2007-2010 Heikki Hokkanen <hoxu@users.sf.net> & others (see doc/author.txt)
17 GNUPLOT_COMMON
= 'set terminal png transparent\nset size 1.0,0.5\n'
18 ON_LINUX
= (platform
.system() == 'Linux')
20 exectime_internal
= 0.0
21 exectime_external
= 0.0
22 time_start
= time
.time()
24 # By default, gnuplot is searched from path, but can be overridden with the
25 # environment variable "GNUPLOT"
26 gnuplot_cmd
= 'gnuplot'
27 if 'GNUPLOT' in os
.environ
:
28 gnuplot_cmd
= os
.environ
['GNUPLOT']
33 'style': 'gitstats.css'
36 def getpipeoutput(cmds
, quiet
= False):
37 global exectime_external
39 if not quiet
and ON_LINUX
and os
.isatty(1):
40 print '>> ' + ' | '.join(cmds
),
42 p0
= subprocess
.Popen(cmds
[0], stdout
= subprocess
.PIPE
, shell
= True)
45 p
= subprocess
.Popen(x
, stdin
= p0
.stdout
, stdout
= subprocess
.PIPE
, shell
= True)
47 output
= p
.communicate()[0]
50 if ON_LINUX
and os
.isatty(1):
52 print '[%.5f] >> %s' % (end
- start
, ' | '.join(cmds
))
53 exectime_external
+= (end
- start
)
54 return output
.rstrip('\n')
56 def getkeyssortedbyvalues(dict):
57 return map(lambda el
: el
[1], sorted(map(lambda el
: (el
[1], el
[0]), dict.items())))
59 # dict['author'] = { 'commits': 512 } - ...key(dict, 'commits')
60 def getkeyssortedbyvaluekey(d
, key
):
61 return map(lambda el
: el
[1], sorted(map(lambda el
: (d
[el
][key
], el
), d
.keys())))
67 VERSION
= getpipeoutput(["git rev-parse --short HEAD"]).split('\n')[0]
71 """Manages data collection from a revision control repository."""
73 self
.stamp_created
= time
.time()
77 # This should be the main function to extract data from the repository.
78 def collect(self
, dir):
80 self
.projectname
= os
.path
.basename(os
.path
.abspath(dir))
84 def loadCache(self
, cachefile
):
85 if not os
.path
.exists(cachefile
):
87 print 'Loading cache...'
90 self
.cache
= pickle
.loads(zlib
.decompress(f
.read()))
92 # temporary hack to upgrade non-compressed caches
94 self
.cache
= pickle
.load(f
)
98 # Produce any additional statistics from the extracted data.
103 # : get a dictionary of author
104 def getAuthorInfo(self
, author
):
107 def getActivityByDayOfWeek(self
):
110 def getActivityByHourOfDay(self
):
113 # : get a dictionary of domains
114 def getDomainInfo(self
, domain
):
118 # Get a list of authors
119 def getAuthors(self
):
122 def getFirstCommitDate(self
):
123 return datetime
.datetime
.now()
125 def getLastCommitDate(self
):
126 return datetime
.datetime
.now()
128 def getStampCreated(self
):
129 return self
.stamp_created
134 def getTotalAuthors(self
):
137 def getTotalCommits(self
):
140 def getTotalFiles(self
):
143 def getTotalLOC(self
):
147 # Save cacheable data
148 def saveCache(self
, cachefile
):
149 print 'Saving cache...'
150 f
= open(cachefile
, 'w')
151 #pickle.dump(self.cache, f)
152 data
= zlib
.compress(pickle
.dumps(self
.cache
))
156 class GitDataCollector(DataCollector
):
157 def collect(self
, dir):
158 DataCollector
.collect(self
, dir)
161 self
.total_authors
= int(getpipeoutput(['git log', 'git shortlog -s', 'wc -l']))
163 self
.total_authors
= 0
164 #self.total_lines = int(getoutput('git-ls-files -z |xargs -0 cat |wc -l'))
166 self
.activity_by_hour_of_day
= {} # hour -> commits
167 self
.activity_by_day_of_week
= {} # day -> commits
168 self
.activity_by_month_of_year
= {} # month [1-12] -> commits
169 self
.activity_by_hour_of_week
= {} # weekday -> hour -> commits
170 self
.activity_by_hour_of_day_busiest
= 0
171 self
.activity_by_hour_of_week_busiest
= 0
172 self
.activity_by_year_week
= {} # yy_wNN -> commits
173 self
.activity_by_year_week_peak
= 0
175 self
.authors
= {} # name -> {commits, first_commit_stamp, last_commit_stamp, last_active_day, active_days, lines_added, lines_removed}
178 self
.domains
= {} # domain -> commits
180 # author of the month
181 self
.author_of_month
= {} # month -> author -> commits
182 self
.author_of_year
= {} # year -> author -> commits
183 self
.commits_by_month
= {} # month -> commits
184 self
.commits_by_year
= {} # year -> commits
185 self
.first_commit_stamp
= 0
186 self
.last_commit_stamp
= 0
187 self
.last_active_day
= None
188 self
.active_days
= set()
192 self
.total_lines_added
= 0
193 self
.total_lines_removed
= 0
196 self
.commits_by_timezone
= {} # timezone -> commits
200 lines
= getpipeoutput(['git show-ref --tags']).split('\n')
204 (hash, tag
) = line
.split(' ')
206 tag
= tag
.replace('refs/tags/', '')
207 output
= getpipeoutput(['git log "%s" --pretty=format:"%%at %%an" -n 1' % hash])
209 parts
= output
.split(' ')
212 stamp
= int(parts
[0])
215 self
.tags
[tag
] = { 'stamp': stamp
, 'hash' : hash, 'date' : datetime
.datetime
.fromtimestamp(stamp
).strftime('%Y-%m-%d'), 'commits': 0, 'authors': {} }
217 # collect info on tags, starting from latest
218 tags_sorted_by_date_desc
= map(lambda el
: el
[1], reversed(sorted(map(lambda el
: (el
[1]['date'], el
[0]), self
.tags
.items()))))
220 for tag
in reversed(tags_sorted_by_date_desc
):
221 cmd
= 'git shortlog -s "%s"' % tag
223 cmd
+= ' "^%s"' % prev
224 output
= getpipeoutput([cmd
])
228 for line
in output
.split('\n'):
229 parts
= re
.split('\s+', line
, 2)
230 commits
= int(parts
[1])
232 self
.tags
[tag
]['commits'] += commits
233 self
.tags
[tag
]['authors'][author
] = commits
235 # Collect revision statistics
236 # Outputs "<stamp> <date> <time> <timezone> <author> '<' <mail> '>'"
237 lines
= getpipeoutput(['git rev-list --pretty=format:"%at %ai %an <%aE>" HEAD', 'grep -v ^commit']).split('\n')
239 parts
= line
.split(' ', 4)
242 stamp
= int(parts
[0])
246 author
, mail
= parts
[4].split('<', 1)
247 author
= author
.rstrip()
248 mail
= mail
.rstrip('>')
250 if mail
.find('@') != -1:
251 domain
= mail
.rsplit('@', 1)[1]
252 date
= datetime
.datetime
.fromtimestamp(float(stamp
))
254 # First and last commit stamp
255 if self
.last_commit_stamp
== 0:
256 self
.last_commit_stamp
= stamp
257 self
.first_commit_stamp
= stamp
262 self
.activity_by_hour_of_day
[hour
] = self
.activity_by_hour_of_day
.get(hour
, 0) + 1
264 if self
.activity_by_hour_of_day
[hour
] > self
.activity_by_hour_of_day_busiest
:
265 self
.activity_by_hour_of_day_busiest
= self
.activity_by_hour_of_day
[hour
]
269 self
.activity_by_day_of_week
[day
] = self
.activity_by_day_of_week
.get(day
, 0) + 1
272 if domain
not in self
.domains
:
273 self
.domains
[domain
] = {}
275 self
.domains
[domain
]['commits'] = self
.domains
[domain
].get('commits', 0) + 1
278 if day
not in self
.activity_by_hour_of_week
:
279 self
.activity_by_hour_of_week
[day
] = {}
280 self
.activity_by_hour_of_week
[day
][hour
] = self
.activity_by_hour_of_week
[day
].get(hour
, 0) + 1
282 if self
.activity_by_hour_of_week
[day
][hour
] > self
.activity_by_hour_of_week_busiest
:
283 self
.activity_by_hour_of_week_busiest
= self
.activity_by_hour_of_week
[day
][hour
]
287 self
.activity_by_month_of_year
[month
] = self
.activity_by_month_of_year
.get(month
, 0) + 1
289 # yearly/weekly activity
290 yyw
= date
.strftime('%Y-%W')
291 self
.activity_by_year_week
[yyw
] = self
.activity_by_year_week
.get(yyw
, 0) + 1
292 if self
.activity_by_year_week_peak
< self
.activity_by_year_week
[yyw
]:
293 self
.activity_by_year_week_peak
= self
.activity_by_year_week
[yyw
]
296 if author
not in self
.authors
:
297 self
.authors
[author
] = {}
299 if 'last_commit_stamp' not in self
.authors
[author
]:
300 self
.authors
[author
]['last_commit_stamp'] = stamp
301 self
.authors
[author
]['first_commit_stamp'] = stamp
302 self
.authors
[author
]['commits'] = self
.authors
[author
].get('commits', 0) + 1
304 # author of the month/year
305 yymm
= date
.strftime('%Y-%m')
306 if yymm
in self
.author_of_month
:
307 self
.author_of_month
[yymm
][author
] = self
.author_of_month
[yymm
].get(author
, 0) + 1
309 self
.author_of_month
[yymm
] = {}
310 self
.author_of_month
[yymm
][author
] = 1
311 self
.commits_by_month
[yymm
] = self
.commits_by_month
.get(yymm
, 0) + 1
314 if yy
in self
.author_of_year
:
315 self
.author_of_year
[yy
][author
] = self
.author_of_year
[yy
].get(author
, 0) + 1
317 self
.author_of_year
[yy
] = {}
318 self
.author_of_year
[yy
][author
] = 1
319 self
.commits_by_year
[yy
] = self
.commits_by_year
.get(yy
, 0) + 1
321 # authors: active days
322 yymmdd
= date
.strftime('%Y-%m-%d')
323 if 'last_active_day' not in self
.authors
[author
]:
324 self
.authors
[author
]['last_active_day'] = yymmdd
325 self
.authors
[author
]['active_days'] = 1
326 elif yymmdd
!= self
.authors
[author
]['last_active_day']:
327 self
.authors
[author
]['last_active_day'] = yymmdd
328 self
.authors
[author
]['active_days'] += 1
330 # project: active days
331 if yymmdd
!= self
.last_active_day
:
332 self
.last_active_day
= yymmdd
333 self
.active_days
.add(yymmdd
)
336 self
.commits_by_timezone
[timezone
] = self
.commits_by_timezone
.get(timezone
, 0) + 1
338 # TODO Optimize this, it's the worst bottleneck
339 # outputs "<stamp> <files>" for each revision
340 self
.files_by_stamp
= {} # stamp -> files
341 revlines
= getpipeoutput(['git rev-list --pretty=format:"%at %T" HEAD', 'grep -v ^commit']).strip().split('\n')
343 for revline
in revlines
:
344 time
, rev
= revline
.split(' ')
345 linecount
= self
.getFilesInCommit(rev
)
346 lines
.append('%d %d' % (int(time
), linecount
))
348 self
.total_commits
= len(lines
)
350 parts
= line
.split(' ')
353 (stamp
, files
) = parts
[0:2]
355 self
.files_by_stamp
[int(stamp
)] = int(files
)
357 print 'Warning: failed to parse line "%s"' % line
360 self
.extensions
= {} # extension -> files, lines
361 lines
= getpipeoutput(['git ls-tree -r -z HEAD']).split('\000')
362 self
.total_files
= len(lines
)
366 parts
= re
.split('\s+', line
, 4)
370 if filename
.find('.') == -1 or filename
.rfind('.') == 0:
373 ext
= filename
[(filename
.rfind('.') + 1):]
374 if len(ext
) > conf
['max_ext_length']:
377 if ext
not in self
.extensions
:
378 self
.extensions
[ext
] = {'files': 0, 'lines': 0}
380 self
.extensions
[ext
]['files'] += 1
382 self
.extensions
[ext
]['lines'] += self
.getLinesInBlob(sha1
)
384 print 'Warning: Could not count lines for file "%s"' % line
388 # N files changed, N insertions (+), N deletions(-)
390 self
.changes_by_date
= {} # stamp -> { files, ins, del }
391 lines
= getpipeoutput(['git log --shortstat --pretty=format:"%at %an"']).split('\n')
393 files
= 0; inserted
= 0; deleted
= 0; total_lines
= 0
400 if line
.find('files changed,') == -1:
404 (stamp
, author
) = (int(line
[:pos
]), line
[pos
+1:])
405 self
.changes_by_date
[stamp
] = { 'files': files
, 'ins': inserted
, 'del': deleted
, 'lines': total_lines
}
406 if author
not in self
.authors
:
407 self
.authors
[author
] = { 'lines_added' : 0, 'lines_removed' : 0 }
408 self
.authors
[author
]['lines_added'] = self
.authors
[author
].get('lines_added', 0) + inserted
409 self
.authors
[author
]['lines_removed'] = self
.authors
[author
].get('lines_removed', 0) + deleted
411 print 'Warning: unexpected line "%s"' % line
413 print 'Warning: unexpected line "%s"' % line
415 numbers
= re
.findall('\d+', line
)
416 if len(numbers
) == 3:
417 (files
, inserted
, deleted
) = map(lambda el
: int(el
), numbers
)
418 total_lines
+= inserted
419 total_lines
-= deleted
420 self
.total_lines_added
+= inserted
421 self
.total_lines_removed
+= deleted
423 print 'Warning: failed to handle line "%s"' % line
424 (files
, inserted
, deleted
) = (0, 0, 0)
425 #self.changes_by_date[stamp] = { 'files': files, 'ins': inserted, 'del': deleted }
426 self
.total_lines
= total_lines
430 # name -> {place_by_commits, commits_frac, date_first, date_last, timedelta}
431 authors_by_commits
= getkeyssortedbyvaluekey(self
.authors
, 'commits')
432 authors_by_commits
.reverse() # most first
433 for i
, name
in enumerate(authors_by_commits
):
434 self
.authors
[name
]['place_by_commits'] = i
+ 1
436 for name
in self
.authors
.keys():
437 a
= self
.authors
[name
]
438 a
['commits_frac'] = (100 * float(a
['commits'])) / self
.getTotalCommits()
439 date_first
= datetime
.datetime
.fromtimestamp(a
['first_commit_stamp'])
440 date_last
= datetime
.datetime
.fromtimestamp(a
['last_commit_stamp'])
441 delta
= date_last
- date_first
442 a
['date_first'] = date_first
.strftime('%Y-%m-%d')
443 a
['date_last'] = date_last
.strftime('%Y-%m-%d')
444 a
['timedelta'] = delta
446 def getActiveDays(self
):
447 return self
.active_days
449 def getActivityByDayOfWeek(self
):
450 return self
.activity_by_day_of_week
452 def getActivityByHourOfDay(self
):
453 return self
.activity_by_hour_of_day
455 def getAuthorInfo(self
, author
):
456 return self
.authors
[author
]
458 def getAuthors(self
):
459 return self
.authors
.keys()
461 def getCommitDeltaDays(self
):
462 return (self
.last_commit_stamp
- self
.first_commit_stamp
) / 86400
464 def getDomainInfo(self
, domain
):
465 return self
.domains
[domain
]
467 def getDomains(self
):
468 return self
.domains
.keys()
470 def getFilesInCommit(self
, rev
):
472 res
= self
.cache
['files_in_tree'][rev
]
474 res
= int(getpipeoutput(['git ls-tree -r --name-only "%s"' % rev
, 'wc -l']).split('\n')[0])
475 if 'files_in_tree' not in self
.cache
:
476 self
.cache
['files_in_tree'] = {}
477 self
.cache
['files_in_tree'][rev
] = res
481 def getFirstCommitDate(self
):
482 return datetime
.datetime
.fromtimestamp(self
.first_commit_stamp
)
484 def getLastCommitDate(self
):
485 return datetime
.datetime
.fromtimestamp(self
.last_commit_stamp
)
487 def getLinesInBlob(self
, sha1
):
489 res
= self
.cache
['lines_in_blob'][sha1
]
491 res
= int(getpipeoutput(['git cat-file blob %s' % sha1
, 'wc -l']).split()[0])
492 if 'lines_in_blob' not in self
.cache
:
493 self
.cache
['lines_in_blob'] = {}
494 self
.cache
['lines_in_blob'][sha1
] = res
498 lines
= getpipeoutput(['git show-ref --tags', 'cut -d/ -f3'])
499 return lines
.split('\n')
501 def getTagDate(self
, tag
):
502 return self
.revToDate('tags/' + tag
)
504 def getTotalAuthors(self
):
505 return self
.total_authors
507 def getTotalCommits(self
):
508 return self
.total_commits
510 def getTotalFiles(self
):
511 return self
.total_files
513 def getTotalLOC(self
):
514 return self
.total_lines
516 def revToDate(self
, rev
):
517 stamp
= int(getpipeoutput(['git log --pretty=format:%%at "%s" -n 1' % rev
]))
518 return datetime
.datetime
.fromtimestamp(stamp
).strftime('%Y-%m-%d')
521 """Creates the actual report based on given data."""
525 def create(self
, data
, path
):
529 def html_linkify(text
):
530 return text
.lower().replace(' ', '_')
532 def html_header(level
, text
):
533 name
= html_linkify(text
)
534 return '\n<h%d><a href="#%s" name="%s">%s</a></h%d>\n\n' % (level
, name
, name
, text
, level
)
536 class HTMLReportCreator(ReportCreator
):
537 def create(self
, data
, path
):
538 ReportCreator
.create(self
, data
, path
)
539 self
.title
= data
.projectname
541 # copy static files. Looks in the binary directory, ../share/gitstats and /usr/share/gitstats
542 binarypath
= os
.path
.dirname(os
.path
.abspath(__file__
))
543 secondarypath
= os
.path
.join(binarypath
, '..', 'share', 'gitstats')
544 basedirs
= [binarypath
, secondarypath
, '/usr/share/gitstats']
545 for file in ('gitstats.css', 'sortable.js', 'arrow-up.gif', 'arrow-down.gif', 'arrow-none.gif'):
546 for base
in basedirs
:
547 src
= base
+ '/' + file
548 if os
.path
.exists(src
):
549 shutil
.copyfile(src
, path
+ '/' + file)
552 print 'Warning: "%s" not found, so not copied (searched: %s)' % (file, basedirs
)
554 f
= open(path
+ "/index.html", 'w')
555 format
= '%Y-%m-%d %H:%M:%S'
558 f
.write('<h1>GitStats - %s</h1>' % data
.projectname
)
563 f
.write('<dt>Project name</dt><dd>%s</dd>' % (data
.projectname
))
564 f
.write('<dt>Generated</dt><dd>%s (in %d seconds)</dd>' % (datetime
.datetime
.now().strftime(format
), time
.time() - data
.getStampCreated()))
565 f
.write('<dt>Generator</dt><dd><a href="http://gitstats.sourceforge.net/">GitStats</a> (version %s)</dd>' % getversion())
566 f
.write('<dt>Report Period</dt><dd>%s to %s</dd>' % (data
.getFirstCommitDate().strftime(format
), data
.getLastCommitDate().strftime(format
)))
567 f
.write('<dt>Age</dt><dd>%d days, %d active days (%3.2f%%)</dd>' % (data
.getCommitDeltaDays(), len(data
.getActiveDays()), (100.0 * len(data
.getActiveDays()) / data
.getCommitDeltaDays())))
568 f
.write('<dt>Total Files</dt><dd>%s</dd>' % data
.getTotalFiles())
569 f
.write('<dt>Total Lines of Code</dt><dd>%s (%d added, %d removed)</dd>' % (data
.getTotalLOC(), data
.total_lines_added
, data
.total_lines_removed
))
570 f
.write('<dt>Total Commits</dt><dd>%s (average %.1f commits per active day, %.1f per all days)</dd>' % (data
.getTotalCommits(), float(data
.getTotalCommits()) / len(data
.getActiveDays()), float(data
.getTotalCommits()) / data
.getCommitDeltaDays()))
571 f
.write('<dt>Authors</dt><dd>%s</dd>' % data
.getTotalAuthors())
574 f
.write('</body>\n</html>')
579 f
= open(path
+ '/activity.html', 'w')
581 f
.write('<h1>Activity</h1>')
584 #f.write('<h2>Last 30 days</h2>')
586 #f.write('<h2>Last 12 months</h2>')
590 f
.write(html_header(2, 'Weekly activity'))
591 f
.write('<p>Last %d weeks</p>' % WEEKS
)
593 # generate weeks to show (previous N weeks from now)
594 now
= datetime
.datetime
.now()
595 deltaweek
= datetime
.timedelta(7)
598 for i
in range(0, WEEKS
):
599 weeks
.insert(0, stampcur
.strftime('%Y-%W'))
600 stampcur
-= deltaweek
602 # top row: commits & bar
603 f
.write('<table class="noborders"><tr>')
604 for i
in range(0, WEEKS
):
606 if weeks
[i
] in data
.activity_by_year_week
:
607 commits
= data
.activity_by_year_week
[weeks
[i
]]
610 if weeks
[i
] in data
.activity_by_year_week
:
611 percentage
= float(data
.activity_by_year_week
[weeks
[i
]]) / data
.activity_by_year_week_peak
612 height
= max(1, int(200 * percentage
))
613 f
.write('<td style="text-align: center; vertical-align: bottom">%d<div style="display: block; background-color: red; width: 20px; height: %dpx"></div></td>' % (commits
, height
))
615 # bottom row: year/week
617 for i
in range(0, WEEKS
):
618 f
.write('<td>%s</td>' % (WEEKS
- i
))
619 f
.write('</tr></table>')
622 f
.write(html_header(2, 'Hour of Day'))
623 hour_of_day
= data
.getActivityByHourOfDay()
624 f
.write('<table><tr><th>Hour</th>')
625 for i
in range(0, 24):
626 f
.write('<th>%d</th>' % i
)
627 f
.write('</tr>\n<tr><th>Commits</th>')
628 fp
= open(path
+ '/hour_of_day.dat', 'w')
629 for i
in range(0, 24):
631 r
= 127 + int((float(hour_of_day
[i
]) / data
.activity_by_hour_of_day_busiest
) * 128)
632 f
.write('<td style="background-color: rgb(%d, 0, 0)">%d</td>' % (r
, hour_of_day
[i
]))
633 fp
.write('%d %d\n' % (i
, hour_of_day
[i
]))
635 f
.write('<td>0</td>')
636 fp
.write('%d 0\n' % i
)
638 f
.write('</tr>\n<tr><th>%</th>')
639 totalcommits
= data
.getTotalCommits()
640 for i
in range(0, 24):
642 r
= 127 + int((float(hour_of_day
[i
]) / data
.activity_by_hour_of_day_busiest
) * 128)
643 f
.write('<td style="background-color: rgb(%d, 0, 0)">%.2f</td>' % (r
, (100.0 * hour_of_day
[i
]) / totalcommits
))
645 f
.write('<td>0.00</td>')
646 f
.write('</tr></table>')
647 f
.write('<img src="hour_of_day.png" alt="Hour of Day" />')
648 fg
= open(path
+ '/hour_of_day.dat', 'w')
649 for i
in range(0, 24):
651 fg
.write('%d %d\n' % (i
+ 1, hour_of_day
[i
]))
653 fg
.write('%d 0\n' % (i
+ 1))
657 f
.write(html_header(2, 'Day of Week'))
658 day_of_week
= data
.getActivityByDayOfWeek()
659 f
.write('<div class="vtable"><table>')
660 f
.write('<tr><th>Day</th><th>Total (%)</th></tr>')
661 fp
= open(path
+ '/day_of_week.dat', 'w')
662 for d
in range(0, 7):
665 commits
= day_of_week
[d
]
666 fp
.write('%d %d\n' % (d
+ 1, commits
))
668 f
.write('<th>%d</th>' % (d
+ 1))
670 f
.write('<td>%d (%.2f%%)</td>' % (day_of_week
[d
], (100.0 * day_of_week
[d
]) / totalcommits
))
672 f
.write('<td>0</td>')
674 f
.write('</table></div>')
675 f
.write('<img src="day_of_week.png" alt="Day of Week" />')
679 f
.write(html_header(2, 'Hour of Week'))
682 f
.write('<tr><th>Weekday</th>')
683 for hour
in range(0, 24):
684 f
.write('<th>%d</th>' % (hour
))
687 for weekday
in range(0, 7):
688 f
.write('<tr><th>%d</th>' % (weekday
+ 1))
689 for hour
in range(0, 24):
691 commits
= data
.activity_by_hour_of_week
[weekday
][hour
]
696 r
= 127 + int((float(commits
) / data
.activity_by_hour_of_week_busiest
) * 128)
697 f
.write(' style="background-color: rgb(%d, 0, 0)"' % r
)
698 f
.write('>%d</td>' % commits
)
706 f
.write(html_header(2, 'Month of Year'))
707 f
.write('<div class="vtable"><table>')
708 f
.write('<tr><th>Month</th><th>Commits (%)</th></tr>')
709 fp
= open (path
+ '/month_of_year.dat', 'w')
710 for mm
in range(1, 13):
712 if mm
in data
.activity_by_month_of_year
:
713 commits
= data
.activity_by_month_of_year
[mm
]
714 f
.write('<tr><td>%d</td><td>%d (%.2f %%)</td></tr>' % (mm
, commits
, (100.0 * commits
) / data
.getTotalCommits()))
715 fp
.write('%d %d\n' % (mm
, commits
))
717 f
.write('</table></div>')
718 f
.write('<img src="month_of_year.png" alt="Month of Year" />')
720 # Commits by year/month
721 f
.write(html_header(2, 'Commits by year/month'))
722 f
.write('<div class="vtable"><table><tr><th>Month</th><th>Commits</th></tr>')
723 for yymm
in reversed(sorted(data
.commits_by_month
.keys())):
724 f
.write('<tr><td>%s</td><td>%d</td></tr>' % (yymm
, data
.commits_by_month
[yymm
]))
725 f
.write('</table></div>')
726 f
.write('<img src="commits_by_year_month.png" alt="Commits by year/month" />')
727 fg
= open(path
+ '/commits_by_year_month.dat', 'w')
728 for yymm
in sorted(data
.commits_by_month
.keys()):
729 fg
.write('%s %s\n' % (yymm
, data
.commits_by_month
[yymm
]))
733 f
.write(html_header(2, 'Commits by Year'))
734 f
.write('<div class="vtable"><table><tr><th>Year</th><th>Commits (% of all)</th></tr>')
735 for yy
in reversed(sorted(data
.commits_by_year
.keys())):
736 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()))
737 f
.write('</table></div>')
738 f
.write('<img src="commits_by_year.png" alt="Commits by Year" />')
739 fg
= open(path
+ '/commits_by_year.dat', 'w')
740 for yy
in sorted(data
.commits_by_year
.keys()):
741 fg
.write('%d %d\n' % (yy
, data
.commits_by_year
[yy
]))
744 # Commits by timezone
745 f
.write(html_header(2, 'Commits by Timezone'))
746 f
.write('<table><tr>')
747 f
.write('<th>Timezone</th><th>Commits</th>')
748 max_commits_on_tz
= max(data
.commits_by_timezone
.values())
749 for i
in sorted(data
.commits_by_timezone
.keys(), key
= lambda n
: int(n
)):
750 commits
= data
.commits_by_timezone
[i
]
751 r
= 127 + int((float(commits
) / max_commits_on_tz
) * 128)
752 f
.write('<tr><th>%s</th><td style="background-color: rgb(%d, 0, 0)">%d</td></tr>' % (i
, r
, commits
))
753 f
.write('</tr></table>')
755 f
.write('</body></html>')
760 f
= open(path
+ '/authors.html', 'w')
763 f
.write('<h1>Authors</h1>')
766 # Authors :: List of authors
767 f
.write(html_header(2, 'List of Authors'))
769 f
.write('<table class="authors sortable" id="authors">')
770 f
.write('<tr><th>Author</th><th>Commits (%)</th><th>+ lines</th><th>- lines</th><th>First commit</th><th>Last commit</th><th class="unsortable">Age</th><th>Active days</th><th># by commits</th></tr>')
771 for author
in sorted(data
.getAuthors()):
772 info
= data
.getAuthorInfo(author
)
773 f
.write('<tr><td>%s</td><td>%d (%.2f%%)</td><td>%d</td><td>%d</td><td>%s</td><td>%s</td><td>%s</td><td>%d</td><td>%d</td></tr>' % (author
, info
['commits'], info
['commits_frac'], info
['lines_added'], info
['lines_removed'], info
['date_first'], info
['date_last'], info
['timedelta'], info
['active_days'], info
['place_by_commits']))
776 # Authors :: Author of Month
777 f
.write(html_header(2, 'Author of Month'))
778 f
.write('<table class="sortable" id="aom">')
779 f
.write('<tr><th>Month</th><th>Author</th><th>Commits (%)</th><th class="unsortable">Next top 5</th></tr>')
780 for yymm
in reversed(sorted(data
.author_of_month
.keys())):
781 authordict
= data
.author_of_month
[yymm
]
782 authors
= getkeyssortedbyvalues(authordict
)
784 commits
= data
.author_of_month
[yymm
][authors
[0]]
785 next
= ', '.join(authors
[1:5])
786 f
.write('<tr><td>%s</td><td>%s</td><td>%d (%.2f%% of %d)</td><td>%s</td></tr>' % (yymm
, authors
[0], commits
, (100.0 * commits
) / data
.commits_by_month
[yymm
], data
.commits_by_month
[yymm
], next
))
790 f
.write(html_header(2, 'Author of Year'))
791 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>')
792 for yy
in reversed(sorted(data
.author_of_year
.keys())):
793 authordict
= data
.author_of_year
[yy
]
794 authors
= getkeyssortedbyvalues(authordict
)
796 commits
= data
.author_of_year
[yy
][authors
[0]]
797 next
= ', '.join(authors
[1:5])
798 f
.write('<tr><td>%s</td><td>%s</td><td>%d (%.2f%% of %d)</td><td>%s</td></tr>' % (yy
, authors
[0], commits
, (100.0 * commits
) / data
.commits_by_year
[yy
], data
.commits_by_year
[yy
], next
))
802 f
.write(html_header(2, 'Commits by Domains'))
803 domains_by_commits
= getkeyssortedbyvaluekey(data
.domains
, 'commits')
804 domains_by_commits
.reverse() # most first
805 f
.write('<div class="vtable"><table>')
806 f
.write('<tr><th>Domains</th><th>Total (%)</th></tr>')
807 fp
= open(path
+ '/domains.dat', 'w')
809 for domain
in domains_by_commits
:
810 if n
== conf
['max_domains']:
814 info
= data
.getDomainInfo(domain
)
815 fp
.write('%s %d %d\n' % (domain
, n
, info
['commits']))
816 f
.write('<tr><th>%s</th><td>%d (%.2f%%)</td></tr>' % (domain
, info
['commits'], (100.0 * info
['commits'] / totalcommits
)))
817 f
.write('</table></div>')
818 f
.write('<img src="domains.png" alt="Commits by Domains" />')
821 f
.write('</body></html>')
826 f
= open(path
+ '/files.html', 'w')
828 f
.write('<h1>Files</h1>')
832 f
.write('<dt>Total files</dt><dd>%d</dd>' % data
.getTotalFiles())
833 f
.write('<dt>Total lines</dt><dd>%d</dd>' % data
.getTotalLOC())
834 f
.write('<dt>Average file size</dt><dd>%.2f bytes</dd>' % ((100.0 * data
.getTotalLOC()) / data
.getTotalFiles()))
837 # Files :: File count by date
838 f
.write(html_header(2, 'File count by date'))
840 # use set to get rid of duplicate/unnecessary entries
841 files_by_date
= set()
842 for stamp
in sorted(data
.files_by_stamp
.keys()):
843 files_by_date
.add('%s %d' % (datetime
.datetime
.fromtimestamp(stamp
).strftime('%Y-%m-%d'), data
.files_by_stamp
[stamp
]))
845 fg
= open(path
+ '/files_by_date.dat', 'w')
846 for line
in sorted(list(files_by_date
)):
847 fg
.write('%s\n' % line
)
848 #for stamp in sorted(data.files_by_stamp.keys()):
849 # fg.write('%s %d\n' % (datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m-%d'), data.files_by_stamp[stamp]))
852 f
.write('<img src="files_by_date.png" alt="Files by Date" />')
854 #f.write('<h2>Average file size by date</h2>')
856 # Files :: Extensions
857 f
.write(html_header(2, 'Extensions'))
858 f
.write('<table class="sortable" id="ext"><tr><th>Extension</th><th>Files (%)</th><th>Lines (%)</th><th>Lines/file</th></tr>')
859 for ext
in sorted(data
.extensions
.keys()):
860 files
= data
.extensions
[ext
]['files']
861 lines
= data
.extensions
[ext
]['lines']
862 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
))
865 f
.write('</body></html>')
870 f
= open(path
+ '/lines.html', 'w')
872 f
.write('<h1>Lines</h1>')
876 f
.write('<dt>Total lines</dt><dd>%d</dd>' % data
.getTotalLOC())
879 f
.write(html_header(2, 'Lines of Code'))
880 f
.write('<img src="lines_of_code.png" />')
882 fg
= open(path
+ '/lines_of_code.dat', 'w')
883 for stamp
in sorted(data
.changes_by_date
.keys()):
884 fg
.write('%d %d\n' % (stamp
, data
.changes_by_date
[stamp
]['lines']))
887 f
.write('</body></html>')
892 f
= open(path
+ '/tags.html', 'w')
894 f
.write('<h1>Tags</h1>')
898 f
.write('<dt>Total tags</dt><dd>%d</dd>' % len(data
.tags
))
899 if len(data
.tags
) > 0:
900 f
.write('<dt>Average commits per tag</dt><dd>%.2f</dd>' % (1.0 * data
.getTotalCommits() / len(data
.tags
)))
903 f
.write('<table class="tags">')
904 f
.write('<tr><th>Name</th><th>Date</th><th>Commits</th><th>Authors</th></tr>')
905 # sort the tags by date desc
906 tags_sorted_by_date_desc
= map(lambda el
: el
[1], reversed(sorted(map(lambda el
: (el
[1]['date'], el
[0]), data
.tags
.items()))))
907 for tag
in tags_sorted_by_date_desc
:
909 authors_by_commits
= getkeyssortedbyvalues(data
.tags
[tag
]['authors'])
910 for i
in reversed(authors_by_commits
):
911 authorinfo
.append('%s (%d)' % (i
, data
.tags
[tag
]['authors'][i
]))
912 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
)))
915 f
.write('</body></html>')
918 self
.createGraphs(path
)
920 def createGraphs(self
, path
):
921 print 'Generating graphs...'
924 f
= open(path
+ '/hour_of_day.plot', 'w')
925 f
.write(GNUPLOT_COMMON
)
928 set output 'hour_of_day.png'
930 set xrange [0.5:24.5]
933 plot 'hour_of_day.dat' using 1:2:(0.5) w boxes fs solid
938 f
= open(path
+ '/day_of_week.plot', 'w')
939 f
.write(GNUPLOT_COMMON
)
942 set output 'day_of_week.png'
947 plot 'day_of_week.dat' using 1:2:(0.5) w boxes fs solid
952 f
= open(path
+ '/domains.plot', 'w')
953 f
.write(GNUPLOT_COMMON
)
956 set output 'domains.png'
961 plot 'domains.dat' using 2:3:(0.5) with boxes fs solid, '' using 2:3:1 with labels rotate by 45 offset 0,1
966 f
= open(path
+ '/month_of_year.plot', 'w')
967 f
.write(GNUPLOT_COMMON
)
970 set output 'month_of_year.png'
972 set xrange [0.5:12.5]
975 plot 'month_of_year.dat' using 1:2:(0.5) w boxes fs solid
979 # commits_by_year_month
980 f
= open(path
+ '/commits_by_year_month.plot', 'w')
981 f
.write(GNUPLOT_COMMON
)
984 set output 'commits_by_year_month.png'
989 set xtics rotate by 90 15768000
992 plot 'commits_by_year_month.dat' using 1:2:(0.5) w boxes fs solid
997 f
= open(path
+ '/commits_by_year.plot', 'w')
998 f
.write(GNUPLOT_COMMON
)
1001 set output 'commits_by_year.png'
1003 set xtics 1 rotate by 90
1004 set ylabel "Commits"
1006 plot 'commits_by_year.dat' using 1:2:(0.5) w boxes fs solid
1011 f
= open(path
+ '/files_by_date.plot', 'w')
1012 f
.write(GNUPLOT_COMMON
)
1015 set output 'files_by_date.png'
1018 set timefmt "%Y-%m-%d"
1019 set format x "%Y-%m-%d"
1021 set xtics rotate by 90
1024 plot 'files_by_date.dat' using 1:2 w steps
1029 f
= open(path
+ '/lines_of_code.plot', 'w')
1030 f
.write(GNUPLOT_COMMON
)
1033 set output 'lines_of_code.png'
1037 set format x "%Y-%m-%d"
1039 set xtics rotate by 90
1041 plot 'lines_of_code.dat' using 1:2 w lines
1046 files
= glob
.glob(path
+ '/*.plot')
1048 out
= getpipeoutput([gnuplot_cmd
+ ' "%s"' % f
])
1052 def printHeader(self
, f
, title
= ''):
1054 """<?xml version="1.0" encoding="UTF-8"?>
1055 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1056 <html xmlns="http://www.w3.org/1999/xhtml">
1058 <title>GitStats - %s</title>
1059 <link rel="stylesheet" href="%s" type="text/css" />
1060 <meta name="generator" content="GitStats %s" />
1061 <script type="text/javascript" src="sortable.js"></script>
1064 """ % (self
.title
, conf
['style'], getversion()))
1066 def printNav(self
, f
):
1070 <li><a href="index.html">General</a></li>
1071 <li><a href="activity.html">Activity</a></li>
1072 <li><a href="authors.html">Authors</a></li>
1073 <li><a href="files.html">Files</a></li>
1074 <li><a href="lines.html">Lines</a></li>
1075 <li><a href="tags.html">Tags</a></li>
1082 def run(self
, args_orig
):
1083 optlist
, args
= getopt
.getopt(args_orig
, 'c:')
1086 key
, value
= v
.split('=', 1)
1088 raise 'Error: no such key "%s" in config' % key
1093 Usage: gitstats [options] <gitpath> <outputpath>
1096 -c key=value Override configuration value
1098 Default config values:
1104 outputpath
= os
.path
.abspath(args
[1])
1105 rundir
= os
.getcwd()
1108 os
.makedirs(outputpath
)
1111 if not os
.path
.isdir(outputpath
):
1112 print 'FATAL: Output path is not a directory or does not exist'
1115 print 'Git path: %s' % gitpath
1116 print 'Output path: %s' % outputpath
1120 cachefile
= os
.path
.join(outputpath
, 'gitstats.cache')
1122 print 'Collecting data...'
1123 data
= GitDataCollector()
1124 data
.loadCache(cachefile
)
1125 data
.collect(gitpath
)
1126 print 'Refining data...'
1127 data
.saveCache(cachefile
)
1132 print 'Generating report...'
1133 report
= HTMLReportCreator()
1134 report
.create(data
, outputpath
)
1136 time_end
= time
.time()
1137 exectime_internal
= time_end
- time_start
1138 print 'Execution time %.5f secs, %.5f secs (%.2f %%) in external commands)' % (exectime_internal
, exectime_external
, (100.0 * exectime_external
) / exectime_internal
)