If it won't be simple, it simply won't be. [Hire me, source code] by Miki Tebeka, CEO, 353Solutions

Friday, December 29, 2006

"svkpull" and "svkpush"

I've been using svk lately. I mirror our main Subversion development branch and then create my own local branch. When my changes are ready I use svk smerge to integrated my changed back to the main branch.

Since I do this a lot of time, I've create two scripts: svkpull and svkpush that updates my local branch from the main branch and push my changes to the main branch.

svkpull

#!/bin/bash

# Get changes from mirrord subversion repository

# Miki Tebeka

case $# in
0 ) up=1; project=`basename $PWD`;;
1 ) up=0; project=$1;;
* ) echo "usage: `basename $0` [PROJECT_NAME]"; exit 1;;
esac

echo "Project is $project"

svk info //$project > /dev/null
if [ $? -ne 0 ]; then
echo "error: bad project - $project"
exit 1
fi

svk sync //$project/mirror
if [ $? -ne 0 ]; then
exit $?
fi

svk smerge --incremental -l //$project/mirror //$project/local
if [ $? -ne 0 ]; then
exit $?
fi

if [ $up == 1 ]; then
svk up
extra=""
else
extra="Don't forget to run 'svk up'"
fi

echo
echo "Done. $extra"


svkpush

#!/bin/bash

# Push changes to mirrord subversion repository

# Miki Tebeka

USAGE="usage: `basename $0` [-m comment] [PROJECT]"
COMMENT=""

while getopts "m:h" opt
do
case $opt in
m ) COMMENT=$OPTARG;;
h ) echo $USAGE; exit;;
* ) echo "error: unrecognized option -$opt"; exit 1;;
esac
done

shift $(($OPTIND - 1))

case $# in
0 ) up=1; project=`basename $PWD`;;
1 ) up=0; project=$1;;
* ) echo $USAGE; exit 1;;
esac

echo "Project is $project"

svk info //$project > /dev/null
if [ $? -ne 0 ]; then
echo "error: bad project - $project"
exit 1
fi

svk sync //$project/mirror
if [ $? -ne 0 ]; then
exit $?
fi

if [ "$COMMENT" == "" ]; then
svk smerge //$project/local //$project/mirror
else
svk smerge -m "$COMMENT" //$project/local //$project/mirror
fi

if [ $? -ne 0 ]; then
exit $?
fi

echo
echo "Done."
Notes:

  • If project is X the mirror is in //X/mirror and local branch is in //X/local
  • I use svk smerge --incremental -l only when getting changes

Saturday, December 02, 2006

DRY

The DRY (do not repeat yourself) principle is a hard to achieve, but worth the effort.
(This rule is also known as SPOT - Single Point Of Truth).

One good solution I find when some piece of information is very configurable and need to be shard with many different programs is to write a little script that prints the required value.

For example, say we need to decide what is the root directory for something. It can be either the debug version of a standard one.

The script rootdir.py will look something like:
from os import environ
from os.path import join
from getpass import getuser

if "DEBUG" in environ:
# Every user has his/her own test root directory
ROOT_DIR = join("/tmp", "testing", getuser())
else:
ROOT_DIR = "/usr/local/cool"

if __name__ == "__main__":
print ROOT_DIR
If you're using Python, you can just write from root import ROOT_DIR, however if you're using bash you can write ROOTDIR=`./root.py` and then use $ROOTDIR

Blog Archive