Saturday, February 7, 2009

Creating a pre-commit svn hook in PHP

SVN is used for varied reasons. We can jump back, having a versioned backup, share-coding with others – its really awesome.

But then the complexities grows, when we have multiple users, accessing the same repository, which includes proper commenting when the commit is done or mail need to be sent to all the members affected with that particular application and etc..etc...

To ease out these particular complexities, svn provides many hooks which we can add/modify in the svn directory.The complete details about the hooks can be found at http://svnbook.red-bean.com/en/1.1/ch05s02.html

Objective :

To create a pre-commit hook in PHP, such that no commit made without adding any comments.

Step-by-step execution :

1. Create a svn application directory.

Ex: Create a svn repository for the application router,

Command: svnadmin create /usr/local/svn/router

2) In the directory, /usr/local/svn/router/hooks/ rename the file pre-commit.tmpl to pre-commit.

Add the following lines of codes to it.

#!/usr/bin/php
$REPOS=$argv[1];
$TXN=$argv[2];
$log=trim(`/usr/bin/svnlook log -t $TXN $REPOS`);
if(empty($log))
{
fwrite(STDERR,"Empty log messages are not allowed, Please provide a proper log message");
exit(1);
}
exit(0);
?>

Checkout the repository, in your local directory, make some modification and try now to commit, without providing any comment.

An error message will be thrown and the commit will fail.

A few details:

Whenever COMMIT is done, svn passes two parameters to the pre-commit executable, one is repository name and other is transaction id. You can view, whats being added in the log, by svnlook command. For options available with svnlook, just type in svnlook help. Now if the log message is empty, an error will be thrown and the it must be directed to STDERR.


No comments:

Post a Comment