GIT 钩子怀疑


GIT Hook Doubts

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 $?

我在预提交中编写了代码,将检查是否没有语法错误。但有一些疑问

  1. 它将根据开发人员的PHP版本(没有变化(检查语法错误,我相信它应该基于服务器PHP版本。 如何处理
  2. 变量的作用域"scopeVar"仅在 while 循环中结束,假设我想根据 scopeVaR 的值在 while 循环之外执行一些操作。我可以处理这个吗

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