可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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 scan through all the directory's and subdirectory's files in order to determine the highest revision number.
Using svn info
on the root doesn't work (it just reports the version of that directory, not files in subdirectories):
I was wondering if there is a shortcut using the svn
command to do this. Otherwise, can anyone suggest a simple script that is network-efficient (I would prefer if it didn't hit the remote server at all)?
I also understand that one alternative approach is to keep a version file with the svn:keywords
. This works (I've used it on other projects), but I get tired of dealing with making sure the file is dirty and dealing with the inevitable merge conflicts.
Answer I see my problem lied with not doing a proper svn up
before calling svn info
in the root directory:
$ svn info
Path: .
...
Last Changed Author: fak
Last Changed Rev: 713
Last Changed Date: 2008-08-29 00:40:53 +0300 (Fri, 29 Aug 2008)
$ svn up
At revision 721.
$ svn info
Path: .
...
Revision: 721
Last Changed Author: reuben
Last Changed Rev: 721
Last Changed Date: 2008-08-31 22:55:22 +0300 (Sun, 31 Aug 2008)
回答1:
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)
回答2:
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}'
回答3:
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.
回答4:
I don't know if you are using MSBuild(Visual Studio) to build your binaries.
But if you would:
there is a connection possible between Subverion and MSBuild through
MSBuild Community Tasks Project
Here's part of our build script: our (C#) application gets the svn revision number included:
<SvnVersion LocalPath="$(MSBuildProjectDirectory)" ToolPath="installationpath\of\subversion\bin">
<Output TaskParameter="Revision" PropertyName="Revision" />
</SvnVersion>
<Message Text="Version: $(Major).$(Minor).$(Build).$(Revision)"/>
...
AssemblyVersion="$(Major).$(Minor).$(Build).$(Revision)"
AssemblyFileVersion="$(Major).$(Minor).$(Build).$(Revision)"
Jan
回答5:
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
.
回答6:
There is a program distributed with Subversion called svnversion that does exactly what you want to do. It's how we tag our websites.
回答7:
"svn info" will show you the working copy's revision number (see the "Revision" line in the output from "svn info"). Your build system probably allows you to place the relevant part of "svn info"'s output somewhere where it will be reflected in your application. For example, you may specify that when building, a temporary (un-versioned) file should be created, containing output from "svn info"; and you then include this file when compiling.
回答8:
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
回答9:
For me the best way to find out the last revision number of the trunk/branch is to get it from the remote URL. It is important NOT to use the working dir because it may be obsolete. Here is a snippet with batch ( I hate a much ;-)):
@for /f "tokens=4" %%f in ('svn info %SVNURL% ^|find "Last Changed Rev:"') do set lastPathRev=%%f
echo trunk rev no: %lastPathRev%
Nevertheless I have a problem to hardcode this number as interim version into sources containing $Rev:$. The problem is that $Rev:$ contains the file rev. no. So if trunk rev no is larger then the rev no of version file, I need to modify this file "artificially" and to commit it to get the correct interim version (=trunk version). This is a pane! Does somebody has better idea?
Many thanks
回答10:
This is ridiculous but svn info
or svnversion
wont take into consideration subdirectories; it's a feature called working 'Mixed Revisions' - I call it torture. I just needed to find the latest 'revision' of the live codebase and the hacked way below worked for me - it might take a while to run:
repo_root# find ./ | xargs -l svn info | grep 'Revision: ' | sort
...
Revision: 86
Revision: 86
Revision: 89
Revision: 90
root@fairware:/home/stage_vancity#