How to include Subversion revision number into Delphi project
I’d like to have revision number of source code to Delphi’s source code and exe version. What is the best way to do this automatically?
I’d like to display the revision number in “About” screen and in the version info of the project.
I’m using currently Delphi IDE (2006/2007) and Tortoise SVN.
5 Solutions collect form web for “How to include Subversion revision number into Delphi project”
I agree with the comments about $Revision$ not being the right tool for the job. Using a tool to extract the revision number from the output of svn info is indeed the correct thing to do.
There are however two more things to note:
svn info will only return the correct information if svn update has been run on the directory with the checked out sources. If you use custom build steps you should probably add a command for it too.
svn info gives you also information about the repository path. This is the only way to differentiate between sources in trunk and somewhere else, like in tags. If you want your About box to contain a string to correctly identify the sources used to build the application, make sure that the repository path is available too.
This is a command script that should be copied to the project top level directory. It will update the sources from the repository, get the SVN revision number from the svn info call and compare it with the constant SVN_REVISION from the file src\SvnRev.inc. If the file is missing it will create it, if the revision is different it will overwrite it. If svn is not available it will write the revision number 0 to the file.
The resulting file src\SvnRev.inc can simply be included in a source file. A similar file could be created to be included in the version resource.
@echo off setlocal rem determine project top level directory from command file name set PRJDIR=%~dp0 cd %PRJDIR% set SVNREVFILE=src\SvnRev.inc rem execute "svn info", extract "Revision: 1234" line, and take SVN rev from there svn update for /F " usebackq tokens=1,2 delims=: " %%i in (`svn info`) do set key=%%i&set value=%%j&call :read-svn-rev @echo SVN revision "%SVNREV%" rem extract "const SVN_REVISION = 1234;" line, and take header SVN rev from there for /F " usebackq tokens=2,4 " %%i in (%SVNREVFILE%) do set name=%%i&set value=%%j&call :read-file-rev @echo Include file revision "%FILEREV%" rem check for valid SVN rev if "%SVNREV%" EQU "" goto :no-svn-ref rem do not write file if SVN ref is equal if "%FILEREV%" EQU "%SVNREV%" goto :EOF @echo Writing svn revision %SVNREV% to %SVNREVFILE% @echo const SVN_REVISION = %SVNREV% ; > %SVNREVFILE% goto :EOF :no-svn-ref if not exist %SVNREVFILE% goto :no-header-file rem do not write file if SVN ref is already unset if "%FILEREV%" EQU "0" goto :EOF @echo Writing svn revision 0 to %SVNREVFILE% goto :write-no-version :no-header-file @echo Creating %SVNREVFILE% with svn revision 0 :write-no-version @echo const SVN_REVISION = 0 ; > %SVNREVFILE% goto :EOF endlocal goto :EOF :read-svn-rev if "%key%" EQU "Revision" set SVNREV=%value%& goto :EOF :read-file-rev if "%name%" EQU "SVN_REVISION" set FILEREV=%value%& goto :EOF
There is a program called SubWCRev.exe that comes with TortoiseSVN that will do token replacement for you. So somewhere in your source, you insert the token, and pass the input and output filenames to SubWCRev.exe, and it will replace the token with various SVN info, e.g., revision number. Note, this is a standalone program you can use with your build script, you do not need to use TortoiseSVN with it.
In a comment you mention you want the global revision, not the revision of any particular file. You’re not going to get that number in a keyword since the number isn’t related to any one file, but rather to whichever file was checked in recently most recently anywhere in the tree.
You can make a script or small program that runs
svn info and then parses the output to grab the revision number you’re after. You can use that number in conjunction with a template of an RC file to insert the revision number in the version-info record. This generated file would not be checked in. Run the script as part of your MSBuild procedure.
For pre-MSBuild Delphi versions, make a project group, and then make the script the first project in the group. “Build all” or “Compile all” will run the script before compiling the main project.
You could also have code in each unit’s
initialization section that adds its revision (as gotten by Ieure’s answer) to a global list. Then select the highest number in the list at run time. That can get you the number to display in the “about” box, but you can’t use it in your program’s version information.
Suggestion: modify your build scripts to change some version file that gets compiled in afterwards. It’s difficult to suggest anything more specific without knowing the build environment you’re using.
If you use svn:keywords, and include
$Revision$ in your file, it will get updated every time that file is committed.
So stick that in a
version file at the top-level of your project, which you change/commit on every build, and read that in to get the version.