Displaying the latest global Subversion revision ID in an application
When you have to display the latest global svn revision number in your application you face different options.
Using Subversion keywords, like $Revision$ or $Id$, sounds like the most natural approach. Unfortunately the keywords are updated only when you change and commit the corresponding file. In short: if you intend to grab the revision ID from a central header file, like version.h, this file has to be edited and committed whenever a svn commit on any file in your project takes place. So either you do that manually (“erm, no?” – right!), or you create a commit hook and bloat your repository.
Another approach is to fetch the latest number of the latest revision and update your version.h as part of your build. In short: whenever you trigger a build by calling make, ant or build your project in your IDE, you invoke a script that generates your header file (or .java, .cs, .rb … you name it). On Linux and Unix, you might use a script just like this:
#!/bin/csh set SVNREV=`svnversion -n` set FILENAME=$my/path/to/include/version.h echo '/*******************************************************************' > $FILENAME echo '* '>> $FILENAME echo '* This include file is automatically generated. All manual ' >> $FILENAME echo '* changes will be lost.' >> $FILENAME echo '* ' >> $FILENAME echo '*******************************************************************/' >> $FILENAME echo '' >> $FILENAME echo '/* latest Subversion revision */' >> $FILENAME echo '#define SVNREV ' $SVNREV >> $FILENAME echo '' >> $FILENAME
If you develop on Windows, you might adopt this script:
@echo off
REM store latest revision number as SVNREV
for /f "delims=:" %%a in ('svnversion') do @set SVNREV=%%a
set FILENAME=%my%\path\to\include\version.h
echo /******************************************************************* > %FILENAME%
echo * >> %FILENAME%
echo * This include file is automatically generated. All manual >> %FILENAME%
echo * changes will be lost. >> %FILENAME%
echo * >> %FILENAME%
echo *******************************************************************/ >> %FILENAME%
echo. >> %FILENAME%
echo /* latest Subversion revision */ >> %FILENAME%
echo #define SVNREV %SVNREV$ >> %FILENAME%
echo. >> %FILENAME%Make sure that you add version.h to svn:ignore.