Subversion updates notification with growlnotify

Here’s another cool use for growlnotify: informing about incoming SVN updates. For me this is crucial when working with TextMate, as it doesn’t have ‘Synchronization view’ like Eclipse has. And keeping up to date with files modified by your teammates helps minimize the number of SVN conflicts you’ll have to resolve.

Prerequisites

You’ll need growlnotify (click for instructions on installing), Ruby interpreter and SVN client installed. The CVS version should be as easy, feel free to modify my script.

The script

Copy the following script and save as ~/svngrowl.rb (or any other name or location, but then don’t forget to update any other references below).

#!/usr/local/bin/ruby 
require 'FileUtils'  

dir = ARGV.shift or abort "no dir?" 
FileUtils.cd(dir) do 
  updated=`svn st -u --password YOUR_PASS_HERE | grep '*'`.split"n" 
  if updated.size > 0 
    title = "#{updated.size} files to update" 
    conflicts = updated.grep(/^M/) 
    if conflicts.size > 0 
      title << ", #{conflicts.size} POSSIBLE CONFLICTS!" 
    end 
    system "growlnotify -n SVN -m '#{updated.join("n")}' #{title}" 
  end 
end

The --password parameter (on line 6) is necessary if you want to run the script from the crontab, at least on my MacBook. (Don’t know why — any ideas?) You can remove it if you don’t plan to use crontab or your crontab is smarter than mine.

Don’t forget to make it an executable file with chmod u+x ~/svngrowl.rb .

Now, ask someone to commit something to your SVN and try running the script from the console with ~/svngrowl.rb path/to/your/project. You should see Growl notification entitled x files to update with a few lines of svn st -u output.

If you see x files to update, y POSSIBLE CONFLICTS! message, don’t worry. This only means that there are some files that were simultaneously modified by you and someone else. There’s a good chance that SVN will be able to merge the changes without any conflicts. Watch the output of svn up command. If you see G in the first column, SVN merged the repository changes with your version without a problem. The C in the first column means there is a conflict and you have to resolve it.

Automating

Now, this script isn’t more useful than simple svn st -u command (although it has more visual appeal). We should automate it and this can be done with at least two methods.

Console

If have a spare console, you can cd to your project directory and loop the script with (assuming you use bash):

while : ; do ~/svngrowl.rb . ; svn st -u ; sleep 300 ; done

This runs the script every 5 minutes. It also calls svn command so you can inspect incoming and outgoing files and decide if you want to run svn up. (And yes, there’s a colon after while, that’s bash’s idiom for infinite loop.)

Crontab

The crontab solution, once implemented, doesn’t require you to run the script manually anymore. To install it, run crontab -e command and add following line:

*/5 9-17 * * 1-5 $HOME/svngrowl.rb /path/to/your/project/

This runs svngrowl.rb every 5 minutes on weekdays (Monday to Friday) from 9 to 17 — change this to your working hours if you like. It should work on any UNIX-like system.

If you have any problems running svngrowl.rb from crontab (the notifies don’t show up or you get some error message in your mail), you may want to set PATH variable at the top of the crontab file. The script needs to access svn and growlnotify, so check their locations like this:

~ > which svn 
/usr/local/bin/svn 
~ > which growlnotify 
/usr/local/bin/growlnotify

On my system the PATH=/usr/local/bin is enough. If you get error messages telling that SVN needs your password, add --password parameter as above. You can also set MAILTO='' if the script is working and you still receive some mail from it (on my system growlnotify complains about some missing resources when run from crontab).

That’s all. Any comments welcome. I’d be glad to see versions of this for CVS and other repositories. Or in other languages (Perl anyone? sh? awk?)


One response to “Subversion updates notification with growlnotify

  • Gustavo Delfino

    Great hint!

    You should replace:
    require ‘FileUtils’
    with
    require ‘fileutils’

    otherwise it will not work on case sensitive file systems. I just lost 1 hour on this.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: