Archive

For: ‘SVN

PHP SVN Commands

Wednesday, April 1st, 2009

I’m currently working on a bespoke application which will allow my colleagues to manipulate our web applications across our development servers. The application must also integrate with our SVN repository.

 

If you have attempted any PHP SVN manipulation you will find that documentation on this subject is extremely limited. Seeing as PHP SVN is in a beta stage it’s no wonder. As I wasn’t willing to start writing my app in ANT I persevered with it and after an hour or so I have had some minor success with the basics of PHP and SVN.

 

To start off we need to set our SVN login parameters. In my case I set the username and password for each individual during the login process.

 

svn_auth_set_parameter(PHP_SVN_AUTH_PARAM_IGNORE_SSL_VERIFY_ERRORS, true);
svn_auth_set_parameter(SVN_AUTH_PARAM_DEFAULT_USERNAME, $username);
svn_auth_set_parameter(SVN_AUTH_PARAM_DEFAULT_PASSWORD, $password);

 

Once we have set the login parameters we can begin to execute php svn commands.
In the example below I was successfully able to set the version number for a website within my repository. I could then save that value to the database.

 


protected function set_svn_version(){
	$root='http://your-repository-location.com/';
 	$project=$root.$this->get_svn().'/';
 	$structure=svn_ls($root);
 	if(array_key_exists($this->get_svn(), $structure)){
		$outputs=svn_ls($project);
 		$outputs=svn_ls($project);
		$keys=array_keys($outputs);
		$this->set_version($outputs[$keys[0]]['created_rev']);
	}
}

 

First of all I look to see if the website I am looking for exists. I do this by doing a svn_ls on the root directory of the repository.

 

$structure=svn_ls($root);

if(array_key_exists($this->get_svn(), $structure)){

 

If the website exists, I request the SVN details of that website. The details are returned in an array similar to below.

 

[0] => Array
    (
        [created_rev] => integer revision number of last edit
        [last_author] => string author name of last edit
        [size] => integer byte file size of file
        [time] => string date of last edit in form 'M d H:i'
                  or 'M d Y', depending on how old the file is
        [time_t] => integer unix timestamp of last edit
        [name] => name of file/directory
        [type] => type, can be 'file' or 'dir'
    )

With the array in hand I can get the element I want (created_rev) and set the revision number of this website.

 

With this knowledge in hand I am about to try a some more tricky procedures.