预推git没有触发

时间:2018-06-08 10:27:28

标签: bash git push hook

我读了所有pre-push stackoverflow问题,我按照每条指令进行操作但是当我调用git push时仍然没有触发我的钩子

这是我的钩子

#!/bin/bash

protected_branch='master'
echo "Pre push hook is running..." # Even this line I can't see it in the output
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')

if [ $protected_branch = $current_branch ]
then
        echo "You can't push to master directly"
        exit 1 # push will not execute
else
        exit 0 # push will execute
fi

我还确保将钩子文件命名为pre-push,并确保它具有执行权限。

我真的看不到我错过了什么,我想做的只是触发钩子。我会弄明白其余的。

注意:我有这个repo并挂钩Debian 8 Jessie

1 个答案:

答案 0 :(得分:2)

我看到你的钩子里有两个小故障。首先,在一次推送中,可能会有多个ref更新,并且您可能有多个保护分支。最好测试所有这些。其次,你可以推出一个不是当前分支的分支。因此,测试当前分支是不安全的。

这是一个基于模板pre-push.sample的钩子。您可以在pre-push.sample下找到.git/hooks的本地副本。

#!/bin/sh

protected_branch='refs/heads/master'
echo "Pre push hook is running..." # Even this line I can't see it in the output

while read local_ref local_sha remote_ref remote_sha
do
    if [ "$remote_ref" = $protected_branch ];then
        echo "You can't push to master directly"
        exit 1 # push will not execute
    fi
done

exit 0

将其命名为pre-push,授予其可执行权限,并将其放在本地存储库的.git/hooks下。

以下是pre-receive的示例。它应该部署在远程存储库的.git/hooks下。

#!/bin/sh

protected_branch='refs/heads/master'
while read old_value new_value ref_name;do
    if [ "$ref_name" = $protected_branch ];then
        echo "You can't push to master directly"
        exit 1
    fi
done

exit 0

参考:pre-pushpre-receive