有没有更好的方法来编写一个git pre-commit钩子来检查提交中的任何php文件是否存在解析错误?

时间:2022-10-13 13:09:23

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

4 个解决方案

#1


1  

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.

如果提交是部分提交(并未提交工作树中的所有更改),则此make会提供不正确的结果,因为它会测试工作副本而不是分阶段副本。

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预提交钩子:已更改/添加的文件)。

#2


3  

I'm sorry if it's offtopic, but aren't you supposed to run some kind of automated tests (which would imply that the code has no syntax errors) before doing a commit?

我很抱歉,如果它是offtopic,但你不应该在进行提交之前运行某种自动化测试(这意味着代码没有语法错误)吗?

#3


0  

If you've got the php5-cli installed you can write your pre-commit in PHP and use the syntax your more familiar with.

如果你已经安装了php5-cli,你可以用PHP编写预提交,并使用你更熟悉的语法。

Just do something more like.

只是做一些更喜欢的事情。

#!/usr/bin/php
<?php /* Your pre-commit check. */ ?>

#4


0  

My PHP implementation of the pre-commit hook checks whether the modified files in git are 'error free' and are as per PSR2 standard using either 'php-code-sniffer' or 'php-cs-fixer'

我的预提交钩子的PHP实现检查git中的修改文件是否是“无错误”,并且使用'php-code-sniffer'或'php-cs-fixer'按照PSR2标准执行

#!/usr/local/bin/php
<?php
    /**
         * Collect all files which have been added, copied or
         * modified and store them in an array - output
         */
        exec('git diff --cached --name-only --diff-filter=ACM', $output);

        $isViolated = 0;
        $violatedFiles = array();
        // $php_cs_path = "/usr/local/bin/php-cs-fixer";
        $php_cs_path = "~/.composer/vendor/bin/phpcs";

        foreach ($output as $fileName) {
            // Consider only PHP file for processing
            if (pathinfo($fileName, PATHINFO_EXTENSION) == "php") {
                $psr_output = array();

                // Put the changes to be made in $psr_output, if not as per PSR2 standard

                // php-cs-fixer
                // exec("{$php_cs_path} fix {$fileName} --rules=@PSR2 --dry-run --diff", $psr_output, $return);

                // php-code-sniffer
                exec("{$php_cs_path} --standard=PSR2 --colors -n {$fileName}", $psr_output, $return);

                if ($return != 0) {
                    $isViolated = 1;
                    $violatedFiles[] = $fileName;
                    echo implode("\n", $psr_output), "\n";
                }
            }
        }
        if ($isViolated == 1) {
            echo "\n---------------------------- IMPORTANT --------------------------------\n";
            echo "\nPlease use the suggestions above to fix the code in the following file: \n";
            echo " => " . implode("\n => ", $violatedFiles);
            echo "\n-----------------------------------------------------------------------\n\n\n";
            exit(1);
        } else {
            echo "\n => Committed Successfully :-)\n\n";
            exit(0);
        }

#1


1  

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.

如果提交是部分提交(并未提交工作树中的所有更改),则此make会提供不正确的结果,因为它会测试工作副本而不是分阶段副本。

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预提交钩子:已更改/添加的文件)。

#2


3  

I'm sorry if it's offtopic, but aren't you supposed to run some kind of automated tests (which would imply that the code has no syntax errors) before doing a commit?

我很抱歉,如果它是offtopic,但你不应该在进行提交之前运行某种自动化测试(这意味着代码没有语法错误)吗?

#3


0  

If you've got the php5-cli installed you can write your pre-commit in PHP and use the syntax your more familiar with.

如果你已经安装了php5-cli,你可以用PHP编写预提交,并使用你更熟悉的语法。

Just do something more like.

只是做一些更喜欢的事情。

#!/usr/bin/php
<?php /* Your pre-commit check. */ ?>

#4


0  

My PHP implementation of the pre-commit hook checks whether the modified files in git are 'error free' and are as per PSR2 standard using either 'php-code-sniffer' or 'php-cs-fixer'

我的预提交钩子的PHP实现检查git中的修改文件是否是“无错误”,并且使用'php-code-sniffer'或'php-cs-fixer'按照PSR2标准执行

#!/usr/local/bin/php
<?php
    /**
         * Collect all files which have been added, copied or
         * modified and store them in an array - output
         */
        exec('git diff --cached --name-only --diff-filter=ACM', $output);

        $isViolated = 0;
        $violatedFiles = array();
        // $php_cs_path = "/usr/local/bin/php-cs-fixer";
        $php_cs_path = "~/.composer/vendor/bin/phpcs";

        foreach ($output as $fileName) {
            // Consider only PHP file for processing
            if (pathinfo($fileName, PATHINFO_EXTENSION) == "php") {
                $psr_output = array();

                // Put the changes to be made in $psr_output, if not as per PSR2 standard

                // php-cs-fixer
                // exec("{$php_cs_path} fix {$fileName} --rules=@PSR2 --dry-run --diff", $psr_output, $return);

                // php-code-sniffer
                exec("{$php_cs_path} --standard=PSR2 --colors -n {$fileName}", $psr_output, $return);

                if ($return != 0) {
                    $isViolated = 1;
                    $violatedFiles[] = $fileName;
                    echo implode("\n", $psr_output), "\n";
                }
            }
        }
        if ($isViolated == 1) {
            echo "\n---------------------------- IMPORTANT --------------------------------\n";
            echo "\nPlease use the suggestions above to fix the code in the following file: \n";
            echo " => " . implode("\n => ", $violatedFiles);
            echo "\n-----------------------------------------------------------------------\n\n\n";
            exit(1);
        } else {
            echo "\n => Committed Successfully :-)\n\n";
            exit(0);
        }