Skip to content

Instantly share code, notes, and snippets.

@petitchevalroux
Created October 11, 2017 13:45
Show Gist options
  • Save petitchevalroux/4851bd35b1e7dc799db574f1bc955663 to your computer and use it in GitHub Desktop.
Save petitchevalroux/4851bd35b1e7dc799db574f1bc955663 to your computer and use it in GitHub Desktop.
create pull request on master
#!/bin/bash
function errorMessage {
printf '%s\n' "Error: $1" >&2
}
function message {
printf '%s\n' "$1"
}
BASE='master'
#Check if hub is installed
if ! hub version > /dev/null ; then
errorMessage 'hub not found'
message "Installing hub using homebrew"
# Try to install hub using brew
if ! brew install hub; then
errorMessage 'homebrew not found, unable to install hub'
exit 1
fi
fi
#Pushing HEAD to origin
message "Pushing to origin:"
if ! git push origin HEAD ; then
exit 1
fi
FIRST_COMMIT=$(git rev-list --max-count=1 upstream/$BASE);
#Generate temporary file for pr message
PR_FILES='/tmp/PR_FILES.md'
#Delete file content
> $PR_FILES
#If we have only one commit, write it as PR title
COMMIT_MSG=$(git --no-pager log --format=%B $FIRST_COMMIT..HEAD | egrep "^[^\s]+")
if [ $(echo $COMMIT_MSG | wc -l | tr -d '[[:space:]]') -eq 1 ]; then
echo $COMMIT_MSG >> $PR_FILES
fi
echo 'ID:'$(git rev-parse --abbrev-ref HEAD | egrep -o "\d+") >> $PR_FILES
echo '# Write a message for this pull request. The first block of text is the title and the rest is the description.' >> $PR_FILES
echo "# Changes: " >> $PR_FILES
git --no-pager log $FIRST_COMMIT..HEAD | sed -e "s~^~# ~g" >> $PR_FILES
#Launch editor in order to write PR title's
EDITOR=$(git config --get core.editor || echo "vi")
$EDITOR "$PR_FILES"
#Remove comments on pr message
sed -i -e "/^#.*$/d" "$PR_FILES"
#Check if title and ticket Id are in message (at least 2 lines)
if [ $(cat $PR_FILES | wc -l | tr -d '[[:space:]]') -lt 2 ]; then
errorMessage 'missing pull request title'
exit 1
fi
#Create pull request on base
message "Creating pull request: "
hub pull-request -F $PR_FILES -b $BASE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment