如何使用git钩子pre-merge-commit获取合并分支的原始分支名称

6

我正在尝试使用新的git钩子pre-merge-commit创建一个特定的脚本,但它没有参数。

有没有什么解决方法可以获取正在合并的分支的名称?

例如: 在myBranch分支上,我调用“git merge testingBranch”,我想在我的脚本中获取“testingBranch”。

原因:我需要阻止将项目中的一个特定分支合并到任何其他分支中。 因此,它将是这样的:

if [[ "$originOfMergeBranchName" == "testingBranch" ]]
    then
        echo "You can't merge this branch to any other"
        exit 2
fi

我很难想象如何获得那个originOfMergeBranchName。

提前感谢:D


我可以运行 git merge <hash>,其中 <hash> 可以是 testingBranch 的末端或落后于 testingBranch 末端的提交,你将无法可靠地检测到这一点(特别是如果该提交也是其他分支的末端)。因此,这可能不是首先要解决的问题。但是,如果您想尝试查找 git merge 命令的参数,请考虑获取进程树,并检查它们的参数。 - torek
1个回答

0

我修改了我在这里找到的脚本,以确保精确匹配。它使用Git环境变量GIT_REFLOG_ACTION来获取合并的分支。

#!/bin/sh

if [[ $GIT_REFLOG_ACTION == *merge* ]]; then
    if [[ $GIT_REFLOG_ACTION =~ ^.*' 'test(' '.*)?$ ]]; then
        echo
        echo \# STOP THE PRESSES!
        echo \#
        echo \# You are trying to merge from test...
        echo \# Surely you don\'t mean that?
        echo \#
        echo \# Run the following command now to discard your working tree changes:
        echo \#
        echo \# git reset --merge
        echo
        exit 1
    fi
fi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接