Last active
January 14, 2024 15:08
-
-
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
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
#!/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: [email protected]: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