statgit 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. lines = getoutput('git-rev-list --pretty=format:"%at %an" HEAD |grep -v ^commit').split('\n')
  78. for line in lines:
  79. parts = line.split(' ')
  80. stamp = int(parts[0])
  81. author = ' '.join(parts[1:])
  82. yymm = datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m')
  83. if yymm in self.author_of_month:
  84. if author in self.author_of_month[yymm]:
  85. self.author_of_month[yymm][author] += 1
  86. else:
  87. self.author_of_month[yymm][author] = 1
  88. else:
  89. self.author_of_month[yymm] = {}
  90. self.author_of_month[yymm][author] = 1
  91. if yymm in self.commits_by_month:
  92. self.commits_by_month[yymm] += 1
  93. else:
  94. self.commits_by_month[yymm] = 1
  95. yy = datetime.datetime.fromtimestamp(stamp).year
  96. if yy in self.author_of_year:
  97. if author in self.author_of_year[yy]:
  98. self.author_of_year[yy][author] += 1
  99. else:
  100. self.author_of_year[yy][author] = 1
  101. else:
  102. self.author_of_year[yy] = {}
  103. self.author_of_year[yy][author] = 1
  104. print self.author_of_month
  105. def getActivityByDayOfWeek(self):
  106. return self.activity_by_day_of_week
  107. def getActivityByHourOfDay(self):
  108. return self.activity_by_hour_of_day
  109. def getAuthorInfo(self, author):
  110. commits = int(getoutput('git-rev-list HEAD --author="%s" |wc -l' % author))
  111. commits_frac = (100 * float(commits)) / self.getTotalCommits()
  112. date_first = '0000-00-00'
  113. date_last = '0000-00-00'
  114. rev_last = getoutput('git-rev-list --all --author="%s" -n 1' % author)
  115. rev_first = getoutput('git-rev-list --all --author="%s" |tail -n 1' % author)
  116. date_first = self.revToDate(rev_first)
  117. date_last = self.revToDate(rev_last)
  118. res = { 'commits': commits, 'commits_frac': commits_frac, 'date_first': date_first, 'date_last': date_last }
  119. return res
  120. def getAuthors(self):
  121. lines = getoutput('git-rev-list --all --pretty=format:%an |grep -v ^commit |sort |uniq')
  122. return lines.split('\n')
  123. def getTags(self):
  124. lines = getoutput('git-show-ref --tags |cut -d/ -f3')
  125. return lines.split('\n')
  126. def getTagDate(self, tag):
  127. return self.revToDate('tags/' + tag)
  128. def getTotalAuthors(self):
  129. return self.total_authors
  130. def getTotalCommits(self):
  131. return self.total_commits
  132. def getTotalFiles(self):
  133. return self.total_files
  134. def getTotalLOC(self):
  135. return self.total_lines
  136. def revToDate(self, rev):
  137. stamp = int(getoutput('git-log --pretty=format:%%at "%s" -n 1' % rev))
  138. return datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m-%d')
  139. class ReportCreator:
  140. def __init__(self):
  141. pass
  142. def create(self, data, path):
  143. self.data = data
  144. self.path = path
  145. class HTMLReportCreator(ReportCreator):
  146. def create(self, data, path):
  147. ReportCreator.create(self, data, path)
  148. f = open(path + "/index.html", 'w')
  149. format = '%Y-%m-%d %H:%m:%S'
  150. self.printHeader(f)
  151. f.write('<h1>StatGit</h1>')
  152. f.write('<dl>');
  153. f.write('<dt>Generated</dt><dd>%s</dd>' % datetime.datetime.now().strftime(format));
  154. f.write('<dt>Report Period</dt><dd>%s to %s</dd>' % (data.getFirstCommitDate().strftime(format), data.getLastCommitDate().strftime(format)))
  155. f.write('<dt>Total Files</dt><dd>%s</dd>' % data.getTotalFiles())
  156. f.write('<dt>Total Lines of Code</dt><dd>%s</dd>' % data.getTotalLOC())
  157. f.write('<dt>Total Commits</dt><dd>%s</dd>' % data.getTotalCommits())
  158. f.write('<dt>Authors</dt><dd>%s</dd>' % data.getTotalAuthors())
  159. f.write('</dl>');
  160. f.write("""<ul>
  161. <li><a href="activity.html">Activity</a></li>
  162. <li><a href="authors.html">Authors</a></li>
  163. <li><a href="files.html">Files</a></li>
  164. <li><a href="lines.html">Lines</a></li>
  165. </ul>
  166. """)
  167. f.write('<h2>Tags</h2>')
  168. f.write('<table>')
  169. f.write('<tr><th>Name</th><th>Date</th><th>Developers</th></tr>')
  170. for tag in data.getTags():
  171. f.write('<tr><td>%s</td><td></td></tr>' % tag)
  172. f.write('</table>')
  173. f.write('</body>\n</html>');
  174. f.close()
  175. # activity.html
  176. f = open(path + '/activity.html', 'w')
  177. self.printHeader(f)
  178. f.write('<h1>Activity</h1>')
  179. f.write('<h2>Last 30 days</h2>')
  180. f.write('<h2>Last 12 months</h2>')
  181. f.write('\n<h2>Hour of Day</h2>\n\n')
  182. hour_of_day = data.getActivityByHourOfDay()
  183. f.write('<table><tr><th>Hour</th>')
  184. for i in range(1, 25):
  185. f.write('<th>%d</th>' % i)
  186. f.write('</tr>\n<tr><th>Commits</th>')
  187. for i in range(0, 24):
  188. if i in hour_of_day:
  189. f.write('<td>%d</td>' % hour_of_day[i])
  190. else:
  191. f.write('<td>0</td>')
  192. f.write('</tr>\n<tr><th>%</th>')
  193. totalcommits = data.getTotalCommits()
  194. for i in range(0, 24):
  195. if i in hour_of_day:
  196. f.write('<td>%.2f</td>' % ((100.0 * hour_of_day[i]) / totalcommits))
  197. else:
  198. f.write('<td>0.00</td>')
  199. f.write('</tr></table>')
  200. ### Day of Week
  201. # TODO show also by hour of weekday?
  202. f.write('\n<h2>Day of Week</h2>\n\n')
  203. day_of_week = data.getActivityByDayOfWeek()
  204. f.write('<table>')
  205. f.write('<tr><th>Day</th><th>Total (%)</th></tr>')
  206. for d in range(0, 7):
  207. f.write('<tr>')
  208. f.write('<th>%d</th>' % (d + 1))
  209. if d in day_of_week:
  210. f.write('<td>%d (%.2f%%)</td>' % (day_of_week[d], (100.0 * day_of_week[d]) / totalcommits))
  211. else:
  212. f.write('<td>0</td>')
  213. f.write('</tr>')
  214. f.write('</table>')
  215. f.close()
  216. # authors.html
  217. f = open(path + '/authors.html', 'w')
  218. self.printHeader(f)
  219. f.write('<h1>Authors</h1>')
  220. f.write('\n<h2>List of authors</h2>\n\n')
  221. f.write('<table class="authors">')
  222. f.write('<tr><th>Author</th><th>Commits (%)</th><th>First commit</th><th>Last commit</th></tr>')
  223. for author in data.getAuthors():
  224. info = data.getAuthorInfo(author)
  225. 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']))
  226. f.write('</table>')
  227. f.write('\n<h2>Author of Month</h2>\n\n')
  228. f.write('<table>')
  229. f.write('<tr><th>Month</th><th>Author</th><th>Commits (%)</th></tr>')
  230. for yymm in reversed(sorted(data.author_of_month.keys())):
  231. authordict = data.author_of_month[yymm]
  232. authors = getkeyssortedbyvalues(authordict)
  233. authors.reverse()
  234. commits = data.author_of_month[yymm][authors[0]]
  235. 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]))
  236. f.write('</table>')
  237. f.write('\n<h2>Author of Year</h2>\n\n')
  238. f.write('<table><tr><th>Year</th><th>Author</th><th>Commits (%)</th></tr>')
  239. for yy in reversed(sorted(data.author_of_year.keys())):
  240. authordict = data.author_of_year[yy]
  241. authors = getkeyssortedbyvalues(authordict)
  242. authors.reverse()
  243. commits = data.author_of_year[yy][authors[0]]
  244. f.write('<tr><td>%s</td><td>%s</td><td>%d</td></tr>' % (yy, authors[0], commits))
  245. f.write('</table>')
  246. f.write('</body></html>')
  247. f.close()
  248. pass
  249. def printHeader(self, f):
  250. f.write("""<html>
  251. <head>
  252. <title>StatGit</title>
  253. <link rel="stylesheet" href="statgit.css" type="text/css" />
  254. </head>
  255. <body>
  256. """)
  257. usage = """
  258. Usage: statgit [options] <gitpath> <outputpath>
  259. Options:
  260. -o html
  261. """
  262. if len(sys.argv) < 3:
  263. print usage
  264. sys.exit(0)
  265. gitpath = sys.argv[1]
  266. outputpath = sys.argv[2]
  267. print 'Git path: %s' % gitpath
  268. print 'Output path: %s' % outputpath
  269. os.chdir(gitpath)
  270. print 'Collecting data...'
  271. data = GitDataCollector()
  272. data.collect(gitpath)
  273. print 'Generating report...'
  274. report = HTMLReportCreator()
  275. report.create(data, outputpath)