Last active
March 20, 2026 11:12
-
-
Save Conduitry/059e153069fe1537dc2143dae3810a06 to your computer and use it in GitHub Desktop.
Checkout PR, using GitHub API to determine what to track
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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://api.github.com/repos/$target/pulls/$1 | perl -MJSON::PP -0777 -E '$head=decode_json(<>)->{head}; say $head->{repo}->{full_name} . ":" . $head->{ref}') | |
| 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated again to use
JSON::PPto actually parse the JSON.