SubversionTips

Tagging in subversion

Subversion doesn't have proper tags, but it does _just_ fine with this little script. It will create a standard tag or branch for you, based on the .svn directory where you currently are. This requires you to be at the top level of the subversion tree (assuming your structure is standard). It will echo what it wants to do before doing it, and ask for confirmation. Subversion also automatically invokes EDITOR so you get one more chance to abort.

#!/bin/sh

usage() {
    echo "usage: $0 [ --branch | -b | -- ] <tag-name>"
    echo "      <tag-name> can contain word characters or +-="
    exit 1;
}

set_tag() {
    if [ -n "$TAG" ]; then
        echo "too many arguments"
        usage
    fi
    TAG=$1
}

CPDIR="tags"
TAG=""
while [ $# -gt 0 ]; do
    case $1 in
        --branch|-b) shift; CPDIR="branches";;
        --) shift; if [ $# -gt 0 ]; then set_tag $1; shift; fi;;
        *) set_tag $1; shift;;
    esac
done

if [ -z "$TAG" ]; then
    echo "no tag supplied";
    usage
fi

CHECK_TAG=`echo $TAG | sed 's&[^a-zA-Z0-9_+=-]&/&'`

if echo $CHECK_TAG | grep -q '/'; then
    echo "bad tag name '$TAG'";
    usage
fi


SVN_TRUNK=`grep url .svn/entries | sed 's/.*url="\([^"]*\)".*/\1/'`
SVN_BASE=`echo $SVN_TRUNK | sed 's&/[^/]*/*$&&'`
SVN_TAG="${SVN_BASE}/${CPDIR}/${TAG}"

echo "copying '$SVN_TRUNK' as '$SVN_TAG', ok (y/n)?"
read CHECK
if echo ${CHECK} | grep -q -v '^y'; then
    echo "Not tagging!"
    exit 1
fi

svn cp ${SVN_TRUNK} ${SVN_TAG}

Import

Nothing seems to do import very gracefully. So here's a hack version for subversion.

#!/bin/sh

usage() {
    echo "Usage: $0 <source> <dest>"
    exit 1
}

if [ $# -lt 2 ]; then
    usage;
fi


SOURCE=$1
DEST=$2

if [ ! -d $1 -o ! -d $2 ]; then
    usage;
fi

find $DEST -name .svn -prune -o -type f -print -exec rm {} \;
cp -Rv $SOURCE/* $DEST
if [ `svn status $DEST | wc -l` -lt 1 ]; then
    echo "Nothing changed"
    exit 0
fi
svn status $DEST | grep '!' | cut -c4- | xargs svn rm
svn status $DEST | grep '?' | cut -c4- | xargs svn add

SubversionTips (last edited 2007-03-25 12:43:16 by ClaraWiddison)