Skip to content

Instantly share code, notes, and snippets.

@achilleas-k
Last active November 21, 2022 12:20
Show Gist options
  • Save achilleas-k/c14d25533852ae2331105b72b71f22f3 to your computer and use it in GitHub Desktop.
Save achilleas-k/c14d25533852ae2331105b72b71f22f3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -euo pipefail
checknargs() {
if (( $1 != $2 )); then
usage
exit 1
fi
}
checkout() {
checknargs $# 1
git fetch -fu "${2:-origin}" "refs/pull/$1/head:pr/$1"
git checkout "pr/$1"
}
fetch() {
checknargs $# 1
git fetch -fu "${2:-origin}" "refs/pull/$1/head:pr/$1"
}
merge() {
checknargs $# 1
git fetch -fu "${2:-origin}" "refs/pull/$1/head:pr/$1"
git merge "pr/$1" --no-ff --edit -m "Merge pull request #$1"
}
clean() {
checknargs $# 0
for ref in $(git for-each-ref refs/heads/pr/* --format="%(refname)"); do
branch=${ref#refs/heads/}
git branch -D "$branch"
done
}
usage() {
echo "git pr <subcommand> [number] [remote]"
echo
echo "subcommands"
echo " checkout Fetch a PR by number and switch to the new branch"
echo " fetch Fetch a PR by number"
echo " merge Merge a PR by number"
echo " clean Delete all local PR branches"
}
if (( $# == 0 )); then
usage
exit 1
fi
cmd="$1"
shift
args=("${@}")
case "${cmd}" in
checkout)
checkout "${args[@]}"
;;
fetch)
fetch "${args[@]}"
;;
merge)
merge "${args[@]}"
;;
clean)
clean "${args[@]}"
;;
*)
echo "Unknown command ${cmd}"
usage
exit 1
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment