Last active
August 29, 2015 14:02
-
-
Save evalphobia/52fd312c2d6958151000 to your computer and use it in GitHub Desktop.
PHP用pre-commit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
↓ install steps