Skip to content

Instantly share code, notes, and snippets.

@trustin
Created November 2, 2012 09:03
Show Gist options
  • Save trustin/3999616 to your computer and use it in GitHub Desktop.
Save trustin/3999616 to your computer and use it in GitHub Desktop.
#!/bin/bash
if [[ $# -lt 0 ]] || [[ $# -gt 2 ]]; then
echo "Usage: $0 <pull request id> [<directory>]"
fi
PULL_REQ_ID="$1"
if [[ "x$2" != 'x' ]]; then
cd "$2" || exit 1
fi
# Git information
GIT_STATUS="$(git status 2> /dev/null)"
if [[ "$?" != "0" ]]; then
echo "Not a git repository: `pwd`"
exit 2
fi
# Origin
GIT_REMOTE="$(git remote --verbose)"
if [[ ${GIT_REMOTE} =~ (origin[^a-z]*([^ ]*) *\(push\)) ]]; then
GIT_ORIGIN="${BASH_REMATCH[2]}"
if [[ ${GIT_ORIGIN} =~ (.*[/@]github\.com[/:]([-_a-zA-Z0-9/\.]*)\.git) ]]; then
GITHUB_ORIGIN="${BASH_REMATCH[2]}"
else
echo "Not a github repository: $GIT_ORIGIN"
exit 4
fi
else
echo "Not a github repository: `pwd`"
exit 3
fi
TMPFILE=`mktemp -q -t pullreq` || echo "Failed to create a temporary file." || exit 5
echo "Fetching the pull request $PULL_REQ_ID from $GITHUB_ORIGIN .. "
PULL_REQ_URL="https://github.com/$GITHUB_ORIGIN/pull/$1.patch"
curl -sS "$PULL_REQ_URL" > "$TMPFILE"
head -1 "$TMPFILE" | grep -q -e '^From'
if [[ $? -ne 0 ]]; then
echo "Not a pull request: $PULL_REQ_URL"
rm -f "$TMPFILE"
exit 6
fi
echo "Running: git am $TMPFILE"
git am "$TMPFILE"
rm -f "$TMPFILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment