dotfiles/bin/git-pullpr

101 lines
2.1 KiB
Bash
Executable File

#!/usr/bin/env bash
set -e
if [[ -z "$3" ]]; then
cat <<END | less -X
Usage: git pullpr <orguser/repo> <user:branch>[/repo] <prnumber>
Merge an arbitrary GitHub PR from some arbitrary repo with
an automated merge message mentioning that PR.
Make sure you're already in the correct git repo.
Example:
> git pullpr \\
main-repo-user/main-repo \\
pr-user:pr-branch/pr-user-repo \\
123
Merges PR #123 from main-repo-user/main-repo, which is
written by pr-user on their pr-user-repo, on pr-branch.
The merge message will be:
> Merge upstream PR #123
>
> main-repo-user/main-repo#123
pr-user-repo can be omitted, in which case it will be
assumed to be the same as main-repo name.
So to merge multiple PRs from the same repo you only need to
change the last two arguments.
This script was born from the need of merging upstream PRs
while working on my fork of github.com/simrat39/symbols-outline.nvim
which turned out to be worth it.
END
exit
fi
userbranch="$2"
if [[ "$2" =~ "/" ]]; then
prrepo=$(echo "$2" | sed "s_.*/__")
userbranch=$(echo "$2" | sed "s_/.*__")
fi
user=$(echo "$userbranch" | sed "s/:.*//")
branch=$(echo "$userbranch" | sed "s/.*://")
orguser=$(echo "$1" | sed "s_/.*__")
repo=$(echo "$1" | sed "s_.*/__")
prnumber="$3"
if [[ -z "$prrepo" ]]; then
prrepo="$repo"
fi
echo "PR: $user/$prrepo on $branch, PR #$prnumber"
echo "Upstream: $orguser/$repo"
echo -n "Press enter to continue..."
read
git remote add "$user" git@github.com:"$user"/"$prrepo"
echo "Added remote $user"\n
echo "Fetching"
git fetch "$user" "$branch"
cat <<END
Merge:
git merge $user/$branch -m "Merge upstream PR $prnumber" -m "" -m "$orguser/$repo#$prnumber"
END
echo -n "Press enter to continue..."
read
git merge $user/$branch -m "Merge upstream PR #$prnumber" -m "" -m "$orguser/$repo#$prnumber"
echo
echo "Merge complete."
git log -n1
cat <<END
Remove remote:
git remote remove $user
END
echo -n "Press enter to continue..."
read
git remote remove $user
echo "Remote removed."
git remote -v
# For gw/gq'ing on usage paragraphs:
# vim: textwidth=60