Check conflicts between multiple branches

12 Nov 2012

I've encountered a weird situation where I need to deploy 5 branches at the same time. I have to check for conflicts between branches, otherwise the conflict pair will be rejected from the deployment.

So… I built a bash script that does exactly that. It utilizes `git merge, ``git tag`, andgit reset --hard``. You can figure it out…

Let's say you want to check conflicts among `b1, `b2, and ``b3```. You can do this:

git_check_conflict b1 b2 b3

Then you will see the result:

--- b1 <---> b2 = OK b1 <---> b3 = CONFLICTED --- b2 <---> b1 = OK b2 <---> b3 = OK --- b3 <---> b1 = CONFLICTED b3 <---> b2 = OK ---

That's all…

The bash script is below:

#!/bin/bash BRANCHES=() PREFIX="tanin_git_test_" CURRENT_BRANCH=`git rev-parse --abbrev-ref HEAD` # Create new temp branches while test $# -gt 0; do NEW_BRANCH_NAME="$PREFIX$1" IGNORED=`git branch -D $NEW_BRANCH_NAME 2>&1` IGNORED=`git checkout -b $NEW_BRANCH_NAME $1 2>&1` LAST_RESULT=$? if [ $LAST_RESULT -ne 0 ]; then echo "Branch $1 doesn't exist" else BRANCHES=(${BRANCHES[@]} $NEW_BRANCH_NAME) fi shift done echo "---" for MAIN in ${BRANCHES[@]}; do for OTHER in ${BRANCHES[@]}; do if [ $MAIN == $OTHER ]; then continue fi ORIG_MAIN=${MAIN:${#PREFIX}:${#MAIN} - ${#PREFIX}} ORIG_OTHER=${OTHER:${#PREFIX}:${#OTHER} - ${#PREFIX}} IGNORED=`git checkout $MAIN 2>&1` TEMP_TAG="${PREFIX}temp" IGNORED=`git tag -f -a $TEMP_TAG -m 'temp tag'` OUTPUT=`git merge $OTHER | grep CONFLICT` if [ $? -ne 0 ]; then echo "$ORIG_MAIN <---> $ORIG_OTHER = OK" else echo "$ORIG_MAIN <---> $ORIG_OTHER = CONFLICTED" fi IGNORED=`git reset --hard $TEMP_TAG` IGNORED=`git tag -d $TEMP_TAG` done echo "---" done IGNORED=`git checkout $CURRENT_BRANCH 2>&1` # Remove temp branches for MAIN in ${BRANCHES[@]}; do IGNORED=`git branch -D $MAIN 2>&1` done

Give it a kudos