about summary refs log tree commit diff
path: root/git-merge-pr
blob: 4e59fe85b7a1c763c7e70451cf4f08653a079760 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/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 '.[] | "\(.number) <\(.user.login)> \(.title)"'
	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='sh -c '\''git -c trailer.closes.ifExists=replace interpret-trailers \
		--trailer "Closes: #'"$PR"' [via git-merge-pr]" "$1" > "$1-" &&
	mv "$1-" "$1"'\'' -' \
git commit --quiet --amend