git Windows 运行 PHP_CodeSniffer 下针对暂存文件的预提交挂钩
git pre-commit hook under Windows run PHP_CodeSniffer against staged files
我尝试将 CodeSniffer 与预提交 git 挂钩结合使用。我可以 运行 phpcs --standard=PSR2 PhpFile.php
所以 CodeSniffer 的安装似乎工作正常。
我尝试使用 this piece of code 作为预制的 git 挂钩。但是我很确定这段代码与 Windows 不兼容,我不知道移植它需要多少努力。所以也许最好编写我自己的代码来使用 CodeSniffer 解析暂存的 (PHP) 文件。我只是可以使用一些帮助如何做到这一点。
尝试过
好吧,我知道我必须像这样获取暂存文件:
git diff --cached --name-only
但我不能使用 grep
来只获取 .php 文件。所以我认为我们需要 Windows?
中的等价物
我做了这个 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
我尝试将 CodeSniffer 与预提交 git 挂钩结合使用。我可以 运行 phpcs --standard=PSR2 PhpFile.php
所以 CodeSniffer 的安装似乎工作正常。
我尝试使用 this piece of code 作为预制的 git 挂钩。但是我很确定这段代码与 Windows 不兼容,我不知道移植它需要多少努力。所以也许最好编写我自己的代码来使用 CodeSniffer 解析暂存的 (PHP) 文件。我只是可以使用一些帮助如何做到这一点。
尝试过
好吧,我知道我必须像这样获取暂存文件:
git diff --cached --name-only
但我不能使用 grep
来只获取 .php 文件。所以我认为我们需要 Windows?
我做了这个 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