Home / Subversion Command Line Script to export changed files

Subversion Command Line Script to export changed files

A couple of days ago I posted how to export just the added/modified files from a subversion repository between two revisions using TortoiseSVN, the Windows Explorer SVN plugin. This post has a command line script which is used to achieve the same thing and means it can be run from UNIX based systems from the CLI.

Updated/alternative version of this script

I have posted an updated/alternative version of this script in response to a comment below. The other version saves the current revision number in a file and on subsequent exports only exports from that revision to the current HEAD.

The BASH script

I’m not really going to bother explaining how the script works; there’s some inline comments and if you don’t understand shell scripting or using SVN from the command line then this probably isn’t for you anyway 🙂

I have tested it on both Debian 5 and OSX 10.6 and it works on those. If there are any issues with it please leave a comment in the comments section and I will make the appropriate updates.

#!/bin/bash

if [ ! $1 ] || [ ! $2 ] || [ ! $3 ] || [ ! $4 ]; then
    echo "Please enter a revision from, revision to, SVN repository, and target directory"
    exit
fi

# set up nice names for the incoming parameters to make the script more readable
revision_from=$1
revision_to=$2
repository=$3
target_directory=$4

# the grep is needed so we only get added/modified files and not the deleted ones or anything else
# if it's a modified directory it's " M" so won't show with this command (good)
# if it's an added directory it's still "A" so will show with this command (not so good)

for line in `svn diff --summarize -r$revision_from:$revision_to $repository | grep "^[AM]"`
do
    # each line in the above command in the for loop is split into two:
    # 1) the status line (containing A, M, AM, D etc)
    # 2) the full repository and filename string
    # so only export the file when it's not the status line
    if [ $line != "A" ] && [ $line != "AM" ] && [ $line != "M" ]; then
        # use sed to remove the repository from the full repo and filename
        filename=`echo "$line" |sed "s|$repository||g"`
        # don't export if it's a directory we've already created
        if [ ! -d $target_directory$filename ]; then
            directory=`dirname $filename`
            mkdir -p $target_directory$directory
            svn export -r $revision_to $line $target_directory$filename
        fi
    fi
done

# to summarize any deleted files or directories at the end of the script uncomment the following line
#svn diff --summarize -r$revision_from:$revision_to $repository | grep "^[D]"

Usage

Save it as a file and change permissions so it’s executable. Then run it like so (where I’ve saved it as svn-export):

./svn-export revision-from revision-to repository target-directory

where revision-from and revision-to are the two revisions to export changes between, repository is the full URL for the repository, and target-directory is where you want to save it to.

For example, to export changes between revisions 20 and 25 from the repository svn://localhost/myrepository to the current directory, do this:

./svn-export 20 25 svn://localhost/myrepository .

This will export all added or modified files from revision 20 to 25, i.e. those files changed in versions 21, 22, 23, 24 and 25.