All too often when peer reviewing code done by other Drupalers, I spot debug code left in the commit, waiting for the chance to be deployed to staging and break everything.
I started to read up on git hooks, paying particular attention to pre-commit:
This hook is invoked by git commit, and can be bypassed with
--no-verify option. It takes no parameter, and is invoked before obtaining the proposed commit log message and making a commit. Exiting with non-zero status from this script causes the git committo abort.
You can write you pre-commit hook in any language, bash seems the most sane due to the power of text analysis tools at your disposal.
Where is the code
Here is a link to the github repository with the pre-commit hook:
git clone https://github.com/seanhamlin/drupal-pre-commit.git
- Executes PHP lint across the PHP files that were changed
- Checks PHP for a blacklist of function names (e.g.
- Ignores deleted files from git and will not check them
- Tells you all of the fails at the end (and stores a log)
- Only lets the commit go ahead when there are no fails
git clone https://github.com/wiifm69/drupal-pre-commit.git
cp scripts/pre-commit.sh [PATH_TO_YOUR_DRUPAL_PROJECT]/scripts
ln -s ../../scripts/pre-commit.sh .git/hooks/pre-commit
I am keen to hear from anyone else on how they do this, and if you have any enhancements to the code then I am happy to accept pull requests on Github. Happy coding.