aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDuncaen <mail@duncano.de>2016-09-12 20:24:29 +0200
committerDuncaen <mail@duncano.de>2016-09-12 20:24:29 +0200
commit5f9e1f1e801481e77a7ee547d97eb955e634bdd8 (patch)
treec32b9aee34427131dc2d30670227f0782dc09f38
parent81f15e53104b799069dca23be42e4de909f46159 (diff)
downloaddotfiles-5f9e1f1e801481e77a7ee547d97eb955e634bdd8.tar.gz
bin/git-merge-pr: add
-rw-r--r--bin/git-merge-pr42
1 files changed, 42 insertions, 0 deletions
diff --git a/bin/git-merge-pr b/bin/git-merge-pr
new file mode 100644
index 0000000..e6f0e5d
--- /dev/null
+++ b/bin/git-merge-pr
@@ -0,0 +1,42 @@
+#!/bin/sh
+# git merge-pr [PRNUM][@REMOTE] [GIT-AM FLAGS...] - list or apply GitHub pull request from command-line
+set -e
+
+PR=$1
+REMOTE=
+case "$PR" in
+*@*)
+ REMOTE=${PR#*@}
+ PR=${PR%%@*}
+esac
+
+URL=$(git ls-remote --get-url $REMOTE)
+
+PROJECT=${URL%.git}
+PROJECT=${PROJECT##*:}
+PROJECT=${PROJECT#//github.com/}
+
+if [ -z "$PR" ]; then
+ wget -q -O- --header 'Accept: application/json' \
+ "https://api.github.com/repos/${PROJECT}/pulls?direction=asc" |
+ jq -r 'if length > 0
+ then .[] | "\(.number) <\(.user.login)> \(.title)"
+ else "No open pull requests." end'
+ exit $?
+else
+ shift
+fi
+
+PATCH="$(mktemp)"
+trap "rm -f $PATCH" INT TERM EXIT
+wget -nv -O "$PATCH" https://github.com/$PROJECT/pull/"$PR".patch
+git am "$@" "$PATCH"
+
+if [ "$(git config --bool --get merge-pr.autoclose)" = false ]; then
+ exit 0
+fi
+
+# Rewrite last commit message to close GitHub issue.
+GIT_EDITOR="git -c trailer.closes.ifExists=replace interpret-trailers \
+ --trailer 'Closes: #$PR [via git-merge-pr]' --in-place" \
+git commit --quiet --amend