Windows下的git pre-commit hook针对分阶段文件运行PHP_CodeSniffer

时间:2018-01-23 15:19:39

标签: git githooks pre-commit-hook codesniffer

我尝试将CodeSniffer与pre-commit git hook结合使用。我可以运行phpcs --standard=PSR2 PhpFile.php所以CodeSniffer的安装似乎工作正常。

我尝试使用this piece of code作为预制的git hook。但是我很确定这段代码与Windows不兼容,我不知道需要花多少钱才能移植它。因此,编写自己的代码以使用CodeSniffer解析分阶段(PHP)文件可能更好。我只是可以使用一些帮助如何做到这一点。

试过

嗯,我知道我必须从获得这样的暂存文件开始:

git diff --cached --name-only

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

1 个答案:

答案 0 :(得分:0)

我制作了符合我需要的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