Source code repositories are fantastic, powerful but simple things. They keep stuff in one place, and track revisions of the things in it so you can see ‘back in time’ at any point. Some systems are good, some are not so good – but the annoying thing I’ve discovered about most of them today is that they tend to work perfectly when I actually need them to fail.
My problem is a fairly dopey one – I want to be able to check if a file in a local working copy of a repository is different to the remote repository. Simple enough, because almost every system has a tool to do this, usually termed the diff command. If you ask your tool to diff against the repository, it’ll say yay or nay, and if it’s different it’ll probably tell you what’s changed too. Thing is, regardless of whether or not it’s different – the command always succeeds. In shell terms, it exits with status 0 – success. That’s all well and good, but when you want to test for the difference against the repository; you’re buggered for a boolean result.
This morning, I knocked this up to solve the problem in bash.
#!/bin/bash# accepts a single argument <filename> and checks if it’s different to the version in the repository.
# reports status 0 if it’s the same, and status 1 if it’s got differences.usage=”Usage: ./checkrepo [-v] <filename>”declare numberargs=$#
if [ $numberargs -gt 2 ] ; then echo ${usage};exit 2 ;fi
if [ $numberargs = 1 ] ; then localfile=$1;fi
if [ $numberargs = 2 ] ; then localfile=$2;fi
if [ "$1" = "-v" ] ; then
output=”/dev/stdout”
else
output=”/dev/null”
fifiledir=`dirname ${localfile}`
filename=`basename ${localfile}`repotype=`ls -a $filedir |grep -e CVS -e .svn`
case “$repotype” in
“CVS”)
repocmd=”cvs diff”
;;
“.svn”)
repocmd=”svn diff”
;;
*)
echo “Not in a working copy!”
exit 3
esac# run diff
cd ${filedir}
${repocmd} ${filename} > results# do something with the result and put it to stdout if verbosity is turned on
if [ -s results ] ; then
changes=”true”
else
changes=”false”
fi# put the results out there
cat results > $output
rm results
cd – > $output 2>&1# exit with status 0 if there’s no change
if [ "$changes" = "false" ] ; then exit 0; fi# exit with status 1 if there is change
if [ "$changes" = "true" ] ; then exit 1; fi
It’s not hugely elegant, but it is quick. Anyone care to play golf with me to improve it?
Yesterday, I recieved my first 'Lava Lamp'. There's (apparently) long been a tradition in Cap One's IT department of giving a Lava Lamp to anyone who makes a particularly memorable schoolboy error. Mine was a pretty special one – launching the new Capital One Mortgages&Insurance website (planned and delivered in a ridiculously short space of time, helped in no small part by myself), I'd written a really noddy script to ssh to a series of boxes and create some new directories, touch some files and populate them with specific variables. I copied the same script I'd used for the SIT implementation, and put a nice big comment at the top saying 'PRODUCTION VERSION' for clearness.

