ACs
ACs

Reputation: 1445

How to connect to remote svn server and retrieve latest revision number

I have a PHP application that runs on my localhost, and I have a remote SVN repository at https://www.assembla.com. I use the SVNTortoise client and it works perfectly, but I'd like to connect to the svn repository via PHP and get the latest revision number, nothing more. I saw other similar questions but in my case the focus is on that i want to connect to a REMOTE repository. How can i run the svn --version command on the remote server and process the response with PHP?

Upvotes: 1

Views: 2155

Answers (2)

Lazy Badger
Lazy Badger

Reputation: 97375

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

Upvotes: 1

Ben Reser
Ben Reser

Reputation: 5765

There are a few of ways to go about this, I don't really use PHP so I'm not going to give you PHP code but I'll tell you how which should be easy enough for you to turn into PHP code.

Command Line Client

Realize that svn --version does not answer the question you want (it says what version the Subversion client is). The currently released versions of Subversion do not include a command to ask for the information you want directly (1.9 will include svn youngest which does). So getting this information from the client typical involves doing svn log -r HEAD $URL and then parsing the revision number out of that. You can use --xml if you prefer to parse XML.

Subversion API

You could use our C API or language bindings to use the svn_ra_get_latest_revnum() function. You'd need to open an repository access (RA) session and then call the function on that session. The implementation of 1.9's new youngest command is an example. However, Subversion provides no API bindings for PHP. So that's probably not terribly useful to you.

OPTIONS request against 1.7 and newer HTTP SVN servers

1.7 and newer servers provide this information in the SVN-Youngest-Rev header in response to an OPTIONS request (which clients do to determine what features a server supports). You just need to request the activity-collection-set in the request body with XML. For example this curl command retrieves the HEAD revision from the ASF repository:

curl -si -X OPTIONS --data-binary '<?xml version="1.0" encoding="utf-8"?><D:options xmlns:D="DAV:"><D:activity-collection-set></D:activity-collection-set></D:options>' http://svn.apache.org/repos/asf | grep SVN-Youngest-Rev

PROPFIND request against 1.6 and older HTTP SVN servers

If you don't at least have 1.7 then you'll need to use a PROPFIND request. First you need to get the version-controlled-configuration URL by doing a PROPFIND request with a Depth: 0 header and a body like this:

<?xml version="1.0" encoding="utf-8"?>
<propfind xmlns="DAV:">
<prop>
<version-controlled-configuration xmlns="DAV:"/>
</prop>
</propfind>

which will give you a response back like this:

<?xml version="1.0" encoding="utf-8"?>
<D:multistatus xmlns:D="DAV:" xmlns:ns0="DAV:">
<D:response xmlns:lp1="DAV:" xmlns:lp3="http://subversion.tigris.org/xmlns/dav/">
<D:href>/repos/asf/</D:href>
<D:propstat>
<D:prop>
<lp1:version-controlled-configuration><D:href>/repos/asf/!svn/vcc/default</D:href></lp1:version-controlled-configuration>
</D:prop>
<D:status>HTTP/1.1 200 OK</D:status>
</D:propstat>
</D:response>
</D:multistatus>

You want the href in the version-controlled-configuration tag. You then make a PROPFIND request on that URL relative to the server root you're requesting again with a Depth: 0 header and the following request body:

<?xml version="1.0" encoding="utf-8"?>
<propfind xmlns="DAV:">
<prop>
<checked-in xmlns="DAV:"/>
</prop></propfind>

Which gives you a response like this:

<?xml version="1.0" encoding="utf-8"?>
<D:multistatus xmlns:D="DAV:" xmlns:ns0="DAV:">
<D:response xmlns:lp1="DAV:" xmlns:lp3="http://subversion.tigris.org/xmlns/dav/">
<D:href>/repos/asf/!svn/vcc/default</D:href>
<D:propstat>
<D:prop>
<lp1:checked-in><D:href>/repos/asf/!svn/bln/1573025</D:href></lp1:checked-in>
</D:prop>
<D:status>HTTP/1.1 200 OK</D:status>
</D:propstat>
</D:response>
</D:multistatus>

The last segment of the href for the checked-in tag is the revision number of HEAD.

get-latest-rev command against an svnserve server (svn:// protocol)

I'm not going to go into detail on how to do this directly with the svn:// protocol but you'd want to use the get-latest-rev command that's documented in the protocol.

Upvotes: 3

Related Questions