gitのpre-commitのでphpの構文チェックとmasterへのコミット禁止
対象のリポジトリで実行
$ touch .git/hooks/pre-commit $ chmod 755 .git/hooks/pre-commit
事前準備はこれでOK
#!/bin/sh ROOT_DIR="$(pwd)/" LIST=$(git status | grep -e '\(modified:\|new file:\)'| grep '\.php' | cut -d':' -f2 ) # syntaxエラーがあるファイルはコミット禁止 ERRORS_BUFFER="" for file in $LIST do ERRORS=$(php -l $ROOT_DIR$file 2>&1 | grep "Parse error") if [ "$ERRORS" != "" ]; then if [ "$ERRORS_BUFFER" != "" ]; then ERRORS_BUFFER="$ERRORS_BUFFER\n$ERRORS" else ERRORS_BUFFER="$ERRORS" fi echo "Syntax errors found in file: $file " exit 1 fi done # マスターへのコミット禁止 branch="$(git symbolic-ref HEAD 2>/dev/null)" || \ "$(git describe --contains --all HEAD)" if [ "${branch##refs/heads/}" = "master" ]; then echo "Do not commit on the master branch!" exit 1 fi
動かす
予め事故ってるPHPファイルを用意
$ git st On branch master Your branch and 'origin/master' have diverged, and have 2 and 5 different commits each, respectively. (use "git pull" to merge the remote branch into yours) Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: README.md new file: index.php $ git add . $ git commit -m 'test' Syntax errors found in file: index.php