php - GIT Hook疑惑

标签 php git shell

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 循环之外执行某些操作。 我可以处理这个吗

最佳答案

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

关于php - GIT Hook疑惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37158287/

相关文章:

git - 在 Jenkins 上推广所有构建以掌握

php - 我可以通过 unix shell 脚本在 Windows 中创建目录吗?

PHP从多维数组中删除重复值并完全唯一

php - 创建 View 还是使用 innerjoins?

git rebase -i HEAD~5 并 pull 请求,但不是我的提交

git 等同于 hg mq?

bash - 如何在不使用 bash 中的 printf 的情况下将字符转换为 ASCII

macos - 有没有办法知道 shell/终端命令/应用程序的选项?

php - setCallback 在 Laravel json 响应中的用途

php - 警告:mysqli_stmt::bind_result():绑定(bind)变量的数量与准备好的语句中的字段数量不匹配错误