Skip to content

Instantly share code, notes, and snippets.

@Conduitry
Last active January 9, 2024 16:54
Show Gist options
  • Save Conduitry/059e153069fe1537dc2143dae3810a06 to your computer and use it in GitHub Desktop.
Save Conduitry/059e153069fe1537dc2143dae3810a06 to your computer and use it in GitHub Desktop.
Checkout PR, using GitHub API to determine what to track
#!/bin/sh
set -o errexit
[ $# != 1 ] && { >&2 echo 'Argument: <pull request id>'; exit 1; }
# Get origin remote
origin=$(git remote get-url origin)
# Find target ("owner/repo") of PR
temp=${origin#*github.com?}
[ $origin = $temp ] && { >&2 echo Unrecognized origin.; exit 1; }
target=${temp%.git}
# Find source ("owner/repo") of PR and remote name and branch name
temp=$(curl --silent https://github.com/$target/pull/$1 | grep --max-count=1 head-ref | cut --delimiter=\" --fields=2)
source=${temp%%:*}
remote=$(echo $origin | sed "s $target $source ")
branch=${temp#*:}
# Set up branch and checkout
git fetch --no-tags $remote $branch:pr/$1
git config branch.pr/$1.remote $remote
git config branch.pr/$1.merge refs/heads/$branch
git checkout pr/$1
@Conduitry
Copy link
Author

This checks out the branch in a fork associated with a given PR number in a local branch called pr/123 and sets it up to track that branch on that fork.

Some notes:

  • You'll need to create a token which permits read access to the repos in question.
  • This was written for Linux, and requires a rather more recent of Bash than macOS currently comes with.
  • You'll also need to set git config push.default upstream to be able to easily push back to the fork, because your branch name will differ from that in the fork.

Usage:

  • ./checkout_pr.sh 123
  • do whatever
  • git push (assuming you've set push.default=upstream)

@Conduitry
Copy link
Author

I've just updated this to make it easier to use in a few ways:

  • It no longer uses the GraphQL API, so you don't need a token anymore. (It instead scrapes the HTML, which is a bit more brittle. If it becomes a problem, I'll probably rework it to use the real REST API instead. The main reason I'm not doing that now is to avoid dealing with JSON or depending on something like jq.)
  • If your repo was cloned with SSH, the new branches pointing at the forks will also be cloned with SSH rather than HTTP. (When I first wrote this, I recall that pushing as a maintainer to a branch of a PR didn't work when it was cloned via SSH. That seems to be working now, so no reason not to support this.)
  • It no longer uses Bash. It works for me on Ubuntu with Dash, so presumably this will also work as-is on macOS without installing anything else.

Usage is still the same:

  • ./checkout_pr.sh 123
  • do whatever
  • git push (assuming you've set push.default=upstream)

@Conduitry
Copy link
Author

Here's a hacky way of using the REST API without jq (which is probably not much better than scraping the HTML like I'm doing now), but which I wanted to stick somewhere for posterity, in case it can become the basis for something useful later:

# Find source ("owner/repo") of PR and remote name and branch name
temp="$(curl --silent https://api.github.com/repos/$target/pulls/$1 | tr --delimiter \\n\\r | tr \" \\n)"
source=$(echo "$temp" | grep --after-context=2 --max-count=1 ^full_name$ | tail --lines=1)
remote=$(echo $origin | sed "s $target $source ")
branch=$(echo "$temp" | grep --after-context=2 --max-count=1 ^ref$ | tail --lines=1)

@Rich-Harris
Copy link

I needed to make the following change to make this work on MacOS:

-temp=$(curl --silent https://github.com/$target/pull/$1 | grep --max-count=1 head-ref | cut --delimiter=\" --fields=2)
+temp=$(curl --silent https://github.com/$target/pull/$1 | grep --max-count=1 head-ref | cut -d\" -f2)

ChatGPT promises me that this should work cross-platform.

@Conduitry
Copy link
Author

Interesting. I've gotten into a habit of using long GNU-style command line options in all my scripts because it's generally a little more clear about what it's actually doing. I did know there were some situations/environments that only support short/POSIX-style switches, and apparently we've run into one here. If I'm going to publish this as a standalone thing, it might make sense to switch everything over to POSIX switches. I'm not sure.

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