Windows下的git预提交钩子对暂存文件运行PHP_CodeSniffer

Windows下的git预提交钩子对暂存文件运行PHP_CodeSniffer,git,githooks,pre-commit-hook,codesniffer,Git,Githooks,Pre Commit Hook,Codesniffer,我尝试与预提交git挂钩结合使用。我可以运行phpcs--standard=psr2phpfile.php,因此CodeSniffer的安装似乎可以正常工作 我试着用它作为预先制作的git钩子。然而,我很确定这段代码与Windows不兼容,我不知道移植它需要多少努力。因此,也许最好编写自己的代码,用CodeSniffer解析分段(PHP)文件。我只是需要一些帮助来做这件事 尝试过 我知道我必须从这样的阶段性文件开始: git diff --cached --name-only 但是我不能使用g

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

我试着用它作为预先制作的git钩子。然而,我很确定这段代码与Windows不兼容,我不知道移植它需要多少努力。因此,也许最好编写自己的代码,用CodeSniffer解析分段(PHP)文件。我只是需要一些帮助来做这件事

尝试过

我知道我必须从这样的阶段性文件开始:

git diff --cached --name-only

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

我制作了这个
预提交
钩子,它适合我的需要

#!/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