git diff --cached --name-only | while read FILE; do
if [[ "$FILE" =~ ^.+(php|inc)$ ]]; then
if [[ -f $FILE ]]; then
php -l "$FILE" 1> /dev/null
if [ $? -ne 0 ]; then
scopeVar=1;
echo -e "\e[1;31m\tAborting commit due to files with syntax errors. $FILE \e[0m" >&2
exit 1
fi
fi
fi
done || exit $?
我在预提交中编写了代码,用于检查是否存在语法错误。但有一些疑问
答案 0 :(得分:0)
There is a workaround了解Bash中的范围问题:
declare myvar
git diff --cached --name-only |
{
while IFS= read -r FILE; do
# your code...
myvar=1
done
echo $myvar # outputs 1
}
关于PHP版本。我会在客户端进行PHP版本检查,例如
fatal_error()
{
echo >&2 "$1"
exit 1
}
test_php_ver()
{
r=`php -r 'echo version_compare(PHP_VERSION, "5.6.19") >= 0 ? 1 : 0;'`
[ $r -eq 1 ] || fatal_error "Invalid php version"
}
test_php_ver