07Jan

git wordcount

Posted by Elf Sternberg as Uncategorized

This is an early draft, but I figured you might enjoy taking a look at my latest little toy:

#!/bin/bash

ARG=""

if [ "${1:0:2}" == '-h' ]; then
    HEAD="${1:1}"
    HEAD=${HEAD//h/^}
    ARG=" HEAD$HEAD..HEAD "
fi

REM=`git diff -U $ARG | grep '^-' | sed 's/^-//' | wc -w`
ADD=`git diff -U $ARG | grep '^+' | sed 's/^+//' | wc -w`
DIF=`expr $ADD - $REM`
echo "Word count: $DIF"

As I’m sure I’ve mentioned before, I write award-winning science fiction as well as hack code, and I use emacs for both activities. I’ve always written primarily in what I think of as Usenet mode, with asterisks and underscores for emphasis and bolding, respectively, a mode that has become codified as Markdown. I used to have my own homegrown toolkit for that, but these days I just use the Python version of Markdown instead. I’ve always used a VCS for that, living entirely in GIT almost since the beginning.

I wanted a simple script to tell me my daily progress. This is it. It compares what’s on the filesystem right now to the latest check-in, or, if you pass it the -h option, it will compare the last check-in to the previous check-in. Each additional -h will push the oldest comparison back one commit, so -hhh will compare the last check-in to the one three check-ins ago.

After writing this, I learned about git-sh-setup and --word-diff=porcelain, both of which will find their way into a future version; hopefully soon you’ll be able to say ‘git wc HEAD^^^..HEAD^^’ or similar, and see how productive you were any two days in the past.

3 Responses to git wordcount

Jason

October 2nd, 2014 at 7:04 am

I modified it to use word-diff, taking into account removed words (note the tail command was also required). Hopefully comment formatting doesn’t destroy it. Thanks! this is so helpful!

#!/bin/bash

ARG=””

if [ “${1:0:2}” == ‘-h’ ]; then
HEAD=”${1:1}”
HEAD=${HEAD//h/^}
ARG=” HEAD$HEAD..HEAD ”
fi

REM=`git diff –word-diff=porcelain | grep ‘^-‘ | sed ‘s/^-//’ | tail -n +2 | wc -w`
ADD=`git diff –word-diff=porcelain | grep ‘^+’ | sed ‘s/^+//’ | tail -n +2 | wc -w`
DIF=`expr $ADD – $REM`
echo “New words: $ADD, Removed words: $REM, Net change: $DIF”

Jason

October 2nd, 2014 at 7:15 am

oops. Tail was a bad idea if there are changes to more than one file. Better way:

#!/bin/bash

ARG=””

if [ “${1:0:2}” == ‘-h’ ]; then
HEAD=”${1:1}”
HEAD=${HEAD//h/^}
ARG=” HEAD$HEAD..HEAD ”
fi

REM=`git diff –word-diff=porcelain | grep ‘^-‘ | grep -v ‘\-\-\-‘ | sed ‘s/^-//’ | wc -w`
ADD=`git diff –word-diff=porcelain | grep ‘^+’ | grep -v ‘+++’ | sed ‘s/^+//’ | wc -w`
DIF=`expr $ADD – $REM`
echo “New words: $ADD, Removed words: $REM, Net change: $DIF”

Ben

September 5th, 2017 at 10:18 am

Very cool, thank you.

Comment Form

Subscribe to Feed

Categories

Calendar

January 2014
M T W T F S S
« Oct   Aug »
 12345
6789101112
13141516171819
20212223242526
2728293031