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


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

    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.

Search This Blog


Most Reading