Skip to content

Instantly share code, notes, and snippets.

@bigsmoke
Created January 13, 2010 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bigsmoke/276574 to your computer and use it in GitHub Desktop.
Save bigsmoke/276574 to your computer and use it in GitHub Desktop.
#!/bin/bash
usage() {
cat <<"EOF"
$0 [--dry-run] <svn_dir> <replacement_dir>
This script replaces the contents of <svn_dir> with the contents of <replacement_dir>,
where <replacement_dir> is not an svn directory.
Copyleft 2010, Rowan Rodrik van der Molen <rowan@bigsmoke.us>
EOF
}
fatal_error() {
message=$1
echo -e "\e[1;31m$message\e[0m"
exit 1
}
usage_error() {
error="Wrong usage."
if [ -n "$1" ]; then
error=$1
fi
echo -e "\e[1;31m$error\e[0m"
exit 1
}
run_command() {
echo -e "\e[1;34m$1\e[0m"
[ $dry_run == 1 ] || eval $1
}
dry_run=0
if [ $1 == '--dry-run' ]; then
dry_run=1
shift
fi
[ $# == 2 ] || usage_error "Wrong number of arguments."
svn_dir=`echo "$1"|sed -e 's#/$##'`
replacement_dir=`echo "$2"|sed -e 's#/$##'`
begin_path=$PWD
#if [ "${svn_dir:0:1}" != "/" ]; then svn_dir="$PWD/$svn_dir"; fi
#if [ "${replacement_dir:0:1}" != "/" ]; then replacement_dir="$PWD/$replacement_dir"; fi
[ -d "$svn_dir" ] || usage_error "$svn_dir is not a directory."
[ -d "$replacement_dir" ] || usage_error "$replacement_dir is not a directory."
# Create all subdirectories in $svn_dir that do not yet exist
cd $replacement_dir
find . -mindepth 1 -type d -print | sed -e 's#^./##' | while read d; do
cd $begin_path/$svn_dir
# Doesn't the destination directory already exist?
if [ ! -d "$d" ]; then
run_command "svn mkdir '$d'"
fi
done
# Copy all files from $replacement_dir to $svn_dir
cd $begin_path/$replacement_dir
find . -type f -print | sed -e 's#^./##' | while read f; do
cd $begin_path
if [ -f "$begin_path/$svn_dir/$f" ]; then
run_command "cp $replacement_dir/$f $svn_dir/$f" # FIXME: Quoting problem
else
run_command "cp $replacement_dir/$f $svn_dir/$f" # FIXME: Quoting problem
cd $begin_path/$svn_dir
run_command "svn add '$f'"
fi
done
# Remove all files that do no longer exist in $replacement dir
cd $begin_path/$svn_dir
find . -type f -print | grep -v '.svn' | while read f; do
if [ ! -f "$begin_path/$replacement_dir/$f" ]; then
run_command "svn rm '$f'"
fi
done
# Remove all subdirs that do no longer exist in $replacement dir
cd $begin_path/$svn_dir
find . -mindepth 1 -type d -print | grep -v '.svn' | while read d; do
if [ ! -d "$begin_path/$replacement_dir/$d" ]; then
run_command "svn rm '$d'"
fi
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment