16

I want to write a shell script in bash to deploy websites from an svn repository. When I deploy a website, I name the exported directory website_name-Rrevision_number. I'd like the bash script to automatically rename the exported directory, so it needs to learn the current revision number from the export directory. If I run

$> svn info http://svn-repository/trunk

Path: trunk
URL: http://svn-repository/mystery/trunk
Repository Root: http://svn-repository/mystery
Repository UUID: b809e6ab-5153-0410-a985-ac99030dffe6
Revision: 624
Node Kind: directory
Last Changed Author: author
Last Changed Rev: 624
Last Changed Date: 2010-02-19 15:48:16 -0500 (Fri, 19 Feb 2010)

The number after the string Revision: is what I want. How do I get that into a bash variable? Do I do string parsing of the output from the svn info command?

user151841
  • 17,377
  • 29
  • 109
  • 171

9 Answers9

32

Use svnversion. This will output the revision number/range with minimal additional cruft

oefe
  • 19,298
  • 7
  • 47
  • 66
  • 1
    This only seems to work with local working copies. When I try with a remote URL (as in the original question), I get an error that says it doesn't exist. See also http://stackoverflow.com/questions/623378/get-revision-number-of-a-remote-repository – JeffB Dec 23 '16 at 02:39
  • it is server side, not client side – Sandburg Dec 07 '18 at 09:19
26
REVISION=`svn info http://svn-repository/trunk |grep '^Revision:' | sed -e 's/^Revision: //'`

It's simple, if inelegant.

Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408
6

Parsing the 'Revision' string is not portable across different locales. Eg. with my locale it is like:

...
Wersja: 6583
Rodzaj obiektu: katalog
Zlecenie: normalne
Autor ostatniej zmiany: ...
Ostatnio zmieniona wersja: 6583
Data ostatniej zmiany: 2013-03-21 11:33:44 +0100 (czw)
...

You don't wanna parse that :)

So, the best approach is to use 'svnversion' as oefe suggested. This is the tool mentioned for this purpose.

bartolomeo_n
  • 61
  • 1
  • 1
5

just use one awk command. much simpler as well.

var=$(svn info http://svn-repository/trunk | awk '/^Revision:/{print $2}')
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
5

Without using sed, grep or awk:

REVISION=`svn info --show-item=revision --no-newline`
AndreyP
  • 2,510
  • 1
  • 29
  • 17
Jaeyoung Chun
  • 671
  • 9
  • 6
  • REVISION=`svn info --show-item=revision --no-newline -r HEAD` to get latest version on server – AndreyP Apr 29 '21 at 11:24
  • Note that this is a revision that you checked out. When you checkout head this might include versions from other branches. So this is not a revision for the path you checkout. – Nux Jul 19 '21 at 16:02
1
svn info http://svn-repository/trunk | grep Revision | tr -d 'Revison: '

Spits out the revision Use backticks in your shell script to execute this and assign the results to a variable:

REVISION=`svn info http://svn-repository/trunk | grep Revision | tr -d 'Revison: '`
user229044
  • 232,980
  • 40
  • 330
  • 338
0

There are probably a dozen different ways to do this, but I'd go with something simple like:

revision="$(svn info http://svn-repository/trunk | grep "^Revision:" | cut -c 11-)"
swestrup
  • 4,079
  • 3
  • 22
  • 33
0

This will give you the head revision number

svn info -r 'HEAD' | grep Revision | egrep -o "[0-9]+"

egrep is extended grep.

Avincoder
  • 11
  • 2
0
REVISION=$(svn info http://svn-repository/trunk |grep '^Revision:' | sed -e 's/^Revision: //p')
echo $REVISION
sai
  • 300
  • 5
  • 19