#!/bin/sh
#/ Usage: gem-apply <gem> <patchfile
#/ Apply a patch to a locally installed gem.
#/
#/ Example:
#/   % curl https://github.com/jekyll/jekyll/pull/3452.patch | gem-apply jekyll
#/   patching file lib/jekyll/commands/serve.rb
#/   Hunk #1 succeeded at 39 (offset -1 lines).
#/   Hunk #2 succeeded at 125 (offset -6 lines).
#/   patching file lib/jekyll/commands/serve.rb
#/   Hunk #1 succeeded at 39 (offset -1 lines).
#/   Hunk #2 succeeded at 94 (offset -5 lines).
#/   Hunk #3 succeeded at 140 (offset -6 lines).
set -e

# Find location of gem and apply patch.
gemroot=$(bundle show "$1")
patch -d "$gemroot" -b -p1