| 1 |
#!/usr/bin/env bash |
| 2 |
|
| 3 |
usage() { |
| 4 |
echo "git pr <pull-request-number>" |
| 5 |
} |
| 6 |
|
| 7 |
egrep-q() { |
| 8 |
egrep "$@" >/dev/null 2>/dev/null |
| 9 |
} |
| 10 |
|
| 11 |
if [[ "$1" = "-h" || "$1" = "--help" ]] ; then |
| 12 |
usage |
| 13 |
exit 0 |
| 14 |
fi |
| 15 |
|
| 16 |
have_python3=false |
| 17 |
python_exe=python |
| 18 |
if type python3 >/dev/null 2>&1; then |
| 19 |
have_python3=true |
| 20 |
python_exe=python3 |
| 21 |
elif type python >/dev/null 2>&1; then |
| 22 |
if echo $(python --version 2>&1) | egrep-q 'Python 3'; then |
| 23 |
have_python3=true |
| 24 |
python_exe=python |
| 25 |
fi |
| 26 |
fi |
| 27 |
if [[ $# -lt 1 ]]; then |
| 28 |
if $have_python3 && type curl >/dev/null 2>&1; then |
| 29 |
config="${BASH_SOURCE%/*}/config" && |
| 30 |
host=$(git config -f "$config" --get github.host || echo "github.com") |
| 31 |
organization_name=$(git config -f "$config" --get github.organization-name) |
| 32 |
repo_name=$(git config -f "$config" --get github.repo-name) |
| 33 |
url="https://api.$host/repos/$organization_name/$repo_name/pulls?sort=updated;direction=desc" |
| 34 |
pr_prompt=$(curl -s "https://api.$host/repos/$organization_name/$repo_name/pulls?sort=updated;direction=desc" \ |
| 35 |
| $python_exe -c "import sys, json; res = json.load(sys.stdin); [print(str(pr['number']) + ': ' + str(pr['title'])) for pr in res] if 'pr' in res else sys.exit(1);") |
| 36 |
if [ $? -ne 0 ]; then |
| 37 |
usage |
| 38 |
exit 1 |
| 39 |
fi |
| 40 |
echo "$pr_prompt" |
| 41 |
echo "" |
| 42 |
pr_number=$(echo "$pr_prompt" | sed -n -e "s/\([[:alnum:]]\+\).*/\1/p" | tail -n1) |
| 43 |
read -ep "Pull request number? [$pr_number]: " ans && |
| 44 |
if [ -n "$ans" ]; then |
| 45 |
pr_number="$ans" |
| 46 |
fi |
| 47 |
else |
| 48 |
usage |
| 49 |
exit 1 |
| 50 |
fi |
| 51 |
else |
| 52 |
pr_number=$1 |
| 53 |
fi |
| 54 |
|
| 55 |
git fetch -fu ${2:-upstream} refs/pull/$pr_number/head:pr/$pr_number && \ |
| 56 |
git checkout pr/$pr_number; |