Tuesday, 30 September 2008

Making Your SVN mod_dav_svn Repository Firefox-Friendly

There's a great add-on module for Subversion - mod_dav_svn, which I've blogged about before - that exposes the contents of your repository through a Web server interface. This is great for bringing up designs, ideas and HTML prototypes in meetings - we've got one of those interactive whiteboard things, and we've  saved lots of time, and probably a couple of acres of forest, by showing designs on the screen instead of printing handouts.

This doesn't quite work out-of-the-box, though. It'll sort-of work if you're using Internet Explorer to browse your WebDAV repository, but Firefox and Opera will probably display everything as plain text. Or gibberish. This is because Apache is sending a Content-Type header telling the browser that the content is text/plain, and Apache in turn is getting this information directly from Subversion. To get everything displaying properly, you'll need to make sure that every file in your repository has the proper MIME type associated with it in Subversion.

Using auto-props to set MIME types automatically when adding files to a repository

This bit depends on your client. Check out the official documentation on the auto-props feature; it's also worth knowing that you can open the svn configuration file in Notepad via the handy Edit button in the Tortoise settings dialog - right-click any folder window in Windows Explorer, hit TortoiseSVN -> Settings...

image 

To update files already in the repository

The auto-props feature is all very well, but if (as I did) you don't find out about it until you've already got a repository full of stuff, you have a second problem - how do you set the MIME-type properties on everything that's already in your repository?

This works on Windows, via the command shell, and needs the command-line version of svn installed - try the SlikSVN installer if you don't have svn already installed. Remember that although your repository is probably organised into projects, with their own trunks, tags and branches, it's still just a great big hierarchy of files and folders - if you do an svn checkout from svn://my.subversion.server/ without specifying /myproject/trunk, you will check out the HEAD version of your entire repository. (These techniques work just as well on individual trunks, branches and sub-folders, of course.)

First, check out the folder, branch or even the entire repository into a working folder - say c:\repository\.

Then run this:

C:\repository>for /r %1 in (*.gif) do svn propset svn:mime-type image/gif "%~f1"

for is the Windows shell command that basically says "repeat the following for every file matching this specification" - and we're saying for /r %1 in (*.gif), meaning "recursively find every file matching *.gif in or below the current folder, temporarily reference that file as %1, and run the following command" - where the command itself is svn propset svn:mime-type image/gif "%~f1"

Note that the %1 reference there is quoted, and we're using the ~f modifier to expand it to the full path - you may find

C:\repository>for /r %1 in (*.gif) do echo "%~f1"

enlightening if this doesn't make sense - remember, everything after the do is invoked for each matching file.

So, when for matches something.gif under myproject\trunk in your repository, it'll call svn.exe with the command line

svn propset mime-type image/gif "C:\repository\myproject\trunk\something.gif"

- which will set the MIME-type on something.gif to image/gif.

Repeat this incantation using the various file extensions and MIME types you need to configure, e.g.

C:\repository>for /r %1 in (*.jpg) do svn propset svn:mime-type image/jpeg "%~f1"
C:\repository>for /r %1 in (*.htm*) do svn propset svn:mime-type text/html "%~f1"

and once you're done, commit your changes back to the repository. You'll see a whole lot of SVN "Property changed" messages, and next time you browse your repository via mod_dav_svn, you should find things are working as expected.

No comments: