Skip to content

Instantly share code, notes, and snippets.

@rhowardiv
Created March 26, 2012 16:26
Show Gist options
  • Save rhowardiv/2206324 to your computer and use it in GitHub Desktop.
Save rhowardiv/2206324 to your computer and use it in GitHub Desktop.
examine pull requests nicely in vim
#!/bin/bash
# Examine a pull request!
# Usage: $0 <github username> <branch name> OR $0 <pull request #>
# TODO: move to env?
MY_GH_USER=rhowardiv
MY_GH_PASS=XXXXXXXX
GH_REPO=AYI
CHECK_ORIGIN=$(git remote -v | grep '^origin.\+(push)$')
if [ -z "$CHECK_ORIGIN" ]; then
echo "You need to be in a git repo with a pushable \"origin\" remote!"
exit 1
fi
GIT_STASH=$(git status --porcelain | grep -v '^??')
if [ -n "$GIT_STASH" ]; then
echo "Stashing current state..."
git stash
fi
ON_BRANCH=$(git branch | grep '^* ' | sed 's/^* //')
on_exit () {
echo "Switching back to $ON_BRANCH..."
git co "$ON_BRANCH"
if [ -n "$GIT_STASH" ]; then
echo "Restoring stash..."
git stash pop
fi
if [ -n "$TEMP_BRANCH" ]; then
git branch -D "$TEMP_BRANCH"
fi
}
trap on_exit EXIT
if [ -z $2 ]; then
echo -n "Fetching pull request info from github..."
GH_PR=$(curl -s "https://$MY_GH_USER:$MY_GH_PASS@api.github.com/repos/SnapInteractive/AYI/pulls/$1")
echo " Done."
LABEL=$(echo "$GH_PR" | grep -m 1 '^[^"]*"label"')
if [ -z "$LABEL" ]; then
echo "Pull request not found or could not get head.label field from pull request!"
echo "To see the pull request data, run:"
echo "https://$MY_GH_USER:[password]@api.github.com/repos/SnapInteractive/AYI/pulls/$1"
exit 1
fi
GH_USER=$(echo "$LABEL" | sed 's/^.*"label": "\([^"]\+\):.\+$/\1/')
GH_BRANCH=$(echo "$LABEL" | sed 's/^.*"label": "[^"]\+:\([^"]\+\).\+$/\1/')
if [ -z "$GH_USER" -o -z "$GH_BRANCH" -o -n "$(echo "$GH_USER:$GH_BRANCH" | grep '"')" ]; then
echo "Couldn't extract github user:branch from PR? ($GH_USER:$GH_BRANCH)"
exit 1
fi
else
GH_USER="$1"
GH_BRANCH="$2"
fi
TEMP_BRANCH="$GH_USER-$GH_BRANCH"
git co -b "$TEMP_BRANCH" master && \
git pull https://$MY_GH_USER:$MY_GH_PASS@github.com/$GH_USER/$GH_REPO.git "$GH_BRANCH"
if [ $? -ne 0 ]; then
echo "Failed fetching branch \"$GH_BRANCH\" from \"$GH_USER\"'s repo!"
exit 1
fi
# see this function in my dotfiles/vimrc file
vim '+call Gbdiff("master")'
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment