For PHP and PHP Composer:
FYI, in the year 2019, there is also this option:
Require this package: "brainmaestro/composer-git-hooks"
And add the following lines to your composer.yaml file:
"extra": {
"hooks": {
"commit-msg": [
"regex=\"^([A-Z]{2,4}-[0-9]{1,4}|(no-ticket|NO-TICKET)):[\\s]*.{10,}\"",
"file=`cat $1`",
"if ! [[ $file =~ $regex ]]; then",
" echo \"ERROR - Commit message is wrong or too short. E.g. XXX-33: Description or no-ticket : Description\"",
" exit 1",
"fi"
],
"pre-commit": [
"git status --porcelain | grep -e '^ [AM]\\(.*\\).php$' | cut -c 3- | while read line; do",
"ROOT=`php -r \"echo __DIR__;\"`",
"bin/php-cs-fixer fix -nq --config=$ROOT/.php_cs \"$line\";",
"bin/phpcbf --standard=PSR2 --encoding=utf-8 -n -p \"$line\";",
"git add \"$line\";",
"done",
"echo committing on branch $(git rev-parse --abbrev-ref HEAD)"
]
}
}
This is the example, which works for me. What it basically does is this:
Every time you run "composer install" or "composer update", the hooks in folder .git/hooks are checked. If the hooks are already in place nothing happens. If they are missing, then the lines from above are parsed into the the hooks with a shebang at the beginning. They are then executed each time somebody is triggering a hook.
If you don't have big scripts that is IMO the better solution than copying scripts around.
Note: if you change the lines in the composer.json for the hooks, you have to delete the respective hook first before you run "composer install" or nothing will change.