aboutsummaryrefslogblamecommitdiff
path: root/git-merge-pr
blob: 6ec620385a6938ffd3bdcef10ce75fcb50257436 (plain) (tree)
1
2
3
4
5
6
7
8
9
         
                                                                                                       

      
     



                       
                    
    






                                      
                                   
                                      








                                                          
                     
                                             
                                                                 
                                    
                                                                                
                                                          




               
                 
                                 
                                                                
                                                            
                                                       





                                                                    

                                                                       
                          
#!/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 command -v wget >/dev/null; then
	API_CMD="wget -q -O- --header"
	PATCH_CMD="wget -nv -O"
elif command -v curl >/dev/null; then
	API_CMD="curl -s -H"
	PATCH_CMD="curl -Lo"
else
	echo "Neither wget or curl could be found in PATH"
	exit 1
fi

if [ -z "$PR" ]; then
	$API_CMD 'Accept: application/json' \
		"https://api.github.com/repos/${PROJECT}/pulls" |
		jq -r 'if length > 0
			then reverse[] | "\(.number) <\(.user.login)> \(.title)"
			else "No open pull requests." end'
	exit $?
else
	shift
fi

PATCH="$(mktemp)"
trap "rm -f $PATCH" INT TERM EXIT
$PATCH_CMD "$PATCH" https://github.com/$PROJECT/pull/"$PR".patch
WHITESPACE="$(git config --get merge-pr.whitespace || true)"
git am "--whitespace=${WHITESPACE:-warn}" "$@" "$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