Skip to content

Instantly share code, notes, and snippets.

@AJamesPhillips
Last active March 30, 2017 13:15
Show Gist options
  • Save AJamesPhillips/0ccd99c1d1841a8a66db4b4647a30512 to your computer and use it in GitHub Desktop.
Save AJamesPhillips/0ccd99c1d1841a8a66db4b4647a30512 to your computer and use it in GitHub Desktop.
Display the number of additions and deletions per commit for a file
#!/usr/bin/env python3
"""
Display the number of additions and deletions per commit for a file e.g.
$ ./git_helper.py index.d.ts
commit added deleted
bd1de57 4 4 index.d.ts Error object fix
c3a54db 25 120 index.d.ts Merge branch 'master' into types-2.0
e9a0e98 58 10 index.d.ts Merge commit 'upstream/master~300' into merge_7_25
"""
import sys
import subprocess
file_path = sys.argv[1] # e.g. 'index.d.ts'
command = 'git log --oneline --format="%h %s" {}'.format(file_path)
subproc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
commits = subproc.stdout.read().decode("utf-8")
commit_lines = commits.split('\n')[:-1]
hashes = []
messages = []
for entry in commit_lines:
hashes.append(entry.split(' ')[0])
messages.append(entry[8:])
print('commit added deleted')
for i, hash in enumerate(hashes):
command = 'git diff --numstat {hash}~1 {hash} {file_path}'.format(hash=hash, file_path=file_path)
subproc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
changes = subproc.stdout.read().decode("utf-8")[:-1]
print(hash + ' ' + changes + ' ' + messages[i])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment