86

Is there a Subversion command to show the current revision number?

After svn checkout I want to start a script and need the revision number in a variable. It would be great if there is a command like svn info get_revision_number.

0

10 Answers 10

131

Newer versions of svn support the --show-item argument:

svn info --show-item revision

For the revision number of your local working copy, use:

svn info --show-item last-changed-revision

You can use os.system() to execute a command line like this:

svn info | grep "Revision" | awk '{print $2}'

I do that in my nightly build scripts.

Also on some platforms there is a svnversion command, but I think I had a reason not to use it. Ahh, right. You can't get the revision number from a remote repository to compare it to the local one using svnversion.

8
  • 4
    Note that there was a cmd.exe tag in the original question, implying the OP is in Windows, where you can not use grep and awk.
    – iamamac
    Jan 2, 2010 at 13:12
  • 1
    os.system only tells you the exit code, it doesn't provide a way to get the output. Instead, executing svn info (such as with the subprocess module), reading the output and doing the grep/awk steps directly in Python (if "Revision" in line: return line.split()[1]) is usually both simpler and easier than trying to write bash commands in Python.
    – Roger Pate
    Jan 2, 2010 at 13:16
  • 4
    get rid of the grep: svn info | awk '/Revision/ { print $2; }' Jan 2, 2010 at 13:27
  • 4
    If you are invoking awk anyway, you can skip the grep part: svn info | awk '/^Revision:/ {print $2;}'
    – ndim
    Jan 2, 2010 at 13:28
  • 1
    If your system is using locales, you should add "LANG=C" before, like this: LANG=C svn info | grep "Revision" | awk '{print $2}'
    – Florent
    Nov 24, 2013 at 19:30
25

There is also a more convenient (for some) svnversion command.

Output might be a single revision number or something like this (from -h):

  4123:4168     mixed revision working copy
  4168M         modified working copy
  4123S         switched working copy
  4123:4168MS   mixed revision, modified, switched working copy

I use this python code snippet to extract revision information:

import re
import subprocess

p = subprocess.Popen(["svnversion"], stdout = subprocess.PIPE, 
    stderr = subprocess.PIPE)
p.wait()
m = re.match(r'(|\d+M?S?):?(\d+)(M?)S?', p.stdout.read())
rev = int(m.group(2))
if m.group(3) == 'M':
    rev += 1
5
  1. First of all svn status has the revision number, you can read it from there.

  2. Also, each file that you store in SVN can store the revision number in itself -- add the $Rev$ keyword to your file and run propset: svn propset svn:keywords "Revision" file

  3. Finally, the revision number is also in .svn/entries file, fourth line

Now each time you checkout that file, it will have the revision in itself.

5
  • For item 3, is it reasonable to expect the revision number will always be on the 4th line? If it is a reasonable expectation, this seems like the easiest way to do it across pretty much any environment.
    – grimus
    Jan 4, 2010 at 21:07
  • Yes, at least in a sensible subversion version range. If they change the format in some future version (possible but not likely soon) then you'd have to adapt. Jan 5, 2010 at 19:30
  • I looked into this a bit, and found this: svnbook.red-bean.com/en/1.4/svn.developer.insidewc.html It sounds like the format is likely to change from time to time. I'm sure for most things, reading the entries file will work fine, but you'll have to make sure to maintain your code if the entries file changes.
    – grimus
    Jan 5, 2010 at 22:22
  • Oops, I didn't see your comment before I posted, but I think we agree anyway.
    – grimus
    Jan 5, 2010 at 22:23
  • Tried on svn, version 1.11.0 (r1845130) compiled Oct 31 2018, 12:40:42 on x86_64-unknown-cygwin: .svn/entries contains only one integer.
    – badams
    Jul 1, 2020 at 17:30
4

REV=svn info svn://svn.code.sf.net/p/retroshare/code/trunk | grep 'Revision:' | cut -d\ -f2

1
3

Nobody mention for Windows world SubWCRev, which, properly used, can substitute needed data into the needed places automagically, if script call SubWCRev in form SubWCRev WC_PATH TPL-FILE READY-FILE

Sample of my post-commit hook (part of)

SubWCRev.exe CustomLocations Builder.tpl  z:\Builder.bat
...
call z:\Builder.bat

where my Builder.tpl is

svn.exe export trunk z:\trunk$WCDATE=%Y%m%d$-r$WCREV$

as result, I have every time bat-file with variable part - name of dir - which corresponds to the metadata of Working Copy

2

svn info, I believe, is what you want.

If you just wanted the revision, maybe you could do something like:

svn info | grep "Revision:"
2

Use something like the following, taking advantage of the XML output of subversion:

# parse rev from popen "svn info --xml"
dom = xml.dom.minidom.parse(os.popen('svn info --xml'))
entry = dom.getElementsByTagName('entry')[0]
revision = entry.getAttribute('revision')

Note also that, depending on what you need this for, the <commit revision=...> entry may be more what you're looking for. That gives the "Last Changed Rev", which won't change until the code in the current tree actually changes, as opposed to "Revision" (what the above gives) which will change any time anything in the repository changes (even branches) and you do an "svn up", which is not the same thing, nor often as useful.

5
  • Asking svn to output XML and then parse it? Is it meant as a joke? Aug 5, 2014 at 1:46
  • 1
    @JulienGuertault, no, the XML output is meant for programmatic use while the non-XML is not. Parsing the human output is risking the format changing in future and breaking your code, and depending on the context may require custom work each time you need to do similar things. If you use the XML approach you may save effort on infrastructure, over time. No joke, but I also wouldn't say doing it the other way is by any means wrong. Just personal preference. I've done both, in different contents. Aug 6, 2014 at 14:20
  • The machine vs human output parsing is a valid point (worth emphasizing in your answer). But why not simply use svnversion? It has a documented compact output and doesn't require something this complex to parse it. Aug 7, 2014 at 6:13
  • @frgtn gave svnversion for his answer above but, as I mentioned, using the --xml output options on the various svn subcommands gives you a unified way of handling a wide variety of requirements. With svnversion you may need yet another custom parser e.g. to deal with an appended "M" if you don't want that. Please just consider my answer one of many possible alternatives, not accepted by the OP but possibly useful to latecomers who get here from a search. Aug 7, 2014 at 14:46
  • Thanks for the additional explanation. Aug 8, 2014 at 6:44
2

Just used @badcat's answer in a modified version, using subprocess.check_output():

import subprocess
revision = subprocess.check_output("svn info | awk '/^Revision:/ {print $2}'", shell=True).strip()

I believe you can also, install and use pysvn if you want to use python to interface with svn.

0

I think I have to do svn info and then retrieve the number with a string manipulation from "Revision: xxxxxx" It would be just nice, if there were a command that returns just the number :)

0

Otherwise for old version, if '--show-item' is not recognize, you can use the following command :

svn log -r HEAD | grep -o -E "^r[0-9]{1,}" | sed 's/r//g'

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.