09 March 2011

Copy SVN working copy changes to a folder

To copy changed files and folders to a temporary folder, I use the following bash script:
#!/bin/bash - 
# FILE: cpsvnst.sh
# USAGE: ./cpsvnst.sh source_dir destination_dir
# DESCRIPTION: Copy changed files and folders from SVN working copy to a temp. dir.
# AUTHOR: Ruslan Osmanov
# CREATED: 11/25/2010 01:37:18 PM UZT

set -o nounset # Treat unset variables as an error

# Displays the message, usage info and exits with error code 1
function my_usage()
{
msg=$1
[[ $msg ]] && echo $msg
echo "Usage:
./$0 source_dir destination_dir
source_dir Source directory
destination_dir Destination directory"
exit 1
}

# Returns dir name with trailing slash
function my_get_dirname()
{
dir=$1
if [[ ${dir:${#dir}-1:1} != '/' ]]; then
dir=$dir"/"
fi
echo $dir
}

src_dir=`my_get_dirname $1`
dst_dir=`my_get_dirname $2`
verbose=1

# Validate args
if [[ ! -d $src_dir ]]; then
usage "'$src_dir' is not a directory"
elif [[ ! -d $dst_dir ]]; then
usage "'$dst_dir' is not a directory"
fi

# Remember current dir
dir=`pwd`
cd $src_dir

# Loop through files and folders
svn st | awk '{ print $2 }' | while read F
do
if [[ $F != 'framework' ]]; then
# Create directory, if not exists
d=`dirname "$dst_dir$F"`
if [[ ! -d $d ]]; then
mkdir -p "$d"
fi

# Copy file or directory
if [[ -f $F ]]; then
[[ $verbose = 1 ]] && echo "FILE $F"
cp -f "$F" "$dst_dir$F"
elif [[ -d $F ]]; then
[[ $verbose = 1 ]] && echo "DIR $F"
[[ ! -d "$dst_dir$F" ]] && mkdir -p "$dst_dir$F"
cp -rf $F $dst_dir$F/../
fi
fi
done

# Go to the initial dir
cd $dir
I'd be glad, if it helped someone.