Search code examples
c#visual-studio-2010checkin-policy

Custom Checkin Policy: Access to filecontent from changeset files


I'm trying to write my own checking policy. I want to review if any .cs file contains some code. So my question is, if its possible to get the content of every file from the changeset in the overridden Initialize-Method and/or Evaluate-Method (from PolicyBase).


Solution

  • You can't get the contents from the files directly, you'll need to open them yourselves. For each checked In your Evaluate method, you should look at the PendingCheckin.PendingChanges.CheckedPendingChanges (to ensure that you only limit yourself to the pending changes that will be checked in.) Each PendingChange has a LocalItem that you can open and scan.

    For example:

    public override PolicyFailure[] Evaluate()
    {
        List<PolicyFailure> failures = new List<PolicyFailure>();
    
        foreach(PendingChange pc in PendingCheckin.PendingChanges.CheckedPendingChanges)
        {
            if(pc.LocalItem == null)
            {
                continue;
            }
    
            /* Open the file */
            using(FileStream fs = new FileStream(pc.LocalItem, ...))
            {
                if(/* File contains your prohibited code */)
                {
                    failures.Add(new PolicyFailure(/* Explain the problem */));
                }
    
                fs.Close();
            }
        }
    
        return failures.ToArray();
    }