Skip to content

Instantly share code, notes, and snippets.

@5263
Created February 1, 2014 13:42
Show Gist options
  • Save 5263/8752519 to your computer and use it in GitHub Desktop.
Save 5263/8752519 to your computer and use it in GitHub Desktop.
git blame on git diff results
#!/usr/bin/env python
"""usage: diffblame.py START..END [path]"""
import os,sys,subprocess
gitpath = subprocess.Popen(["git", "rev-parse","--show-toplevel"],\
stdout=subprocess.PIPE).communicate()[0].strip()
diff = subprocess.Popen(["git", "diff","-U0"]+sys.argv[1:],\
stdout=subprocess.PIPE).communicate()[0]
for l in diff.splitlines():
if l=='':
break
if l.startswith('+++'):
filename= os.path.join(gitpath,l[6:])
if l.startswith('@@'):
l2=l[3:l.find('@@',4)]
to=l2[l2.find('+')+1:].strip()
if ',' in to:
to=',+'.join(to.split(','))
else:
to='%s,%s' % (to,to)
print filename, '-L', to
blame = subprocess.Popen(["git", "blame","-L %s" % to,sys.argv[1],\
'--',filename],stdout=subprocess.PIPE).communicate()[0]
print blame
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment