Generating Custom TFS Check-in E-mails – Part 2

As discussed in Part 1, I wanted to provide the functionality of CVSspam for TFS in order to give everyone on the team a better understanding of the code being checked in. The first step in this was, obviously, to find a way to compare text files so that I could present the differences between two versions of a file.

I was much too lazy to actually implement a difference algorithm myself and instead went to search for an existing library to do this. After a bit of searching I came across the article An O(ND) Difference Algorithm for C# which after writing some test code seemed like it would do the trick nicely.

The article provides the Diff class (I won’t list the source here, but you can get it through the link above) which provides a pretty straight-forward set of methods for comparing the contents of two strings as this trivial sample code shows:

private static void CompareFiles( string oldFile, string newFile )
{
    string[] oldLines;
    string[] newLines;
    string oldContent;
    string newContent;
    Diff differ = new Diff();
    Diff.Item[] differences;

    GetFileContent( oldFile, out oldContent, out oldLines );
    GetFileContent( newFile, out newContent, out newLines );

    differences = differ.DiffText( oldContent, newContent );
    foreach ( Diff.Item difference in differences )
    {
        Console.WriteLine( "Difference:" );
        Console.WriteLine( "\tStartA:   {0}",
                           difference.StartA );
        Console.WriteLine( "\tDeleted:  {0}",
                           difference.deletedA );
        Console.WriteLine( "\tStartB:   {0}",
                           difference.StartB );
        Console.WriteLine( "\tInserted: {0}",
                           difference.insertedB );
    }
}

Each difference between the two files is reported as a Diff.Item instance which tells us:

  • The line number in the old file where the difference starts (StartA)
  • The number of lines deleted from the old file (deletedA)
  • The line number in the new file where the difference starts (StartB)
  • The number of lines from the new file that were added to the resulting file (insertedB)

With this information it’s easy to generate the relevant information from the two versions of the file.

With this in hand, the next step was to get information out of TFS about the changeset, the items included in the changeset and the new and previous versions of each item. This will the topic for my next post.

One Response to “Generating Custom TFS Check-in E-mails – Part 2”

  1. Eric Johnson Says:

    This is a great article, I’m looking forward to your next post!

Leave a Reply