sonrac / git-php-hook-runner

PHP项目的pre-commit钩子

v1.0 2024-02-21 00:07 UTC

This package is auto-updated.

Last update: 2024-09-11 18:54:07 UTC


README

该软件包是一个用于git钩子配置的工具,用于加快git钩子的工作。

钩子命令并行启动,由于并行化,钩子工作更快。

安装

composer require sonrac/git-php-hook-runner

用法

  • 复制配置
cp vendor/sonrac/git-hook-runner/config/hook.yaml /<path-to-source-root>/<path-to-new-location-config>
  • 编辑钩子配置。完整选项列表请参阅此处
  • .git/hooks目录中创建钩子,内容如下
#!/bin/sh

set -e

XDEBUG_MODE=off
set -e

XDEBUG_MODE=off

PROJECT_DIR="$(dirname ${0})/../../"

__docker_cmd_runner() {
  docker-compose exec -T -e XDEBUG_MODE=off php "$@"
}

CHANGED_FILES=$(git -C "$PROJECT_DIR" diff --staged --relative --name-only --diff-filter=ACMR)

if [ -z "$CHANGED_FILES" ]; then
  echo "Empty changed files list"
  exit 0;
fi

echo '\nRun PHPCS Beautifier:\n'

__docker_cmd_runner which ./vendor/bin/phpcbf
if [ $? -eq 1 ]; then
  echo "\033[41mPlease install PHPCS\033[0m"
  exit 1
fi

PHPCBF_OUTPUT=$(__docker_cmd_runner ./vendor/bin/phpcbf $CHANGED_FILES)

case $? in
  0)
    echo "\033[32mNothing found that could be fixed \033[0m"
    ;;
  1)
    echo "\033[32mPHPCBF fixed all fixable errors \033[0m"
    ;;
  2)
    echo "\033[43mSomething failed to fix. Please change your files \033[0m"
    exit 1
    ;;
esac

BEAUTIFIED_FILES=$(echo "$PHPCBF_OUTPUT" | grep 'src\|tests\|app' |  awk -v pr_dir="$PROJECT_DIR" '{print pr_dir$1}' | tr -s '\r\n' ' ');

if [ ! -z "$BEAUTIFIED_FILES" ]; then
  git add $BEAUTIFIED_FILES
fi

php ../../vendor/bin/hook hook -p  <path-to-source-root> -c <path-to-new-location-config> $CHANGED_FILES
  • 保存钩子文件内容
  • 为git钩子设置Git执行权限
chmod +x .git/hook/<git-hook-file-name>

禁用提交钩子

在提交模态对话框中取消勾选复选框,如果您需要在不进行钩子运行器检查的情况下提交

img.png

有用链接