Mohan Cheema's Online Diary

Site Just to Keep Track of My Day to Day Work.

Shell Script for Auto Merging of Subversion code

| 0 comments

We have multiple project developments going on simulteanously and always had issues while merging the code to live. Issues were like code getting overwritten, full fucntionality loss. Hence I have written this script for auto merging of the code which has gone live to the development branch. This is done to eliminate the possibilities of above issues. I have used svnmerge.py utility which is available at http://www.orcaware.com/svn/wiki/Svnmerge.py#Downloads. I hope this script will be helpful for you

svnautomerge.sh

#!/bin/sh
############################################################
# ScriptName: svnautomerge.sh
# Date Created: 24/07/2008
# Date Implemented:
# Version: 1.1
# Author: Mohan Cheema [mohan at mohancheema dot net]
# Purpose:
# This Script is written to auto merge the repositories which are in development state.
# So that the changes that have already gone live doesn't get missed and the
# Development branch are always in sync with live code.
# Copyright (c) 2008 Mohan Cheema <http://www.mohancheema.net>
# This script is licensed under GNU GPL version 2.0 or above
# This script is part of Mohan Cheema shell script collection
# Visit http://www.mohancheema.net/ for more information.
############################################################
SVNEXE="/usr/bin/svn"
SVNREP="/path/to/checkout/repo"
SVNURL="http://subverion.ip.addr.ess/repos"
SVNUSER="svnadmin"
SVNPASS="password"
SVNM="/path/to/svnmerge.py"
LOGFILE="/var/log/svnsync.log"
LOGDATE=`date +"%d/%m/%Y %Z %H:%M:%S"`
PROGNAME=$(basename $0)

LOGFUN()
{
        echo "$LOGDATE - $1" >> $LOGFILE
}

initializeANSI()
{
  esc="E"
  blackf="${esc}[30m";   redf="${esc}[31m";    greenf="${esc}[32m"
  yellowf="${esc}[33m"   bluef="${esc}[34m";   purplef="${esc}[35m"
  cyanf="${esc}[36m";    whitef="${esc}[37m"

  blackb="${esc}[40m";   redb="${esc}[41m";    greenb="${esc}[42m"
  yellowb="${esc}[43m"   blueb="${esc}[44m";   purpleb="${esc}[45m"
  cyanb="${esc}[46m";    whiteb="${esc}[47m"

  boldon="${esc}[1m";    boldoff="${esc}[22m"
  italicson="${esc}[3m"; italicsoff="${esc}[23m"
  ulon="${esc}[4m";      uloff="${esc}[24m"
  invon="${esc}[7m";     invoff="${esc}[27m"

  reset="${esc}[0m"
}

initializeANSI

USAGE()
{
        echo -e "${redf}Usage: $PROGNAME -c [Options] -r [RepositoryName/<branch|trunk|tags>] [-u <Repository URL>]${reset}"
        echo -e "${yellowf}Example for Checkout : $PROGNAME -c co -r TestRepo/tags/buddyrelease/28072008/"
        echo -e "Example for Commit   : $PROGNAME -c ci -r TestRepo/tags/buddyrelease/28072008/"
        echo -e "Example for List     : $PROGNAME -c ls -r TestRepo/tags/buddyrelease/28072008/"
        echo -e "Example for Update   : $PROGNAME -c co -r TestRepo/tags/buddyrelease/28072008/${reset}"
        echo -e "${boldon}Options Available for -c:${reset}"
        echo -e "t${boldon}${italicson}ls - List Repository"
        echo -e "tco - Checkout Repository"
        echo -e "tci - Commit Repository"
        echo -e "tup - Update Repository"
        echo -e "tmg - Merge Repository${reset}"
        echo
}

if [ $# -le 1 ]
then
        USAGE
        LOGFUN "NO ARGUMENTS PASSED!!!"
        exit
fi

while getopts dc:r:u: OPTIONS
do
        case ${OPTIONS} in
           c) SVNARG=${OPTARG};;
           r) REPOARG=${OPTARG};;
           u) URLARG=${OPTARG};;
           *) USAGE
              exit 2;;
        esac
done

if [ "$REPOARG" == "" ] || [ "$SVNARG" == "" ]
then
        echo "-c and -r are Mandatory "
        echo -e "tExample for Checkout : $PROGNAME -c co -r TestRepo/tags/buddyrelease/28072008/"
        echo -e "tExample for Commit : $PROGNAME -c ci -r TestRepo/tags/buddyrelease/28072008/"
        echo -e "tExample for List : $PROGNAME -c ls -r TestRepo/tags/buddyrelease/28072008/"
        echo -e "tExample for Update : $PROGNAME -c co -r TestRepo/tags/buddyrelease/28072008/"
        exit 3
fi

if [ "$SVNARG" == "ls" ]
then
        $SVNEXE $SVNARG $SVNURL/$REPOARG/
        exit
fi

if [ "$SVNARG" == "co" ]
then
        cd $SVNREP
        if ! test -d $REPOARG
        then
                /bin/mkdir -p $REPOARG
        fi

        $SVNEXE $SVNARG $SVNURL/$REPOARG/ $SVNREP/$REPOARG --username $SVNUSER --password $SVNPASS
        cd $SVNREP/$REPOARG
        $SVNM init -f /tmp/initmessage.txt

        $SVNEXE ci -m "Need to Commit after initialising auto merging." $SVNREP/$REPOARG --username $SVNUSER --password $SVNPASS

        $SVNEXE up .

        exit
fi

if [ "$SVNARG" == "ci" ]
then
        $SVNEXE $SVNARG -m "" $SVNREP/$REPOARG --username $SVNUSER --password $SVNPASS

        exit
fi

if [ "$SVNARG" == "up" ]
then
        $SVNEXE $SVNARG $SVNREP/$REPOARG --username $SVNUSER --password $SVNPASS

        exit
fi

if [ "$SVNARG" == "mg" ]
then
        CONT=0
        CONT2=0
        if [ ! -d "$SVNREP/$REPOARG" ]
        then
                $SVNEXE co $SVNURL/$REPOARG/ $SVNREP/$REPOARG --username $SVNUSER --password $SVNPASS

                cd $SVNREP/$REPOARG
                $SVNM init -f /tmp/initmessage.txt

                $SVNEXE ci -m "Need to Commit after initialising auto merging." 
		    $SVNREP/$REPOARG --username $SVNUSER --password $SVNPASS

                $SVNEXE up .

        fi
        cd $SVNREP/$REPOARG
        echo "Updating Repo $SVNREP/$REPOARG...."
        $SVNEXE --username $SVNUSER --password $SVNPASS up .

        $SVNM --username $SVNUSER --password $SVNPASS merge -f /tmp/CommitMessage.txt

        CONT=`find . -name "*.merge-*.r*" |wc -l`
        CONT2=`find . -name "*.working"|wc -l`
        if [ $CONT -gt 0 ] && [ $CONT2 -gt 0 ]
        then
                $SVNEXE revert $SVNREP/$REPOARG

                LOGFUN "There is conflict in $SVNREP/$REPOARG. Please Resolve this manually with the help of developer(s)."
                /bin/rm -f /tmp/CommitMessage.txt
        else
                LOGFUN "No Conflicts found Merging $REPOARG"
                echo "Merging..."
                if [ -f /tmp/CommitMessage.txt ]
                then
                        echo "Commiting..."
                        $SVNEXE ci -F /tmp/CommitMessage.txt $SVNREP/$REPOARG --username $SVNUSER --password $SVNPASS

                        /bin/rm -f /tmp/CommitMessage.txt
                else
                        echo "Nothing to merge...."
                        LOGFUN "Nothing to merge for $REPOARG"
                fi
        fi
        exit
fi

Automating

Once my above script was ready only task remaining was to automate it. I have written this shell script which does that it is been added to crontab to run at specific intervals.

repomerge.sh

#!/bin/sh
############################################################
# ScriptName: repomerge.sh
# Date Created: 24/07/2008
# Date Implemented:
# Version: 1.1
# Author: Mohan Cheema [mohan at mohancheema dot net]
# Copyright (c) 2008 Mohan Cheema <http://www.mohancheema.net>
# This script is licensed under GNU GPL version 2.0 or above
# This script is part of Mohan Cheema shell script collection
# Visit http://www.mohancheema.net/ for more information.
############################################################
SYNCSCRIPT="/path/to/svnautomerge.sh"

$SYNCSCRIPT -c mg -r project1/branch/developmentBranch1
$SYNCSCRIPT -c mg -r project1/branch/developmentBranch2
$SYNCSCRIPT -c mg -r project2/branch/developmentBranch1
Share

Author: Mohan Cheema

I am a Commerce Graduate currently I am working as Senior Support Analyst (Linux Administrator) with medium sized MNC Company. If time permits I do freelance work like setting up the servers as per the requirement, do performance tuning and so on.

Leave a Reply

Required fields are marked *.

*

* Copy this password:

* Type or paste password here:

2,920 Spam Comments Blocked so far by Spam Free Wordpress


CommentLuv badge

Notify me of followup comments via e-mail. You can also subscribe without commenting.