Skip to content

Instantly share code, notes, and snippets.

@evalphobia
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evalphobia/52fd312c2d6958151000 to your computer and use it in GitHub Desktop.
Save evalphobia/52fd312c2d6958151000 to your computer and use it in GitHub Desktop.
PHP用pre-commit
#!/bin/sh
# @from http://blog.manaten.net/entry/645
IMAGES='\.(gif|png|jpg)$'
BLOCK_FUNCS='(var_dump)\('
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
# Redirect output to stderr.
exec 1>&2
# PHPシンタックスエラー時の処理
function phpError() {
IS_ERROR=1
echo "! $1: PHP Syntax check failed."
php -l "$1"
echo ""
}
# コンフリクト未解消時のエラー
function conflictError() {
IS_ERROR=1
echo "! $1: Confliction is not resolved."
grep -n '^<<<<<<< ' "$1"
echo ""
}
# 禁止関数時のエラー
function blacklistError() {
IS_ERROR=1
echo "! $1: has BlackListed functions in $BLOCK_FUNCS"
egrep -n "$BLOCK_FUNCS" "$1" | grep -v '//'
echo ""
}
IS_ERROR=0
# コミットされるファイルのうち、画像ファイル以外
for FILE in `git diff-index --name-status $against -- | egrep -v "$IMAGES" | cut -c3-`; do
# PHPファイルの場合はシンタックスチェック
if [ `echo "$FILE" | egrep '\.php$'` ]; then
php -l "$FILE" > /dev/null || phpError "$FILE"
fi
# コンフリクト解消されていないファイルがあるかどうかチェック
grep '^<<<<<<< ' "$FILE" > /dev/null && grep '^>>>>>>> ' "$FILE" > /dev/null && grep '^=======' "$FILE" > /dev/null && conflictError "$FILE"
# 禁止関数があるかチェック
egrep -n "$BLOCK_FUNCS" "$FILE" | grep -v '//' > /dev/null && blacklistError "$FILE"
done
exit $IS_ERROR
@evalphobia
Copy link
Author

↓ install steps

$ wget https://gist.githubusercontent.com/evalphobia/52fd312c2d6958151000/raw/88843184f54f755a58774f7780d97323ad92866e/pre-commit  # ←ダウンロード
$ chmod +x pre-commit  # ←実行権限付与
$ mv pre-commit /path/to/project/.git/hooks/  # ←hookスクリプトへ配置

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment