【问题标题】:git pre-commit hook under Windows run PHP_CodeSniffer against staged filesWindows 下的 git pre-commit hook 对暂存文件运行 PHP_CodeSniffer
【发布时间】:2018-01-23 15:19:39
【问题描述】:

我尝试将CodeSniffer 与预提交 git 挂钩结合使用。我可以运行phpcs --standard=PSR2 PhpFile.php,所以 CodeSniffer 的安装似乎工作正常。

我尝试使用 this piece of code 作为预制的 git 钩子。但是我很确定这段代码与 Windows 不兼容,我不知道移植它需要多少努力。所以也许最好编写我自己的代码来使用 CodeSniffer 解析暂存的(PHP)文件。我只是可以使用一些帮助来做到这一点。

试过

好吧,我知道我必须从获取这样的暂存文件开始:

git diff --cached --name-only

但我不能使用grep 只获取 .php 文件。所以我认为我们需要 Windows 中的等价物?

【问题讨论】:

    标签: git githooks pre-commit-hook codesniffer


    【解决方案1】:

    我制作了这个适合我需要的 pre-commit 钩子

    #!/bin/bash
    echo "Running Code Sniffer. Code standard PSR2."
    # php files that are staged in git but not deleted
    
    PHP_FILES=$(git diff --diff-filter=d --cached --name-only | grep -E '\.php$')
    JS_FILES=$(git diff --diff-filter=d --cached --name-only | grep -E '\.js$')
    for file in $PHP_FILES
    do
    echo $file
    
     phpcs --standard=PSR2 --encoding=utf-8 -n -p $file
      if [ $? -ne 0 ]; then
        echo "Fix the error before commit please"
            echo "Run phpcbf --standard=PSR2 $file for automatic fix"
            echo "or fix it manually. (PHPStorm can help you)"
        exit 1 # exit with failure status
      fi
    done
    
    for file in $JS_FILES
    do
    echo $file
     eslint $file
      if [ $? -ne 0 ]; then
        echo "Fix the error before commit please"
        exit 1 # exit with failure status
      fi
    done
    

    【讨论】:

    • 它给出 .git/hooks/pre-commit: line 11: phpcs: command not found
    • @BhumiShah 确保 phpcs 已安装并在您的路径中(或使用完整路径)。只需在命令行上直接调用 phpcs 进行测试。
    • 如果我在我的 laravel 下运行命令它可以工作:“./vendor/bin/phpcs ./app”但是当我从预提交挂钩文件中尝试相同的命令时,它给了我错误
    猜你喜欢
    • 2015-08-19
    • 2015-09-02
    • 1970-01-01
    • 2012-11-26
    • 2013-02-19
    • 2019-06-18
    • 2023-04-11
    • 2017-08-16
    • 2012-01-18
    相关资源
    最近更新 更多