簡體   English   中英

如何訪問當前的Subversion內部版本號?

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

如何在subversion中自動導入最新的構建/修訂號?

我們的目標是讓您的網頁頁腳上的數字顯示為SO。

讓構建過程調用svnversion命令,並將其輸出嵌入到生成的{source | binaries}中。 這不僅會給出當前版本(這里有許多其他示例),但是它的輸出字符串還將告訴構建是在混合樹還是樹中完成,這與所討論的版本號不完全匹配(即一棵有局部變化的樹。

使用標准樹:

$ svnversion
3846

使用修改后的樹:

$ echo 'foo' >> project-ext.dtd
$ svnversion                   
3846M

使用混合修訂,修改后的樹:

$ (cd doc; svn up >/dev/null 2>/dev/null)
$ svnversion
3846:4182M

svnversion命令是執行此操作的正確方法。 如果您的工作副本混合,它會輸出您的整個工作副本所在的修訂號,或者輸出一系列修訂版(例如,某些目錄是最新的,而某些目錄不是)。 它還將指示工作副本是否具有本地修改。 例如,在一個相當不干凈的工作目錄中:

$ svnversion
662:738M

$ Revision $關鍵字不能達到您想要的效果:它僅在包含文件時才會更改。 Subversion書提供了更多細節 “svn info”命令也不能執行您想要的操作,因為它只會告訴您當前目錄的狀態,而忽略任何子目錄的狀態。 在與前一個示例相同的工作樹中,我有一些比我所在的目錄更新的子目錄,但是“svn info”沒有注意到:

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

將svnversion合並到構建過程中很容易,因此每個構建都以一些運行時可訪問的形式獲取修訂號。 例如,對於Java項目,我讓makefile將svnversion輸出轉儲到.properties文件中。

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信息然后處理是要走的路。

將svn:keywords添加到源文件的SVN屬性中:

svn:keywords Revision

然后在源文件中包含:

private const string REVISION = "$Revision$";

修訂版將在下次提交時使用修訂號更新(例如) "$Revision: 4455$" 您可以解析此字符串以僅提取修訂號。

如果你有陸龜SVN,你可以使用SubWCRev.exe

創建一個名為的文件:

RevisionInfo.tmpl

SvnRevision = $WCREV$;

然后執行以下命令:

SubWCRev.exe . RevisionInfo.tmpl RevisionInfo.txt

它將使用您的修訂號創建一個ReivisonInfo.txt文件,如下所示:

SvnRevision = 5000;

但是,您可以使用所需的任何源文件,而不是使用.txt,並且可以訪問源代碼中的reivsion編號。

您沒有說明您正在使用的編程語言/框架。 以下是使用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

使用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;
    });
}

這是一個提示,您可以如何使用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

在我的最新項目中,我使用了幾個工具,SVN,NAnt和自定義的NAnt任務解決了這個問題。

  1. 使用NAnt執行svn info --xml ./svnInfo.xml
  2. 使用NAnt從<xmlpeek>的xml文件中提取修訂號
  3. 在編譯項目之前,使用自定義NAnt任務更新AssemblyInfo.cs文件中的AssemblyVersion屬性,其中包含最新版本號(例如major.minor.maintenance,revision)。

我的構建腳本的版本相關部分如下所示:

<!-- 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}"/>

正則表達式的功勞歸結為: http//code.mattgriffith.net/UpdateVersion/ 但是,我發現UpdateVersion無法滿足我的需求,因為我的構建中的引腳功能被破壞了。 因此自定義的NAnt任務。

如果有人對代碼感興趣,對於自定義NAnt replace任務,請告訴我。 由於這是一個與工作相關的項目,我需要與管理層核實,看看我們是否可以在友好(免費)許可下發布。

在Rails中,我在我的environment.rb中使用了這個片段,它給了我一個可以在整個應用程序中使用的常量(比如在應用程序布局的頁腳中)。

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

好吧,你可以運行'svn info'來確定當前的版本號,你可以用正則表達式輕松地提取它,比如“Revision:([0-9] +)”。

如果您在GNU / Linux下運行,請cd到工作副本的目錄並運行:

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

根據我的經驗,svn info在重命名目錄后沒有給出可靠的數字。

您需要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下提取的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;
        }
    }
}

(在我的例子中,我們使用Subversion修訂版作為程序集版本號的一部分。)

我使用MSBuild社區任務項目,該項目有一個工具來檢索SVN修訂版號並將其添加到AssemblyInfo.vb。 然后,您可以使用反射來檢索此字符串以在UI中顯示它。

這是一篇包含說明完整博客文章

如果你正在使用svnant,你可以使用wcVersion,它復制svnveresion並返回易消化的值。 請參閱: http//subclipse.tigris.org/svnant/svn.html#wcVersion

我在CodePlex上為Build Version Increment項目創建了一個SVN版本插件 此SVN插件將從您的工作副本中提取最新的更改修訂版號,並允許您在版本號中使用該編號,這應該完全符合您的要求。 構建版本增量非常靈活,允許您以多種方式設置版本控制的完成方式。

BVI僅適用於Visual Studio,但由於您使用的是Asp.Net,因此不會出現問題。 它不需要編寫任何代碼或編輯xml,所以耶!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM