How can I get the Subversion revision number in PHP?
SVN keywords is not a good solution. As others pointed out adding $Revision$ in a file only affects the specific file, which may not change for a long time.
Remembering to "edit" a file (by adding or removing a blank line) before every commit is pointless. You could as well just type the revision by hand.
One good way to do it (that I know of) is to have an automated deployment process (which is always a good thing) and using the command svnversion. Here is what I do:
Wherever I need the revision I do an include: <?php include 'version.php'; ?>
. This "version.php" file only has the revision number. Moreover it is not part of the repository (it set to be ignored). Here is how I create it:
1) On projects where SVN is installed on the server, I also use it for deployment. Getting the latest version to the server I have a script that among other things does the following (it runs on the server):
cd /var/www/project
svn update
rm version.php
svnversion > version.php
2) On projects where SVN is not installed my deployment script is more complex: it creates the version.php file locally, zips the code, uploads and extracts it
How to programmatically get SVN revision number?
Subversion includes the svnversion tool for exactly this purpose. A working copy may actually have local modifications, or may consist of a mix of revisions. svnversion
knows how to handle that; see the examples in the linked documentation.
You can invoke svnversion
from python like this:
import subprocess
def svnversion():
p = subprocess.Popen("svnversion", stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = p.communicate()
return stdout
Getting the last revision number in SVN?
<?php
$url = 'your repository here';
$output = `svn info $url`;
echo "<pre>$output</pre>";
?>
You can get the output in XML like so:
$output = `svn info $url --xml`;
If there is an error then the output will be directed to stderr. To capture stderr in your output use thusly:
$output = `svn info $url 2>&1`;
Easy way to embed svn revision number in page in PHP?
You can use the svnversion
CLI utility to get a more specific look at the revision, including the highest number. You could then use regular expressions to parse this.
Subversion has no concept of a global revision; rather, you'd have to recursively look through the working copy to find the highest revision number. svnversion
does that for you.
How to connect to remote svn server and retrieve latest revision number
Easy version of Ben answer
In order to get latest revision for repository (or path inside repo) you can
- Call OS-commmand
svn info URL/TO/REPO
inside PHP, parse answer and extract relevant string (if you have TortoieSVN, you have or can have CLI-version of svn client)
Sample from Assembla's repo
>svn info https://subversion.assembla.com/svn/subversion-troubleshoot-b/
Path: subversion-troubleshoot-b
URL: https://subversion.assembla.com/svn/subversion-troubleshoot-b
Relative URL: ^/
Repository Root: https://subversion.assembla.com/svn/subversion-troubleshoot-b
Repository UUID: faaf0d5a-5d64-45f4-9e2a-979cddfce0d6
Revision: 7
Node Kind: directory
Last Changed Author: abream
Last Changed Rev: 7
Last Changed Date: 2012-10-17 22:14:36 +0600 (Ср, 17 окт 2012)
You'll use string Revision
for global repository (or some subtree) Revision-ID
- Get PHP SVN Client class and use it's method GetVersion()
Automatic commit/revision number in a php file or txt file
You could use the SubWCRev program that comes with Tortoise to do this - the official page is here: http://tortoisesvn.net/docs/nightly/TortoiseSVN_en/tsvn-subwcrev.html
I've found if you navigate to a working copy folder in the windows command prompt and run SubWCRev . > version.txt
you'll get the following in the version.txt file:
SubWCRev: 'C:\Users\User\SVN\repo1'
Last committed at revision X
Updated to revision Y
Local modifications found
Unversioned items found
Hope that gets you what you want.
Portably retrieve Subversion 1.7 revision in PHP?
You can write a script to svn update
for you every time, and scrap the revision number from the update's output into a file. The following bash script should more or less do it:
#!/bin/bash
svn --non-interactive update ..
svn --non-interactive update .. | perl -p -e "s/.* revision ([\d]*)\./\$1/" > ../version.phtml;
Related Topics
Using Usort in PHP with a Class Private Function
Understanding MVC: Whats the Concept of "Fat" on Models, "Skinny" on Controllers
When *Not* to Use Prepared Statements
Why Does 1234 == '1234 Test' Evaluate to True
PHP Namespace with Dynamic Class Name
Flatten Multidimensional Array Concatenating Keys
Logging in to Joomla 1.5 Using External Form (Not Within Joomla Folder, But on Same Server)
Comment Associative Array in PHP Documentor
Phpexcel Auto Size Column Width
What Security Issues Should I Look Out for in PHP
Php.Ini Changes, But Not Effective on Ubuntu
Multiple Index Variables in PHP Foreach Loop