Hooking up on Subversion

So one of the many things that i have been meaning to do for ages is to create my own Post Commit Script. While this may seems scarily easy i have just never tried it.

Firstly a few things to note i will be using Visual SVN which is a free windows subversion server that makes doing everything we want very easy. Secondly all the hook scripts will be created using C#.

Setting Up Visual SVN

If you don’t already have the server installed , i suggest you grab it from here. The product is free and works brilliantly on windows.

Once the download is complete run the setup and walk through the simple setup by clicking next until you reach the end and accepting the license agreement along the way.

At the end of the setup leave the option to launch the SVN Manager ticked and click finish.

Once the manager launches you will be shown the following window.ManagerWindow

At this point you have 2 option. Create a new Repository for testing or import and existing repository. For this tutorial i am going to create a new Repository.

Creating a new Repository is really simple. Just right click on the Repository icon as seen in the picture above and choose create repository.Once you do this the following message box will be shown.

CreateRepo

Enter a name for the repository and leave the check box selected and then hit the create button.

At this point we have a fully functional repo and all we need to use it now is to create a user. Doing this is simple as well , simply right click on your newly created repo and select security. You will then be show the form below.

security

Since this is a clean install we should have no users setup and as such need to create some users. To do this click the add button and the form below will be displayed.

CreateUser

At this point hit the create user button and enter a username and password for the new user then hit okay.Keep in mind this is the user that you will be using to access your repo. Once the user is create it will be displayed in the list of available users as seen in the picture above. Now select the user and hit okay. This will assign the user to your repo and give the user read/write access.

If you want to change access for a a user simply select the user and then choose the appropriate option (read/write/none).

And thats about it for the Server setup assuming that you did not have a server or you are setting up a personal/demo server.

Creating the Hook Script

At this point there is one last thing to do which i will not cover in this tutorial and that is importing your project into SVN as that is a tutorial on its own.

So i am assuming that you managed to get your code imported and want to start doing some cool stuff when your code gets committed.

The first and simplest example is a mail notification that gets sent out to a list of users when a project member commits code to the repo.

Doing this is quite simple and once you know how opens up a world of possibilities.

Creating the scripts project

Keep in mind that you can create your hook scripts in anything that is executable in windows. This can be anything from a powershell script to a batch file or in our case a custom console app.

Yes i will be creating the hook script in c# using a console app just because i can :P

Now to get started create a simple console project in the IDEĀ  of your choosing , i am using VS 2008 but the express editions work fine as well. One thing to remember is to try to get your app to exit as fast as possible otherwise it will slow down your commits a little bit (depending on how long the app runs).

With the basic project created you can now start doing what you want. But since this is a tutorial i am going to explain a bit more. The First and most important thing to remember is that your application will be called with 2 parameters so you should always check that you have these values filled in before continuing. e.g.

if (args.Length >= 2 )
{
//Execute your code.
}

The 2 parameters that are passed to the app are (in the following order) the repo path and the revision that was committed.

Now with those 2 pieces of info it might not seem like there is much you can do but with a little help from subversion you can get a whole lot more. To get more info you need to call another subversion app called svnlook that comes bundled with Visual SVN and just about every other svn server.I suggest that you read up a little on it since i wont cover every detail about it, you can do this buy running ‘svnlook help’ in the command line without the quotes.

Once you know the command that you want to use , you can simply call it from c# passing in the repo path(argument 1) and the revision (argument 2) to get detailed info about what was just committed to the repo.

In the example below i use the ‘svnlook info’ to get info about the basic details of the commit (user name , time commited etc) and then add it to the body of a email which i then send. The basic class can be found below. I should not this is a very hacky way of doing things and if you plan to create larger and more complex modules i suggest using the awesome Captain Hook Lib which makes creating and calling modules really easy not to mention testable :P


class SVNRunner
{
//Change the location of the exe's to suit your system
string SVNLookPath = @"c:\Program Files\VisualSVN Server\bin\svnlook.exe";
string SVNexe = @"c:\Program Files\VisualSVN Server\bin\svn.exe";

public string GetLatestCommitInfo(string Command, string CommitPath, string Revision)
{
ProcessStartInfo processor = new ProcessStartInfo(SVNLookPath, Command + ” ” + CommitPath + ” -r ” + Revision);
processor.RedirectStandardError = true;
processor.RedirectStandardOutput = true;
processor.UseShellExecute = false;
Process process = Process.Start(processor);
process.Start();

string Results = process.StandardOutput.ReadToEnd();
process.WaitForExit(100);
return Results;
}

public static string ParseLookInfo(string Info)
{
Info = Info.Replace(Environment.NewLine, “\n”);
string[] data = Info.Split(‘\n’);
string Results = “”;
Results = “New Code Commited By : ” + data[0] + Environment.NewLine;
Results += “Commited On : ” + data[1] + Environment.NewLine;
Results += “Comments : ” + data[3] + Environment.NewLine;
return Results;
}

public void UpgradeSite(string SiteLocation)
{
ProcessStartInfo processor = new ProcessStartInfo(SVNexe, “update ” + SiteLocation);
processor.RedirectStandardError = true;
processor.RedirectStandardOutput = true;
processor.UseShellExecute = false;
Process process = Process.Start(processor);
}
}

At this point i dont think there is a need to explain the code in detail but basically each method in the class above does a very simple job. The first method does the basic ‘svnlook info’ call on your latest commit , the parseinfo method parses the output and makes it a bit easier to read and the last method run a ‘svn update’ command on a specific working folder , this allows you to auto update a for on the server when ever code is committed. As you can probably see i dont handle the emails in this class , i simply return the data to another class which sends out the emails for me.

And finally the last part which i so almost forgot to write up is the actually calling of the console app. To call it simply open up the visual SVN manager then navigate to your repo. Right click on it and choose properties. Next click the hooks tab and then double click on the post-commit hook. A window with a text box will pop up. In there enter the path to your exe and include the 2 parameters as well. e.g. This is what my path looks like ‘D:\Projects\ConsoleTester\ConsoleTester\bin\Debug\ConsoleTester.exe %1 %2′ , %1 and %2 are the parameters that you are parsing to your application and get populated by the subversion server.

I should also note that i tested this on my linux server as well and it works fine as long as you change the paths to match that of unix format and make sure you have mono installed. Setting up a linux server is a bit out of the scope of this article but if you need help let me know.

Finally I don’t think i have ever written a post this long so please forgive any of my mistakes and please leave some feedback about things i should change or fix. Also i don’t mind mailing you the basic template that i use for my post-commits , just drop me a mail at jameelhaffejee at gmail dot com.

~stalkerh

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks

2 Comments to “Hooking up on Subversion”

  1. Craig 24 June 2009 at 9:45 am #

    Nice article, but quick question: “One thing to remember is to try to get your app to exist as fast as possible otherwise it will slow down your commits a little bit”

    Is that “exist” meant to be “exit”? meaning is a bit fuzzy as it currently stands.

  2. jameel 24 June 2009 at 9:51 am #

    Thanks bud you catch the worst of my mistakes :P , and yeah its supposed to be exit


Leave a Reply