Git 在 Gitlab 上打开合并审查时 hook hack 以解决错误的目标分支

Git hook hack to workaround bad target branch when opening merge review on Gitlab

我有通过单击远程服务器在推送时打印的 link 创建合并请求 (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分支*

所以基本上我更愿意将 url 的 merge_request%5Btarget_branch%5D= url 参数设置为父分支(this script 找到它)。

我可以编写一个本地 pre-push 挂钩(因为本地 post-push 操作不存在)来构建这个 url 我会点击它,但是你能找到一个不那么难看的黑客?


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

这是我最后做的事情:

~/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 `
commit2=`git merge-base $branch `
ancestor=`git merge-base ${commit1} ${commit2}`
if [[ "$commit1" == "$ancestor" ]]; then
    echo 
else
    echo 
fi

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

.git/hooks/pre-push

#!/bin/bash

remote=""
url=""

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