Skip to content

Instantly share code, notes, and snippets.

@objarni
Created April 25, 2017 14:42
Show Gist options
  • Save objarni/d032f1b875b20c91fc222fd541593439 to your computer and use it in GitHub Desktop.
Save objarni/d032f1b875b20c91fc222fd541593439 to your computer and use it in GitHub Desktop.
#!/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(str(row))
if m:
email = m.group(0)
author_line_count[email] += 1
def sorter(kv):
(k, v) = kv
return (-v,k)
for key, value in sorted(author_line_count.items(), key=sorter):
print("%s: %s" % (key, value))
@Neppord
Copy link

Neppord commented Jun 25, 2017

git rev-list master | 
while read hash ; do 
  echo $hash 
 { 
   git ls-tree --name-only -r $hash |
   xargs -n1 git blame --line-porcelain $hash --
 } | 
 sed -n 's/author-mail //p' | 
 sort |
 uniq -c |
 sort -r 
done

@Neppord
Copy link

Neppord commented Jun 25, 2017

i could not resist putting the result in a sqlite database, in a oneline.

sqlite3 db.db "CREATE TABLE IF NOT EXISTS author_score (sha, lines, email)" ".separator ' '" ".import "<(git log --pretty="%h" master | while read hash ; do { git ls-tree --name-only -r $hash | xargs -n1 git blame --line-porcelain $hash -- ; } | sed -n 's/author-mail //p' | sort | uniq -c | sort -r | xargs -n2 echo $hash  ; done)" author_score"

ofc the last sort is probably un needed, but i don't dare to edit this monster ;).

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