如何在subversion中自动导入最新的构建/修订号?
目标是让您的网页页脚上显示该号码,就像SO一样。
答案 0 :(得分:31)
让您的构建过程调用svnversion命令,并将其输出嵌入到生成的{source | binaries}中。这不仅会给出当前版本(这里有许多其他示例),但是它的输出字符串还将告诉构建是在混合树还是树中完成,这与所讨论的版本号不完全匹配(即一棵有局部变化的树。)
使用标准树:
$ svnversion
3846
使用修改后的树:
$ echo 'foo' >> project-ext.dtd
$ svnversion
3846M
使用混合修订,修改后的树:
$ (cd doc; svn up >/dev/null 2>/dev/null)
$ svnversion
3846:4182M
答案 1 :(得分:19)
svnversion
命令是执行此操作的正确方法。如果您的工作副本是混合的(例如某些目录是最新的,而某些目录不是),它会输出您的整个工作副本所在的修订号,或者一系列修订版。它还将指示工作副本是否具有本地修改。例如,在一个相当不干净的工作目录中:
$ svnversion
662:738M
$ Revision $关键字无法执行您想要的操作:它仅在包含文件时更改。 Subversion的书给出了more detail。 “svn info”命令也不能执行您想要的操作,因为它只会告诉您当前目录的状态,而忽略任何子目录的状态。在与前一个示例相同的工作树中,我有一些子目录比我所在的目录更新,但“svn info”没有注意到:
$ svn info
... snip ...
Revision: 662
将svnversion合并到构建过程中很容易,因此每个构建都以某种运行时可访问的形式获取修订号。例如,对于Java项目,我让makefile将svnversion输出转储到.properties文件中。
答案 2 :(得分:14)
svn info <Repository-URL>
或
svn info --xml <Repository-URL>
然后看看结果。对于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>
我为自己编写了一个工具(cmdnetsvnrev,包含源代码),取代了AssemblyInfo.cs文件中的Revision。但它仅限于此目的,但通常svn信息然后处理是可行的方法。
答案 3 :(得分:12)
将svn:keywords添加到源文件的SVN属性中:
svn:keywords Revision
然后在源文件中包含:
private const string REVISION = "$Revision$";
修订版将在下次提交(例如)"$Revision: 4455$"
时使用修订号更新。您可以解析此字符串以仅提取修订号。
答案 4 :(得分:7)
如果您已经使用了SVN,可以使用SubWCRev.exe
创建一个名为:
的文件RevisionInfo.tmpl
SvnRevision = $WCREV$;
然后执行以下命令:
SubWCRev.exe . RevisionInfo.tmpl RevisionInfo.txt
它将使用您的修订号创建一个ReivisonInfo.txt文件,如下所示:
SvnRevision = 5000;
但是,您可以使用所需的任何源文件,而不是使用.txt,并且可以访问源代码中的reivsion编号。
答案 5 :(得分:5)
您没有说明您正在使用的编程语言/框架。以下是使用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
答案 6 :(得分:5)
使用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;
});
}
答案 7 :(得分:3)
在我的最新项目中,我使用了几个工具,SVN,NAnt和自定义的NAnt任务解决了这个问题。
svn info --xml ./svnInfo.xml
<xmlpeek>
我的构建脚本的版本相关部分如下所示:
<!-- 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*?\"(?<version>(?<major>[0-9]+)\.(?<minor>[0-9]+)\.(?<build>[0-9]+)\.(?<revision>[0-9]+))\"\s*?\)"
value="AssemblyVersion(${build.version})"
outfile="${filename}"/>
正则表达式的功劳归于:http://code.mattgriffith.net/UpdateVersion/。但是,我发现UpdateVersion无法满足我的需求,因为我的构建中的引脚功能被破坏了。因此自定义NAnt任务。
如果有人对代码感兴趣,请告诉我自定义的NAnt replace
任务。由于这是与工作相关的项目,我需要与管理层核实,看看我们是否可以在友好(免费)许可下发布。
答案 8 :(得分:3)
这是一个提示,您可以如何使用 Netbeans '功能 创建自定义ant任务,这将生成scm-version.txt:
打开 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>
现在,Netbeans编译了Subversion版本字符串 每次进行清理/构建时 scm-version.txt 。
您可以通过执行以下操作在运行时读取文件:
getClass().getResourceAsStream("scm-version.txt"); // ...
不要忘记将文件 scm-version.txt 标记为 svn:ignore 。
答案 9 :(得分:2)
在Rails中,我在我的environment.rb中使用了这个片段,它给了我一个常量,我可以在整个应用程序中使用(比如在应用程序布局的页脚中)。
SVN_VERSION = IO.popen("svn info").readlines[4].strip.split[1]
答案 10 :(得分:1)
好吧,你可以运行'svn info'来确定当前的版本号,你可以用正则表达式轻松地提取它,比如“Revision:([0-9] +)”。
答案 11 :(得分:1)
如果您在GNU / Linux下运行,请cd到工作副本的目录并运行:
svn -u status | grep Status\ against\ revision: | awk '{print $4}'
根据我的经验,重命名目录后,svn info不会提供可靠的数字。
答案 12 :(得分:0)
您需要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)
在这种情况下,有两个修订号:4190和3397. 4190是存储库的最后一个修订号,3397是该工作区签出的子树的最后一次更改的修订号。您可以指定工作空间的路径或存储库的URL。
在Windows下提取此内容的C#片段如下所示:
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;
}
}
}
(在我的例子中,我们使用Subversion修订版作为程序集版本号的一部分。)
答案 13 :(得分:0)
我使用MSBuild Community Tasks项目,该项目有一个工具来检索SVN修订版号并将其添加到AssemblyInfo.vb中。然后,您可以使用反射来检索此字符串,以在UI中显示它。
答案 14 :(得分:0)
如果你正在使用svnant,你可以使用wcVersion,它复制svnveresion并返回易消化的值。看到: http://subclipse.tigris.org/svnant/svn.html#wcVersion
答案 15 :(得分:0)
我为CodePlex上的SVN version plug-in项目创建了Build Version Increment。此SVN插件将从您的工作副本中提取最新的更改修订版号,并允许您在版本号中使用该编号,这应该完全符合您的要求。 Build Version Increment非常灵活,允许您以多种方式设置版本控制的方式。
BVI仅适用于Visual Studio,但由于您使用的是Asp.Net,因此不会出现问题。它不需要编写任何代码或编辑xml,所以耶!