Automatically create subversion revision file
I have a project who's structure in Subversion goes as follows,
branch
tags
trunk
-- Library
---- Library Code
-- Website
---- Website Code
The Library folder contains most of the project cod开发者_JAVA技巧e and is a class library. The Website folder is a website project which uses the library. The website is what gets deployed to the live site.
I would like to automatically create a file in the root of the website directory that stores the latest revision number of the trunk directory. Is this possible and if so how would I go about it?
The subversion server is VisualSVN on Windows using TortoiseSVN with VisualSVN for the client.
Since you're using TortoiseSVN, I suggest using SubWCRev for this task instead of svnversion.
Well, the usual way of including a revision number is to put
$Revision$
somewhere into the source file which gets expanded on commit, provided the property svn:keywords
property contains Revision
.
That will only give you the revision number when that file was last committed, though, not the current HEAD
revision number.
In a past project I used a little script that called the command line subversion program to extract the current revision number. You can call svn info
and get all kinds of information back, including the revision number of the working copy. This gets updated for a directory when you perform an update on that directory or one of its parents and for files when they are actually updated so it might not be fully accurate. However it was enough for me at that time as I only ever updated the top-level folder anyway:
>svn info trunk | findstr /r /c:"^Revision"
Revision: 14466
There's a command line tool called svnversion that can be used to generate the value you need. However, you still need to execute it manually. And there's an additional issue:
If you want to store the result into the repository, you'll have to execute it before you commit and the stored value will always be outdated.
If you want to keep the result unversioned, you'll have to execute after you commit. But then it won't get exported.
So there's no simple answer to the question. A proposal:
- Have a version.txt file or whatever with the $Revision$ keyword.
- Write a pre-commit hook script that rejects the commit if version.txt has not been modified
- Optionally, write a TortoiseSVN client-side hook script to modify the file automatically on before commit.
Have a look at the svn info
command. Running this as a pre/post build event in your project will get you the information you need (and some additional information). You then need to write the revision number to a file. (If you do this, make sure you SVN ignore this file otherwise it will enter an endless loop of the file changing, you checking it it, the file changing, etc...)
精彩评论