bin/git-where-pr
#!/usr/bin/env bash
# Usage: git where-pr [--print] <ref>
#
# Opens the Pull Request on GitHub where the commit originated.
# https://github.com/mislav/dotfiles/blob/master/bin/git-where-pr
set -e
mode=open
case "$1" in
-h | --help )
sed -ne '/^#/!q;s/.\{1,2\}//;1d;p' < "$0"
exit 0
;;
--print )
mode=echo
shift 1
;;
esac
ref="$1"
if [ -z "$ref" ]; then
"$0" --help >&2
exit 1
fi
pull_id="$(git log --merges --ancestry-path --oneline "${ref}.." | awk '
/Merge pull request #/ { sub("#", "", $5); print $5 }
' | tail -1)"
if [ -n "$pull_id" ]; then
url="$(git remote -v | awk '
/^origin\t/ { print $2 }
' | head -1 | sed -E 's!(git@github\.com:|git://github\.com/)!https://github.com/!')"
url="${url%.git}"
$mode "${url}/pull/${pull_id}"
else
echo "Could not find pull request" >&2
exit 1
fi