有没有更好的方式来编写git pre-commit钩子来检查提交中的任何php文件是否存在解析错误? [英] Is there a better way of writing a git pre-commit hook to check any php file in a commit for parse errors?

查看:101
本文介绍了有没有更好的方式来编写git pre-commit钩子来检查提交中的任何php文件是否存在解析错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 #!/ bin / sh 

php_syntax_check()
{
retval = 0
for i in $(git-diff-index --name-only --cached HEAD - | grep -e'\.php $');如果[-f $ i]做
;那么
output = $(php -l $ i)
retval = $?
if [$ retval -gt 0];然后
回声=========================================== ===================================
echo由于后续解析错误
echo$ output
git reset -q HEAD $ i
fi
fi
完成

if [ $ retval -gt 0];然后
退出$ retval
fi
}
php_syntax_check




一种方法可以是:
$ b $ $ p $ g $ git diff - -cached -name-only --diff-filter = ACMR | xargs git checkout-index --prefix = $ TMPDIR / -
find $ TMPDIR -name'* .php'-print | xargs -n 1 php -l

这会将暂存图片复制到临时空间中然后在那里运行测试命令。如果任何文件包含构建中的其他文件,则可能必须在测试树中重新创建整个分阶段映像,然后在那里测试更改后的文件(请参阅: Git pre-commit hook:changed / added files )。


What I have so far is

#!/bin/sh

php_syntax_check()
{
    retval=0
    for i in $(git-diff-index --name-only --cached HEAD -- | grep -e '\.php$'); do
        if [ -f $i ]; then
            output=$(php -l $i)
            retval=$?
            if [ $retval -gt 0 ]; then
                echo "=============================================================================="
                echo "Unstaging $i for the commit due to the follow parse errors"
                echo "$output"
                git reset -q HEAD $i
            fi
        fi
    done

    if [ $retval -gt 0 ]; then
        exit $retval
    fi
}
php_syntax_check

解决方案

If the commit is a partial commit (not all the changes in the working tree are committed), then this make give incorrect results since it tests the working copy and not the staged copy.

One way to do this could be:

git diff --cached --name-only --diff-filter=ACMR | xargs git checkout-index --prefix=$TMPDIR/ --
find $TMPDIR -name '*.php' -print | xargs -n 1 php -l

Which would make a copy of the staged images into a scratch space and then run the test command on them there. If any of the files include other files in the build then you may have to recreate the whole staged image in the test tree and then test the changed files there (See: Git pre-commit hook : changed/added files).

这篇关于有没有更好的方式来编写git pre-commit钩子来检查提交中的任何php文件是否存在解析错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆