Getting the subversion repository number into code
Asked Answered
S

7

41

I'd like to implement a way of recording the version of a project within code, so that it can be used when testing and to help track bugs. It seems the best version number to use would just be the current revision number from Subversion. Is there an easy way to hook this number into a (C++ in my case) header file or something, which I can then get at in code? I guess this is a post commit hook or something?

Does anyone have any experience of implementing this (with code to share, please?), or can suggest a better alternative? Thanks.

Succinctorium answered 19/8, 2008 at 14:19 Comment(0)
R
14

While nifty, the revision keyword trick only updates the file when it's changed in that revision - if you don't change the file, then it will continue to reflect the old revision.

If you want the software to always reflect the overall revision number, then you'll have to delve into the relevant SVN entries file and extract it, which isn't too difficult (it's an XML file).

Wikipedia does this on their version page to indicate the revision of the software that's running live; the code is here - look for the getSvnRevision() method.

Rasbora answered 30/8, 2008 at 4:23 Comment(3)
You would have to recursively check all entries files, to find the highest revision number. I guess that's what svnversion does.Christiechristin
svnversion seems to return the last changed revision of the whole repository (not just the path of your WC). See my answer to this related question on how I've solved this: #56727Blasting
It looks like this breaks with Subversion 1.7, which eliminates the .svn directory everywhere except the top level of the sandbox.Noles
I
27

Two ways:

Embed $Id$ or $Revision$ within the code. Then set svn:keywords="Id Revision" property on the file. This will give you the last modified revision of that source file. Good for smaller projects and scripts.

Alternatively, use a Makefile driven process and the command line tool svnversion. (Language specific - this should work for C/C++)

echo -n "#define VERSION 1.0.1-" > version.h
svnversion -n . >> version.h

Or some more complex build script with sed and version.h.in. Then just #include version.h

That will give you the repository version number, which will change with every commit / update, and is probably a more appropriate version number for most projects.

Note: I also used a human readable version string that I manually update. The example would give: Version: 1.0.1-r13445

~J

Illhumored answered 19/8, 2008 at 14:30 Comment(0)
R
14

While nifty, the revision keyword trick only updates the file when it's changed in that revision - if you don't change the file, then it will continue to reflect the old revision.

If you want the software to always reflect the overall revision number, then you'll have to delve into the relevant SVN entries file and extract it, which isn't too difficult (it's an XML file).

Wikipedia does this on their version page to indicate the revision of the software that's running live; the code is here - look for the getSvnRevision() method.

Rasbora answered 30/8, 2008 at 4:23 Comment(3)
You would have to recursively check all entries files, to find the highest revision number. I guess that's what svnversion does.Christiechristin
svnversion seems to return the last changed revision of the whole repository (not just the path of your WC). See my answer to this related question on how I've solved this: #56727Blasting
It looks like this breaks with Subversion 1.7, which eliminates the .svn directory everywhere except the top level of the sandbox.Noles
M
14

You can also use SubWCRev which is part of TortoiseSVN.

SubWCRev is Windows console program which can be used to read the status of a Subversion working copy and optionally perform keyword substitution in a template file. This is often used as part of the build process as a means of incorporating working copy information into the object you are building. Typically it might be used to include the revision number in an “About” box.

http://tortoisesvn.net/docs/release/TortoiseSVN_en/tsvn-subwcrev.html

Mors answered 9/10, 2008 at 10:16 Comment(0)
B
7

in your Makefile, add:

SVNDEV := -D'SVN_REV="$(shell svnversion -n .)"'
CFLAGS := $(SVNDEV) ...

then you can use macro SVN_REV anywhere in your code, eg:

printf ("Version: SVN %s\n", SVN_REV);
Biggin answered 11/5, 2013 at 0:51 Comment(0)
V
5

You can use the svn:keywords property to enable the Rev keyword.

You can then use $Rev$ in your code and SVN will expand it automatically when updating to $Rev: 256 $ which can then parse...

More info on the Subversion manual

Vaivode answered 19/8, 2008 at 14:26 Comment(1)
This is not the Right Thing if you want a full-project version number, as $Rev$ changes only when that file is updated, not when anything in the project is updated.Longstanding
F
0

A good up-to-date solution:

Create a Makefile containing the following line (in the same folder as YourFile.dox):

sed "s~RevNumber~$(shell svnversion ../)~g" YourFile.dox > YourFileDummy.dox; doxygen YourFileDummy.dox

And YourFile.dox should contain this:

...
PROJECT_NUMBER         = "Revision RevNumber"
...

Now:

  1. sed replaces RevNumber in the .dox with the output of svnversion (executed in the main folder of your repository) and saves the modified file to YourFileDummy.dox
  2. doxygen is executed on YourFileDummy.dox to generate the documentation
  3. Your documentation will now contain the revision number!
Franklinfranklinite answered 18/4, 2017 at 9:26 Comment(0)
E
0

Adding the following to the top of the file should do the trick

[Subversion]
Revision = "$Revision: $"
Author   = "$Author:   $"
Date     = "$Date:     $"
HeadURL  = "$HeadURL:  $"
Ehling answered 13/7, 2023 at 9:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.