当在Gitlab上打开合并审查时,Git会钩住黑客以解决错误的目标分支

时间:2018-11-13 16:01:31

标签: git gitlab hook githooks

我习惯通过单击远程服务器在推送时打印的链接来创建合并请求(MR):

╰─ git push
Counting objects: 33, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (33/33), done.
Writing objects: 100% (33/33), 3.46 KiB | 1.73 MiB/s, done.
Total 33 (delta 31), reused 0 (delta 0)
remote: 
remote: To create a merge request for modelref, visit:
remote:   https://gitlab.com/foo/bar/merge_requests/new?merge_request%5Bsource_branch%5D=mybranch

问题是MR的目标分支将设置为预先配置的master分支*

因此,基本上,我希望将一个merge_request%5Btarget_branch%5D= URL参数设置为父分支的URL(this script找到它)。

我可以编写一个本地pre-push钩子(因为不存在本地post-push操作),该钩子可以代替我单击该URL,但是您能找到一个不太丑陋的hack吗?


* ,如果我不立即更改目标分支,则会浪费时间,因为更改此表单字段会重置所有其他字段(https://gitlab.com/gitlab-org/gitlab-ce/issues/22090

1 个答案:

答案 0 :(得分:1)

这就是我最终要做的事情:

〜/ bin / git-branchestor.sh

#!/bin/bash

# Find closest ancestor given two candidate branches

branch=`git rev-parse --abbrev-ref HEAD`
commit1=`git merge-base $branch ${1}`
commit2=`git merge-base $branch ${2}`
ancestor=`git merge-base ${commit1} ${commit2}`
if [[ "$commit1" == "$ancestor" ]]; then
    echo ${2}
else
    echo ${1}
fi

我的功能分支从hotfixdevelop分支出来,这些是我在git-branchestor.sh脚本中提供的参数:

.git / hooks / pre-push

#!/bin/bash

remote="$1"
url="$2"

z40=0000000000000000000000000000000000000000

while read local_ref local_sha remote_ref remote_sha
do
    if [ "$local_sha" = $z40 ]
    then
        # Handle delete
        :
    else
        if [ "$remote_sha" = $z40 ]
        then
            # New branch, examine all commits
            range="$local_sha"
        else
            # Update to existing branch, examine new commits
            range="$remote_sha..$local_sha"
        fi
        branch=$(git rev-parse --abbrev-ref HEAD)
        if [[ "$branch" != "hotfix" && "$branch" != "master" && "$branch" != "develop" ]]; then
          ancestor=`~/bin/git-branchestor.sh hotfix develop`
          echo "Open MR: https://gitlab.com/user/project/merge_requests/new?merge_request%5Bsource_branch%5D=${branch}&merge_request%5Btarget_branch%5D=${ancestor}"
          echo ""
        fi
    fi
done

exit 0