Last active
July 15, 2018 17:22
-
-
Save mugijiru/339f66ef2900513f8ad3891f4443c4b1 to your computer and use it in GitHub Desktop.
Checkout branch from pull request number
This file contains 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/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