Skip to content

Instantly share code, notes, and snippets.

@yanatan16
Last active January 11, 2020 22:53
Show Gist options
  • Save yanatan16/6071081 to your computer and use it in GitHub Desktop.
Save yanatan16/6071081 to your computer and use it in GitHub Desktop.
pullreq - A bash function to make a pull request.
# Push your current branch to origin and open a pull request to upstream:$1 or upstream:master
pullreq()
{
local browser=google-chrome # replace with your browser command
local regex='(https?://|git://|git@)([a-zA-Z0-9\.]+)[:/]([a-zA-Z0-9]+)/(.+)?'
local origin=$(git remote -v | grep origin | grep push | cut -f2 | cut -d' ' -f1)
local upstream=$(git remote -v | grep upstream | grep fetch | cut -f2 | cut -d' ' -f1)
local params=$(python -c "import sys;import re;print ':'.join(re.match('${regex}',sys.argv[1]).groups()[1:])" $origin)
local site=$(echo $params | cut -d':' -f1)
local user=$(echo $params | cut -d':' -f2)
local repo=$(echo $params | cut -d':' -f3 | sed 's/\.git//')
local branch=$(git branch | grep \* | cut -d' ' -f2)
if [ ${upstream} = "" ]; then
local uuser=user
else
local uparams=$(python -c "import sys;import re;print ':'.join(re.match('${regex}',sys.argv[1]).groups()[1:])" $upstream)
local uuser=$(echo $uparams | cut -d':' -f2)
fi
if [ ${#@} -eq 1 ]; then
local tobranch=${1};
else
local tobranch="master";
fi
git push origin ${branch}
${browser} http://${site}/${user}/${repo}/pull/new/${uuser}:${tobranch}...${branch}
}
@yanatan16
Copy link
Author

Usage example:

git commit -a -m "A new update"
pullreq
# or
pullreq 1.0.1 # a different branch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment