SVN is Subversion and it is a source control system. It is a PHP web based tool to administer an Apache Subversion repository server. It allows PHP scripts to communicate with SVN repositories and working copies without direct command line calls to the svn executable. If you are not currently using some other source control system; you are wise to invest in one. SVN is rather easy to use, compared to some other open source control systems, and has some neat tools on top of the source repository to make things easier for you.
With this tool you can remotely:
- Create, remove, load and dump repositories
- Manage user accounts for access to the repositories
- Manage groups for access to the repositories
- Invite users by email to create an account on the server
In order to work on SVN, you must meet the following requirements:
- A PHP-enabled web server
- A Linux server with Subversion installed
- PHP function shell_exec() must be allowed
Not all servers or hosting providers allow their users to execute direct shell commands. Please confirm with your hosting provider that this is enabled and allowed by their terms of service.
Here we are going to show how to checkout files from an SVN repository. If you are using a public repository, you probably will not need to provide a username or password to access the files. However if you are using a private repository, you will be required to provide a username and password. This is the SVN command you will need to use to check out a file:
svn checkout SVNURL PATH
Now let’s go ahead and explain what is going on here. First off the command we are using in svn checkout to pull the files from the SVN repository. SVNURL should be replaced with the link to the SVN repository (for the example we are going to use “ hello”).
PATH should be replaced with the name of the folder you want the files to be placed in to. You can use the path depending on the location you are executing this command at, or you could use the full path to the folder. If you are executing the command in /home/user/public_html/ and list the path as “hello”, then it will place the files in /home/user/public_html/hello/. You could also just use the full path (for example, /home/user/public_html/hello) to specify a path anywhere on the server.
As we mentioned, we are going to use the hello repository and put all the repository files in a folder named “hello”.
svn checkout http://hello-php.googlecode.com/svn/trunk/distribution/libs/hello
Now that we have our command, we can go ahead and begin putting this command in to a PHP script.
<?php $cmd="svn checkout http://hello-php.googlecode.com/svn/trunk/distribution/libs/ hello"; $svn = shell_exec($cmd); echo $svn; ?>
We are now ready to use PHP script to check out the latest version of hello. Obviously, you should replace the hello URL with the URL of the SVN repository you plan on using. Also you can change the path to whatever you want your folder named.
Quick Tips for SVN Checkout
If you are using a private repository, you will be forced to authenticate yourself with a username and password. To authenticate, use the following script below. All you have to do is replace USERNAME with your username, PASSWORD with your password from your SVN account that has at least read-only access. Also be sure to replace SVNURL with the SVN address and PATH with where you want the files placed.
<?php $cmd = "svn --username USERNAME --password PASSWORD checkout SVNURL PATH"; $svn = shell_exec($cmd); echo $svn; ?>
If you would like the SVN repository files to be placed within the current folder that the command is being executed within, you use a period in the place of PATH.
<?php $cmd = "svn checkout SVNURL ."; $svn = shell_exec($cmd); echo $svn; ?>
Now that we have our files checked out, we may need to update the files if they are changed on the repository. Here we are showing you how to update your files with the latest ones from the SVN repository you used to check out the files. Here is the shell command we are going to be using:
svn update PATH
So that is the command we are using, it is much more simple than the svn checkout function because all the variables such as the repository URL and username/password (if applicable) are already stored on the server.
The function we are looking at is svn update to pull the latest files from the SVN repository. PATH should be replaced with the path of the folder where you checked the files out to. If you placed the files in a folder named “hello” you can just replace PATH with “hello”. If you are executing the command from a different folder, you should use the full path to the working folder (for example /home/user/public_html/hello).
Now let’s go ahead and pretend we are going to update a folder named “hello”, this will be the command you are going to use.
svn update hello
Now that we know how to structure the command, it is time to show this command being executed through PHP function shell_exec().
<?php $cmd = "svn update hello"; $svn = shell_exec($cmd); echo $svn; ?>
Quick Tips for SVN Update
If you are using a private SVN repository, when you are updating a working folder, you do not need to re-authenticate again as your credentials are already stored on the server.
If you are updating a working folder and are executing the command from within that folder, you do not need to enter the PATH of the working folder. Below is a simple example of how you can execute the command from within the working folder.
<?php $cmd = "svn update"; $svn = shell_exec($cmd); echo $svn; ?>
You now know the basics of performing an SVN checkout or update from a remote repository. Please remember that not all hosting servers allow the PHP function shell_exec() to be executed for server security reasons. It is best to check with your hosting provider before attempting any of these commands.