Skip to content

Instantly share code, notes, and snippets.

@spmason
Last active July 15, 2022 10:54

Revisions

  1. spmason revised this gist Dec 15, 2021. 1 changed file with 11 additions and 11 deletions.
    22 changes: 11 additions & 11 deletions pr
    Original file line number Diff line number Diff line change
    @@ -11,12 +11,11 @@
    # (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
    USERNAME=$(gh api user --jq .login)
    MERGE_BRANCH=$(gh repo view --json defaultBranchRef --jq .defaultBranchRef.name)

    while test $# -gt 0
    do
    @@ -41,21 +40,22 @@ if ! git remote | grep $USERNAME 1> /dev/null; then
    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 ! gh pr view ${REMOTE}:${LOCAL_BRANCH} --json url --jq .url &> /dev/null; then
    if [[ $SHOW_BROWSER == 1 ]]; then
    # Open browser to create new PR
    hub compare -b "${MERGE_BRANCH}" ${HUB_ARGS}
    HEAD="${REMOTE}:${LOCAL_BRANCH}"
    if [[ "${REMOTE}" = "origin" ]]; then
    echo "Remote is ${REMOTE}"
    HEAD="${LOCAL_BRANCH}"
    fi
    gh pr create --web --head "$HEAD"
    fi
    elif [[ $SHOW_BROWSER == 1 ]]; then
    # Show existing PR
    hub pr show
    gh pr view ${REMOTE}:${LOCAL_BRANCH} --web
    else
    # Just output the PR URL
    hub pr show --url
    gh pr view ${REMOTE}:${LOCAL_BRANCH} --json url --jq .url
    fi
  2. spmason revised this gist Mar 4, 2021. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions pr
    Original file line number Diff line number Diff line change
    @@ -48,8 +48,10 @@ fi
    git push ${REMOTE} ${LOCAL_BRANCH} ${PUSH_ARGS}

    if ! hub pr show --url &> /dev/null; then
    # Open browser to create new PR
    hub compare -b "${MERGE_BRANCH}" ${HUB_ARGS}
    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
  3. spmason revised this gist Mar 4, 2021. 1 changed file with 30 additions and 53 deletions.
    83 changes: 30 additions & 53 deletions pr
    Original file line number Diff line number Diff line change
    @@ -6,77 +6,54 @@
    # on the branch you want to open a Pull Request for
    #
    # NOTES:
    # - If working from a fork, name your fork's remote "origin" and the main repo "upstream"
    # - `pr <branch_name>` will open a PR against the branch you give - rather than "master".
    # - Use `git config pr.defaultbranch <branch_name>` to tell this script the correct branch
    # to use by default (ie if your repos default branch is not "master")
    # - If you're working against a GitHub enterprise instance, set the github URL with `git config pr.githuburl`
    # eg $ git config pr.githuburl "https://git.mycompany.com"
    set -e
    # - 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=""
    OPENBROWSER=1
    LOCAL_BRANCH=`git branch | grep ^\* | sed 's/^* //'`
    BOX=`uname -s`
    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"
    --force) PUSH_ARGS="--force-with-lease"
    ;;
    -f) PUSH_ARGS="--force"
    -f) PUSH_ARGS="--force-with-lease"
    ;;
    --no-browser) OPENBROWSER=0
    --no-browser) SHOW_BROWSER=0
    ;;
    -n) OPENBROWSER=0
    -n) SHOW_BROWSER=0
    ;;
    *) MERGE_BRANCH=$1
    ;;
    esac
    shift
    done

    git push origin $LOCAL_BRANCH $PUSH_ARGS

    set +e
    ORIGIN_ORG_NAME=`git config remote.origin.url | sed -E 's/.*:(.*)\/.*/\1/'`
    UPSTREAM_ORG_NAME=`git config remote.upstream.url | sed -E 's/.*:(.*)\/.*/\1/'`
    REPO_NAME=`git config remote.upstream.url | sed -E 's/.*\/([^\/]*)\.git/\1/'`
    GITHUB_URL=`git config pr.githuburl`
    set -e

    if [ "$GITHUB_URL" == "" ]; then
    GITHUB_URL=https://github.com
    fi
    if [ "$MERGE_BRANCH" == "" ]; then
    set +e
    MERGE_BRANCH=`git config pr.defaultbranch`
    if [ "$MERGE_BRANCH" == "" ]; then
    MERGE_BRANCH=master
    fi
    set -e
    REMOTE=$USERNAME
    if ! git remote | grep $USERNAME 1> /dev/null; then
    echo "No remote $USERNAME found. Pushing to origin"
    REMOTE=origin
    fi

    if [ "$ORIGIN_ORG_NAME" == "" ]; then
    echo Cannot divine the origin organisation name. Ensure you have a remote called "origin"
    exit 1
    if [[ $SHOW_BROWSER == 0 ]]; then
    HUB_ARGS="$HUB_ARGS --url"
    fi
    if [ "$UPSTREAM_ORG_NAME" == "" ]; then
    UPSTREAM_ORG_NAME=$ORIGIN_ORG_NAME
    fi
    if [ "$REPO_NAME" == "" ]; then
    REPO_NAME=`git config remote.origin.url | sed -E 's/.*\/([^\/]*)\.git/\1/'`
    fi

    URL=$GITHUB_URL/$UPSTREAM_ORG_NAME/$REPO_NAME/pull/new/$MERGE_BRANCH...$ORIGIN_ORG_NAME:$LOCAL_BRANCH

    if [ "$OPENBROWSER" == "1" ]; then
    echo Opening $URL
    if [ "$BOX" == "Linux" ]; then
    xdg-open $URL > /dev/null
    elif [ "$BOX" == "Darwin" ]; then
    open $URL
    else
    explorer.exe $URL
    fi
    git push ${REMOTE} ${LOCAL_BRANCH} ${PUSH_ARGS}

    if ! hub pr show --url &> /dev/null; then
    # Open browser to create new PR
    hub compare -b "${MERGE_BRANCH}" ${HUB_ARGS}
    elif [[ $SHOW_BROWSER == 1 ]]; then
    # Show existing PR
    hub pr show
    else
    # Just output the PR URL
    hub pr show --url
    fi
  4. spmason revised this gist Sep 29, 2017. No changes.
  5. spmason revised this gist Aug 12, 2015. 1 changed file with 30 additions and 10 deletions.
    40 changes: 30 additions & 10 deletions pr
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    # GitHub "Open Pull Request" script
    #
    # Add somewhere in your path, then opening a PR is as easy as typing `pr` while you're
    # 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:
    @@ -14,11 +14,29 @@
    # eg $ git config pr.githuburl "https://git.mycompany.com"
    set -e

    PUSH_ARGS=""
    OPENBROWSER=1
    LOCAL_BRANCH=`git branch | grep ^\* | sed 's/^* //'`
    MERGE_BRANCH=$1
    BOX=`uname -s`

    git push origin $LOCAL_BRANCH
    while test $# -gt 0
    do
    case "$1" in
    --force) PUSH_ARGS="--force"
    ;;
    -f) PUSH_ARGS="--force"
    ;;
    --no-browser) OPENBROWSER=0
    ;;
    -n) OPENBROWSER=0
    ;;
    *) MERGE_BRANCH=$1
    ;;
    esac
    shift
    done

    git push origin $LOCAL_BRANCH $PUSH_ARGS

    set +e
    ORIGIN_ORG_NAME=`git config remote.origin.url | sed -E 's/.*:(.*)\/.*/\1/'`
    @@ -52,11 +70,13 @@ fi

    URL=$GITHUB_URL/$UPSTREAM_ORG_NAME/$REPO_NAME/pull/new/$MERGE_BRANCH...$ORIGIN_ORG_NAME:$LOCAL_BRANCH

    echo Opening $URL
    if [ "$BOX" == "Linux" ]; then
    xdg-open $URL > /dev/null
    elif [ "$BOX" == "Darwin" ]; then
    open $URL
    else
    explorer.exe $URL
    if [ "$OPENBROWSER" == "1" ]; then
    echo Opening $URL
    if [ "$BOX" == "Linux" ]; then
    xdg-open $URL > /dev/null
    elif [ "$BOX" == "Darwin" ]; then
    open $URL
    else
    explorer.exe $URL
    fi
    fi
  6. spmason revised this gist Jan 13, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pr
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ set +e
    ORIGIN_ORG_NAME=`git config remote.origin.url | sed -E 's/.*:(.*)\/.*/\1/'`
    UPSTREAM_ORG_NAME=`git config remote.upstream.url | sed -E 's/.*:(.*)\/.*/\1/'`
    REPO_NAME=`git config remote.upstream.url | sed -E 's/.*\/([^\/]*)\.git/\1/'`
    GITHUB_URL=`git config pr.github.url`
    GITHUB_URL=`git config pr.githuburl`
    set -e

    if [ "$GITHUB_URL" == "" ]; then
  7. spmason revised this gist Jan 13, 2014. 1 changed file with 10 additions and 6 deletions.
    16 changes: 10 additions & 6 deletions pr
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,17 @@
    #!/usr/bin/env bash

    # 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 opena PR for
    # 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:
    # - If working from a fork, name your fork's remove "origin" and the main repo "upstream"
    # - Use pr <branch_name> to open a PR against a branch other than "master". If the default branch is not "master", use `git config pr.defaultbranch <branch_name>` to tell this script the correct branch to use
    # - If you're working against a GitHub enterprise instance, set `git config pr.githuburl`
    # - If working from a fork, name your fork's remote "origin" and the main repo "upstream"
    # - `pr <branch_name>` will open a PR against the branch you give - rather than "master".
    # - Use `git config pr.defaultbranch <branch_name>` to tell this script the correct branch
    # to use by default (ie if your repos default branch is not "master")
    # - If you're working against a GitHub enterprise instance, set the github URL with `git config pr.githuburl`
    # eg $ git config pr.githuburl "https://git.mycompany.com"
    set -e

    LOCAL_BRANCH=`git branch | grep ^\* | sed 's/^* //'`
    @@ -53,6 +58,5 @@ if [ "$BOX" == "Linux" ]; then
    elif [ "$BOX" == "Darwin" ]; then
    open $URL
    else
    echo Are you using Windows??? Come oooon!
    explorer.exe $URL
    fi
  8. spmason revised this gist Dec 10, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions pr
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,6 @@
    # - If working from a fork, name your fork's remove "origin" and the main repo "upstream"
    # - Use pr <branch_name> to open a PR against a branch other than "master". If the default branch is not "master", use `git config pr.defaultbranch <branch_name>` to tell this script the correct branch to use
    # - If you're working against a GitHub enterprise instance, set `git config pr.githuburl`

    set -e

    LOCAL_BRANCH=`git branch | grep ^\* | sed 's/^* //'`
    @@ -16,15 +15,16 @@ BOX=`uname -s`

    git push origin $LOCAL_BRANCH

    set +e
    ORIGIN_ORG_NAME=`git config remote.origin.url | sed -E 's/.*:(.*)\/.*/\1/'`
    UPSTREAM_ORG_NAME=`git config remote.upstream.url | sed -E 's/.*:(.*)\/.*/\1/'`
    REPO_NAME=`git config remote.upstream.url | sed -E 's/.*\/([^\/]*)\.git/\1/'`
    GITHUB_URL=`git config pr.github.url`
    set -e

    if [ "$GITHUB_URL" == "" ]; then
    GITHUB_URL=https://github.com
    fi

    if [ "$MERGE_BRANCH" == "" ]; then
    set +e
    MERGE_BRANCH=`git config pr.defaultbranch`
  9. spmason revised this gist Dec 10, 2013. 1 changed file with 11 additions and 5 deletions.
    16 changes: 11 additions & 5 deletions pr
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,12 @@
    #!/usr/bin/env bash

    # 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 pull request for
    # Add somewhere in your path, then opening a PR is as easy as typing `pr` on the branch you want to opena PR for
    #
    # NOTES:
    # - If working from a fork, name your fork's remove "origin" and the main repo "upstream"
    # - If the default branch is not "master", use `git config pr.defaultbranch <branch_name>` to tell this script the correct branch to use
    # - Use pr <branch_name> to open a PR against a branch other than "master". If the default branch is not "master", use `git config pr.defaultbranch <branch_name>` to tell this script the correct branch to use
    # - If you're working against a GitHub enterprise instance, set `git config pr.githuburl`

    set -e

    @@ -18,11 +19,16 @@ git push origin $LOCAL_BRANCH
    ORIGIN_ORG_NAME=`git config remote.origin.url | sed -E 's/.*:(.*)\/.*/\1/'`
    UPSTREAM_ORG_NAME=`git config remote.upstream.url | sed -E 's/.*:(.*)\/.*/\1/'`
    REPO_NAME=`git config remote.upstream.url | sed -E 's/.*\/([^\/]*)\.git/\1/'`
    GITHUB_URL=`git config pr.github.url`

    if [ "$GITHUB_URL" == "" ]; then
    GITHUB_URL=https://github.com
    fi

    if [ "$MERGE_BRANCH" == "" ]; then
    set +e
    MERGE_BRANCH=`git config pr.defaultbranch 2> /dev/null`
    if [ "$0" != "0" -o "$REPO_NAME" == "" ]; then
    MERGE_BRANCH=`git config pr.defaultbranch`
    if [ "$MERGE_BRANCH" == "" ]; then
    MERGE_BRANCH=master
    fi
    set -e
    @@ -39,7 +45,7 @@ if [ "$REPO_NAME" == "" ]; then
    REPO_NAME=`git config remote.origin.url | sed -E 's/.*\/([^\/]*)\.git/\1/'`
    fi

    URL=https://github.com/$UPSTREAM_ORG_NAME/$REPO_NAME/pull/new/$MERGE_BRANCH...$ORIGIN_ORG_NAME:$LOCAL_BRANCH
    URL=$GITHUB_URL/$UPSTREAM_ORG_NAME/$REPO_NAME/pull/new/$MERGE_BRANCH...$ORIGIN_ORG_NAME:$LOCAL_BRANCH

    echo Opening $URL
    if [ "$BOX" == "Linux" ]; then
  10. spmason revised this gist Dec 10, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pr
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    #!/usr/bin/env bash

    # 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 opena PR for
    # Add somewhere in your path, then opening a PR is as easy as typing `pr` on the branch you want to open a pull request for
    #
    # NOTES:
    # - If working from a fork, name your fork's remove "origin" and the main repo "upstream"
  11. spmason created this gist Dec 10, 2013.
    52 changes: 52 additions & 0 deletions pr
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    #!/usr/bin/env bash

    # 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 opena PR for
    #
    # NOTES:
    # - If working from a fork, name your fork's remove "origin" and the main repo "upstream"
    # - If the default branch is not "master", use `git config pr.defaultbranch <branch_name>` to tell this script the correct branch to use

    set -e

    LOCAL_BRANCH=`git branch | grep ^\* | sed 's/^* //'`
    MERGE_BRANCH=$1
    BOX=`uname -s`

    git push origin $LOCAL_BRANCH

    ORIGIN_ORG_NAME=`git config remote.origin.url | sed -E 's/.*:(.*)\/.*/\1/'`
    UPSTREAM_ORG_NAME=`git config remote.upstream.url | sed -E 's/.*:(.*)\/.*/\1/'`
    REPO_NAME=`git config remote.upstream.url | sed -E 's/.*\/([^\/]*)\.git/\1/'`

    if [ "$MERGE_BRANCH" == "" ]; then
    set +e
    MERGE_BRANCH=`git config pr.defaultbranch 2> /dev/null`
    if [ "$0" != "0" -o "$REPO_NAME" == "" ]; then
    MERGE_BRANCH=master
    fi
    set -e
    fi

    if [ "$ORIGIN_ORG_NAME" == "" ]; then
    echo Cannot divine the origin organisation name. Ensure you have a remote called "origin"
    exit 1
    fi
    if [ "$UPSTREAM_ORG_NAME" == "" ]; then
    UPSTREAM_ORG_NAME=$ORIGIN_ORG_NAME
    fi
    if [ "$REPO_NAME" == "" ]; then
    REPO_NAME=`git config remote.origin.url | sed -E 's/.*\/([^\/]*)\.git/\1/'`
    fi

    URL=https://github.com/$UPSTREAM_ORG_NAME/$REPO_NAME/pull/new/$MERGE_BRANCH...$ORIGIN_ORG_NAME:$LOCAL_BRANCH

    echo Opening $URL
    if [ "$BOX" == "Linux" ]; then
    xdg-open $URL > /dev/null
    elif [ "$BOX" == "Darwin" ]; then
    open $URL
    else
    echo Are you using Windows??? Come oooon!
    explorer.exe $URL
    fi