Custom Checkin Policy: Access to filecontent from changeset files

最后都变了- 提交于 2019-12-10 18:53:26

问题


I'm trying to whrite my own checkin 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 overriden Initialize-Methode and/or Evaluate-Methode (from PolicyBase).

Thanks in advanced!


回答1:


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();
}


来源:https://stackoverflow.com/questions/8821614/custom-checkin-policy-access-to-filecontent-from-changeset-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!