Presenting: PHP Commit Hooks

First published at Tuesday, 20 July 2010

This blog post has first been published in the Qafoo blog and is duplicated here since I wrote it or participated in writing it.

Warning: This blog post is more then 14 years old – read and use with care.

Presenting: PHP Commit Hooks

For those of us using SVN there are some quite common pre- and post-commit hooks in a lot of company repositories, all using their own implementation: Lint checks, CodeSniffer checks, etc. We created a small framework for those commit hooks, which makes it easy to create own checks and custom reporters.

Beside the Continuous Integration system, which provides information for the full project it can be useful to report errors directly after a commit, only for the files which were affected by the commit. Additionally it can make sense to stop commits, which would break the build (for example because of parse errors).

SVN provides commit hooks, where you can hook into the commit process and intercept it. The pre-commit hooks allow to call scripts before the change is comitted to the repository, and thus aborting the change and report errors back to the comitter. Post-commit hooks are executed after the transaction and may be used to asynchronously report errors back to the commiter, which are not important enough to stop the user from comitting.

Classic examples for pre-commit hooks are to check the changed files for parse errors, or even if there are serious coding style violations and abort the commit with an error message in this case.

Coding style violations may also be checked in a post-commit hook, for example, so that the comitter will receive a list of violations in the files he just edited. This is more visible then the CS violations of the full project in the CI tool (which easily might be hundreds of thousands in some projects). Seeing only the violations in the just comitted files, it is easier for the user to just fix those and leave all changed files cleaned up.

PHP Commit Hooks

HTML commit mailHTML commit mail

To make creating such commit hooks trivial, we created PHP Commit Hooks which implements those common commit hooks and makes it easy to configure them.

The framework basically knows checks and reporters. The checks perform actions like lint checks or scanning for CS errors, while the reporters report those changes back to the user. For more details read the README on github.

PHP Commit Hooks also implements sending commit mails, so that you normally only need them installed as a singular commit hooks, which makes maintaining commit hooks easier.

The project is licensed under New BSD license and is available on Github for forking. Remember to call make init after the checkout to fetch its dependencies.

Subscribe to updates

There are multiple ways to stay updated with new posts on my blog: