Skip to content

Instantly share code, notes, and snippets.

@dbaynard
Created February 11, 2019 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbaynard/4e2bf2b3a1fd8284c361c985882471cf to your computer and use it in GitHub Desktop.
Save dbaynard/4e2bf2b3a1fd8284c361c985882471cf to your computer and use it in GitHub Desktop.
Pull github PRs for review
#!/usr/bin/env zsh
#
# Review github PRs.
#
# This script makes reviewing github PRs simple. Supply the requester's branch
# in the form it is presented at the top of the PR on github (i.e. user:branch).
# This adds:
#
# - a new remote corresponding to the user, with only the PR branch
# - a new local branch (unless a branch with that name exists already, in
# which case it refuses to check out a branch).
#
# Supplying an optional argument that is the name you wish to give your branch
# will force that branch to be used, even if you already have a branch with that name.
#
# Use: git-review user:their-branch [my-branch]
#
# or add an alias to gitconfig to use as 'git review'
#
# > git config alias.review '!git-review'
user="${1%:*}"
their_branch="${1#*:}"
local_repo="$(git rev-parse --show-toplevel 2> /dev/null)"
repo="${local_repo:t}"
add_remote()
{
echo "Adding remote github branch $their_branch of repo $repo belonging to user $user."
git remote add -t "$their_branch" "$user" "git@github.com:$user/$repo" || git remote set-branches --add "$user" "$their_branch"
echo "Fetching remote branch $user/$their_branch (and any other already added $user branches)."
git fetch "$user"
}
create_their()
{
add_remote
echo "Creating local branch $their_branch"
git branch "$their_branch" "$user/$their_branch" && checkout "$their_branch"
}
create_my()
{
add_remote
echo "Creating local branch $my_branch"
git branch -f "$my_branch" "$user/$their_branch"
checkout "$my_branch"
}
checkout()
{
echo "Checking out local branch $1"
git checkout "$1"
}
case "$#" in
1)
create_their
;;
2)
my_branch="$2"
create_my
;;
*)
echo "Invalid syntax. Use 'git-review user:their_branch [my_branch]'"
exit 3
esac
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment