#!/bin/bash
#
# check whether current directory is inside a git repository
#

is_git_repo() {
  git rev-parse --show-toplevel > /dev/null 2>&1
  result=$?
  if test $result != 0; then
    >&2 echo 'Not a git repo!'
    exit $result
  fi
}

is_git_repo
# Based on https://gist.github.com/gnarf/5406589

if test "$1" = "clean"; then
  git for-each-ref refs/heads/pr/* --format='%(refname)' | while read ref; do
    git branch -D ${ref#refs/heads/}
  done
else
  test -z $1 && echo "pr number required." 1>&2 && exit 1
  git fetch -fu ${2:-origin} refs/pull/$1/head:pr/$1 && git checkout pr/$1
fi
