预推 git 未触发

Pre-push git is not triggering

我阅读了所有推送前的 Whosebug 问题,并遵循了每一条指令,但是当我调用 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,.*/\(.*\),,')

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,并确保它具有执行权限。

我真的看不出我错过了什么,我只想触发钩子。我会解决剩下的问题。

注意:我在 Debian 8 Jessie 上有这个 repo 和 hooks

我看到你的挂钩有两个问题。首先,在一次推送中,可能有多个 ref 要更新,并且您可能有多个 protect 分支。最好测试所有这些。其次,您可以推送一个不是当前分支的分支。所以测试当前分支是不安全的。

这里是一个基于模板的钩子pre-push.sample。您可以在 .git/hooks.

下找到 pre-push.sample 的本地副本
#!/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-push, pre-receive