Created
February 20, 2014 21:52
-
-
Save jrk/9124049 to your computer and use it in GitHub Desktop.
Count words changed in plain text by parsing color codes in git diff --color-words output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys, re | |
import datetime | |
from subprocess import Popen, PIPE | |
rev_cmd = 'git rev-list -n1 --before="%s" master' | |
diff_cmd = 'git diff --color-words $(%s) -- *.md' % rev_cmd # FIXME: hard coded to markdown files | |
def count_changed(stream): | |
total_added, total_removed = 0,0 | |
for line in stream: | |
added = re.findall("\x1b\[32m([^\x1b]+)\x1b\[m", line) | |
removed = re.findall("\x1b\[31m([^\x1b]+)\x1b\[m", line) | |
total_added += sum([len(s.split()) for s in added]) | |
total_removed += sum([len(s.split()) for s in removed]) | |
return total_added,total_removed | |
# NOTE: set up to compute changes since last 4am local time. Change rev_cmd or date computation here for diffs against something else | |
tod = datetime.time(4) | |
timestring = tod.strftime('%H:%M%p') | |
if datetime.datetime.now().time() < tod: | |
timestring += ' yesterday' | |
cmd = diff_cmd % timestring | |
p = Popen(cmd, shell=True, stdout=PIPE) | |
total_added,total_removed = count_changed(p.stdout) | |
print >>sys.stderr, 'Added:',total_added | |
print >>sys.stderr, 'Removed:',total_removed | |
print '%d,%d' % (total_added,total_removed) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment