链接git钩子 [英] Chaining git hooks

查看:124
本文介绍了链接git钩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很多人可能知道,git中只能有一种钩子类型。
如果需要评估两个更新钩子。 git admin留下了两个无法管理的解决方案:


  1. 合并钩子脚本

  2. 使用 exec

手动链接它们一个优雅的解决方案(写在BASH中),就像一个文件夹 hooks / update.d hooks / post-receive.d ,这将允许钩子评估的松散耦合。



实际上我发现了一个可接受的解决方案,用perl写在这个URL http://blog.bluefeet.net/2011/08/chained-git-hooks



<问题:我的服务器运行不同版本的Perl,并且我得到perllib版本不匹配。它会失败。

解决方案

经过进一步的调查和测试,这里有一个工作解决方案:

创建文件 .git / hooks / hook-chain 如下

 #!/ bin / bash 

#author:orefalo

hookname =`basename $ 0`


FILE =`mktemp`
trap'rm -f $ FILE'EXIT
cat - > $ FILE

for $ GIT_DIR / hooks / $ hookname。*
do
如果测试-x$ hook;然后
#echo $ hook
cat $ FILE | $ hook$ @
status = $?

如果测试$ status -ne 0;然后
echo Hook $ hook失败,错误代码为$ status
exit $ status
fi
fi
done

现在链接任何需要链接的钩子,例如


  • ln - s链接更新

  • ln -s hook-chain post-receive



通过将它们重命名为 hookname action

  -rwxr-xr-x。 1 git git 6710函数
-rwxr-xr-x。 1 git git 280钩链
-rwxr-xr-x。 1 git git 1524 post-mirror
lrwxrwxrwx。 1 root root 10 post-receive - >钩链
-rwxr-xr-x。 1 git git 8763 post-receive.1email
-rwxr-xr-x。 1 git git 1745 post-receive.2github
-rwxr-xr-x。 1 git git 473 post-upload-pack
-rwxr-xr-x。 1 git git 346预收
lrwxrwxrwx。 1根root 10更新 - >钩链
-rwxr-xr-x。 1 git git 2975 update.1acl
-rwxr-xr-x。 1 git git 328 update.2github

例如,在上面的示例中,更新挂钩将运行 update.1acl ,然后是 update.2github 接收钩子,然后运行 post-receive.1email 后接 post-receive.2github

As many of you probably know, there can be only one hook type in git. If two update hooks need to be evaluated. The git admin is left with two unmanageable solutions:

  1. Merge the hook scripts together
  2. Manually chain them with an exec

I am looking for an elegant solution (written in BASH),something like a folder hooks/update.d or hooks/post-receive.d that will allow the loosely coupling of hook evaluations. The chaining should stop as soon as a hook fails.

I actually found an acceptable solution written in perl at this URL http://blog.bluefeet.net/2011/08/chained-git-hooks

The problem: my server runs different versions of perl and I am getting perllib version mismatches. It fails.

解决方案

After further investigation and testing, here is a working solution:

create file .git/hooks/hook-chain as follows

#!/bin/bash
#
# author: orefalo

hookname=`basename $0`


FILE=`mktemp`
trap 'rm -f $FILE' EXIT
cat - > $FILE

for hook in $GIT_DIR/hooks/$hookname.*
do
    if test -x "$hook"; then
#       echo $hook
        cat $FILE | $hook "$@"
        status=$?

        if test $status -ne 0; then
            echo Hook $hook failed with error code $status
            exit $status
        fi
    fi
done

Now link any hook that requires chaining, for instance

  • ln -s hook-chain update
  • ln -s hook-chain post-receive

finally, create the chains by renaming them as hookname.action

 -rwxr-xr-x. 1 git  git  6710  functions
 -rwxr-xr-x. 1 git  git   280  hook-chain
 -rwxr-xr-x. 1 git  git  1524  post-mirror
 lrwxrwxrwx. 1 root root   10  post-receive -> hook-chain
 -rwxr-xr-x. 1 git  git  8763  post-receive.1email
 -rwxr-xr-x. 1 git  git  1745  post-receive.2github
 -rwxr-xr-x. 1 git  git   473  post-upload-pack
 -rwxr-xr-x. 1 git  git   346  pre-receive
 lrwxrwxrwx. 1 root root   10  update -> hook-chain
 -rwxr-xr-x. 1 git  git  2975  update.1acl
 -rwxr-xr-x. 1 git  git   328  update.2github

for instance, in the sample above, the update hook will run update.1acl followed by update.2github.

The post-receive hook with run post-receive.1email followed by post-receive.2github

这篇关于链接git钩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆