Click here to Skip to main content
Click here to Skip to main content

Adding Post-commit Hook to SVN Source Control

, 27 Jul 2012
Rate this:
Please Sign up or sign in to vote.
How to add a post-commit hook to SVN source control that sends email to the team

Introduction

Tortoise SVN is a free source control tool that is used by many. One of the things I find missing is email notifications on check-in or commit (as it is called in SVN). Here, I try to share how we can send email notifications on commit.

The code presented in this article sends an automated email to predefined list of people with details about files that were checked-in/committed.

Background

While working in a team, it is possible that two or more people are working on different but interdependent code blocks. For instance, one person working one data layer of the application may have changed the method signatures in the classes. Now, the person working on business layer will be affected since his code is outdated.

So, instead of people running to each others seats or typing emails about the code changes they have performed, it can be time saving to have automated emails.

An automated build is another step that could be added to the post commit hook (I intend to update the current code with that step later).

Using the code

I intend to keep it as a installation manual and will try to tell step by step process to use the executable and configuration file. If you are not interested in the code details, avert.

This code uses two worth mentioning classes:

  • Process
  • SmtpClient

Before we get into the details, lets step back and see what SVNLook.exe provides us with. This executable is provided with the VirtualSVN server and lets user examine the transactions that have occurred. This executable can be used to diagnose the issues that may have occurred in the source control. Here, we are using this to identify the most recent commit details.

The program executes two commands using SVNLook.exe. First, to get the author information. Following C# code executes a command returns the username for the person who performed last commit operation on a given repository (branch, if you are VSS/TFS friendly):

                svnLook = new Process();
                svnLook.StartInfo = new ProcessStartInfo();
 
                svnLook.StartInfo.FileName = Settings.Default.SVNLookFilePath;
                svnLook.StartInfo.Arguments = "author " + Settings.Default.RepositoryPath;
                svnLook.StartInfo.UseShellExecute = false;
                svnLook.StartInfo.RedirectStandardOutput = true;
 
                string consoleMessage = string.Empty;
 
                svnLook.Start();
                svnLook.WaitForExit();
                consoleMessage = svnLook.StandardOutput.ReadToEnd(); 


This message will later be used in the email content.

Next, we execute changed command to get the details of commit performed by aforementioned user.

svnLook.StartInfo.Arguments = "changed " + Settings.Default.RepositoryPath; 
svnLook.Start(); 
svnLook.WaitForExit();
consoleMessage = svnLook.StandardOutput.ReadToEnd();  

Once we have received the information, we would compose and send an email to the list of users. Following code sends the email to users:

 mailMessage.From = new MailAddress(Settings.Default.FromAddress);
                mailMessage.Subject = Settings.Default.MailSubject;
                mailMessage.Priority = MailPriority.Low;
                foreach (var email in Settings.Default.ToAdress)
                {
                    mailMessage.To.Add(email);
                }
                mailMessage.IsBodyHtml = false;
                mailMessage.Body = mailContent;
                SmtpClient smtpClient = new SmtpClient();
                smtpClient.Host = Settings.Default.SMTPHost;
                smtpClient.Port = Settings.Default.SMTPPort;
                smtpClient.EnableSsl = Settings.Default.EnableSMTPSSL;
                smtpClient.Credentials = new NetworkCredential(Settings.Default.FromAddress, Settings.Default.FromAddressPassword);
                smtpClient.Send(mailMessage); 

You may have noticed the usage of Settings here in the code. Settings is an very easy way to keep things configurable in your application. Here is a MSDN link that tells more about it.

Weak Link

The current code version does not employ good exception handling and logging mechanism. Although, it may well be improved using Microsoft Enterprise Library's Exception Handling and Logging blocks. One may prefer to write his own library to efficiently perform these operations.

Using the executable

If you need to use the executable right away, here are the steps to follow:

Open SVNPostCommitHook.exe.config file and set the following
values:

  • FromAddress: Email address from which email will be sent
  • MailSubject: Subject for automated email
  • ToAdress: Email addresses to which mail is to be sent (multiple addresses can be added)
  • SMTPHost: Name/IP of the host used for SMTP transaction
  • SMTPPort: Port number used for SMTP transactions
  • EnableSMTPSSL: Sets whether SMTP transactions should be carried out over https
  • FromAddressPassword: Password for email address from which mail will be sent
  • LogFilePath: Path of the log file. All the error information will be logged into this file
  • SVNLookFilePath: Path of SVNLook.exe file
  • RepositoryPath: Path of repository for which hook is set
Once these settings have been updated, perform following actions in VirtualSVN server manger:

  1. Open
    VirtualSVN server manager. Right click on the repository for which post commit
    hook is needed. Click on Properties
  2. Switch to the second tab: “Hooks” on the properties
    window. Select “Post-commit hook” and click on edit.
  3. Paste
    the path for the executable file that will perform post commit
    work. Click on OK for all the windows.

Points of Interest

I am interested in writing more and more useful hooks for SVN and sharing it here at CodeProject.

History

Version 1.0

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

d@nish
Software Developer Self Employed
India India
No Biography provided

Comments and Discussions

 
Generalunable to download source code Pinmemberdeorerohan8-May-14 23:39 
GeneralRe: unable to download source code Pinmember d@nish 8-May-14 23:46 
GeneralRe: unable to download source code Pinmember d@nish 8-May-14 23:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 28 Jul 2012
Article Copyright 2012 by d@nish
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid