Git Commit Hook Script – Word Count and ‘Now Playing’

I’ve been quite happily using Git in my writing workflow for a few months now. But I wanted to be able to add a little more information into my commit messages.

Inspired a little by Cory Doctorow’s post, ‘Flashbake: Free version-control for writers using git’, about his use of scripts to add extra info to his Git commits, I decided to hack together my own brief post commit hook.

The first part of this basic bash script is a simple word count total on all changed files in that commit. I use a daily word quota when I write. So being able to track how much I have written since the previous commit(s) is been very useful for me.

The second part of the script outputs the song title and artist name (from the id3 tags) if there is a song playing at the time of the commit. This one is more for my own amusement, since I listen to music frequently while working. I restricted the programs to check to clementine and audacious since I typically only listen to music in those apps. (This bit was almost wholesale lifted from a script in this Ubuntuforums thread.)

All of this is added onto the end of the message in the git commit call.

#!/bin/sh
 
for i in $(git diff --cached --name-only)
	do
		count=$(wc -w $i)
		echo "$count" >> "$1"
	done
 
apps="clementine audacious"
 
for app in $apps
	do
	  pat="([^\w-]$app)"
	  if ps ux | grep -P $pat | grep -vq grep; then
		 file=`lsof -F n -c "$app" | grep -i "^.*\.mp3$" | sed 's/^n//g'`
		 if [ ! -z "$file" ]; then
		   title=`id3info "$file" | grep "Title" | sed 's/^===.*[:].//g'`
		   perf=`id3info "$file" | grep "performer" | sed 's/^===.*[:].//g'`
		   if [ -z "$title" ] && [ -z "$perf" ]; then
		     echo "Now playing: No MP3 info" >> "$1"
		   else
		     echo "Now playing:" \'"$title"\' "by" "$perf" >> "$1"
		   fi
		 fi
	  fi
	done

Comments are closed.