代码之家  ›  专栏  ›  技术社区  ›  kraymer

在Gitlab上打开合并审查时,Git钩子破解了错误的目标分支

  •  1
  • kraymer  · 技术社区  · 6 年前

    我有通过单击远程服务器在推送上打印的链接来创建合并请求(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= this script 找到它)。

    pre-push 钩(如本地 post-push 操作不存在),建立这个url,我会点击,但你能找到一个不那么难看的黑客?


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

    1 回复  |  直到 6 年前
        1
  •  1
  •   kraymer    6 年前

    以下是我最后做的:

    #!/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
    

    “我的要素”分支是从其中一个分支分支而来的 hotfix develop 这些都是我给我父母的理由 git-branchestor.sh 脚本:

    #!/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