Skip to content

Instantly share code, notes, and snippets.

@icio
Last active October 13, 2015 23:27
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 icio/4272493 to your computer and use it in GitHub Desktop.
Save icio/4272493 to your computer and use it in GitHub Desktop.
Git review

About

A utility for interfacing with reviewable git branches, as per GitHub issues. This is a wrapper around ghi.

Usage

git review [-h | -l [LABEL] | [-w] ISSUE | -s | -u]
   git review            List reviewable issues
   git review -h         Print usage instructions
   git review -l         Print the review label filtered by
   git review -l LABEL   Set the review label filtered by
   git review ISSUE      Checkout the given issue
   git review -w [ISSUE] Open [a specific issue of] all reviewable issues
   git review -s         Open the source gist
   git review -u         Update the local copy of git-review from source

Installation

$ https://gist.github.com/icio/4272493/raw/ghi-review > git-review && \
  chmod 755 git-review && \
  mv git-review /usr/local/bin

Updating

Git review can self-update from this Gist using

git review -u
#!/usr/bin/env bash
# Ensure that the user has `ghi` installed
if [ -z "$(which ghi)" ]; then
echo "Missing dependency: ghi"
GHI=https://github.com/stephencelis/ghi#install
echo $GHI
open $GHI
exit 1
fi
# Ensure that the user has `jsawk` installed
if [ -z "$(which jsawk)" ]; then
echo "Missing dependency: jsawk"
JSAWK=https://github.com/micha/jsawk#install
echo $JSAWK
open $JSAWK
exit 1
fi
usage() { cat <<-USAGE
git review [-h | -l [LABEL] | [-w] ISSUE | -s | -u]
git review List reviewable issues
git review -h Print usage instructions
git review -l Print the review label filtered by
git review -l LABEL Set the review label filtered by
git review ISSUE Checkout the given issue
git review -w [ISSUE] Open [a specific issue of] all reviewable issues
git review -s Open the source gist
git review -u Update the local copy of git-review from source
USAGE
}
# Read the issue label from git configuration
CONFIG_VAR=review.label
LABEL=$(git config --get $CONFIG_VAR)
LABEL=${LABEL:-review}
if [ $# == 0 ]; then
# List reviewable issues
ghi list -L $LABEL
elif [ $1 == "-h" ]; then
# Print the usage statement
usage
exit 1
elif [ $1 == "-s" ]; then
# Open the source
open https://gist.github.com/4272493
elif [ $1 == "-u" ]; then
# Update the script
curl -s https://gist.github.com/icio/4272493/raw/ghi-review > $0
echo Updated $0
elif [ $1 == "-l" ]; then
if [ -z $2 ]; then
# Show label being used
echo $LABEL
else
# Set the label being used $2
git config $CONFIG_VAR $2
fi
elif [ $1 == "-w" ]; then
if [ -z $2 ]; then
# Open all issues
$0 | grep -oP '^\s*\d+' | xargs -n1 ghi show -w
else
# Open a specific issue
ghi show -w $2
fi
elif [[ "$1" =~ ^[0-9]+$ ]]; then
# Ensure that we've got nothing to commit
if [ -n "$(git status --porcelain)" ]; then
echo Cowardly refusing to checkout another branch with uncommited changes.
exit 1
fi
# Determine the request branch
BRANCH="$(curl -s https://api.github.com/repos/duedil-ltd/duedil/pulls/$1\?access_token=$(git config --global ghi.token) | jsawk 'return this.head.ref' &2>/dev/null)"
if [ -z "$BRANCH" ]; then
echo Could not determine branch. Is \#$1 a pull request? $BRANCH
exit 1
fi
git checkout $BRANCH
else
# Bah?
echo "Que?"
usage
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment