I would like to start tagging my deployed binaries with the latest SVN revision number.
However, because SVN is file-based and not directory/project-based, I need to
Duplicate of this question. As I posted there, the svnversion
command is your friend. No need to parse the output, no need to update first, just does the job.
There is a program distributed with Subversion called svnversion that does exactly what you want to do. It's how we tag our websites.
If you just want the revision number of the latest change that was committed, and are using Windows without grep/awk/xargs, here is the bare-bones command to run (command line):
X:\trunk>svn info -r COMMITTED | for /F "tokens=2" %r in ('findstr /R "^Revision"') DO @echo %r
67000
svn info -r COMMITTED
will give you the latest committed change to the directory you are currently in:
X:\Trunk>svn info -r COMMITTED
Path: trunk
URL: https://svn.example.com/svn/myproject/trunk
Repository Root: https://svn.example.com/svn/
Repository UUID: d2a7a951-c712-0410-832a-9abccabd3052
Revision: 67400
Node Kind: directory
Last Changed Author: example
Last Changed Rev: 67400
Last Changed Date: 2008-09-11 18:25:27 +1000 (Thu, 11 Sep 2008)
The for loop runs findstr
to locate the Revision portion of the output from svn info
. The output from this will be (you won't see this):
Revision: 67000
Which then splits the tokens, and returns the 2nd, to be echo
ed out:
67000
One way. When you check out the code, look at the last line of svn output:
$ svn up
...stuff...
Updated to revision 66593.
A more direct way:
$ svn info
Path: .
URL: https://svn.example.com/svn/myproject/trunk
Repository Root: https://svn.example.com/svn/
Repository UUID: d2a7a951-c712-0410-832a-9abccabd3052
Revision: 66593
Node Kind: directory
Schedule: normal
Last Changed Author: bnguyen
Last Changed Rev: 66591
Last Changed Date: 2008-09-11 18:25:27 +1000 (Thu, 11 Sep 2008)
The answers provided by @Charles Miller and @Troels Arvin are correct - you can use the output of the svn update
or svn info
, but as you hint, the latter only works if the repository is up to date. Then again, I'm not sure what value any revision number is going to be to you if part of your source tree is on a different revision than another part. It really sounds to me like you should be working on a homogeneous tree.
I'd suggest either updating before running info (or if you've already updated for your build, you're golden) or using svn info URL-to-source
.
svnversion seems to be the cleanest way to do this:
svnversion -c /path/to/your-projects-local-working-copy/. | sed -e 's/[MS]//g' -e 's/^[[:digit:]]*://'
The above command will clean out any M and S letters (indicating local modifications or switchedness) from the output, as well as the smaller revision number in case svnversion
returns a range instead of just one revision number (see the docs for more info). If you don't want to filter the output, take out the pipe and the sed
part of that command.
If you want to use svn info
, you need to use the "recursive" (-R
) argument to get the info from all of the subdirectories as well. Since the output then becomes a long list, you'll need to do some filtering to get the last changed revision number from all of those that is the highest:
svn info -R /path/to/your-projects-local-working-copy/. | awk '/^Last Changed Rev:/ {print $NF}' | sort -n | tail -n 1
What that command does is that it takes all of the lines that include the string "Last Changed Rev"
, then removes everything from each of those lines except the last field (i.e. the revision number), then sorts these lines numerically and removes everything but the last line, resulting in just the highest revision number. If you're running Windows, I'm sure you can do this quite easily in PowerShell as well, for example.
Just to be clear: the above approaches get you the recursive last changed revision number of just the path in the repo that your local working copy represents, for that local working copy, without hitting the server. So if someone has updated something in this path onto the repository server after your last svn update
, it won't be reflected in this output.
If what you want is the last changed revision of this path on the server, you can do:
svn info /path/to/your-projects-local-working-copy/.@HEAD | awk '/^Last Changed Rev:/ {print $NF}'