Handy Shell Scripts – part I – svntag.sh

Everyone has ‘em. Well, maybe not EVERYONE, but a good many of us, programmers do; those handy little scripts and one-liners that make our jobs a little easier.

The first I’d like to share is one I use daily about a dozen times; svntag.sh

svntag.sh (download file)#!/bin/sh

trunk_or_branch_status () {
  echo "Status on SVN ($1)"
  TRUNKREV=` svn info "$1" | grep "Last Changed Rev" | egrep -o [0-9]+ `
  TAGSREV=` svn info "$2"/tags | grep "Last Changed Rev" | egrep -o [0-9]+ `
  [ $TRUNKREV -gt $TAGSREV ] && echo There are changes not yet tagged \! \( HEAD: $TRUNKREV, Tags: $TAGSREV \) && svn log -r $TAGSREV:$TRUNKREV
  [ $TRUNKREV -le $TAGSREV ] && echo No action needed\!

}

#echo $1
[ ! -d ".svn" ] && echo Use only inside the main project directory under SVN control && exit
S=` svn info | grep URL: | cut -d " " -f 2 `
#echo $S
#exit;
BSE=` echo $S | sed 's/\/trunk/ /' | sed 's/\/branches/ /' | sed 's/\/tags/ /' | cut -d " " -f 1 `
B=$BSE/tags
T=$BSE/tags/$1

[ "$1" == "" ] && echo Usage: $0 TAG Commit comment && echo latest tags: && svn ls "$B" | tail -n 5
[ "$1" == "" ] && trunk_or_branch_status "$S" "$BSE"
[ "$1" == "" ] && exit

IFS=$'\n';
tags=` svn ls $B `
for i in $tags; do
  lasttag=$i
done
lasttag=` echo $lasttag | sed 's_/__g' `
for i in $tags; do
  [ "$1/" == "$i" ] && echo tag $1 already exists, please create a tag higher than $lasttag && exit;
done
#echo $T
M=$@
echo Executing: svn cp "$S" "$T" -m "Release $M"
svn cp "$S" "$T" -m "Release $M"

After committing the latest changes to my software, I make a tag of it for installing/deployment. Instead of typing the svn copy command again and again and running the risk of creating a duplicate tag, I use

Shell commandsvntag.sh 1.2.3 New release for the public

Now a new tag is created after having checked if it does’t already exist.

Just remember; This script works for me, but it might not for you. Don’t hold me responsible if anything goes wrong.