mirror of
https://github.com/tj/git-extras.git
synced 2026-09-10 07:26:17 -04:00
43 lines
1.3 KiB
Bash
Executable file
43 lines
1.3 KiB
Bash
Executable file
#!/bin/sh
|
|
|
|
# check if arg 1 is a number or not
|
|
echo $1 | egrep "^[[:digit:]]+$" > /dev/null
|
|
is_not_number=$?
|
|
repo=${PWD##*/}
|
|
user=`git config github.user`
|
|
|
|
test -z "$repo" && echo "failed to detect repo" 1>&2 && exit 2
|
|
test -z "$user" && echo "failed to fetch github.user" 1>&2 && exit 3
|
|
|
|
if [ ! $is_not_number ] ; then
|
|
# apply existing pull request
|
|
url="https://github.com/$user/$repo/pull/$1.patch"
|
|
echo "pulling $url"
|
|
curl -# $url \
|
|
| git am --signoff \
|
|
&& echo "completed pull request $request"
|
|
else
|
|
# push
|
|
branchname=`git branch --no-color | sed -n -e 's/^\* \(.*\)/\1/p'`
|
|
echo "git push origin $branchname"
|
|
git push origin $branchname
|
|
|
|
# create new pull request
|
|
url="https://api.github.com/repos/$user/$repo/pulls"
|
|
echo "creating pull request"
|
|
oauthToken=`git gh-oauth`
|
|
body="{\"title\": \"$branchname\", \"head\": \"$user:$branchname\", \"base\": \"master\"}"
|
|
curl_result=$(curl -H "Authorization: token $oauthToken" $url -d "$body" --silent \
|
|
--write-out "%{http_code}")
|
|
json=`echo ${curl_result} | sed 's/...$//'`
|
|
http_code=`echo ${curl_request} | perl -pe 's/(.*)(...)$/$2/'`
|
|
if [ "${http_code:0:1}" == "2" ] ; then
|
|
pr_num=`echo "$json" | jshon -e number`
|
|
echo "Created pull request #${pr_num}"
|
|
else
|
|
echo "$json" | jshon -e message
|
|
echo "$json"
|
|
fi
|
|
fi
|
|
|