Looking for a maintainer. Please create an issue if you're interested.
Rename pre-commit.sample to pre-commit. The file is located in {GitRootDir}/.git/hooks
Replace the content with the attached file.
Make sure that the file has an execute permission.
$ sudo chmod +x /path/to/.git/hooks/pre-commit
You need to have a Code Sniffer installed before using this pre-commit script. To install.
For Debian-based systems:
$ sudo apt-get install php-pear
For Mac OS:
http://jason.pureconcepts.net/2012/10/install-pear-pecl-mac-os-x/
Or see the general installation guidelines:
http://pear.php.net/manual/en/installation.getting.php
$ sudo pear update-channels
$ sudo pear install PHP_CodeSniffer
$ sudo ln -sv /path/to/coder/coder_sniffer/Drupal $(pear config-get php_dir)/PHP/CodeSniffer/Standards/Drupal
Basically, the idea in #4 is to link/include the Drupal's code sniffer module to the standard PHP Code Sniffer. Here is a sample/actual command for #4:
$ sudo ln -sv /home/ranelpadon/dev/cnngod7/html/sites/all/modules/contribs/coder/coder_sniffer/Drupal $(pear config-get php_dir)/PHP/CodeSniffer/Standards/Drupal
The $(pear config-get php_dir) part in the #4 command will be usually evaluated in Ubuntu as /usr/share/php
To make your Drupal code sniffer module global and not dependent on any Drupal project you can put the module in Drush home folder.
$ cd ~/.drush
$ drush dl coder
$ sudo ln -sv ~/.drush/coder/coder_sniffer/Drupal $(pear config-get php_dir)/PHP/CodeSniffer/Standards/Drupal
The pre-commit hook will only run when using git commit
If the commit contains any violations of the following type:
it will exit before the commit get indexed and will display the offending file(s).
it will display the line of code and the filename that contain bad code. The developer must resolve the problem first in order to stage the commit.
If you're really sure that it is ok to commit the changes without resolving the coding standard problem detected by the script, you can skip or bypass the validation by using --no-verify
$ git commit -am "#1452 Commit message | Bypassing validation process" --no-verify
This is optional and not required by pre-commit.
Install Esprima as JS validator.
To install Esprima:
sudo npm -g install esprima
To manually run Esprima from commandline interface.
Drupal Pre Commit Filter by Gerald Villorente is licensed under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
Based on a work at http://www.niden.net/2011/11/git-pre-commit-another-check-to-ensure.html.
Please see this issue.
This drupal-pre-commit
is only compatible with PHP_CodeSniffer 1.x. If you are using version 2.x you need to downgrade to stable version 1.5.6.
Steps to downgrade:
1. sudo pear uninstall PHP_CodeSniffer
2. sudo pear install PHP_CodeSniffer-1.5.6
Any any case that the pre-commit is jumping to Step #3, you have to update your precommit $LIST
variable to remove \#
. So from
LIST=$( git status | grep -e '\#.*\(modified\|added\|new file\)' | grep -v '\#.*\(features\|contribs\|devel\)' )
to
LIST=$( git status | grep -e '.*\(modified\|added\|new file\)' | grep -v '.*\(features\|contribs\|devel\)' )