Skip to content

Instantly share code, notes, and snippets.

@SmallJoker
Last active August 24, 2025 12:09
Show Gist options
  • 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
#!/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
# Before: [email protected]:foo/bar.git
# After: https://gitservice.tld/foo/bar
url="${url:4:-4}"
url="https://${url/:/\/}"
fi
url=${url%.git} # trim tailing ".git"
# GitHub: USER/REPO/pull/NUMBER.patch
# Forgejo: USER/REPO/pulls/NUMBER.patch
p_pull="pull"
[[ "$url" == *codeberg.org* ]] && p_pull="pulls"
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 $p_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