Skip to content

Instantly share code, notes, and snippets.

@objarni
Last active April 25, 2017 14:42
Show Gist options
  • Save objarni/d473301b7bef324b00ad784b64d445c0 to your computer and use it in GitHub Desktop.
Save objarni/d473301b7bef324b00ad784b64d445c0 to your computer and use it in GitHub Desktop.
authorship.py
#!/usr/bin/env python
import os
import re
from collections import defaultdict
import subprocess
author_line_count = defaultdict(lambda: 0)
email_re = re.compile(r'<.*?>')
devnull = open(os.devnull, 'w')
def git_blame(path):
process = subprocess.Popen(['git', 'blame', '-e', path], stdout=subprocess.PIPE, stderr=devnull)
out, err = process.communicate()
return out.splitlines()
for root, dirs, files in os.walk(".", topdown=False):
for name in files:
ext = os.path.splitext(name)[1]
if ext in ['.py', '.h', '.cpp', '.c', '.ino']:
for row in git_blame(os.path.join(root, name)):
m = email_re.search(row)
if m:
email = m.group(0)
author_line_count[email] += 1
for key, value in sorted(author_line_count.iteritems(), key=lambda (k,v): (-v,k)):
print "%s: %s" % (key, value)
@objarni
Copy link
Author

objarni commented Apr 17, 2017

git log --pretty=%H

... gives list of all commit on current branch since inital commit. Could be used to determine authorship throughout repository history.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment