Handle multiple pre-commit hooks
Should all of them be combined into one single large pre-commit script ?
Yes and no: you can only declare one pre-commit script, so this script should be in charge to:
- call the actual pre-commit scripts
- chose an order for those scripts to be called.
So:
- one pre-commit script
- calling multiple scripts, each one allowing or not (with their exit status) the commit to proceed.
You can use a delegating script that only calls others scripts (subhooks):
https://gist.github.com/carlos-jenkins/89da9dcf9e0d528ac978311938aade43
Usage:
Make your building system to create a symbolic link in the Git hooks directory to this script with the name of the hook you want to attend.
For example, pre-commit
.
This hook will then execute, in alphabetic order, all executables files(subhooks) found under a folder named after the hook type you're attendingsuffixed with .d
.
For example, pre-commit.d
.
How it could look:
.git/hooks/ |_ pre-commit -> ../../tools/git/multihooks.py |_ pre-commit.d/ |_ 01-cpp_coding_standard |_ 02-python_coding_standard |_ 03-something_else
This requires Python3, but you could came with something similar using other technologies.
You can only have one pre-commit script, so you'll have to use that one to call multiple other scripts.
Create an executable /PATH/TO/GIT/.git/hooks/pre-commit
file, with these contents:
#!/bin/bashcd "$(dirname "$0")/pre-commit.d"for hook in *; do bash $hook RESULT=$? if [ $RESULT != 0 ]; then echo "pre-commit.d/$hook returned non-zero: $RESULT, abort commit" exit $RESULT fidoneexit 0
Then put all your pre-commit scripts inside a new pre-commit.d/
directory.
/PATH/TO/GIT/.git/hooks/└── pre-commit.d └── SCRIPT1 └── SCRIPT2
Make sure the script files are executable. Remember that the working dir will be /PATH/TO/GIT/.git/hooks/pre-commit.d/
by default inside the scripts.