How can you automatically import the latest build/revision number in subversion?
The goal would be to have that number visible on your webpage footer like SO does.
Have your build process call the svnversion command, and embed its output into generated {source|binaries}. This will not only give the current revision (as many other examples here do), but its output string will also tell whether a build is being done in a mixed tree or a tree which doesn't exactly match the revision number in question (ie. a tree with local changes).
With a standard tree:
$ svnversion
3846
With a modified tree:
$ echo 'foo' >> project-ext.dtd
$ svnversion
3846M
With a mixed-revision, modified tree:
$ (cd doc; svn up >/dev/null 2>/dev/null)
$ svnversion
3846:4182M
You want the Subversion info subcommand, as follows:
$ svn info .
Path: .
URL: http://trac-hacks.org/svn/tracdeveloperplugin/trunk
Repository Root: http://trac-hacks.org/svn
Repository UUID: 7322e99d-02ea-0310-aa39-e9a107903beb
Revision: 4190
Node Kind: directory
Schedule: normal
Last Changed Author: coderanger
Last Changed Rev: 3397
Last Changed Date: 2008-03-19 00:49:02 -0400 (Wed, 19 Mar 2008)
In this case, there are two revision numbers: 4190 and 3397. 4190 is the last revision number for the repository, and 3397 is the revision number of the last change to the subtree that this workspace was checked out from. You can specify a path to a workspace, or a URL to a repository.
A C# fragment to extract this under Windows would look something like this:
Process process = new Process();
process.StartInfo.FileName = @"svn.exe";
process.StartInfo.Arguments = String.Format(@"info {0}", path);
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
// Parse the svn info output for something like "Last Changed Rev: 1234"
using (StreamReader output = process.StandardOutput)
{
Regex LCR = new Regex(@"Last Changed Rev: (\d+)");
string line;
while ((line = output.ReadLine()) != null)
{
Match match = LCR.Match(line);
if (match.Success)
{
revision = match.Groups[1].Value;
}
}
}
(In my case, we use the Subversion revision as part of the version number for assemblies.)
Here is a hint, how you might use Netbeans' capabilities to
create custom ant tasks which would generate scm-version.txt:
Open your build.xml file, and add following code right after
<import file="nbproject/build-impl.xml"/>
<!-- STORE SUBVERSION BUILD STRING -->
<target name="-pre-compile">
<exec executable="svnversion"
output="${src.dir}/YOUR/PACKAGE/NAME/scm-version.txt"/>
</target>
Now, Netbeans strores the Subversion version string to scm-version.txt everytime you make clean/build.
You can read the file during runtime by doing:
getClass().getResourceAsStream("scm-version.txt"); // ...
Don't forget to mark the file scm-version.txt as svn:ignore.
Well, you can run 'svn info' to determine the current revision number, and you could probably extract that pretty easily with a regex, like "Revision: ([0-9]+)".