Created
July 30, 2015 03:58
-
-
Save informationsea/e25ef6336c2b4c9d6487 to your computer and use it in GitHub Desktop.
Git Contribution Analysis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import argparse | |
import git | |
import os | |
import os.path | |
import collections | |
VERBOSE = False | |
def _main(): | |
parser = argparse.ArgumentParser(description="Git repository contribution analysis") | |
parser.add_argument('repository') | |
parser.add_argument('-s', '--subdirectory', default=None) | |
parser.add_argument('-v', action='store_true') | |
parser.add_argument('-x', '--suffix', nargs='+') | |
options = parser.parse_args() | |
repo = git.Repo(options.repository) | |
headcommit = repo.head.commit | |
global VERBOSE | |
VERBOSE = options.v | |
#print repo | |
#print repr(headcommit) | |
print '>> Commit base contribution' | |
commit_contribution = collections.defaultdict(int) | |
for one in repo.iter_commits(): | |
commit_contribution[one.committer.email] += 1 | |
for k, v in commit_contribution.iteritems(): | |
print k, v | |
print '>> Line base contribution' | |
for k, v in treeanalysis(repo, headcommit, headcommit.tree, options.subdirectory, options.suffix).iteritems(): | |
print k, v | |
def treeanalysis(repo, ref, tree, subdirectory=None, suffix=None, root='', result=collections.defaultdict(int)): | |
for one in tree: | |
if isinstance(one, git.Tree): | |
treeanalysis(repo, ref, one, subdirectory, suffix, os.path.join(root, one.name), result) | |
else: | |
if subdirectory and not root.startswith(subdirectory): continue | |
if suffix: | |
if not any([one.name.endswith(x) for x in suffix]): | |
continue | |
linenum = 0 | |
for v in repo.blame(ref, os.path.join(root, one.name)): | |
result[v[0].committer.email] += len(v[1]) | |
linenum += len(v[1]) | |
if VERBOSE: | |
print os.path.join(root, one.name), one.mime_type, linenum | |
return result | |
if __name__ == '__main__': | |
_main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment