【问题标题】:Tfs Check-in using PendAdd: The array must contain at least one elementTfs Check-in using PendAdd:数组必须至少包含一个元素
【发布时间】:2023-03-18 04:10:01
【问题描述】:

所以我在自动化我的代码以将文件签入到 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);
        }

问题在于,每当我的代码尝试签入的新文件(当文件已存在于 TFS 中时,PendEdit 正常工作)时,它都会运行此代码:

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

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

当实际执行签入的行运行时,我会收到“数组必须包含至少一个元素”错误,修复它的唯一方法是手动添加检测到的更改,并提升它们包含更改,而我一生都无法弄清楚如何通过 C# 以编程方式做到这一点。如果有人对我应该采取的方向有任何指导,我将不胜感激!谢谢!

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

响应 TToni 的下一个编辑:

响应 TToni 的下一个编辑:

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

【问题讨论】:

  • 既然您似乎确切地知道添加了哪些文件,您可以尝试使用特定文件路径调用PendAdd,而不是使用递归重载的目录吗?
  • 这似乎给出了同样的错误,我编辑显示。不过谢谢你的想法:)
  • 我感觉它与整个“本地与服务器”工作区有关,但我不知道如何解决这个问题。
  • 这将是我的下一个问题。您是否尝试过两种类型的工作区?在使用CreateWorkspace 创建WS 时,您可以使用CreateWorkspaceParameters 重载来指定Location。默认值取决于您的服务器设置。
  • 嘿 TToni,查看我的编辑以获取我的回复

标签: c# visual-studio tfs


【解决方案1】:

查看this blog:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-29
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多