Last active
February 2, 2019 19:26
-
-
Save aikar/856e2e87070f16a6e633e7270fdd0a4e to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env bash | |
if [ -z "$1" ]; then | |
echo "$0 <prID>" | |
exit 1; | |
fi | |
repo=$(git remote get-url origin | sed -E 's/(.*@)?github.com(:|\/)//g' | sed 's/.git$//g') | |
data=$(curl -q https://api.github.com/repos/$repo/pulls/$1 2>/dev/null) | |
url=$(echo -e "$data" | grep --color=none ssh_url | head -n 1 |awk '{print $2}' | sed 's/"//g' | sed 's/,//g') | |
ref=$(echo -e "$data" | grep --color=none '"head":' -A 3 | grep ref | head -n 1 |awk '{print $2}' | sed 's/"//g' | sed 's/,//g') | |
prevbranch=$(\git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/') | |
branch="pr/$1" | |
up="pr-$1" | |
git remote remove $up 2>&1 1>/dev/null | |
git remote add -f $up $url | |
git branch -D $branch 2>/dev/null 1>&2 | |
git checkout -b $branch $up/$ref 2>/dev/null|| true | |
echo "Merging $prevbranch into $branch" | |
git fetch origin | |
read -p "Press 'm' to merge, 'r' to rebase, or 'n' for nothing" -n 1 -r >&2 | |
echo | |
if [[ "$d$REPLY" =~ ^d[Mm]$ ]]; then | |
git merge origin/$prevbranch | |
elif [[ "$d$REPLY" =~ ^d[Rr]$ ]]; then | |
git rebase origin/master | |
fi | |
echo "Dropping to new shell, exit to delete the refs" | |
bash -i | |
read -p "Press 'p' to push. " -n 1 -r >&2 | |
echo | |
pushed=0 | |
if [[ "d$REPLY" =~ ^d[Pp]$ ]]; then | |
git push $up $branch:$ref -f | |
pushed=1 | |
echo "Pushed" >&2 | |
fi | |
echo "Deleting branch/upstream" | |
git checkout $prevbranch | |
if [[ "$pushed" == "1" ]]; then | |
read -p "Press 'm' to merge. " -n 1 -r >&2 | |
if [[ "d$REPLY" =~ ^d[Mm]$ ]]; then | |
git rebase $branch | |
fi | |
fi | |
git branch -D $branch | |
git remote remove $up | |
git gc | |
#git branch -u $up/$ref $branch |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment