Skip to content

Instantly share code, notes, and snippets.

@williame
Created September 12, 2011 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save williame/1211071 to your computer and use it in GitHub Desktop.
Save williame/1211071 to your computer and use it in GitHub Desktop.
git churn script
#!/usr/bin/env python
import sys, datetime, subprocess
churn = {} # lines per date
# get a list of tracked files
git = subprocess.Popen(("git","ls-files"),stdout=subprocess.PIPE)
files = git.communicate()[0].strip().split("\n")
if git.returncode: sys.exit(1)
# count them
for f in files:
# get the blame
git = subprocess.Popen(("git","blame","-c",f),stdout=subprocess.PIPE)
blame = git.communicate()[0].strip().split("\n")
if git.returncode: sys.exit(1)
for line in blame:
# split it up
sha1,user,when,line = line.split("\t",3)
line = line.split(")",1)[1].strip()
if line == "": continue # empty lines? ignore
y,m,d = when.partition(" ")[0].split("-")
when = datetime.date(int(y),int(m),int(d))
# remember it
if when not in churn: churn[when] = 0
churn[when] += 1
# show it
for when in sorted(churn.keys()):
print when,churn[when]
print len(files),"files,",sum(churn.values()),"lines"
# work out how many in last year
pivot = datetime.date.today() - datetime.timedelta(365)
print sum(lines for when,lines in churn.iteritems() if when > pivot),"in the last year"
# get the current branch name
git = subprocess.Popen(("git","name-rev","--name-only","HEAD"),stdout=subprocess.PIPE)
branch = git.communicate()[0].strip()
if git.returncode: sys.exit(1)
# now go back to a year ago
git = subprocess.Popen(("git","rev-list","-n","1","--before=\"%04d-%02d-%02d\""%(pivot.year,pivot.month,pivot.day),"HEAD"),stdout=subprocess.PIPE)
pivot_commit = long(git.communicate()[0].strip(),16)
if git.returncode: sys.exit(1)
if subprocess.call(("git","checkout","-q","%x"%pivot_commit)): sys.exit(1)
# get a list of tracked files then
git = subprocess.Popen(("git","ls-files"),stdout=subprocess.PIPE)
files = git.communicate()[0].strip().split("\n")
if git.returncode: sys.exit(1)
# and count the lines
lines = 0
for f in files:
lines += len(file(f).readlines())
print lines,"lines then"
# done
subprocess.call(("git","checkout",branch))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment