【问题标题】:How to get origin merge branch name with git hook pre-merge-commit如何使用 git hook pre-merge-commit 获取原始合并分支名称
【发布时间】:2019-12-02 12:40:26
【问题描述】:

我正在尝试使用新的 git hook 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 命令的参数,请考虑获取进程树并检查它们的参数。

标签: git shell hook


【解决方案1】:

我修改了找到here 的脚本以确保完全匹配。它使用Git environment variable 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-07
    • 2011-12-16
    • 2017-03-16
    • 1970-01-01
    • 1970-01-01
    • 2020-04-08
    • 1970-01-01
    相关资源
    最近更新 更多