tags:

views:

217

answers:

7

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

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

A: 

svn info, I believe, is what you want.

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

svn info | grep "Revision:"
Dave Markle
+6  A: 

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
frgtn
+3  A: 
  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.

Kornel Kisielewicz
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
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.
Kornel Kisielewicz
I looked into this a bit, and found this:http://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
Oops, I didn't see your comment before I posted, but I think we agree anyway.
grimus
+3  A: 

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.

BastiBense
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
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
Iamamac: there is cygwin and independent ports (plus SfU and whatever msft calls it now), but your point that it's not as commonly available is right.
Roger Pate
get rid of the `grep`: `svn info | awk '/Revision/ { print $2; }'`
just somebody
If you are invoking awk anyway, you can skip the grep part: `svn info | awk '/^Revision:/ {print $2;}'`
ndim
The OP didn't say if he was running Windows. I was assuming Linux.And yes, you are right. You can leave out the grep part if you are using awk anyway.
BastiBense
A: 

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 :)

creativz
A: 

thanks!

is there also a command just to check which revision number is on the svn server WITHOUT making a checkout?

creativz
Did you mean to make this a comment on another answer or edit the question instead? (Feel free to post in the correct place and delete this answer.)
Roger Pate
A: 

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.

Peter Hansen