Sunday, October 17, 2010

export files from svn for sync using ftp upload

Well I finally got to write my first real shell script.
As always I wrote it because I found my self doing the same repetitive task again, for the thousand time.
In this particular situation it was the svn export of the files changed across several consecutive revisions with the purpose of updating a live site via FTP. I usually use the smarter way and have the production version as a svn checkout and update it using just svn up.
But sometimes that's not the best option. I know it's hard to believe but it can happen...
I am usually interacting with svn via Subclipse and despite all the complains I've found online I can only say that it works for me. But it doesn't provide this trick of exporting a bunch of files so I can ftp copy a folder onto another and update the darn site in a blink. Well a long blink but still I shouldn't have to individually select each file and press the copy button.
So I started digging. First I found a one liner that did a zip from the local changes with svn status. Well that's too bold for me. I like to commit my changes and then think that I have something to put live...
But that was a start. After I read some basics about bash my fears were confirmed. It was to be worse than when I had to code in perl :)
So probably the code can be tweaked and made look smarter. If so please comment. Although it feels archaic this style of coding definitely has some appeal.  Once you get it. And I haven't got it yet :D
An important note would be that I wrote it on OSX 10.5.8 Leopard. I first tried svn export but I got some errors from svn complaining that it cannot move it's tmp files to the target locations because the target is a directory. Which it was. Some people got it too and there was a fix involving a chmod but it did not work for me.
I also am using SSHKeychain because my svn path is in the form svn+ssh://user@IP/... so if you are not using svn over ssh you will not need SSHKeychain either.
The script asks for the repository root, start revision, end revision and the local path where to put the files. I get that info using Project properties - Subclipse.
So here's the trickster:

#!/bin/sh
# Exports SVN changed files to a chosen location

echo "Please fire up your SSH Keychain!"
echo "Repository Root URL:"
read repositoryUrl
echo "Start revision:"
read startRevision
echo "End revision:"
read endRevision
echo "Export location:"
read exportLocation

svn log -r $startRevision:$endRevision -q -v $repositoryUrl | grep "^ *[AM].*\.\w\{2,4\}$" | awk '{print $2}' | sort -u | \
while read FN
do
        echo "exporting $FN..."
        mkdir -p $exportLocation`dirname $FN`
        svn cat -r $endRevision $repositoryUrl$FN > $exportLocation$FN
done

As stated above this is my first shell script. I hope it works just fine for you but until you really understand what it does I advise you to double check the exported data before using it to update a production environment. There are no warranties whatsoever.

0 Comments:

Post a Comment

<< Home