Created
May 29, 2019 14:47
-
-
Save alalek/03f3cd09af9e9e100d89ff02e5974c25 to your computer and use it in GitHub Desktop.
Fetch updates from GitHub Pull requests
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 -e | |
# Usage: | |
# $ pr_get <number> | |
# | |
# Configure remote 'upstream': https://help.github.com/en/articles/configuring-a-remote-for-a-fork | |
# $ git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git | |
PR=${1?Missing PR number} | |
if ! git diff-index --quiet HEAD --; then | |
echo "You have uncommited changes. Please commit your changes first" | |
exit 1 | |
fi | |
PR_UPSTREAM=${PR_UPSTREAM:-upstream} | |
DATE=`date +%m%d` | |
TARGET_BRANCH=pr${PR}_r | |
C=0 | |
while true; do | |
if [[ $C > 0 ]]; then SUFFIX="_${C}"; else SUFFIX=""; fi | |
UPDATE_BRANCH=pr${PR}_${DATE}${SUFFIX} | |
if git show-ref --verify --quiet refs/heads/${UPDATE_BRANCH}; then | |
C=$((${C}+1)) | |
else | |
break | |
fi | |
done | |
if git show-ref --verify --quiet refs/heads/${TARGET_BRANCH}; then | |
git branch -f ${TARGET_BRANCH}_old ${TARGET_BRANCH} | |
fi | |
( | |
set -x | |
git fetch ${PR_UPSTREAM} +pull/${PR}/head:${UPDATE_BRANCH} | |
git checkout -B ${TARGET_BRANCH} FETCH_HEAD | |
) | |
echo Done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment