Getting SVN revision number into a program automatically

svn info revision
python svn commands
python svn exception
access svn from python
svn remote
subversion python 3
python svn username password

I have a python project under SVN, and I'm wanting to display the version number when it is run. Is there any way of doing this (such as automatically running a short script on commit which could update a version file, or querying an SVN repository in Python?)

I'm not sure about the Python specifics, but if put the string $Revision$ into your file somewhere and you have enable-auto-props=true in your SVN config, it'll get rewritten to something like $Revision: 144$. You could then parse this in your script.

There are a number of property keywords you can use in this way.

This won't have any overhead, e.g. querying the SVN repo, because the string is hard-coded into your file on commit or update.

I'm not sure how you'd parse this in Python but in PHP I'd do:

$revString = '$Revision: 144$';
if(preg_match('/: ([0-9]+)\$/', $revString, $matches) {
    echo 'Revision is ' . $matches[1];

SvnRev: a utility for Subversion, SvnRev is a little program that writes the current revision number of project It gets the revision number from the "RCS keywords" in the source files, it generates and an automatically increasing "build" number to another. I have a python project under SVN, and I'm wanting to display the version number when it is run. Is there any way of doing this (such as automatically running a short script on commit which could u

Similar to, but a little more pythonic than the PHP answer; put this in your module's

__version__ = filter(str.isdigit, "$Revision: 13 $")

and make sure you add the Revision property:

svn propset svn:keywords Revision

SVN: Head revision number auto update in code files, I guess you want to display the version somewhere in the binary program? When I try to hit the link in this topic to see the file, I get a basic  Automatically get the SVN Revision Number of your Project When you are handling so many projects and those projects are saved in different repository locations, you may find it very difficult to keep track all the versions and revisions manually and it will be much harder if you are developing in multiple repository branches.

Or you can do like this:

import re,subprocess

svn_info = subprocess.check_output("svn info")

print ("Revision:\s\d+", svn_info)).group()

it prints "Revision: 2874" in my project

Or like this:

print (subprocess.check_output("svnversion")).split(":")[0]

it prints "2874" in my project

Automatically adding version number to the code, But build number is inferior to a proper version control system, e.g. Subversion. TortoiseSVN as subversion client has a tool to add revision number, last commit  Getting SVN revision number into a program automatically (6) Check out pysvn. it exposes extensions in Python for Subversion runtime functionality.

The file hooks/pre-commit in your repository is a script or program which will be executed before a successful commit. Simply make this a script which updates your file with the proper version number as it is being committed (the revision number is passed to your script as it's run). Here's a tutorial with an example of writing a pre-commit hook:

Version Control the Subversion Way, Unlike most version control systems, Subversion's revision numbers apply to the entire programs use URLs to identify versioned files and directories in Subversion The Subversion client will automatically encode URLs as necessary, just like a To get a working copy, you must check out some subtree of the repository. A project I’ve been working on uses an SVN repository for its version control. I needed a way to automatically insert the SVN revision into the code at compile time so that an “about” window could be automatically updated to display the current revision number.

There's a snippet of code in Django that allows you to do this, I'd recommend looking at it.

Subversion Tutorial, Pull: Subversion copies the current revision in the repository to the client. The revision number of a file or directory is the state of that file or subversion can sometimes resolve the conflict by automatically merging the revisions). The svn program has subcommands like checkout, add, commit, that are  5 Comments on Embed SVN’s revision into AssemblyInfo’s version number Automatically sync the two numbers on each build. Recall that version numbers are of the form Major.Minor.BuildDay.BuildTime .

Apache Subversion, Used to track merge data (revision numbers) in Subversion 1.5 (or later). This property is automatically maintained by the merge command, and it is not  You'd really like to have the revision number from subversion as the subminor revision in the PACKAGE_VERSION preprocessor object macro #define -d in config.h so that it automatically appears in your software. Sounds easy enough, but it's both surprisingly difficult and surprisingly easy to achieve.

Use Subversion revision number as version number with MSBuild , NET revision numbers; How to use Bamboo to get the latest revision number from Subversion and How to create an MSBuild target to automatically insert the revision number on build NET applications and deploy them. svn info gets the repository info, then pipes it to find, which strips out everything except the line containing the revision number. The for command gives you the second "token" on that line (the first one is Revision: ).

Appendix D. Automating TortoiseSVN, The TortoiseSVN GUI program is called TortoiseProc.exe . The date format is the same as used for svn date revisions. To avoid the dialog specify a revision number /rev:1234 . New files are added automatically, and missing files get removed in the target working copy, basically ensuring that source and destination  Subversion ships with a tool called svnversion, which was designed for just this purpose. It crawls your working copy and generates as output the revision(s) it finds. You can use this program, plus some additional tooling, to embed that revision information into your files.

  • This is the right answer! Also, remember to set the property 'keywords=Revision' on each file you want to do this to.
  • Drew - I think if enable-auto-props=true is set, then the default keywords are all set?
  • +1 That's awesome! In my case, though, is there a way to have SVN edit the revision number if it's a different file that was changed in this revision? For instance, if I have the revision number keyword in, but I don't change in revision nnn, it won't update it, will it? If not, is there a way to use this method in that way?
  • @Smashery I'm afraid you'd have to write your own commit hook to do that I suspect - you could use the svn:keywords property though by setting your own keyword and letting SVN do the substitution for you.
  • That will only work on the file(s) being committed. We handle it in the build script, easy to do.
  • or ''.join(c for c in "$LastChangedRevision: 1546 $" if c.isdigit()) if you don't like filter.
  • Not only with working copy. You can retrieve svn remote URL from "svn info" first, like execute "svn info", then take "URL" param value from the output and execute same code, but using "svn info value_of_URL_param" So, updated code is: import re,subprocess svn_info = subprocess.check_output("svn info svn.you_remote_repo_url") print ("Revision:\s\d+", svn_info)).group()
  • I get an invalid syntax error on your print statement. print ("Revision:\s\d+", svn_info)).group()
  • Ouch. Letting the pre-commit hook modify files is generally a bad idea. The version in the repo will be different from the version in the working copy, and this may lead to some confusion. The committer has to revert to an earlier version of the file, and then get the new one, probably resulting in some kind of conflict, as svn has stored the original version in the text-base of the working copy.
  • I agree that this is dangerous and not usually a desired behavior, but if you limit yourself appropriately, then I don't see the problem. For example, if you have a "svn_version.txt" file that contains nothing but the current revision number, then it would be perfectly safe; your local copy would always have the version which you've checked out and the repository would always have the latest (often the same as your working copy).
  • This function only works when you're running from within a subversion checkout. If this is what the OP wants, then problem solved. However, I suspect he may want the script to have contained within itself or a config file or something its subversion revision, even if the script is no longer under subversion, in which case that link won't be useful.
  • Yeah, this is great fallback option, but having the version number also under source control would be better. Thanks! +1