<p>I would like to start tagging my deployed binaries with the latest SVN revision number.</p>

<p>However, because SVN is file-based and not directory/project-based, I need to scan through all the directory's and subdirectory's files in order to determine the highest revision number.</p>

<p>Using <code>svn info</code> on the root doesn't work (it just reports the version of that directory, not files in subdirectories):</p>

<p>I was wondering if there is a shortcut using the <code>svn</code> command to do this. Otherwise, can anyone suggest a simple script that is network-efficient (I would prefer if it didn't hit the remote server at all)?</p>

<p>I also understand that one alternative approach is to keep a <em>version file</em> with the <code>svn:keywords</code>. This works (I've used it on other projects), but I get tired of dealing with making sure the file is dirty and dealing with the inevitable merge conflicts.</p>

<p><strong>Answer</strong> I see my problem lied with not doing a proper <code>svn up</code> before calling <code>svn info</code> in the root directory:</p>

<pre><code>$ svn info
Path: .
...
Last Changed Author: fak
Last Changed Rev: 713
Last Changed Date: 2008-08-29 00:40:53 +0300 (Fri, 29 Aug 2008)

$ svn up
At revision 721.

$ svn info
Path: .
...
Revision: 721
Last Changed Author: reuben
Last Changed Rev: 721
Last Changed Date: 2008-08-31 22:55:22 +0300 (Sun, 31 Aug 2008)
</code></pre>

[{'answer_id': 56240, 'author': 'Charles Miller', 'author_id': 3052, 'author_profile': 'https://Stackoverflow.com/users/3052', 'pm_score': 6, 'selected': True, 'text': '<p>One way. When you check out the code, look at the last line of svn output:</p>\n\n<pre><code>$ svn up\n...stuff...\nUpdated to revision 66593.\n</code></pre>\n\n<p>A more direct way:</p>\n\n<pre><code>$ svn info\nPath: .\nURL: https://svn.example.com/svn/myproject/trunk\nRepository Root: https://svn.example.com/svn/\nRepository UUID: d2a7a951-c712-0410-832a-9abccabd3052\nRevision: 66593\nNode Kind: directory\nSchedule: normal\nLast Changed Author: bnguyen\nLast Changed Rev: 66591\nLast Changed Date: 2008-09-11 18:25:27 +1000 (Thu, 11 Sep 2008)\n</code></pre>\n'}, {'answer_id': 56241, 'author': 'Troels Arvin', 'author_id': 4462, 'author_profile': 'https://Stackoverflow.com/users/4462', 'pm_score': 1, 'selected': False, 'text': '<p>"svn info" will show you the working copy\'s revision number (see the "Revision" line in the output from "svn info"). Your build system probably allows you to place the relevant part of "svn info"\'s output somewhere where it will be reflected in your application. For example, you may specify that when building, a temporary (un-versioned) file should be created, containing output from "svn info"; and you then include this file when compiling.</p>\n'}, {'answer_id': 56258, 'author': 'jan', 'author_id': 1163, 'author_profile': 'https://Stackoverflow.com/users/1163', 'pm_score': 2, 'selected': False, 'text': '<p>I don\'t know if you are using MSBuild(Visual Studio) to build your binaries.\nBut if you would: \nthere is a connection possible between Subverion and MSBuild through\n<a href="http://msbuildtasks.tigris.org/" rel="nofollow noreferrer">MSBuild Community Tasks Project</a> </p>\n\n<p>Here\'s part of our build script: our (C#) application gets the svn revision number included:</p>\n\n<pre><code>  &lt;SvnVersion LocalPath="$(MSBuildProjectDirectory)" ToolPath="installationpath\\of\\subversion\\bin"&gt;\n     &lt;Output TaskParameter="Revision" PropertyName="Revision" /&gt;\n  &lt;/SvnVersion&gt;\n  &lt;Message Text="Version: $(Major).$(Minor).$(Build).$(Revision)"/&gt;\n...\n    AssemblyVersion="$(Major).$(Minor).$(Build).$(Revision)"\n     AssemblyFileVersion="$(Major).$(Minor).$(Build).$(Revision)"\n</code></pre>\n\n<p>Jan</p>\n'}, {'answer_id': 56267, 'author': 'Blair Conrad', 'author_id': 1199, 'author_profile': 'https://Stackoverflow.com/users/1199', 'pm_score': 2, 'selected': False, 'text': "<p>The answers provided by @Charles Miller and @Troels Arvin are correct - you can use the output of the <code>svn update</code> or <code>svn info</code>, but as you hint, the latter only works if the repository is up to date. Then again, I'm not sure what value <em>any</em> revision number is going to be to you if part of your source tree is on a different revision than another part. It really sounds to me like you should be working on a homogeneous tree.\nI'd suggest either updating before running info (or if you've already updated for your build, you're golden) or using <code>svn info URL-to-source</code>. </p>\n"}, {'answer_id': 249905, 'author': 'hasseg', 'author_id': 4111, 'author_profile': 'https://Stackoverflow.com/users/4111', 'pm_score': 5, 'selected': False, 'text': '<p><a href="http://svnbook.red-bean.com/en/1.7/svn.ref.svnversion.re.html" rel="noreferrer"><code>svnversion</code></a> seems to be the cleanest way to do this:</p>\n\n<pre><code>svnversion -c /path/to/your-projects-local-working-copy/. | sed -e \'s/[MS]//g\' -e \'s/^[[:digit:]]*://\'\n</code></pre>\n\n<p>The above command will clean out any M and S letters (indicating local modifications or switchedness) from the output, as well as the smaller revision number in case <code>svnversion</code> returns a range instead of just one revision number (see <a href="http://svnbook.red-bean.com/en/1.1/re57.html" rel="noreferrer">the docs</a> for more info). If you don\'t want to filter the output, take out the pipe and the <code>sed</code> part of that command.</p>\n\n<p>If you want to use <code>svn info</code>, you need to use the "recursive" (<code>-R</code>) argument to get the info from all of the subdirectories as well. Since the output then becomes a long list, you\'ll need to do some filtering to get the last changed revision number from all of those that is the highest:</p>\n\n<pre><code>svn info -R /path/to/your-projects-local-working-copy/. | awk \'/^Last Changed Rev:/ {print $NF}\' | sort -n | tail -n 1\n</code></pre>\n\n<p>What that command does is that it takes all of the lines that include the string <code>"Last Changed Rev"</code>, then removes everything from each of those lines except the last field (i.e. the revision number), then sorts these lines numerically and removes everything but the last line, resulting in just the highest revision number. If you\'re running Windows, I\'m sure you can do this quite easily in PowerShell as well, for example.</p>\n\n<p>Just to be clear: the above approaches get you the recursive last changed revision number of just the path in the repo that your local working copy represents, <em>for that local working copy,</em> without hitting the server. So if someone has updated something in this path onto the repository server after your last <code>svn update</code>, it won\'t be reflected in this output. </p>\n\n<p>If what you want is the last changed revision of this path <em>on the server</em>, you can do:</p>\n\n<pre><code>svn info /path/to/your-projects-local-working-copy/.@HEAD | awk \'/^Last Changed Rev:/ {print $NF}\'\n</code></pre>\n'}, {'answer_id': 250254, 'author': 'Sam Stokes', 'author_id': 20131, 'author_profile': 'https://Stackoverflow.com/users/20131', 'pm_score': 3, 'selected': False, 'text': '<p>Duplicate of <a href="https://stackoverflow.com/questions/110175/how-to-access-the-current-subversion-build-number#112674">this question</a>.  As I posted there, the <code>svnversion</code> command is your friend.  No need to parse the output, no need to update first, just does the job.</p>\n'}, {'answer_id': 250258, 'author': 'David', 'author_id': 26144, 'author_profile': 'https://Stackoverflow.com/users/26144', 'pm_score': 2, 'selected': False, 'text': '<p>There is a program distributed with Subversion called <a href="http://svnbook.red-bean.com/en/1.1/re57.html" rel="nofollow noreferrer">svnversion</a> that does exactly what you want to do. It\'s how we tag our websites.</p>\n'}, {'answer_id': 13057020, 'author': 'Daniel Sokolowski', 'author_id': 913223, 'author_profile': 'https://Stackoverflow.com/users/913223', 'pm_score': 0, 'selected': False, 'text': "<p>This is ridiculous but <code>svn info</code> or <code>svnversion</code> wont take into consideration subdirectories; it's a feature called working 'Mixed Revisions' - I call it torture. I just needed to find the latest 'revision' of the live codebase and the hacked way below worked for me - it might take a while to run:</p>\n\n<pre><code>repo_root# find ./ | xargs -l svn info  | grep 'Revision: ' | sort\n...\nRevision: 86\nRevision: 86\nRevision: 89\nRevision: 90\nroot@fairware:/home/stage_vancity#\n</code></pre>\n"}, {'answer_id': 14368458, 'author': 'Maria Ananieva', 'author_id': 1985264, 'author_profile': 'https://Stackoverflow.com/users/1985264', 'pm_score': 1, 'selected': False, 'text': '<p>For me the best way to find out the last revision number of the trunk/branch is to get it from the remote URL. It is important NOT to use the working dir because it may be obsolete. Here is a snippet with batch ( I hate a much ;-)):</p>\n\n<pre><code>@for /f "tokens=4" %%f in (\'svn info %SVNURL% ^|find "Last Changed Rev:"\') do set lastPathRev=%%f\n\necho trunk rev no: %lastPathRev%\n</code></pre>\n\n<p>Nevertheless I have a problem to hardcode this number as interim version into sources containing $Rev:$. The problem is that $Rev:$ contains the file rev. no. So if trunk rev no is larger then the rev no of version file, I need to modify this file "artificially" and to commit it to get the correct interim version (=trunk version). This is a pane! Does somebody has better idea? \nMany thanks</p>\n'}, {'answer_id': 34048048, 'author': 'KymikoLoco', 'author_id': 2196304, 'author_profile': 'https://Stackoverflow.com/users/2196304', 'pm_score': 2, 'selected': False, 'text': '<p>If you just want the revision number of the latest change that was committed, and are using Windows without grep/awk/xargs, here is the bare-bones command to run (command line):</p>\n\n<pre><code>X:\\trunk&gt;svn info -r COMMITTED | for /F "tokens=2" %r in (\'findstr /R "^Revision"\') DO @echo %r\n67000\n</code></pre>\n\n<p><code>svn info -r COMMITTED</code> will give you the latest committed change to the directory you are currently in:</p>\n\n<pre><code>X:\\Trunk&gt;svn info -r COMMITTED\nPath: trunk\nURL: https://svn.example.com/svn/myproject/trunk\nRepository Root: https://svn.example.com/svn/\nRepository UUID: d2a7a951-c712-0410-832a-9abccabd3052\nRevision: 67400\nNode Kind: directory\nLast Changed Author: example\nLast Changed Rev: 67400\nLast Changed Date: 2008-09-11 18:25:27 +1000 (Thu, 11 Sep 2008)\n</code></pre>\n\n<p>The for loop runs <code>findstr</code> to locate the Revision portion of the output from <code>svn info</code>. The output from this will be (you won\'t see this): </p>\n\n<pre><code>Revision: 67000\n</code></pre>\n\n<p>Which then splits the <a href="http://ss64.com/nt/for_cmd.html" rel="nofollow">tokens</a>, and returns the 2nd, to be <code>echo</code>ed out:</p>\n\n<pre><code>67000\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/56227', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/338/']