Skip to content

Instantly share code, notes, and snippets.

@dwmkerr
Last active October 22, 2023 12:10
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dwmkerr/bae3fdca2d7208ec5d0008911d79b47d to your computer and use it in GitHub Desktop.
Save dwmkerr/bae3fdca2d7208ec5d0008911d79b47d 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.
# This function pushes the current branch to 'origin'. If a web address is shown
# in the output, it opens it up. On GitHub, GitLab and BitBucket, this means it
# will open the pull request for you!
#
# This is also part of the 'dotfiles' configration at:
# github.com/effective-shell/dotfiles
# Push the current branch to origin, set upstream, open the PR page if possible.
# 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
gpr() {
# Colour constants for nicer output.
green='\e[0;32m'
reset='\e[0m'
# 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.
printf "Opening pull request for ${green}${branch}${reset}...\n"
# Push to origin, grabbing the output but then echoing it back.
push_output=`git push origin -u ${branch} 2>&1`
printf "\n${push_output}\n"
# If there's anything which starts with http, it's a good guess it'll be a
# link to GitHub/GitLab/Whatever. So open the first link found.
link=$(echo ${push_output} | grep -o 'http.*' | head -n1 | sed -e 's/[[:space:]]*$//')
if [ ${link} ]; then
printf "\nOpening: ${green}${link}${reset}...\n"
python -mwebbrowser ${link}
fi
}
@18info
Copy link

18info commented May 4, 2021

Hi!
Thanks for sharing :)

  • open SUSE 15.2 / BaSH 4.4.23, echo needs '-e' to correctly parse escape char.
  • Debian buster / BaSH 5.0.3 idem
  • ArchLinux / BaSH 5.1.4 idem
    I changed from '\033' to '\e' to make it work on all OS I use. Ex:
black="\e[30m"
green="\e[32m"
greeb="\e[1;32m"
bold="\e[1m"
uline="\e[4m"
Soff="\e[0m"

So, I can do echo -e "${bold}${green}TEST${Soff}" or echo -e "${uline}${greeb}TEST${Soff}"

Hope this helps

@dwmkerr
Copy link
Author

dwmkerr commented May 10, 2021

This is super helpful thanks @XlllllllX! I've updated the gist, as well as adding the head -n1 call so that we only open a single url.

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