INSTALLATION OF PRE COMMIT HOOK - Libbna/CUSTOM-CMS GitHub Wiki
What are Git Hooks?
Git hooks are programs that automatically run at different points in the lifecycle of a Git repository. The most common one I see in JavaScript projects is the “pre-commit” hook.
As the name suggests this runs right before a commit is made (i.e. after a developer runs git commit in the repo).
This allows you to “hook” into the Git process to run your own code, and potentially prevent the commit.
Installation Process:
-
Go to .git/hook/ and create a file pre-commit.
-
In pre-commit file write bash scripts - e.g.
STANDARD="Drupal,DrupalPractice"
BIN="./vendor/bin"
echo "RUNNING phpcbf"
vendor/bin/phpcbf --standard=$STANDARD src
echo "RUNNING phpcs"
vendor/bin/phpcs --standard=$STANDARD src
-
Now you have to change the permissions of this file so it is executable (since Git needs to be able to execute it):
chmod +x .git/hooks/pre-commit
-
That’s it—now whenever you git commit you’ll see - the above mentioned scripts (2.) printed in your terminal.
How can you share hooks?
- .git/hooks folder isn’t shared by other people cloning your repository.
- So for that you can tell Git to use different directory to look for hooks.
- Create a folder
.githooks
in the root directory of your project and inside this folder create a filepre-commit
. - After this, go to composer.json file and right this script under scripts listener
"post-install-cmd": "git config core.hooksPath .githooks"
- Once this is done run a command
composer install
to execute the scripts of composer.json file.
NOTE: Make sure before adding pre-commit hook - php_codesniffer library and Drupal and DrupalPractice standards are installed in your system. If not you can follow this link to install them. ----> php_codesniffer