123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. class DataCollector:
  10. def __init__(self):
  11. pass
  12. ##
  13. # This should be the main function to extract data from the repository.
  14. def collect(self, dir):
  15. self.dir = dir
  16. ##
  17. # TODO: get a dictionary of author
  18. def getAuthorInfo(self, author):
  19. return None
  20. ##
  21. # Get a list of authors
  22. def getAuthors(self):
  23. return []
  24. def getFirstCommitDate(self):
  25. return datetime.datetime.now()
  26. def getLastCommitDate(self):
  27. return datetime.datetime.now()
  28. def getTotalAuthors(self):
  29. return -1
  30. def getTotalCommits(self):
  31. return -1
  32. def getTotalFiles(self):
  33. return -1
  34. def getTotalLOC(self):
  35. return -1
  36. class GitDataCollector(DataCollector):
  37. def collect(self, dir):
  38. DataCollector.collect(self, dir)
  39. self.total_authors = int(commands.getoutput('git-log |git-shortlog -s |wc -l'))
  40. self.total_commits = int(commands.getoutput('git-rev-list --all |wc -l'))
  41. self.total_files = int(commands.getoutput('git-ls-files |wc -l'))
  42. self.total_lines = int(commands.getoutput('git-ls-files |xargs cat |wc -l'))
  43. def getAuthorInfo(self, author):
  44. commits = int(commands.getoutput('git-rev-list --all --author="%s" |wc -l' % author))
  45. commits_frac = (100 * float(commits)) / self.getTotalCommits()
  46. date_first = '0000-00-00'
  47. date_last = '0000-00-00'
  48. rev_last = commands.getoutput('git-rev-list --all --author="%s" -n 1' % author)
  49. rev_first = commands.getoutput('git-rev-list --all --author="%s" |tail -n 1' % author)
  50. date_first = self.revToDate(rev_first)
  51. date_last = self.revToDate(rev_last)
  52. res = { 'commits': commits, 'commits_frac': commits_frac, 'date_first': date_first, 'date_last': date_last }
  53. return res
  54. def getAuthors(self):
  55. lines = commands.getoutput('git-rev-list --all --pretty=format:%an |grep -v ^commit |sort |uniq')
  56. return lines.split('\n')
  57. def getTotalAuthors(self):
  58. return self.total_authors
  59. def getTotalCommits(self):
  60. return self.total_commits
  61. def getTotalFiles(self):
  62. return self.total_files
  63. def getTotalLOC(self):
  64. return self.total_lines
  65. def revToDate(self, rev):
  66. stamp = int(commands.getoutput('git-log --pretty=format:%%at "%s" -n 1' % rev))
  67. return datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m-%d')
  68. class ReportCreator:
  69. def __init__(self):
  70. pass
  71. def create(self, data, path):
  72. self.data = data
  73. self.path = path
  74. class HTMLReportCreator(ReportCreator):
  75. def create(self, data, path):
  76. ReportCreator.create(self, data, path)
  77. f = open(path + "/index.html", 'w')
  78. f.write("""<html>
  79. <head>
  80. <title>StatGit</title>
  81. </head>
  82. <body>
  83. """)
  84. format = '%Y-%m-%d %H:%m:%S'
  85. f.write('<h1>StatGit</h1>')
  86. f.write('<dl>');
  87. f.write('<dt>Generated</dt><dd>%s</dd>' % datetime.datetime.now().strftime(format));
  88. f.write('<dt>Report Period</dt><dd>%s to %s</dd>' % (data.getFirstCommitDate().strftime(format), data.getLastCommitDate().strftime(format)))
  89. f.write('<dt>Total Files</dt><dd>%s</dd>' % data.getTotalFiles())
  90. f.write('<dt>Total Lines of Code</dt><dd>%s</dd>' % data.getTotalLOC())
  91. f.write('<dt>Total Commits</dt><dd>%s</dd>' % data.getTotalCommits())
  92. f.write('<dt>Authors</dt><dd>%s</dd>' % data.getTotalAuthors())
  93. f.write('</dl>');
  94. f.write('<h2>Authors</h2>')
  95. f.write('<table class="authors">')
  96. f.write('<tr><th>Author</th><th>Commits (%)</th><th>First commit</th><th>Last commit</th></tr>')
  97. for author in data.getAuthors():
  98. info = data.getAuthorInfo(author)
  99. 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']))
  100. f.write('</table>')
  101. f.write('<h2>Tags</h2>')
  102. f.write('<table>')
  103. f.write('<tr><th>Name</th><th>Date</th><th>Developers</th></tr>')
  104. f.write('</table>')
  105. f.write('</body>\n</html>');
  106. f.close()
  107. pass
  108. usage = """
  109. Usage: statgit [options] <gitpath> <outputpath>
  110. Options:
  111. -o html
  112. """
  113. if len(sys.argv) < 3:
  114. print usage
  115. sys.exit(0)
  116. gitpath = sys.argv[1]
  117. outputpath = sys.argv[2]
  118. print 'Git path: %s' % gitpath
  119. print 'Output path: %s' % outputpath
  120. os.chdir(gitpath)
  121. print 'Collecting data...'
  122. data = GitDataCollector()
  123. data.collect(gitpath)
  124. print 'Generating report...'
  125. report = HTMLReportCreator()
  126. report.create(data, outputpath)