How to access the current Subversion build number?

前端 未结 16 2145
刺人心
刺人心 2020-11-27 05:43

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.

相关标签:
16条回答
  • 2020-11-27 06:01
    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 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. It's limited to that purpose though, but generally svn info and then processing is the way to go.

    0 讨论(0)
  • 2020-11-27 06:01

    If you are running under GNU/Linux, cd to the working copy's directory and run:

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

    From my experience, svn info does not give reliable numbers after renaming directories.

    0 讨论(0)
  • 2020-11-27 06:02

    If you have tortoise SVN you can use SubWCRev.exe

    Create a file called:

    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:

    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.

    0 讨论(0)
  • 2020-11-27 06:02

    Using c# and SharpSvn (from http://sharpsvn.net) the code would be:

    //using SharpSvn;
    long revision = -1;
    using(SvnClient client = new SvnClient())
    {
      client.Info(path,
        delegate(object sender, SvnInfoEventArgs e)
        {
           revision = e.Revision;
        });
    }
    
    0 讨论(0)
  • 2020-11-27 06:04

    In my latest project I solved this problem by using several tools, SVN, NAnt, and a custom NAnt task.

    1. Use NAnt to execute svn info --xml ./svnInfo.xml
    2. Use NAnt to pull the revision number from the xml file with <xmlpeek>
    3. Use a custom NAnt task to update the AssemblyVersion attribute in the AssemblyInfo.cs file with the latest with the version number (e.g., major.minor.maintenance, revision) before compiling the project.

    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/. However, I found that UpdateVersion did not meet my needs as the pin feature was broken in the build I had. Hence the custom NAnt task.

    If anyone is interested in the code, for the custom NAnt replace task let me know. 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.

    0 讨论(0)
  • 2020-11-27 06:05

    In Rails I use this snippet in my environment.rb which gives me a constant I can use throughout the application (like in the footer of an application layout).

    SVN_VERSION  = IO.popen("svn info").readlines[4].strip.split[1]
    
    0 讨论(0)
提交回复
热议问题