creativz
creativz

Reputation: 10807

Current Subversion revision command

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.

Upvotes: 87

Views: 131239

Answers (10)

Sant Erwann
Sant Erwann

Reputation: 11

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.

Upvotes: 1

BastiBen
BastiBen

Reputation: 19880

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.

Upvotes: 139

RSuser
RSuser

Reputation: 41

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

Upvotes: 4

monkut
monkut

Reputation: 43870

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.

Upvotes: 2

Lazy Badger
Lazy Badger

Reputation: 97355

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

Upvotes: 3

Saulius Žemaitaitis
Saulius Žemaitaitis

Reputation: 2974

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

Upvotes: 27

Peter Hansen
Peter Hansen

Reputation: 22097

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.

Upvotes: 2

creativz
creativz

Reputation: 10807

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

Upvotes: 0

Kornel Kisielewicz
Kornel Kisielewicz

Reputation: 57615

  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.

Upvotes: 5

Dave Markle
Dave Markle

Reputation: 97811

svn info, I believe, is what you want.

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

svn info | grep "Revision:"

Upvotes: 2

Related Questions