Idle Hands
27 Dec 2012Can be amazingly productive!
I have a love/hate relationship with version numbers. On the one hand, they make life very easy when it comes to looking up versions of a program, and the entire Lein/Maven combo as used for Clojure is positively amazing thanks to a standardized version format.
However I’m bad at version numbers. As a lone dev, I’ve never really
had or needed a bump script, so when I do a git merge master
on the
repo that backends this blog I’m often at a loss as to what my version
number is because it doesn’t matter to me at all. Going by tags, this
repo’s history indicates that I counted up to 9.0.0
, and then it
seems that I reinitialized git-flow or just lost count so I’m back
down at 1.9.5
at the moment.
Sh*t like this should never happen, and it’s positively idiotic that
I was counting version numbers by hand, so I sat down last night and
built a solution, idle hands
being the devils workshop and all that. It’s a pretty simple solution,
use a simple regex split to parse the VERSION
file in the git repo,
and the use
git’s hooks
feature to automate execution of the bump script.
The really fun part of the solution (and the strange part of the hooks
files) was that I forgot the recursion case. See… what happens when
I add a post-commit
hook that generates a commit?
change & commit ./A
- git commits changes to file ./A
- git invokes post-commit hook
- hook changes ./VERSION
- hook commits ./VERSION
- git invokes post-commit
- hook changes ./VERSION
- hook commits ./VERSION
- git invokes post-commit
....
recur until heat death of universe
To fix this, I have the hooks check the environment variable $_BUMP
which serves as a flag to prevent this recursion by ensuring that the
bump hook will only execute once as the second execution will see
that the flag has been set and exit without doing anything
noteworthy. This shouldn’t really be surprising, but I overlooked this
possibility when I first sketched my solution which lead to no little
hilarity when I realized that I was suddenly at version 0.0.173
because I had neglected the recursion case.
Right now this is a pretty brittle solution, the bump program will
explode with a variety of nasty errors if there is no VERSION
file
or if it contains an improperly formatted version number, but that’s
what the 0.0.1 version number on the repo is supposed to indicate!
While it is a brittle solution, I have it deployed on four or five
repositories successfully, and will be investigating a way to add
these scripts to git so that they exist “out of the box” as it were
when I git init
. Fixing the various bad behaviors with malformed
VERSION files and adding an install script will probably be today’s
diversion.