Reputation: 3434
I have the habit of creating merge requests (MR) by clicking the link printed by remote server on pushes :
╰─ 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
Problem is that the target branch of the MR will be set to the pre-configured master
branch *
So basically I would prefer to have an url with the merge_request%5Btarget_branch%5D=
url parameter set to the parent branch (this script finds it).
I could write a local pre-push
hook (as local post-push
operations do not exist) that builds this url that I would click instead, but can you find a less ugly hack ?
* which will result to a loss of time if I don't change the target branch immediatley as changing this form field reset all others fields (https://gitlab.com/gitlab-org/gitlab-ce/issues/22090)
Upvotes: 1
Views: 169
Reputation: 3434
Here is what I ended up doing :
~/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
My feature branches are branched off from either hotfix
or develop
ones, those are the arguments I give to my git-branchestor.sh
script :
.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
Upvotes: 1