Skip to content

Instantly share code, notes, and snippets.

@SmallJoker
Last active May 10, 2026 08:59
Show Gist options
  • Select an option

  • Save SmallJoker/4d411b958295d9e694cd9aa5984563db to your computer and use it in GitHub Desktop.

Select an option

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
#!/usr/bin/env 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
# Replace SSH URI with HTTPS
# SSH: ssh://git@gitservice.tld:foo/bar.git
url="${url#ssh://}"
url="${url#git@}"
url="https://${url/:/\/}" # replace ':' with '/'
fi
url="${url%.git}" # trim tailing ".git"
# GitHub: USER/REPO/pull/NUMBER.patch
# Forgejo: USER/REPO/pulls/NUMBER.patch
p_pull="pulls"
[[ "$url" == *github.com* ]] && p_pull="pull"
set -e
case "$2" in
"")
url="$url/$p_pull/$1.patch"
curl -Ls "$url" | git am -3 --ignore-space-change -
;;
"diff")
url="$url/$p_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