123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. #!/usr/bin/python
  2. # Copyright (c) 2007 Heikki Hokkanen <hoxu@users.sf.net>
  3. # GPLv2
  4. import commands
  5. import datetime
  6. import os
  7. import re
  8. import sys
  9. def getoutput(cmd):
  10. print '>> %s' % cmd
  11. output = commands.getoutput(cmd)
  12. return output
  13. def getkeyssortedbyvalues(dict):
  14. return map(lambda el : el[1], sorted(map(lambda el : (el[1], el[0]), dict.items())))
  15. class DataCollector:
  16. def __init__(self):
  17. pass
  18. ##
  19. # This should be the main function to extract data from the repository.
  20. def collect(self, dir):
  21. self.dir = dir
  22. ##
  23. # : get a dictionary of author
  24. def getAuthorInfo(self, author):
  25. return None
  26. def getActivityByDayOfWeek(self):
  27. return {}
  28. def getActivityByHourOfDay(self):
  29. return {}
  30. ##
  31. # Get a list of authors
  32. def getAuthors(self):
  33. return []
  34. def getFirstCommitDate(self):
  35. return datetime.datetime.now()
  36. def getLastCommitDate(self):
  37. return datetime.datetime.now()
  38. def getTags(self):
  39. return []
  40. def getTotalAuthors(self):
  41. return -1
  42. def getTotalCommits(self):
  43. return -1
  44. def getTotalFiles(self):
  45. return -1
  46. def getTotalLOC(self):
  47. return -1
  48. class GitDataCollector(DataCollector):
  49. def collect(self, dir):
  50. DataCollector.collect(self, dir)
  51. self.total_authors = int(getoutput('git-log |git-shortlog -s |wc -l'))
  52. self.total_commits = int(getoutput('git-rev-list HEAD |wc -l'))
  53. self.total_files = int(getoutput('git-ls-files |wc -l'))
  54. self.total_lines = int(getoutput('git-ls-files |xargs cat |wc -l'))
  55. self.activity_by_hour_of_day = {} # hour -> commits
  56. self.activity_by_day_of_week = {} # day -> commits
  57. # activity
  58. lines = getoutput('git-rev-list HEAD --pretty=format:%at |grep -v ^commit').split('\n')
  59. for stamp in lines:
  60. date = datetime.datetime.fromtimestamp(float(stamp))
  61. # hour
  62. hour = date.hour
  63. if hour in self.activity_by_hour_of_day:
  64. self.activity_by_hour_of_day[hour] += 1
  65. else:
  66. self.activity_by_hour_of_day[hour] = 1
  67. # day
  68. day = date.weekday()
  69. if day in self.activity_by_day_of_week:
  70. self.activity_by_day_of_week[day] += 1
  71. else:
  72. self.activity_by_day_of_week[day] = 1
  73. # TODO author of the month
  74. self.author_of_month = {} # month -> author -> commits
  75. self.author_of_year = {} # year -> author -> commits
  76. self.commits_by_month = {} # month -> commits
  77. self.first_commit_stamp = 0
  78. self.last_commit_stamp = 0
  79. lines = getoutput('git-rev-list --pretty=format:"%at %an" HEAD |grep -v ^commit').split('\n')
  80. for line in lines:
  81. parts = line.split(' ')
  82. stamp = int(parts[0])
  83. author = ' '.join(parts[1:])
  84. if self.last_commit_stamp == 0:
  85. self.last_commit_stamp = stamp
  86. self.first_commit_stamp = stamp
  87. yymm = datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m')
  88. if yymm in self.author_of_month:
  89. if author in self.author_of_month[yymm]:
  90. self.author_of_month[yymm][author] += 1
  91. else:
  92. self.author_of_month[yymm][author] = 1
  93. else:
  94. self.author_of_month[yymm] = {}
  95. self.author_of_month[yymm][author] = 1
  96. if yymm in self.commits_by_month:
  97. self.commits_by_month[yymm] += 1
  98. else:
  99. self.commits_by_month[yymm] = 1
  100. yy = datetime.datetime.fromtimestamp(stamp).year
  101. if yy in self.author_of_year:
  102. if author in self.author_of_year[yy]:
  103. self.author_of_year[yy][author] += 1
  104. else:
  105. self.author_of_year[yy][author] = 1
  106. else:
  107. self.author_of_year[yy] = {}
  108. self.author_of_year[yy][author] = 1
  109. def getActivityByDayOfWeek(self):
  110. return self.activity_by_day_of_week
  111. def getActivityByHourOfDay(self):
  112. return self.activity_by_hour_of_day
  113. def getAuthorInfo(self, author):
  114. commits = int(getoutput('git-rev-list HEAD --author="%s" |wc -l' % author))
  115. commits_frac = (100 * float(commits)) / self.getTotalCommits()
  116. date_first = '0000-00-00'
  117. date_last = '0000-00-00'
  118. rev_last = getoutput('git-rev-list --all --author="%s" -n 1' % author)
  119. rev_first = getoutput('git-rev-list --all --author="%s" |tail -n 1' % author)
  120. date_first = self.revToDate(rev_first)
  121. date_last = self.revToDate(rev_last)
  122. res = { 'commits': commits, 'commits_frac': commits_frac, 'date_first': date_first, 'date_last': date_last }
  123. return res
  124. def getAuthors(self):
  125. lines = getoutput('git-rev-list --all --pretty=format:%an |grep -v ^commit |sort |uniq')
  126. return lines.split('\n')
  127. def getFirstCommitDate(self):
  128. return datetime.datetime.fromtimestamp(self.first_commit_stamp)
  129. def getLastCommitDate(self):
  130. return datetime.datetime.fromtimestamp(self.last_commit_stamp)
  131. def getTags(self):
  132. lines = getoutput('git-show-ref --tags |cut -d/ -f3')
  133. return lines.split('\n')
  134. def getTagDate(self, tag):
  135. return self.revToDate('tags/' + tag)
  136. def getTotalAuthors(self):
  137. return self.total_authors
  138. def getTotalCommits(self):
  139. return self.total_commits
  140. def getTotalFiles(self):
  141. return self.total_files
  142. def getTotalLOC(self):
  143. return self.total_lines
  144. def revToDate(self, rev):
  145. stamp = int(getoutput('git-log --pretty=format:%%at "%s" -n 1' % rev))
  146. return datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m-%d')
  147. class ReportCreator:
  148. def __init__(self):
  149. pass
  150. def create(self, data, path):
  151. self.data = data
  152. self.path = path
  153. class HTMLReportCreator(ReportCreator):
  154. def create(self, data, path):
  155. ReportCreator.create(self, data, path)
  156. f = open(path + "/index.html", 'w')
  157. format = '%Y-%m-%d %H:%m:%S'
  158. self.printHeader(f)
  159. f.write('<h1>StatGit</h1>')
  160. f.write('<dl>');
  161. f.write('<dt>Generated</dt><dd>%s</dd>' % datetime.datetime.now().strftime(format));
  162. f.write('<dt>Report Period</dt><dd>%s to %s</dd>' % (data.getFirstCommitDate().strftime(format), data.getLastCommitDate().strftime(format)))
  163. f.write('<dt>Total Files</dt><dd>%s</dd>' % data.getTotalFiles())
  164. f.write('<dt>Total Lines of Code</dt><dd>%s</dd>' % data.getTotalLOC())
  165. f.write('<dt>Total Commits</dt><dd>%s</dd>' % data.getTotalCommits())
  166. f.write('<dt>Authors</dt><dd>%s</dd>' % data.getTotalAuthors())
  167. f.write('</dl>');
  168. f.write("""<ul>
  169. <li><a href="activity.html">Activity</a></li>
  170. <li><a href="authors.html">Authors</a></li>
  171. <li><a href="files.html">Files</a></li>
  172. <li><a href="lines.html">Lines</a></li>
  173. </ul>
  174. """)
  175. f.write('<h2>Tags</h2>')
  176. f.write('<table>')
  177. f.write('<tr><th>Name</th><th>Date</th><th>Developers</th></tr>')
  178. for tag in data.getTags():
  179. f.write('<tr><td>%s</td><td></td></tr>' % tag)
  180. f.write('</table>')
  181. f.write('</body>\n</html>');
  182. f.close()
  183. # activity.html
  184. f = open(path + '/activity.html', 'w')
  185. self.printHeader(f)
  186. f.write('<h1>Activity</h1>')
  187. f.write('<h2>Last 30 days</h2>')
  188. f.write('<h2>Last 12 months</h2>')
  189. f.write('\n<h2>Hour of Day</h2>\n\n')
  190. hour_of_day = data.getActivityByHourOfDay()
  191. f.write('<table><tr><th>Hour</th>')
  192. for i in range(1, 25):
  193. f.write('<th>%d</th>' % i)
  194. f.write('</tr>\n<tr><th>Commits</th>')
  195. for i in range(0, 24):
  196. if i in hour_of_day:
  197. f.write('<td>%d</td>' % hour_of_day[i])
  198. else:
  199. f.write('<td>0</td>')
  200. f.write('</tr>\n<tr><th>%</th>')
  201. totalcommits = data.getTotalCommits()
  202. for i in range(0, 24):
  203. if i in hour_of_day:
  204. f.write('<td>%.2f</td>' % ((100.0 * hour_of_day[i]) / totalcommits))
  205. else:
  206. f.write('<td>0.00</td>')
  207. f.write('</tr></table>')
  208. ### Day of Week
  209. # TODO show also by hour of weekday?
  210. f.write('\n<h2>Day of Week</h2>\n\n')
  211. day_of_week = data.getActivityByDayOfWeek()
  212. f.write('<table>')
  213. f.write('<tr><th>Day</th><th>Total (%)</th></tr>')
  214. for d in range(0, 7):
  215. f.write('<tr>')
  216. f.write('<th>%d</th>' % (d + 1))
  217. if d in day_of_week:
  218. f.write('<td>%d (%.2f%%)</td>' % (day_of_week[d], (100.0 * day_of_week[d]) / totalcommits))
  219. else:
  220. f.write('<td>0</td>')
  221. f.write('</tr>')
  222. f.write('</table>')
  223. f.close()
  224. # authors.html
  225. f = open(path + '/authors.html', 'w')
  226. self.printHeader(f)
  227. f.write('<h1>Authors</h1>')
  228. f.write('\n<h2>List of authors</h2>\n\n')
  229. f.write('<table class="authors">')
  230. f.write('<tr><th>Author</th><th>Commits (%)</th><th>First commit</th><th>Last commit</th></tr>')
  231. for author in data.getAuthors():
  232. info = data.getAuthorInfo(author)
  233. 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']))
  234. f.write('</table>')
  235. f.write('\n<h2>Author of Month</h2>\n\n')
  236. f.write('<table>')
  237. f.write('<tr><th>Month</th><th>Author</th><th>Commits (%)</th></tr>')
  238. for yymm in reversed(sorted(data.author_of_month.keys())):
  239. authordict = data.author_of_month[yymm]
  240. authors = getkeyssortedbyvalues(authordict)
  241. authors.reverse()
  242. commits = data.author_of_month[yymm][authors[0]]
  243. 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]))
  244. f.write('</table>')
  245. f.write('\n<h2>Author of Year</h2>\n\n')
  246. f.write('<table><tr><th>Year</th><th>Author</th><th>Commits (%)</th></tr>')
  247. for yy in reversed(sorted(data.author_of_year.keys())):
  248. authordict = data.author_of_year[yy]
  249. authors = getkeyssortedbyvalues(authordict)
  250. authors.reverse()
  251. commits = data.author_of_year[yy][authors[0]]
  252. f.write('<tr><td>%s</td><td>%s</td><td>%d</td></tr>' % (yy, authors[0], commits))
  253. f.write('</table>')
  254. f.write('</body></html>')
  255. f.close()
  256. pass
  257. def printHeader(self, f):
  258. f.write("""<html>
  259. <head>
  260. <title>StatGit</title>
  261. <link rel="stylesheet" href="statgit.css" type="text/css" />
  262. </head>
  263. <body>
  264. """)
  265. usage = """
  266. Usage: statgit [options] <gitpath> <outputpath>
  267. Options:
  268. -o html
  269. """
  270. if len(sys.argv) < 3:
  271. print usage
  272. sys.exit(0)
  273. gitpath = sys.argv[1]
  274. outputpath = sys.argv[2]
  275. print 'Git path: %s' % gitpath
  276. print 'Output path: %s' % outputpath
  277. os.chdir(gitpath)
  278. print 'Collecting data...'
  279. data = GitDataCollector()
  280. data.collect(gitpath)
  281. print 'Generating report...'
  282. report = HTMLReportCreator()
  283. report.create(data, outputpath)