Skip to content

Instantly share code, notes, and snippets.

@koverholt
Last active May 6, 2016 16:05
Show Gist options
  • Save koverholt/ddcb4a3d67b99dd22de8f61d8212d922 to your computer and use it in GitHub Desktop.
Save koverholt/ddcb4a3d67b99dd22de8f61d8212d922 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
Display the per-commit size of a file in a git repository.
$ python counter.py setup.py
82e2452, 2016-02-23T23:57:25-05:00, 14 lines
94e26ae, 2016-02-24T12:01:39-06:00, 15 lines
26e51c5, 2016-03-07T13:35:19-06:00, 19 lines
fed0eb8, 2016-03-15T15:03:36-05:00, 18 lines
8b996ee, 2016-03-22T13:45:43-04:00, 18 lines
6c1c996, 2016-03-22T20:04:41-04:00, 53 lines
336f19e, 2016-04-07T15:01:09-05:00, 43 lines
ee9bd08, 2016-04-25T13:30:58-04:00, 41 lines
"""
import subprocess
import re
import sys
def git_line_count(fn):
git = subprocess.Popen(["git", "log", "--shortstat", "--reverse",
"--pretty=format:%h %aI", fn],
stdout=subprocess.PIPE)
out, err = git.communicate()
out = out.decode('ascii')
line_count = 0
for line in out.split('\n'):
if not line.strip():
continue
if line[0] != ' ':
hash, time = line.split(' ')
elif 'file' in line:
try:
insertions = int(re.findall(r'(\d+) insertion[\w]?\(\+\)', line)[0])
line_count += insertions
except IndexError:
pass
try:
deletions = int(re.findall(r'(\d+) deletion[\w]?\(\-\)', line)[0])
line_count -= deletions
except IndexError:
pass
print("%s, %s, %d lines" % (hash, time, line_count))
if __name__ == '__main__':
fn = sys.argv[1]
sys.exit(git_line_count(fn))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment