【问题标题】:GIT Hook DoubtsGIT 钩子怀疑
【发布时间】:2016-09-06 14:21:12
【问题描述】:
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 $?

我已经在 pre-commit 中编写了代码来检查是否没有语法错误。但有一些疑问

  1. 它将根据开发人员的 PHP 版本检查语法错误(可能会有所不同),我相信它应该基于服务器 PHP 版本。如何处理这个
  2. 变量“scopeVar”的范围仅在 while 循环中结束,假设我想根据 scopeVar 的值在 while 循环之外执行一些操作。 我可以处理吗?

【问题讨论】:

  • 如果你使用 bash,你可以使用进程替换而不是管道来避免这个问题:done <(git diff --cached --name-only)

标签: php git shell


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-18
    • 2018-10-04
    • 2017-10-01
    • 2011-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多