svn diff via vim

When I’m reviewing the changes I’ve made to an svn checkout I prefer to see the changes in colour. This would be very easy if I was someone that used a graphical editor, but I’m one of those people that prefer to use vim, or vi if I really have to. As a result I had to think of a way of changing svn diff into something that was easy to read. I found out that vim has a syntax highlight template for diff files, so it got me thinking. What about pushing the diff into a file and then viewing the file in vim.
This is easy enough to achieve with:

svn diff > changes.diff

You then open the file in vim as normal, but then this is already 2 commands rather then just one, and that is before we’ve even thought about cleaning up the file.

This then leads to the idea of setting up a simple bash alias to run the diff, open the diff in vim, and then remove the temporary diff file. To do that you just add an alias into your ~/.profile and source it.

echo "alias svndiff='svn diff > changes.diff; vim changes.diff; rm changes.diff;'" >> ~/.profile
source ~/.profile

This is good, but we can make it better. What happens if we’ve picked a filename that is part of the checkout? It is possible that a file called changes.diff exists within the checkout, and then what? We’ve just overritten it and changed something we didn’t want to. To fix this we want to give the filename a unique name, and even better we want to put it outside of the current directory and checkout.

What we need is a new shell script in our ~/bin directory. If you’ve not set one of these up before then you should create one and put it into your PATH settings. I covered this in a few other posts, but here it is again:

mkdir ~/bin
echo "export PATH=~/bin:$PATH" >> ~/.profile
source ~/.profile

Once you have this local bin directory we can create our custom diff tool. The contents of which will be a little more advanced then the alias we created above. Oh, you want to remove that from your ~/.profile if you created it so we can use the same command as the filename.

To make the filename unique for our diff file we need to use the $RANDOM variable within bash. When you use this you get a random integer. We also want to put the diff into a folder we know about, but for sanities sake, we can make the directory in our home directory every time we run the script (just in case it’s been deleted, or it is the first time we’ve run the script.

So to the script itself.

###############################################
# Syntax highlighted diff (thanks to vim)
###############################################
# Copyright Carl Casbolt 2011
#
# What does this script do?
#  - create the tmp directory (incase it does 
#    not exist
#  - generate a random filename to use
#  - push the content from an svn diff, from 
#    the current location into the diff file
#  - open the diff file in vim
#  - remove the diff file when we exit vim
#
###############################################
mkdir ~/tmp
FILENAME=~/tmp/svndiff-$RANDOM$RANDOM.diff
svn diff > $FILENAME
vim $FILENAME
rm $FILENAME

Once you’ve created this script do not forget to make it executable, or it won’t be picked up in your PATH as a script.

Well there you have it. Syntax highlighted diff’s.

 

Leave a Reply

Your email address will not be published. Required fields are marked *