Skip to content

Instantly share code, notes, and snippets.

@gamma
Created November 15, 2021 10:45
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 gamma/88e328a7a043a6b7639675ef93aacd88 to your computer and use it in GitHub Desktop.
Save gamma/88e328a7a043a6b7639675ef93aacd88 to your computer and use it in GitHub Desktop.
SVN relocate for a list of directories
#!/bin/bash
# This script is used to relocate a Subversion repository from one
# filesystem location to another. It is intended to be run from the
# command line, but it can be run from any other program too.
# You can prepare the new repository location using:
#
# export NEW_SVN_LOCATION=file:///path/to/new/repository
#
# The script can be run using the following command - The list
# will be created using find for the .svn directories therein:
# bash ./relocate.sh /path/to/directory/list
#
# Alternatively run:
# env NEW_SVN_LOCATION=file:///path/to/new/repository bash ./relocate.sh /path/to/directory/list
# ask for new location
NEW_SVN_LOCATION=${NEW_SVN_LOCATION:-$(read -p "Please enter the new SVN location: " NEW_SVN_LOCATION ; echo $NEW_SVN_LOCATION)}
# remove trailing slash
NEW_SVN_LOCATION=${NEW_SVN_LOCATION%/}
echo "New SVN location: ${NEW_SVN_LOCATION}"
# get first parameter as directory or working directory
if [ -z "$1" ]; then
DIR="$(pwd)"
else
DIR="$1"
fi
# ask if location is correct or exit
read -p "Is $DIR the correct location? [y/n] " -n 1 -r
echo
# exit if answer was no
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Aborting..."
exit 1
fi
beginswith() { case $2 in "$1"*) true;; *) false;; esac; }
for i in $(find $DIR -type d -name .svn); do
PARENT="$(dirname $i)"
echo "Relocating $PARENT"
# get current url
SVN_URL="$(svn info $PARENT | grep "^Repository Root" | cut -d ' ' -f 3 | tr -d '\n')"
SVN_PATH="$(svn info $PARENT | grep "^Relative" | cut -d ' ' -f 3 | tr -d '\n' | awk -F^ '{print $2}')"
# remove leading slash
if beginswith "/" $SVN_PATH; then
SVN_PATH="${SVN_PATH:1}"
fi
echo "Current URL: '${SVN_URL}' - Path: '${SVN_PATH}'"
if beginswith $NEW_SVN_LOCATION $SVN_URL; then
echo "New URL already begins with the current URL. Checking next..."
continue
fi
# get new url
NEW_SVN_URL="${NEW_SVN_LOCATION}/${SVN_PATH}"
# switch svn url
echo "Relocating URL from '${SVN_URL}' to '${NEW_SVN_LOCATION}'"
# relocate url
svn relocate $SVN_URL $NEW_SVN_LOCATION $PARENT
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment