I'm a web developer, freelancer, author, speaker, entrepreneur, technical reviewer and blogger based in the North East, living in a village just outside of Chester-le-Street.
My blog
Subversion post commit hooks with PHP
I'm mainly posting this for my own reference for the future when I next want to set this up, there isn't anything overly technical or complex here. I've wanted to use post-commit hooks with subversion for a while, I've just never got round to setting them up.
Post commit hooks are just that, they hook into subversion and are executed after a commit.
The PHP script to be called post commit
Firstly, we need a PHP script which will do something with our commit log. I put together a simple class which wraps a few svnlook exec calls which return the author of the commit, the commit log, and the files which changed. When the script is called, it grabs the latest commit, checks it has been marked as safe for public consumption, and then does something with it.
#!/usr/bin/php
<?php
$repository = $argv[1];
$revision = $argv[2];
$cp = new CommitProcessor( $repository, $revision );
// I check the commit log is marked as public before putting it into a public log
if( strpos( $cp->getLog(), '#public' ) !== false )
{
// do something
// - cURL to an API
// - Tweet it?
// - email a summary?
}
/**
* CommitProcessor class
*
* @author Michael Peacock
*/
class CommitProcessor{
/**
* Path to the svnlook file
* @var String
*/
private $svnPath = '/usr/bin/';
/**
* The path to the repository
* @var String
*/
private $repository;
/**
* The revision that was commited
* @var int
*/
private $revision;
/**
* Author of the revision
* @var String
*/
private $author = '';
/**
* Message left with the commit
* @var String
*/
private $log = '';
/**
* List of files which changed
* @var String
*/
private $changed = '';
/**
* Constructor
* @param String $repository the path to the repository
* @param int $revision the revision
* @return void
*/
public function __construct( $repository, $revision )
{
$this->repository = $repository;
$this->revision = $revision;
}
/**
* Get the commit log
* @return String
*/
public function getLog()
{
if( $this->log == '' )
{
$this->log = exec( $this->svnPath . 'svnlook log ' . $this->repository );
}
return $this->log;
}
/**
* Get the author of the commit
* @return String
*/
public function getAuthor()
{
if( $this->author == '' )
{
$this->author = exec( $this->svnPath . 'svnlook author ' . $this->repository );
}
return $this->author;
}
/**
* Get list of changed files
* @return String
*/
public function getChanged()
{
if( $this->changed == '' )
{
$this->changed = exec( $this->svnPath . 'svnlook changed ' . $this->repository );
}
return $this->changed;
}
}
?>
Hooking the PHP Script to run post commit
With our PHP script saved somewhere on our server (not in a web accessible place), we now need a post commit hook to actually call the script each time an SVN commit has been made. To do this, we navigate to the hooks folder within our SVN project folder. For my server, the projects folders are owned by www-data, so first
sudo su www-data
Navigate into the appropriate folder, and create a post-commit file (assuming it doesn't exist; you can also copy the existing post-commit.tmpl file if you wish, which contains reference information for post-commit hooks!).
cd /var/svn/projects/projectname/hooks nano post-commit
Use the following code to run our post commit script with PHP, passing the repository path and revision as command line arguments. Replace the paths with the paths to your PHP interpreter and to your post commit PHP script.
#!/bin/sh REPOSITORY="$1" REVISION="$2" /usr/bin/php /var/svn/postcommit/pcscript.php $REPOSITORY $REVISION
Finally, we need to make the post-commit file executable.
chmod 0755 post-commit
And there you have it, a simple subversion post-commit hook which calls a PHP script which does something.
Posted by Michael on 25th Nov 2010 at 09:09


