Saturday, July 19, 2008

SVN diffs from the Mac finder


As I recently blogged, I've installing SCPlugin to get Finder integration of a (limited) set of Subversion operations from the Finder contextual menu. However, this set of operations does not include the subversion diff. I've therefore been looking for an additional solution since I really like SCPlugin as a client otherwise and have considered various methods and third-party tools.

The solution I've come up with uses just standard MacOSX stuff: FileMerge that comes with the Apple Developer Tools (and it's command line helper 'opendiff') and Automator that originally shipped with MacOSX 10.4 Tiger (although I'm using MacOSX 10.5 Leopard and I don't know if this will work exactly the same on 10.4).

Using this solution you right-click (control-click) on one or more files and select More->Automator->svn-diff and it shows you the differences between the current and baseline in Apple's FileMerge. Just what I wanted.

The chain of events that do the work are:
  1. Automator step 1 gets the selected finder items
  2. Automator step 2 runs a Shell Script, using /usr/bin/python (since I know more Python than standard shell script). The inputs are passed as arguments. The script is shown below.
  3. This Python script runs the Subversion command line (svn) and asks it do a diff using a separate diffing command.
  4. The diff command passed is a script called ''. This is a minor modification of the script '' which I downloaded from the Subversion site 's tools contribution page along with a cool conflict resolution script that uses FileMerge as well. The minor modification takes out the check for Apple's terminal. I store all these shell script files in /usr/local/bin (which is referenced in the Python script - so it needs to be there otherwise the script won't work).
  5. This calls 'opendiff' which is a command line tool which sends the files to FileMerge.
  6. FileMerge launches and shows you the files.

Not exactly the shortest chain of events in the history of viewing a file! It is possible we could skip steps 3 and 4 totally, since we are differencing always between the baseline and current - the base text file is stored in the '.svn' directory. However, this information is private and may have/could change on different versions of Subversion. Additionally the script '' sorts out a lot of unusual conditions like temporary files. Step 5 is another one that seems to add no direct value - but does make the whole process simpler avoiding us figuring out about running processes, apple events and the like. So I think - unless I get major delays or problems, I'll leave it like this for the moment.

The Python script executed by the Automator looks like this:
import sys
import os
for f in sys.argv[1:]:
command = 'svn diff "'+f+'" --diff-cmd /usr/local/bin/'
print command
print "Completed"

The two print statements are simply for debugging purposes in Automator.

It's slightly slow - on my machine it takes about 3 seconds for the command line version to load into FileMerge (including FileMerge to updating the screen to show differences) and about 10 seconds using the Automator menu. Most of that time appears to be the Automator loading and set-up. However, you can select multiple files across multiple directories (opened with the disclosure triangle) and they load a couple of seconds after each other. There is a possibilities of shortening the delays in the shell script as well. Something for another day.

You can find copies of all the scripts mentioned here along with installation instructions on the Lightsoft web site in the 'more stuff' section.


Post a Comment

<< Home

Newer›  ‹Older