By Kore Nordmann, first published at Tue, 20 Jul 2010 08:54:54 +0200
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.
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.