Skip to content

Instantly share code, notes, and snippets.

@stafot
Forked from garno/open-pr
Created February 21, 2019 19:48
Show Gist options
  • Save stafot/81412b0d2d14822feefae221e07584b0 to your computer and use it in GitHub Desktop.
Save stafot/81412b0d2d14822feefae221e07584b0 to your computer and use it in GitHub Desktop.
Small bash script to automatically open Pull Request page with branches correctly selected
#!/bin/sh
# Retrieve branch name
branch=$(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
# Retrieve destination branch
destination_branch=${branch%--*}
# Determine the default origin branch
if [[ $branch == $destination_branch ]]; then
master_exists=$(git branch | grep 'master')
if [[ -z $master_exists ]]; then
destination_branch=develop
else
destination_branch=master
fi
fi
# Retrieve repository URL
repository_url=$(git remote get-url origin | sed -e 's/git@//' -e 's/.git//' -e 's/:/\//')
if [[ $repository_url == github* ]]; then
pr_url=https://$repository_url/compare/$destination_branch...$branch
elif [[ $repository_url == gitlab* ]]; then
pr_url="https://$repository_url/merge_requests/new?merge_request%5Bsource_branch%5D=$branch&merge_request%5Btarget_branch%5D=$destination_branch"
fi
# Open the new Pull Request URL
open $pr_url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment