2 # Copyright (c) 2007 Heikki Hokkanen <hoxu@users.sf.net>
12 output
= commands
.getoutput(cmd
)
20 # This should be the main function to extract data from the repository.
21 def collect(self
, dir):
25 # : get a dictionary of author
26 def getAuthorInfo(self
, author
):
30 # Get a list of authors
34 def getFirstCommitDate(self
):
35 return datetime
.datetime
.now()
37 def getLastCommitDate(self
):
38 return datetime
.datetime
.now()
43 def getTotalAuthors(self
):
46 def getTotalCommits(self
):
49 def getTotalFiles(self
):
52 def getTotalLOC(self
):
55 class GitDataCollector(DataCollector
):
56 def collect(self
, dir):
57 DataCollector
.collect(self
, dir)
59 self
.total_authors
= int(getoutput('git-log |git-shortlog -s |wc -l'))
60 self
.total_commits
= int(getoutput('git-rev-list --all |wc -l'))
61 self
.total_files
= int(getoutput('git-ls-files |wc -l'))
62 self
.total_lines
= int(getoutput('git-ls-files |xargs cat |wc -l'))
64 def getAuthorInfo(self
, author
):
65 commits
= int(getoutput('git-rev-list --all --author="%s" |wc -l' % author
))
66 commits_frac
= (100 * float(commits
)) / self
.getTotalCommits()
67 date_first
= '0000-00-00'
68 date_last
= '0000-00-00'
69 rev_last
= getoutput('git-rev-list --all --author="%s" -n 1' % author
)
70 rev_first
= getoutput('git-rev-list --all --author="%s" |tail -n 1' % author
)
71 date_first
= self
.revToDate(rev_first
)
72 date_last
= self
.revToDate(rev_last
)
74 res
= { 'commits': commits
, 'commits_frac': commits_frac
, 'date_first': date_first
, 'date_last': date_last
}
78 lines
= getoutput('git-rev-list --all --pretty=format:%an |grep -v ^commit |sort |uniq')
79 return lines
.split('\n')
82 lines
= getoutput('git-show-ref --tags |cut -d/ -f3')
83 return lines
.split('\n')
85 def getTagDate(self
, tag
):
86 return self
.revToDate('tags/' + tag
)
88 def getTotalAuthors(self
):
89 return self
.total_authors
91 def getTotalCommits(self
):
92 return self
.total_commits
94 def getTotalFiles(self
):
95 return self
.total_files
97 def getTotalLOC(self
):
98 return self
.total_lines
100 def revToDate(self
, rev
):
101 stamp
= int(getoutput('git-log --pretty=format:%%at "%s" -n 1' % rev
))
102 return datetime
.datetime
.fromtimestamp(stamp
).strftime('%Y-%m-%d')
108 def create(self
, data
, path
):
112 class HTMLReportCreator(ReportCreator
):
113 def create(self
, data
, path
):
114 ReportCreator
.create(self
, data
, path
)
116 f
= open(path
+ "/index.html", 'w')
119 <title>StatGit</title>
120 <link rel="stylesheet" href="statgit.css" type="text/css" />
124 format
= '%Y-%m-%d %H:%m:%S'
126 f
.write('<h1>StatGit</h1>')
129 f
.write('<dt>Generated</dt><dd>%s</dd>' % datetime
.datetime
.now().strftime(format
));
130 f
.write('<dt>Report Period</dt><dd>%s to %s</dd>' % (data
.getFirstCommitDate().strftime(format
), data
.getLastCommitDate().strftime(format
)))
131 f
.write('<dt>Total Files</dt><dd>%s</dd>' % data
.getTotalFiles())
132 f
.write('<dt>Total Lines of Code</dt><dd>%s</dd>' % data
.getTotalLOC())
133 f
.write('<dt>Total Commits</dt><dd>%s</dd>' % data
.getTotalCommits())
134 f
.write('<dt>Authors</dt><dd>%s</dd>' % data
.getTotalAuthors())
138 <li><a href="activity.html">Activity</a></li>
139 <li><a href="authors.html">Authors</a></li>
140 <li><a href="files.html">Files</a></li>
144 f
.write('<h2>Authors</h2>')
146 f
.write('<table class="authors">')
147 f
.write('<tr><th>Author</th><th>Commits (%)</th><th>First commit</th><th>Last commit</th></tr>')
148 for author
in data
.getAuthors():
149 info
= data
.getAuthorInfo(author
)
150 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']))
153 f
.write('<h2>Tags</h2>')
155 f
.write('<tr><th>Name</th><th>Date</th><th>Developers</th></tr>')
156 for tag
in data
.getTags():
157 f
.write('<tr><td>%s</td><td></td></tr>' % tag
)
160 f
.write('</body>\n</html>');
165 Usage: statgit [options] <gitpath> <outputpath>
171 if len(sys
.argv
) < 3:
175 gitpath
= sys
.argv
[1]
176 outputpath
= sys
.argv
[2]
178 print 'Git path: %s' % gitpath
179 print 'Output path: %s' % outputpath
183 print 'Collecting data...'
184 data
= GitDataCollector()
185 data
.collect(gitpath
)
187 print 'Generating report...'
188 report
= HTMLReportCreator()
189 report
.create(data
, outputpath
)