Skip to content

Instantly share code, notes, and snippets.

@lrytz
Created March 11, 2021 08:22
Show Gist options
  • Save lrytz/b7575e29fa8055b12570c8fafa244c32 to your computer and use it in GitHub Desktop.
Save lrytz/b7575e29fa8055b12570c8fafa244c32 to your computer and use it in GitHub Desktop.
fetchpr
#!/bin/bash
set -e
[[ $# == 1 ]] || {
echo "usage: $0 <pr-number>"
exit 1
}
pr=$1
upstream=$(git remote -v | grep -E '^upstream\s+' | head -1 | sed 's/upstream.*github.com.\(.*\)\.git.*/\1/')
git fetch upstream pull/$pr/head -f
git checkout pr$pr 2> /dev/null || git checkout -b pr$pr
git reset --hard FETCH_HEAD
prInfo=$(curl -s https://api.github.com/repos/$upstream/pulls/$pr)
remoteName=$(echo -E $prInfo | jq -r '.head.user.login')
git remote -v | grep -E "^$remoteName\s+" > /dev/null || {
remoteUrl=$(echo -E $prInfo | jq -r '.head.repo.ssh_url')
echo "Adding remote $remoteName - $remoteUrl"
git remote add $remoteName $remoteUrl
}
branch=$(echo -E $prInfo | jq -r '.head.ref')
git fetch -f $remoteName $branch
echo "Push changes with:"
echo git push -f $remoteName HEAD:$branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment