Skip to content

Instantly share code, notes, and snippets.

@stafot
Forked from dwmkerr/gpr.sh
Created December 28, 2018 15:18
Show Gist options
  • Save stafot/abacdaada171d1399c68b83f01c99518 to your computer and use it in GitHub Desktop.
Save stafot/abacdaada171d1399c68b83f01c99518 to your computer and use it in GitHub Desktop.
A shell command to push and open a pull request. Tested on OSX/Ubuntu. No dependencies.
#!/usr/bin/env bash
# gpr - push the current branch to origin and attempt to open the Create Pull
# Request or Create Merge Request page if available.
#
# Inspired by: https://gist.github.com/tobiasbueschel/ba385f25432c6b75f63f31eb2edf77b5
# How to get the current branch: https://stackoverflow.com/questions/1593051/how-to-programmatically-determine-the-current-checked-out-git-branch
# How to open the browser: https://stackoverflow.com/questions/3124556/clean-way-to-launch-the-web-browser-from-shell-script
# Colour constants for nicer output.
GREEN='\033[0;32m'
RESET='\033[0m'
# Push the current branch to origin, set upstream, open the PR page if possible.
gpr() {
# Get the current branch name, or use 'HEAD' if we cannot get it.
branch=$(git symbolic-ref -q HEAD)
branch=${branch##refs/heads/}
branch=${branch:-HEAD}
# Pushing take a little while, so let the user know we're working.
echo "Opening pull request for ${GREEN}${branch}${RESET}..."
# Push to origin, grabbing the output but then echoing it back.
push_output=`git push origin -u ${branch} 2>&1`
echo ""
echo ${push_output}
# If there's anything which starts with http, it's a good guess it'll be a
# link to GitHub/GitLab/Whatever. So open it.
link=$(echo ${push_output} | grep -o 'http.*' | sed -e 's/[[:space:]]*$//')
if [ ${link} ]; then
echo ""
echo "Opening: ${GREEN}${link}${RESET}..."
python -mwebbrowser ${link}
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment