使用PendAdd签入Tfs:该数组必须至少包含一个元素

时间:2016-07-21 13:07:31

标签: c# visual-studio tfs

所以我在将代码自动化到TFS签入文件方面遇到了问题,而且它一直困扰着我!这是我的代码:

tableView.tableHeaderView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 0.0, height: 0.01))

问题是每当我的代码试图检入时,只要它是新文件(PendEdit在TFS中已经存在的文件时正常工作),它就会运行这段代码:

        string location = AppDomain.CurrentDomain.BaseDirectory;

        TfsTeamProjectCollection baseUserTpcConnection = new TfsTeamProjectCollection(uriToTeamProjectCollection);
        IIdentityManagementService ims = baseUserTpcConnection.GetService<IIdentityManagementService>();
        TeamFoundationIdentity identity = ims.ReadIdentity(IdentitySearchFactor.AccountName, @"PROD1\JR", MembershipQuery.None, ReadIdentityOptions.None);

        TfsTeamProjectCollection impersonatedTpcConnection = new TfsTeamProjectCollection(uriToTeamProjectCollection, identity.Descriptor);

        VersionControlServer sourceControl = impersonatedTpcConnection.GetService<VersionControlServer>();

        Workspace workspace = sourceControl.CreateWorkspace("MyTempWorkspace", sourceControl.AuthorizedUser);

        String topDir = null;

        try
        {   

        Directory.CreateDirectory(location + "TFS");

        String localDir = location + "TFS";

        workspace.Map("$/Automation/", localDir);

        workspace.Get();

        destinationFile = Path.Combine(localDir, Name + ".xml");
        string SeconddestinationFile = Path.Combine(localDir, Name + ".ial");

        bool check = sourceControl.ServerItemExists(destinationFile, ItemType.Any);

        PendingChange[] pendingChanges;
        File.Move(sourceFile, destinationFile);
        File.Copy(destinationFile, sourceFile, true);
        File.Move(SecondsourceFile, SeconddestinationFile);
        File.Copy(SeconddestinationFile, SecondsourceFile, true);

        if (check == false)
        {
            workspace.PendAdd(localDir,true);
            pendingChanges = workspace.GetPendingChanges();
            workspace.CheckIn(pendingChanges, Comments);
        }

        else
        {
            workspace.PendEdit(destinationFile);
            pendingChanges = workspace.GetPendingChanges();
            workspace.CheckIn(pendingChanges, Comments);
        }

这些文件不是在待处理更改中包含的更改中,而是包含在排除的更改中,如下所示:

enter image description here

当实际执行签入的行运行时,我将得到“数组必须包含至少一个元素”错误,并且修复它的唯一方法是手动添加检测到的更改,并进行提升他们包括了变化,我根本不能为我的生活弄清楚如何以编程方式做到这一点虽然C#。如果有人对我应该采取的方向有任何指导,我将非常感激!谢谢!

编辑:我还发现了通过协调文件夹来解决这个问题的另一种方法,这也促进了检测到的更改,但问题是我似乎无法弄清楚如何编程自动执行此操作。 我知道运行visual studio开发人员命令提示符,重定向到此映射所在的文件夹,运行“tf reconcile / promote”是一种方式,但我只能将其自动化到/ promote部分,因为提出了一个用户必须输入的工具箱,这违背了自动化的目的。我不知所措。

下一页编辑回应TToni:

enter image description here

下一页编辑回应TToni:

我不完全确定我是否正确地执行了此CreateWorkspaceParameters(参见图1),但这次它给出了相同的错误,但文件甚至不在排除的部分中。他们只是没有出现在待定更改的任何地方(见图2)。

Picture 1

Picture 2

1 个答案:

答案 0 :(得分:1)

检查this blog

工作区有一个方法GetPendingChangesWithCandidates,它实际上获得了所有“排除”的更改。代码段如下:

private void PendChangesAndCheckIn(string pathToWorkspace)
{
    //Get Version Control Server object
    VersionControlServer vs = collection.GetService(typeof
(VersionControlServer)) as VersionControlServer;
    Workspace ws = vs.TryGetWorkspace(pathToWorkspace);

    //Do Delete and Copy Actions to local path

    //Create a item spec from the server Path
    PendingChange[] candidateChanges = null;
    string serverPath = ws.GetServerItemForLocalItem(pathToWorkspace);
    List<ItemSpec> its = new List<ItemSpec>();
    its.Add(new ItemSpec(serverPath, RecursionType.Full));

    //get all candidate changes and promote them to included changes
    ws.GetPendingChangesWithCandidates(its.ToArray(), true, 
out candidateChanges);
foreach (var change in candidateChanges)
    {
        if (change.IsAdd)
        {
            ws.PendAdd(change.LocalItem);
        }
        else if (change.IsDelete)
        {
            ws.PendDelete(change.LocalItem);
        }
    }

    //Check In all pending changes
    ws.CheckIn(ws.GetPendingChanges(), "This is a comment");
}