Skip to content

Instantly share code, notes, and snippets.

@mugijiru
Last active July 15, 2018 17:22
Show Gist options
  • Save mugijiru/339f66ef2900513f8ad3891f4443c4b1 to your computer and use it in GitHub Desktop.
Save mugijiru/339f66ef2900513f8ad3891f4443c4b1 to your computer and use it in GitHub Desktop.
Checkout branch from pull request number
#!/bin/bash
pr=$1
if [ ! $pr ]; then
echo "Require PR number."
exit 1
fi
pr=`sed -E "s%^.*pulls?/%%g; s%/files$%%" <<< $pr` # GitHub の URL だった場合に PR 番号を抜き出す
ref=`git ls-remote | grep refs/pull/${pr}/head | cut -f 1`
if [[ -z `git show-ref | grep origin | grep $ref | cut -f 1` ]]; then
echo "$ref Not found local repository."
echo "Fetching..."
git fetch
fi
branch_name=`git show $ref --quiet --format='%D' | sed -e 's%origin/%%' -e 's/, .*$//'`
if [[ -n `echo $branch_name | grep HEAD` ]]; then
echo "Target branch checkouted"
exit 1
fi
git checkout $branch_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment