Skip to content

Instantly share code, notes, and snippets.

@lrytz
Last active September 25, 2019 07:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lrytz/28dd202acfa06f4d1155fda3cdc0ee3a to your computer and use it in GitHub Desktop.
Save lrytz/28dd202acfa06f4d1155fda3cdc0ee3a to your computer and use it in GitHub Desktop.
#!/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
base=$(echo -E $prInfo | jq -r '.base.sha')
git rebase -i $base
git push -f $remoteName HEAD:$branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment