Automatic commit of a Subversion repository

This is a short script which allows an automatic update of a subversion repository on Linux :

Create a log-directory into your Subversion repository

$mkdir logs

and save the following script into the repository as svn_cron.sh

#!/bin/bash
#
# Variable REPOS is the path to the repository on the server

REPOS=.
echo “”
date +”%B %d %Y”

test $# = 0 || ( echo “Please enter the path to the local copy”; exit 1 )

cd $1 2>&1 >/dev/null || ( echo “$1 does not exist”; exit 2 )

/usr/local/bin/svn info 2>&1 >/dev/null|| ( echo “$1 is not a working copy”; exit 3 )

if [ `/usr/local/bin/svn update $1 | sed -n "/^C/p" | wc -l` -ne 0 ]
then

# Collects all files and directorys which are to add on the repository
/usr/local/bin/svn status | sed -n “/^\??*/p” | sed “s/^\? *//g” > $REPOS/logs/toadd

# Collects all files and directorys which are to delete on the repository
/usr/local/bin/svn status | sed -n “/^\!?*/p” | sed “s/^\! *//g” > $REPOS/logs/todelete*//g” > $REPOS/logs/todelete

# Add and delete Subversion actions
cat $REPOS/logs/todelete | while read line; do /usr/local/bin/svn delete “${line}”; done
cat $REPOS/logs/toadd | while read line; do /usr/local/bin/svn add “${line}”; done

# SVN commit in order to save the changes with a log message
if !(/usr/local/bin/svn commit -m “Daily commit for changes done directly on the server”)
then echo “A problem has occured during the commit”
exit 3
fi

else
echo “Problem with the daily update, there may be a conflict within the following files  \! ”

# Displays the files which are currently in conflict
svn status $1
exit 4
fi
exit 0

Then edit the crontab

$crontab -e

or if you want the uploads to be done by another user

$crontab -e -u username

Of course, the user must have write permission to write on the logs directory.

Put the following lines at the end of the file

##Automatic update of the Subversion repository every day at 0:00
0  0   * * *  /path_to_your_repository/svn_cron.sh /path_to_your_local_copy >> /path_to_your_repository/logs/script_cron

And that’s it !

Comments (2)

hobbyDecember 15th, 2008 at 9:40 pm

There’s an error on this line. You are redirecting the output to the todelete file twice.

/usr/local/bin/svn status | sed -n “/^\!?*/p” | sed “s/^\! *//g” > $REPOS/logs/todelete*//g” > $REPOS/logs/todelete

Also, shouldn’t the first if check be -eq 0? You’re checking for any conflicts and if none exist (eq 0), then proceed on.

rajOctober 25th, 2011 at 7:33 am

Hi

Please clear my question when is my repository under /var/www/svn/repo .

when i run sh svn_cron.sh it shows .is not working copy

please help

Leave a comment

Your comment