Skip to content

Instantly share code, notes, and snippets.

@zbuc
Created October 11, 2012 17:50
Show Gist options
  • Select an option

  • Save zbuc/3874268 to your computer and use it in GitHub Desktop.

Select an option

Save zbuc/3874268 to your computer and use it in GitHub Desktop.
pony
import collections
# NOTE requires GitPython>=0.3.2.RC1 for 'blame' support
from git import Repo
def autodict():
return collections.defaultdict(autodict)
repo = Repo('/path/to/repo')
line_counts = autodict()
for f in repo.tree().traverse():
if f.type == 'blob':
try:
for com in repo.blame('master', f.path):
if com[0]:
pointer = line_counts
# vivify to filepath
for part in f.path.split('/'):
pointer = pointer[part]
if not pointer[com[0].committer.name]:
pointer[com[0].committer.name] = len(com[1])
else:
pointer[com[0].committer.name] += len(com[1])
except AttributeError:
# AttributeError: 'NoneType' object has no attribute 'groups' git/repo/base.py", line 625, in blame
pass
# now calculate sub-tree aggregate line counts for every node in the tree
def aggregate(pointer, prev_pointer=None, fpath=''):
if type(collections.defaultdict()) == type(pointer):
# pointer is on a directory with subchildren
for k in pointer.keys():
if type(collections.defaultdict()) == type(pointer[k]):
if fpath:
aggregate(pointer[k], pointer, fpath + "/" + k)
else:
aggregate(pointer[k], pointer, k)
else:
aggregate(pointer[k], pointer, fpath)
else:
# pointer is at the end of the line -- int
if prev_pointer:
print fpath
for k in prev_pointer.keys():
print k, "-", prev_pointer[k], "lines total"
print "\n"
aggregate(line_counts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment