简体   繁体   English

如何访问当前的Subversion内部版本号?

[英]How to access the current Subversion build number?

How can you automatically import the latest build/revision number in subversion? 如何在subversion中自动导入最新的构建/修订号?

The goal would be to have that number visible on your webpage footer like SO does. 我们的目标是让您的网页页脚上的数字显示为SO。

Have your build process call the svnversion command, and embed its output into generated {source|binaries}. 让构建过程调用svnversion命令,并将其输出嵌入到生成的{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

The svnversion command is the correct way to do this. svnversion命令是执行此操作的正确方法。 It outputs the revision number your entire working copy is at, or a range of revisions if your working copy is mixed (eg some directories are up to date and some aren't). 如果您的工作副本混合,它会输出您的整个工作副本所在的修订号,或者输出一系列修订版(例如,某些目录是最新的,而某些目录不是)。 It will also indicate if the working copy has local modifications. 它还将指示工作副本是否具有本地修改。 For example, in a rather unclean working directory: 例如,在一个相当不干净的工作目录中:

$ svnversion
662:738M

The $Revision$ keyword doesn't do what you want: it only changes when the containing file does. $ Revision $关键字不能达到您想要的效果:它仅在包含文件时才会更改。 The Subversion book gives more detail . Subversion书提供了更多细节 The "svn info" command also doesn't do what you want, as it only tells you the state of your current directory, ignoring the state of any subdirectories. “svn info”命令也不能执行您想要的操作,因为它只会告诉您当前目录的状态,而忽略任何子目录的状态。 In the same working tree as the previous example, I had some subdirectories which were newer than the directory I was in, but "svn info" doesn't notice: 在与前一个示例相同的工作树中,我有一些比我所在的目录更新的子目录,但是“svn info”没有注意到:

$ svn info
... snip ...
Revision: 662

It's easy to incorporate svnversion into your build process, so that each build gets the revision number in some runtime-accessible form. 将svnversion合并到构建过程中很容易,因此每个构建都以一些运行时可访问的形式获取修订号。 For a Java project, for example, I had our makefile dump the svnversion output into a .properties file. 例如,对于Java项目,我让makefile将svnversion输出转储到.properties文件中。

svn info <Repository-URL>

or 要么

svn info --xml <Repository-URL>

Then look at the result. 然后看看结果。 For xml, parse /info/entry/@revision for the revision of the repository (151 in this example) or /info/entry/commit/@revision for the revision of the last commit against this path (133, useful when working with tags): 对于xml,对存储库的修订版本的parse / info / entry / @ revision(本例中为151)或/ info / entry / commit / @对于此路径的最后一次提交的修订版本(133,在使用时很有用)标签):

<?xml version="1.0"?>
<info>
<entry
   kind="dir"
   path="cmdtools"
   revision="151">
<url>http://myserver/svn/stumde/cmdtools</url>
<repository>
<root>http://myserver/svn/stumde</root>
<uuid>a148ce7d-da11-c240-b47f-6810ff02934c</uuid>
</repository>
<commit
   revision="133">
<author>mstum</author>
<date>2008-07-12T17:09:08.315246Z</date>
</commit>
</entry>
</info>

I wrote a tool ( cmdnetsvnrev , source code included) for myself which replaces the Revision in my AssemblyInfo.cs files. 我为自己编写了一个工具( cmdnetsvnrev ,包含源代码),它取代了AssemblyInfo.cs文件中的Revision。 It's limited to that purpose though, but generally svn info and then processing is the way to go. 但它仅限于此目的,但通常svn信息然后处理是要走的路。

Add svn:keywords to the SVN properties of the source file: 将svn:keywords添加到源文件的SVN属性中:

svn:keywords Revision

Then in the source file include: 然后在源文件中包含:

private const string REVISION = "$Revision$";

The revision will be updated with the revision number at the next commit to (eg) "$Revision: 4455$" . 修订版将在下次提交时使用修订号更新(例如) "$Revision: 4455$" You can parse this string to extract just the revision number. 您可以解析此字符串以仅提取修订号。

If you have tortoise SVN you can use SubWCRev.exe 如果你有陆龟SVN,你可以使用SubWCRev.exe

Create a file called: 创建一个名为的文件:

RevisionInfo.tmpl RevisionInfo.tmpl

SvnRevision = $WCREV$;

Then execute this command: 然后执行以下命令:

SubWCRev.exe . RevisionInfo.tmpl RevisionInfo.txt

It will create a file ReivisonInfo.txt with your revision number as follows: 它将使用您的修订号创建一个ReivisonInfo.txt文件,如下所示:

SvnRevision = 5000;

But instead of using the .txt you could use whatever source file you want, and have access to the reivsion number within your source code. 但是,您可以使用所需的任何源文件,而不是使用.txt,并且可以访问源代码中的reivsion编号。

You don't say what programming language/framework you're using. 您没有说明您正在使用的编程语言/框架。 Here's how to do it in Python using PySVN 以下是使用PySVN在Python中执行此操作的方法

import pysvn
repo = REPOSITORY_LOCATION
rev = pysvn.Revision( pysvn.opt_revision_kind.head )
client = pysvn.Client()
info = client.info2(repo,revision=rev,recurse=False)
revno = info[0][1].rev.number # revision number as an integer

Using c# and SharpSvn (from http://sharpsvn.net ) the code would be: 使用c#和SharpSvn(来自http://sharpsvn.net )代码将是:

//using SharpSvn;
long revision = -1;
using(SvnClient client = new SvnClient())
{
  client.Info(path,
    delegate(object sender, SvnInfoEventArgs e)
    {
       revision = e.Revision;
    });
}

Here is a hint, how you might use Netbeans ' capabilities to 这是一个提示,您可以如何使用Netbeans的功能
create custom ant tasks which would generate scm-version.txt: 创建自定义ant任务 ,生成scm-version.txt:

Open your build.xml file, and add following code right after 打开你的build.xml文件,右添加以下代码
<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 . 现在,每次进行清理/构建时 ,Netbeans都会将Subversion版本字符串编译为scm-version.txt

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 . 不要忘记将文件scm-version.txt标记为svn:ignore

In my latest project I solved this problem by using several tools, SVN, NAnt, and a custom NAnt task. 在我的最新项目中,我使用了几个工具,SVN,NAnt和自定义的NAnt任务解决了这个问题。

  1. Use NAnt to execute svn info --xml ./svnInfo.xml 使用NAnt执行svn info --xml ./svnInfo.xml
  2. Use NAnt to pull the revision number from the xml file with <xmlpeek> 使用NAnt从<xmlpeek>的xml文件中提取修订号
  3. Use a custom NAnt task to update the AssemblyVersion attribute in the AssemblyInfo.cs file with the latest with the version number (eg, major.minor.maintenance, revision) before compiling the project. 在编译项目之前,使用自定义NAnt任务更新AssemblyInfo.cs文件中的AssemblyVersion属性,其中包含最新版本号(例如major.minor.maintenance,revision)。

The version related sections of my build script look like this: 我的构建脚本的版本相关部分如下所示:

<!-- Retrieve the current revision number for the working directory -->
<exec program="svn" commandline='info --xml' output="./svnInfo.xml" failonerror="false"/>
<xmlpeek file="./svnInfo.xml" xpath="info/entry/@revision" property="build.version.revision" if="${file::exists('./svnInfo.xml')}"/>

<!-- Custom NAnt task to replace strings matching a pattern with a specific value -->
<replace file="${filename}" 
         pattern="AssemblyVersion(?:Attribute)?\(\s*?\&quot;(?&lt;version&gt;(?&lt;major&gt;[0-9]+)\.(?&lt;minor&gt;[0-9]+)\.(?&lt;build&gt;[0-9]+)\.(?&lt;revision&gt;[0-9]+))\&quot;\s*?\)" 
         value="AssemblyVersion(${build.version})"
         outfile="${filename}"/>

The credit for the regular expression goes to: http://code.mattgriffith.net/UpdateVersion/ . 正则表达式的功劳归结为: http//code.mattgriffith.net/UpdateVersion/ However, I found that UpdateVersion did not meet my needs as the pin feature was broken in the build I had. 但是,我发现UpdateVersion无法满足我的需求,因为我的构建中的引脚功能被破坏了。 Hence the custom NAnt task. 因此自定义的NAnt任务。

If anyone is interested in the code, for the custom NAnt replace task let me know. 如果有人对代码感兴趣,对于自定义NAnt replace任务,请告诉我。 Since this was for a work related project I will need to check with management to see if we can release it under a friendly (free) license. 由于这是一个与工作相关的项目,我需要与管理层核实,看看我们是否可以在友好(免费)许可下发布。

在Rails中,我在我的environment.rb中使用了这个片段,它给了我一个可以在整个应用程序中使用的常量(比如在应用程序布局的页脚中)。

SVN_VERSION  = IO.popen("svn info").readlines[4].strip.split[1]

好吧,你可以运行'svn info'来确定当前的版本号,你可以用正则表达式轻松地提取它,比如“Revision:([0-9] +)”。

If you are running under GNU/Linux, cd to the working copy's directory and run: 如果您在GNU / Linux下运行,请cd到工作副本的目录并运行:

svn -u status | grep Status\ against\ revision: | awk '{print $4}'

From my experience, svn info does not give reliable numbers after renaming directories. 根据我的经验,svn info在重命名目录后没有给出可靠的数字。

You want the Subversion info subcommand, as follows: 您需要Subversion info子命令,如下所示:

$ 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. 在这种情况下,有两个修订号:4190和3397. 4190是存储库的最后一个修订号,3397是该工作区签出的子树的最后一次更改的修订号。 You can specify a path to a workspace, or a URL to a repository. 您可以指定工作空间的路径或存储库的URL。

AC# fragment to extract this under Windows would look something like this: 在Windows下提取的AC#片段看起来像这样:

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.) (在我的例子中,我们使用Subversion修订版作为程序集版本号的一部分。)

I use the MSBuild Community Tasks project which has a tool to retrieve the SVN revision number and add it to your AssemblyInfo.vb. 我使用MSBuild社区任务项目,该项目有一个工具来检索SVN修订版号并将其添加到AssemblyInfo.vb。 You can then use reflection to retrieve this string to display it in your UI. 然后,您可以使用反射来检索此字符串以在UI中显示它。

Here's a full blog post with instructions . 这是一篇包含说明完整博客文章

if you're using svnant, you can use wcVersion, which duplicates svnveresion and returns digestible values. 如果你正在使用svnant,你可以使用wcVersion,它复制svnveresion并返回易消化的值。 see: http://subclipse.tigris.org/svnant/svn.html#wcVersion 请参阅: http//subclipse.tigris.org/svnant/svn.html#wcVersion

I created an SVN version plug-in for the Build Version Increment project on CodePlex. 我在CodePlex上为Build Version Increment项目创建了一个SVN版本插件 This SVN plug-in will pull the latest change revision number from your working copy and allow you to use that in your version number, which should accomplish exactly what you're trying to do. 此SVN插件将从您的工作副本中提取最新的更改修订版号,并允许您在版本号中使用该编号,这应该完全符合您的要求。 Build Version Increment is pretty flexible and will allow you to set up how the versioning is done in a number of ways. 构建版本增量非常灵活,允许您以多种方式设置版本控制的完成方式。

BVI only works with Visual Studio, but since you're using Asp.Net, that won't be a problem. BVI仅适用于Visual Studio,但由于您使用的是Asp.Net,因此不会出现问题。 It doesn't require writing any code or editing xml, so yay! 它不需要编写任何代码或编辑xml,所以耶!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM