vim .git/hooks/pre-commit
#!/bin/bash
ROOT_DIR=$(git rev-parse --show-toplevel)
LIST=$(git status | grep -e '\(modified:\|new file:\)'| grep '\.php' | cut -d':' -f2 )
error=false
for file in $LIST
do
$ROOT_DIR/vendor/bin/php-cs-fixer fix --path-mode=intersection --dry-run $ROOT_DIR/$file > /dev/null 2>&1
if [ $? != 0 ]; then
echo -e " please, cs fix to $ROOT_DIR/$file"
error=true
fi
done
if "${error}"; then
echo
echo -e "\033[31mCommit fail\033[m please run \"vendor/bin/php-cs-fixer fix\" command"
exit 1
fi
chmod u+x .git/hooks/pre-commit
composerのscriptに以下のように登録しておくと、
別の作業者のリポジトリにも設定が反映することができる。
....省略
"scripts": {
....省略
"post-install-cmd": [
"cp ./pre-commit .git/hooks/pre-commit",
"chmod u+x .git/hooks/pre-commit"
],
"post-update-cmd": [
"cp ./pre-commit .git/hooks/pre-commit",
"chmod u+x .git/hooks/pre-commit"
],
....省略
},
....省略
post-install-cmd
、でcomposer install時
post-install-cmd
でcomposer update時にそれぞれ実行される。