Skip to content

Instantly share code, notes, and snippets.

@edhgoose
Created January 29, 2016 23:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edhgoose/1ed8a2bba52abd0549d2 to your computer and use it in GitHub Desktop.
Save edhgoose/1ed8a2bba52abd0549d2 to your computer and use it in GitHub Desktop.
Merginator: Merge master/release branches back into development
#!/bin/bash -ex
# -ex = e: fail if any non zero, x: print commands prior to running
# This script ensures that the development branch and all release branches are up to date with the branches that preceed it. master is first, then release-X in order by name, followed by development.
# How it works:
#
# 1. Strip the current branch to its raw form. (origin/master -> master, origin/release-2.5.3 -> release-2.5.3)
# 2. If the current branch is master, set the START_MERGING variable to true.
# 3. Get all the current release branches, ordered by name ascending (so 2.5.3 comes before 2.5.4 and 2.5.5)
# 4. Loop over each release branch.
# a. Keep going through the branches until you find the current branch. Once you've found it, set the START_MERGING to true
# b. If START_MERGING is set:
# - Checkout that release branch
# - Pull it
# - Merge in the current branch
# - Push it
# - Exit.
# 5. If you never exited (i.e. never found another branch) then merge the branch into development and push that.
echo_green() {
echo -e "\E[32;47m$1\E[30;47m" # Echo in green, then reset to black
}
merge_branch() {
echo_green "Checking out $1 and merging $2 into it"
git checkout $1
git pull
git merge $2
git push
}
GIT_BRANCH=`echo $1`
CURRENT_BRANCH=`echo $1 | cut -d'/' -f2-` # Strip off the origin/ from the branch.
START_MERGING=false
git remote update origin --prune # Update the remote branch list
# If we're on the master branch, we'll merge back through all the release branches.
if [[ $CURRENT_BRANCH == 'master' ]]
then
echo_green "On master branch, so merging this and all release branches backwards"
START_MERGING=true
fi
# Loop through each of the current release branches (ordered by name, because 2.5.4 is after 2.5.3 when ordered by name)
while read -r releaseBranch
do
echo_green "Checking Release Branch: $releaseBranch"
if [[ $releaseBranch = $CURRENT_BRANCH ]]
then
START_MERGING=true
continue # Don't merge this branch. That'd be silly.
fi
if [[ $START_MERGING = true ]]
then
merge_branch $releaseBranch $GIT_BRANCH
exit 0 # Exit. The reason for this is we've just pushed to a branch that is also monitored by Jenkins. That will trigger this script and handle the next branch in the pile.
fi
done < <(git for-each-ref --sort=refname refs/remotes/origin --format='%(refname)' | grep "release-" | cut -d'/' -f4-)
# Only merge into development if we haven't merged any others. I.e. this is the last one.
merge_branch development $GIT_BRANCH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment