Skip to content

Instantly share code, notes, and snippets.

@spmason
Last active Mar 4, 2021
Embed
What would you like to do?
GitHub "open PR" script. Add somewhere in your path, then opening a PR is as easy as typing `pr` on the branch you want to open a PR for
#!/usr/bin/env bash
# GitHub "Open Pull Request" script
#
# Add somewhere in your path, then opening a PR is as easy as typing `pr` while you're
# on the branch you want to open a Pull Request for
#
# NOTES:
# - Uses the `hub` tool: https://cli.github.com/
# - Assumes that the upstream repository is called 'origin' and your fork is named after your github username
# (this is how `hub clone` and then `hub fork` should set things up)
PUSH_ARGS=""
HUB_ARGS=""
SHOW_BROWSER=1
LOCAL_BRANCH=$(git branch | grep ^\* | sed 's/^* //')
BOX=$(uname -s)
USERNAME=$(hub api user | jq .login -r)
MERGE_BRANCH=master
while test $# -gt 0
do
case "$1" in
--force) PUSH_ARGS="--force-with-lease"
;;
-f) PUSH_ARGS="--force-with-lease"
;;
--no-browser) SHOW_BROWSER=0
;;
-n) SHOW_BROWSER=0
;;
*) MERGE_BRANCH=$1
;;
esac
shift
done
REMOTE=$USERNAME
if ! git remote | grep $USERNAME 1> /dev/null; then
echo "No remote $USERNAME found. Pushing to origin"
REMOTE=origin
fi
if [[ $SHOW_BROWSER == 0 ]]; then
HUB_ARGS="$HUB_ARGS --url"
fi
git push ${REMOTE} ${LOCAL_BRANCH} ${PUSH_ARGS}
if ! hub pr show --url &> /dev/null; then
if [[ $SHOW_BROWSER == 1 ]]; then
# Open browser to create new PR
hub compare -b "${MERGE_BRANCH}" ${HUB_ARGS}
fi
elif [[ $SHOW_BROWSER == 1 ]]; then
# Show existing PR
hub pr show
else
# Just output the PR URL
hub pr show --url
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment