Skip to content

Instantly share code, notes, and snippets.

@hmps
Last active March 4, 2016 07:57
Show Gist options
  • Save hmps/4a36ec7a8c4dbfa40498 to your computer and use it in GitHub Desktop.
Save hmps/4a36ec7a8c4dbfa40498 to your computer and use it in GitHub Desktop.
Git flow finish with bash

Setup

  1. Put finish.sh (see below) in your repo root.
  2. Make finish.sh executable: $ chmod u+x finish.sh

If you want to have easy access to it without having to the script it in every repo you can save it somewhere safe and alias it. Something like this:

  1. Save to ~/finish.sh. Make it executable. (chmod +x finish.sh)
  2. Create an alias by adding alias finish='bash ~/finish.sh'. Make your alias whatever you want. Just remember it. :)

Feature

  1. Create a feature branch. It must be named feature/x where x is your feature name.
  2. Do work.
  3. Once the feature branch is finished and ready to merge into develop, run $ ./finish.sh.

The script will

  1. Pull develop and rebase it onto the feature branch.
  2. Merge the feature branch with develop, using --no-ff.
  3. Publish develop to origin.
  4. Delete the feature branch.

Release

  1. Create a release branch. It must be named release/x.y.z where x.y.z is a valid semver version number.
  2. Finish whatever work you need to do on it.
  3. Run $ ./finish.sh

The script will

  1. Pull develop and rebase it onto the release branch.
  2. Pull master and merge the release branch with master.
  3. Create a vx.y.z annotated tag from master.
  4. Publish master and the tag to origin.
  5. Merge the release branch with develop.
  6. Publish develop to origin.
  7. Delete the release branch.
#!/bin/bash
#
# Instructions at: https://gist.github.com/hmps/4a36ec7a8c4dbfa40498
# VARIABLES
DEVELOP="develop"
MASTER="master"
RED="\033[31m"
BLACK="\033[0m"
BLUE="\033[30m"
WHITE="\033[37m"
# HELPERS
function echoerror()
{
echo -en "$RED$1$BLACK"
echo;
}
function echomessage()
{
echo -en "$BLUE$1$BLACK"
echo;
}
function testbranch()
{
if [ $1 != 'release' ] && [ $1 != 'feature' ]
then
echoerror "ERROR: This branch does not seem to be a feature or release branch."
exit 1
fi
if [ $1 == 'release' ] && ! [ ${#2} -ge 5 ]
then
echoerror "$2 does not seem to be a valid semver version."
fi
}
function finishfeature()
{
echomessage "CHECKING OUT AND UPDATING DEVELOP"
git checkout $DEVELOP || exit 1
git pull origin $DEVELOP --no-edit || exit 1
echo ""
echomessage "MAKING SURE FEATURE BRANCH IS UP TO DATE"
git checkout $WORKINGBRANCH || exit 1
git rebase $DEVELOP || exit 1
echo ""
echomessage "CHECKING OUT DEVELOP"
git checkout $DEVELOP || exit 1
echo ""
echomessage "MERGING FEATURE BRANCH WITH DEVELOP"
git merge $WORKINGBRANCH -m"chore: Merge $WORKINGBRANCH into $DEVELOP" --no-ff || exit 1
echo ""
echomessage "PUBLISHING DEVELOP"
git push origin $DEVELOP || exit 1
echo ""
echomessage "DELETING FEATURE BRANCH"
git branch -d $WORKINGBRANCH || exit 1
# SUMMARY
echo ""
echo ""
echo "SUCCESS!"
echo "======================================="
echomessage "Here's what was done:"
echomessage "1. Merged $WHITE$WORKINGBRANCH$BLUE into $WHITE$DEVELOP$BLUE"
echomessage "2. Published $WHITE$DEVELOP$BLUE to remote"
echomessage "3. Deleted $WHITE$WORKINGBRANCH$BLUE"
}
function finishrelease()
{
echomessage "CHECKING OUT AND UPDATING DEVELOP"
git checkout $DEVELOP || exit 1
git pull origin $DEVELOP --no-edit || exit 1
echo ""
echomessage "MAKING SURE RELEASE BRANCH IS UP TO DATE"
git checkout $WORKINGBRANCH || exit 1
git rebase $DEVELOP || exit 1
echo ""
echomessage "CHECKING OUT AND UPDATING MASTER"
git checkout $MASTER || exit 1
git pull origin $MASTER --no-edit || exit 1
echo ""
echomessage "MERGING RELEASE WITH MASTER"
git merge $WORKINGBRANCH -m"chore: Merge $WORKINGBRANCH into $MASTER" --no-ff || exit 1
echo ""
echomessage "CREATING RELEASE TAG"
git tag -a v$BRANCHSUFFIX -m"version $BRANCHSUFFIX" || exit 1
echo ""
echomessage "PUBLISHING MASTER"
git push origin $MASTER || exit 1
echo ""
echomessage "PUBLISHING RELEASE"
git push origin v$BRANCHSUFFIX || exit 1
echo ""
echomessage "CHECKING OUT DEVELOP"
git checkout $DEVELOP || exit 1
echo ""
echomessage "MERGING RELEASE WITH DEVELOP"
git merge $WORKINGBRANCH -m"chore: Merge $WORKINGBRANCH into $DEVELOP" --no-ff || exit 1
echo ""
echomessage "PUBLISHING DEVELOP"
git push origin $DEVELOP || exit 1
echo ""
echomessage "DELETING RELEASE BRANCH"
git branch -d $WORKINGBRANCH || exit 1
echo ""
echo ""
echo "SUCCESS!"
echo "======================================="
echomessage "Here's what was done:"
echomessage "1. Merged $WHITE$WORKINGBRANCH$BLUE into $WHITE$MASTER$BLUE"
echomessage "2. Tagged $WHITE$MASTER$BLUE with $WHITEv$BRANCHSUFFIX$BLUE"
echomessage "3. Published $WHITE$MASTER$BLUE and $WHITEv$BRANCHSUFFIX$BLUE to remote"
echomessage "4. Merged $WHITE$WORKINGBRANCH$BLUE into $WHITE$DEVELOP$BLUE"
echomessage "5. Published $WHITE$DEVELOP$BLUE to remote"
echomessage "6. Deleted $WHITE$WORKINGBRANCH$BLUE"
}
function getRandomMemeId()
{
# 516587 = Sound of music
# 61532 = The most interesting man in the world
# 5496396 = leo toasting
# 563423 = that would be great
# 61582 = willy wonka
# 61544 = success kid
# 28251713 = oprah
# 61556 = grandma
# 15878567 = you the real mvp
# Get a random image id from a list of ids
declare -a expressions=('516587' '61532' '5496396' '563423' '61582' '61544' '28251713' '61556' '15878567')
declare -a first_text=(
"The hills are alive with the sound of"
"When I use $(basename $(pwd)), I always use"
"This is to you $(basename $(pwd))"
"It would be reaaaaally great if you updated to"
"Gosh, I just dunno. Maybe $(basename $(pwd))"
"OH YEAH!"
"YOU ALL GET A NEW VERSION OF"
"What is that dear? "
"We managed to publish $(basename $(pwd)) $BRANCHSUFFIX!"
)
declare -a second_text=(
""
"version $BRANCHSUFFIX"
"may v$BRANCHSUFFIX be the best one yet"
"$(basename $(pwd)) $BRANCHSUFFIX before you go home"
"is the golden ticket"
"$(basename $(pwd)) $BRANCHSUFFIX was published"
"$(basename $(pwd)) ($BRANCHSUFFIX)"
"$(basename $(pwd)) $BRANCHSUFFIX was published?"
"My team are the true MVPs today."
)
index=$( jot -r 1 0 $((${#expressions[@]} - 1)) )
RANDOMID=${expressions[index]}
TEXT0=${first_text[index]}
TEXT1=${second_text[index]}
}
function sendMessageToSlack()
{
echo "Sending message to Slack..."
SLACKCHANNEL=""
getRandomMemeId
SLACKTEAM=""
SLACKTOKEN=""
IMGFLIPUSER=""
IMGFLIPPASS=""
RESPONSE=$(curl --data "template_id=$RANDOMID&username=$IMGFLIPUSER&password=$IMGFLIPPASS&text0=$TEXT0&text1=$TEXT1" -s https://api.imgflip.com/caption_image) > /dev/null
IMAGEURL=$(node -pe 'JSON.parse(process.argv[1]).data.url' $RESPONSE)
MESSAGE="Greetings humans! Apsis component *$(basename $(pwd))@$BRANCHSUFFIX* was just published. :rocknroll: $IMAGEURL"
URL="https://$SLACKTEAM.slack.com/services/hooks/slackbot?token=$SLACKTOKEN&channel=%23$SLACKCHANNEL"
curl -Ss --data "$MESSAGE" $"$URL" > /dev/null
echo "Done!"
}
if branch=$(git symbolic-ref --short -q HEAD)
then
WORKINGBRANCH=$branch
BRANCHPREFIX=$(echo $WORKINGBRANCH | cut -d'/' -f 1)
BRANCHSUFFIX=$(echo $WORKINGBRANCH | cut -d'/' -f 2)
testbranch $BRANCHPREFIX $BRANCHSUFFIX
if [ $BRANCHPREFIX == 'release' ]
then
finishrelease
# sendMessageToSlack
elif [ $BRANCHPREFIX == 'feature' ]
then
finishfeature
fi
else
echoerror "Couldn't find a branch"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment