Skip to content

Instantly share code, notes, and snippets.

@garno
Last active September 19, 2022 21:18
Show Gist options
  • Save garno/b9e02113f638d928e2be0a76dbf5b8d1 to your computer and use it in GitHub Desktop.
Save garno/b9e02113f638d928e2be0a76dbf5b8d1 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
@mlovic
Copy link

mlovic commented Jul 25, 2017

Nice.

A couple changes required to make it run on my machine (Ubuntu):

  • open -> xdg-open on last line
  • #!/bin/sh -> #!/usr/bin/env bash (bin/sh was symlinked to dash on my installation...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment