Creating a Check-in Policy to warn when checking into multiple branches at once

2014-01-31

Update: Project has been uploaded to GitHub.

One of the teams I work with recently had strange issues when they tried to merge their feature branches to the main branch. It turned out that one developer had accidentally misconfigured his workspace and was check-in in half of his changes in a solution in one branch and the other half of his changes into another. Looking at the version history things like these happened in the past as well and explained some of the issues they had been experiencing.

The team has learned it's lesson, but to prevent these kind of accidents in the future we're creating a check-in policy that flags a warning to the developer that he's doing something that is potentially dangerous.

The policy itself is very simple to build. Follow one of the existing walk-throughs to create the project  and make sure you use the correct CLR and TFS Client Object Model assemblies for your target visual studio version.

A check-in policy has an Evaluate method, which is where all the magic happens. To make sure you're only checking into one branch at a time, we request the BranchRoot objects and compare it against the server path of all the items selected to be checked in:


public override PolicyFailure[] Evaluate()
{
    if (PendingCheckin.PendingChanges.AffectedTeamProjectPaths.Length > 1)
    {
        return new[]{new PolicyFailure("Checking into multiple projects at the same time", this)};
    }

    var branches = this.PendingCheckin.PendingChanges.Workspace.VersionControlServer
        .QueryRootBranchObjects(RecursionType.Full);

    var groupedChanges = PendingCheckin.PendingChanges.CheckedPendingChanges.GroupBy(
       change => branches.SingleOrDefault(branch => change.ServerItem.StartsWith(branch.Properties.RootItem.Item)));

    if (groupedChanges.Count() > 1)
    {
       return new[]{new PolicyFailure("Checking into multiple branches at the same time", this)};
    }

    return new PolicyFailure[0];
}

I've placed the solution onto GitHub and might add a vsix at some later point.

Creating a custom check-in policy pack that works with multiple versions of Visual Studio.

Anyone who has ever tried to create a custom check-in policy for Visual Studio probably knows that the policy isn't specific to the TFS server version you're using, but to the version of Visual Studio that is connecting. This can be confusing for companies that are upgrading their Visual Studio, but not their TFS installation (or for people who've moved to Visual Studio Online).

You might be able to get away with using Binding Redirects in the Visual Studio ..config file (and in the .config file of any other process that might invoke the policy) as documented here. This essentially tells .NET to load a newer version of a dependent assembly and will trick your policy that was created for an older version of Visual Studio to load in a newer one. You cannot use this trick to load a policy created for a newer version of Visual Studio in a older version.

The official way to resolve this, is to create a different binary for each Visual Studio Version and register that binary in the corresponding config hyve.

Each of these projects will be very similar and in many cases you can reuse the same .cs file for each version of Visual Studio (as long as you're not using any API that was introduced in a later version.

Step one, is to create the right projects:

Visual Studio Project Type CPU .NET Version TFS OM Version Visual Studio Version
2015 Class LibraryAnyCPU 4.5 14.0.0.0 v14.0
2013 Class LibraryAnyCPU 4.5 12.0.0.0 v12.0
2012 Class LibraryAnyCPU 4.0 11.0.0.0 v11.0
2010 Class LibraryAnyCPU 4.0 10.0.0.0 v10.0
2008 Class LibraryAnyCPU 3.0 9.0.0.0 v9.0
2005 Class LibraryAnyCPU 2.0 8.0.0.0 v8.0

Step two is to add the right references to it;

From the Visual Studio installation directory find:
  • Microsoft.TeamFoundation.VersionControl.Client.dll
  • Microsoft.TeamFoundation.Client.dll
  • Microsoft.TeamFoundation.dll
You can find these in: C:\Program Files (x86)\Microsoft Visual Studio {VisualStudioVersion}\Common7\IDE\PrivateAssemblies\.

Update:  For Visual Studio 2015, instead use the NuGet package.

Step 3, is to add a new class to it and have it inherit from PolicyBase.

Step 4, mark the class as [Serializable()].

If your code doesn't use any special features of the more recent versions of the Client Object Model, you can use "Add As Link" to add the same .cs to all the projects you've created in your solution.

Step 5 to each project add a compiler constant for the visual studio version you're targeting. This allows you to use #IF (VS2010) #ENDIF to add Visual Studio version specific code.

Now you're set to implement all the abstract methods of the PolicyBase class.

When you've implemented your policy you need to deploy it to all of your clients. This can either be done using a MSI, a VSIX, the Power Tools or manually. The key thing is to deploy the policy assemblies to the client and then adding a registry key to register your policy. Make sure you register for each version of Visual Studio by substituting the correct Visual Studio Version.

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\12.0\TeamFoundation\SourceControl\Checkin Policies]
"JesseHouwing.CheckinPolicies"="C:\Program Files(x86)\\MyCompany\\Checkin Policies\\v12.0\\JesseHouwing.CheckinPolicies.dll"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\12.0\TeamFoundation\SourceControl\Checkin Policies]
"JesseHouwing.CheckinPolicies"="C:\Program Files(x86)\\MyCompany\\Checkin Policies\\v12.0\\JesseHouwing.CheckinPolicies.dll"

One of my new favorite plugins for Visual Studio and TFS

2014-01-28

Today I stumbled upon a new Team Explorer extension for TFS in Visual Studio. It almost immediately
made it on my shortlist of things that I need to have on every Visual Studio installation. It's simple, but brilliant.

Ok, enough praise :).

This extension nests itself into Team Explorer and adds a Merge button there. Upon selection of the source and target branch it will automatically list all Change Sets that need merging and when you merge them it automatically associates the correct Change Sets and Work Items. The latter is exactly why I instantly fell in love.

The extension is available for both Visual Studio 2012 and 2013.

Install Team Foundation Server Power Tools side-by-side

2014-01-21

If you're like me, then you probably have more than one version of Visual Studio installed on your system. And you might be connecting to multiple versions of TFS as well. Currently I have Visual Studio 2010, 2012 and 2013 installed on my laptop and I connect to TFS 2010, 2012, 2013 and Visual Studio Online on a regular basis.

I've found that the Visual Studio Power Tools add a lot of valuable tricks to Team Explorer and offer some features on the commandline that you'd otherwise need to build your own custom tools for. However, some of the commands must be executed from the TFS server and some of the items that are installed are targeting a specific version of TFS.

As long as you install the Power Tools for "all users" on your machine and have installed the matching Team Explorer version (and patches), you can install them side-by-side, except for one feature, the Windows Explorer Extensions.

 

Most Reading