Using the Subversion pre-commit Hook

Using the Subversion pre-commit Hook – How to configure the Subversion pre-commit hook

What Does the Subversion pre-commit Hook Do?

The SVN pre-commit hook has different requirements than the main PHP_CodeSniffer package. See the Requirements page for more information.

A pre-commit hook is a feature available in the Subversion version control system that allows code to be validated before it is committed to the repository. The PHP_CodeSniffer pre-commit hook allows you to check code for coding standard errors and stop the commit process if errors are found. This ensures developers are not able to commit code that violates your coding standard. Instead, they are presented with the list of errors they need to correct before committing.

Sample pre-commit output


$ svn commit -m "Test" temp.php
Sending        temp.php
Transmitting file data .svn: Commit failed (details follow):
svn: 'pre-commit' hook failed with error output:

FILE: temp.php
---------------------------------------------------------------
FOUND 1 ERROR(S) AND 0 WARNING(S) AFFECTING 1 LINE(S)
---------------------------------------------------------------
 2 | ERROR | Missing file doc comment
--------------------------------------------------------------

Configuring the pre-commit Hook

Edit /path/to/PHP_CodeSniffer/scripts/phpcs-svn-pre-commit and replace @php_bin@ in the first line with the path to the PHP CLI. For example, #!@php_bin@ becomes #!/usr/bin/php.

Now ensure the path to svnlook is correct by modifying the following line, if required:

Changing the path to svnlook


define('PHP_CODESNIFFER_SVNLOOK', '/usr/bin/svnlook');

Now add the following line to your pre-commit file in the Subversion hooks directory:

Adding the pre-commit hook to the Subversion config file


/path/to/PHP_CodeSniffer/scripts/phpcs-svn-pre-commit "$REPOS" -t "$TXN" >&2 || exit 1

You can also use all the standard phpcs command line options to do things like set the standard to use, the tab width and the error report format:

Adding the pre-commit hook to the Subversion config file


/path/to/PHP_CodeSniffer/scripts/phpcs-svn-pre-commit --standard=Squiz --tab-width=4 "$REPOS" -t "$TXN" >&2 || exit 1
A sample ruleset.xml file that describes all features of the format (Previous) Frequently asked questions (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

Note by: thinice@gmail.com
I've created a Mercurial pre-commit hook here: http://hg.itnobody.com/hg-checkstyle-hook/src
Note by: soenke@ruempler.eu
I wrote a pre-commit hook for git: http://github.com/s0enke/git-hooks/tree/master/phpcs-pre-commit/
Note by: kguest
If you've installed PHP_CodeSniffer via PEAR you won't have to replace "@php_bin@" with anything as the install process takes care of that for you.