甜豆/git-hooks

在Git钩子和VCS下的脚本之间提供桥梁。

安装次数: 10,964

依赖项: 58

建议者: 0

安全性: 0

星标: 2

关注者: 6

分支: 0

开放问题: 0

类型:composer-plugin

v0.1.0 2020-04-13 14:50 UTC

This package is auto-updated.

Last update: 2024-09-20 09:43:44 UTC


README

从Git钩子触发自定义脚本。

此包提供了未版本化的./.git/hooks/*脚本和Git仓库中的脚本之间的桥梁。

CircleCI

何时使用

如果您想将Git钩子脚本放在VCS下以便与您的同事共享,那么这正是您需要的工具。

如何使用

  1. 进入您现有的包目录(或使用git init && composer init创建一个新的目录)
  2. 运行composer require --dev 'sweetchuck/git-hooks'
  3. 然后您有两个选择
    1. 依赖于此包提供的Git钩子脚本,并在您的./.git-hooks文件中实现逻辑。
    2. 或者创建一个./git-hooks目录,并在其中创建Git钩子文件(例如:./git-hooks/pre-commit)。
  4. 部署脚本将由post-install-cmd Composer事件自动触发。

配置

示例composer.json文件

{
    "extra": {
        "sweetchuck/git-hooks": {
            "core.hooksPath": "./git-hooks",
            "symlink": true
        }
    }
}

配置 - core.hooksPath

类型:字符串

默认值:vendor/sweetchuck/git-hooks/git-hooks(动态检测)

如果Git版本大于等于v2.9,则此值将用于设置git config core.hooksPath <PATH>。如果Git版本小于2.9,则此目录的内容将被符号链接或复制到./.git/hooks目录。

配置 - symlink

类型:布尔值

默认值:false

此配置选项仅适用于Git版本小于v2.9的情况。将Git钩子文件从原始位置(由core.hooksPath配置提供)复制或符号链接到./.git/hooks

示例 ./.git-hooks文件

如果您使用此包中的Git钩子脚本(vendor/sweetchuck/git-hooks/git-hooks),则需要自定义脚本,该脚本可以捕获Git钩子添加并触发一些真正有用的操作。

将以下内容复制到./.git-hooks

#!/usr/bin/env bash

echo "BEGIN Git hook: ${sghHookName}"

function sghExit ()
{
    echo "END   Git hook: ${sghHookName}"

    exit $1
}

# @todo Better detection for executables: php, composer.phar.
sghRobo="$(composer config 'bin-dir')/robo"

test -s "${sghBridge}.local" && . "${sghBridge}.local"

sghTask="githook:${sghHookName}"

# Exit without error if "robo" doesn't exists or it has no corresponding task.
test -x "$sghRobo" || sghExit 0
"${sghRobo}" help "${sghTask}" 1> /dev/null 2>&1 || sghExit 0

if [ "$sghHasInput" = 'true' ]; then
    "$sghRobo" "${sghTask}" $@ <<< $(</dev/stdin) || sghExit $?
else
    "$sghRobo" "${sghTask}" $@ || sghExit $?
fi

sghExit 0

示例 ./RoboFile.php

<?php

/**
 * Git hook tasks have to be started with 'githook' prefix.
 * So the method name format is: githook<GitHookNameInCamelCaseFormat>
 * Or use the @command annotation.
 */
class RoboFile extends \Robo\Tasks
{

    /**
     * Demo pre-commit callback.
     *
     * @command githook:pre-commit
     */
    public function githookPreCommit()
    {
        $this->say('The Git pre-commit hook is running');
    }
}