Skip to content

Instantly share code, notes, and snippets.

@SmallJoker
Last active January 14, 2024 15:08
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SmallJoker/4d411b958295d9e694cd9aa5984563db to your computer and use it in GitHub Desktop.
Save SmallJoker/4d411b958295d9e694cd9aa5984563db to your computer and use it in GitHub Desktop.
Applies PRs to any repository. Place into ~/.local/bin and run in the target git directory
#!/bin/bash
remote="upstream"
url=$(LANG=C git remote -v get-url $remote 2>&1)
if [[ $url == fatal* || $url == error* ]]; then
remote="origin"
url=$(LANG=C git remote -v get-url $remote 2>&1)
fi
if [[ $url == fatal* || $url == error* ]]; then
echo "Can neither find 'upstream' nor 'origin' git remote."
exit 1
fi
if [[ $url =~ git@.+\.git ]]; then
# SSH: git@gitservice.tld:foo/bar.git
url="${url:4:-4}"
url="https://${url/:/\/}/"
fi
case "$2" in
"")
url="${url%.git}/pull/$1.patch"
curl -Ls $url | git am -3 --ignore-space-change -
;;
"diff")
url="${url%.git}/pull/$1.diff"
curl -Ls $url | git apply -
;;
"merge")
git fetch $remote pull/$1/head && git merge --no-edit FETCH_HEAD
;;
*)
echo "Invalid usage. Examples:"
echo " pr_apply 123 # Apply patch"
echo " pr_apply 123 merge # Quick merge"
echo " pr_apply 123 diff # Apply diff"
exit 1
;;
esac
if [ $? -ne 0 ]; then
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment