Sunday, December 13, 2009

Post commit notifications in Subversion on Windows

Recently I needed to extend svn functionality by adding post commit email notifications for small open source project. The idea was to have some possibility to make commit visible for team members without annoying manual emailing (like “please see commit #123 – I added fluent nhibernate configuration”). We wanted to save our time and have some kind of switch for indicating that this commit is public. Another requirement is that we didn’t want all commits public – we wanted to notify only about those commits which were useful for other team members (of course author of commit decided is it useful or not for other :) ).

In order to implement described behavior I needed to make several things:

  1. Add post commit hook in svn server which sends email notifications
  2. Make it possible to send notifications by some condition. We decided to use ‘!’ symbol as indicator that commit is public. I.e. if contributor adds ‘!’ as first symbols in commit description, then notification should be sent (like “!Fluent nhibernate configuration added")

In order to achieve 1st step I decided to use standard mailer.py script which comes from subversion-tools. There were some drawbacks with installing it on windows. So I will describe the process step by step:

  1. Install python 2.6.x on svn server. On the moment when this post is written it is 2.6.4. Note that currently I didn’t not use 3.x version as I didn’t find python bindings for svn for 3.x versions (see below)
  2. Install python bindings from subversion site . On the moment of installing python bindings I had svn 1.6.6 but binsings was only for 1.6.2. Nevertheless it worked on 1.6.6.
  3. Copy mailer.py and mailer.conf into svn server (I copied it into …\repository\hooks\hook-scripts folder). mailer.conf file can be found on the same page with subversion-tools.
  4. Edit mailer.conf. There are many comments in this file which describe mailer configuration. I needed simple solution with sending mails via smtp. So this is stripped configuration file I used:
  5.    1: [general]
       2: smtp_hostname = your_smtp_server
       3: smtp_username = your_smtp_username
       4: smtp_password = your_smtp_password 
       5:  
       6: [defaults] 
       7:  
       8: diff = /usr/bin/diff -u -L %(label_from)s -L %(label_to)s %(from)s %(to)s 
       9:  
      10: commit_subject_prefix = [SVN-Commit]
      11: propchange_subject_prefix =
      12: lock_subject_prefix =
      13: unlock_subject_prefix = 
      14:  
      15: from_addr = your_from_address
      16: to_addr = your_receiver_1@example.com your_receiver_2@example.com [... your_receiver_n@example.com] 
      17:  
      18: reply_to =
      19: generate_diffs = none
      20: show_nonmatching_paths = yes 
      21:  
      22: [maps]

    Note that it is important to have “generate_diffs = none”. Without it you will need to configure diff tool (via “diff” option)

  6. Create post-commit.cmd script in …\repository\hooks folder:
  7.    1: set REPOS=%1
       2: set REV=%2
       3:  
       4: [your_absolute_path_to_repository]/repository/hooks/hook-scripts/mailer.py commit %REPOS% %REV%

    Note that absolute path is used here

After that recipients (specified in “to_addr” option in mailer.conf) will receive notifications after each commit.

Second step is to make these notifications conditional. It will require slightly change of mailer.py script:

image

After that notifications are sent only if contributor typed ‘!’ symbol in commit description.

6 comments:

  1. Hi ...
    This post was very helpful. I would want to send email to distribution list as its difficult to maintain the email list. Is there a way to extract the ACL username based on the repository name...

    ReplyDelete
  2. Hi!) I wonder what email program you use? I try mail_command = sendEmail.exe from http://caspian.dotconf.net/menu/Software/SendEmail/ but for now with no succes.

    ReplyDelete
  3. Hi,
    as far as I remember mailer.py sends emails without any additional clients by smtp.

    ReplyDelete
  4. You right it doesn't need any additional client. Thanks

    ReplyDelete
  5. I tried facing Error: post-commit hook failed (exit code 1) with output:

    help me

    ReplyDelete