调用`Workspace.PendAdd`不添加项目

Cha*_*ert 2 c# tfs-sdk tfs2010

我有一个循环中调用的以下函数,用于添加到源代码控制的两个新项目.循环的每次迭代都会获取源代码,将其复制到文件夹,创建tfs团队项目,为该项目创建工作区,然后尝试将代码添加到源代码控制中.

static void Main(string[] args) {
    var tfsWorkItems = _<IWorkItems>();
    var workItems = tfsWorkItems.GetProjectsToMigrate();
    var tfs = _<ITfs>();
    var logFilePath = new DirectoryInfo("C:\\log");
    var workingDirectory = new DirectoryInfo("C:\\m");
    Cleanup(workItems, tfs, logFilePath, workingDirectory);
    var svn = _<ISvn>();
    var app = _<IApplication>();
    foreach (var workItem in workItems)
    {
        var root = Path.Combine(workingDirectory.FullName, workItem.Id.ToString());
        var svnBase = Path.Combine(root, "s");
        var localWorkspacePath = Path.Combine(root, "t");
        var tfsBase = Path.Combine(localWorkspacePath, workItem.TfsProjectName, "Main");
        var tfsProject = workItem.ProjectType.ToLower() == "php" ? Path.Combine(tfsBase, "src")
                                                                    : tfsBase;
        svn.CheckoutFromSvn(workItem.SvnLocation, svnBase);
        app.CopyToTfsFolderStructure(svnBase, tfsProject);
        tfs.CreateTeamProject(workItem.TfsProjectName, logFilePath);
        tfs.CreateWorkspace(workItem.WorkspaceName, localWorkspacePath);
        tfs.AddToSourceControl(workItem.WorkspaceName, localWorkspacePath, workItem.TfsProjectName);
    }
}
Run Code Online (Sandbox Code Playgroud)

有两个项目.第一个项目正常工作,但第二个项目没有.第二个项目创建项目和工作区,但是在AddToSourceControl中

public void AddToSourceControl(string workspaceName, string localPath, string projectName) {
    var tfs = new TfsTeamProjectCollection(_collection);
    var vcs = tfs.GetService<VersionControlServer>();
    var user = vcs.AuthorizedUser;
    var workspace = vcs.GetWorkspace(workspaceName, user);
    var serverPath = workspace.GetServerItemForLocalItem(Path.Combine(localPath, projectName, "Main"));
    var itemSpec = new ItemSpec[] {
        new ItemSpec(serverPath, RecursionType.Full)
    };
    workspace.PendAdd(serverPath, true);

    // doesn't return anything
    var pendingSets = vcs.QueryPendingSets(
        itemSpec, workspace.Name, user, true);
    var pendingChanges = pendingSets.Aggregate(new List<PendingChange>(), (acc, item) => {
        acc.AddRange(item.PendingChanges);
        return acc;
    });
    var parameters = new WorkspaceCheckInParameters(pendingChanges, "svn to tfs migration") {
        OverrideGatedCheckIn = ((CheckInOptions2)vcs.SupportedFeatures & CheckInOptions2.OverrideGatedCheckIn) == CheckInOptions2.OverrideGatedCheckIn,
        PolicyOverride = new PolicyOverrideInfo("migration triggered check-in", null),
        SuppressEvent = true,
    };
    workspace.CheckIn(parameters);
}
Run Code Online (Sandbox Code Playgroud)

workspace.PendAdd(serverPath, true)对于第二个项目,总是返回零,无论哪个项目是第二个.第一个项目总是正确完成.哪个项目排在第二位并不重要.第二个项目总是返回零项.我显然希望所有项目都能正确添加到源代码控制中.这里发生了什么?

Edw*_*son 9

我不知道为什么你的应用程序不能正常工作,但TFS API能够创建多个工作区并从中检入,无需任何处理或调用GC或类似的任何奇怪的东西.

这是一个示例程序,从您发布到连接站点的程序修改,表现出正确的行为:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using System.IO;
using Microsoft.TeamFoundation.VersionControl.Common;

namespace AddToSourceControl
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Uri serverUri = new Uri(args[0]);
                string serverPath = args[1];
                string localPath = args[2];

                for (int i = 0; i < 5; i++)
                {
                    string uniqueId = Guid.NewGuid().ToString();

                    if (i > 0)
                    {
                        Console.WriteLine();
                    }

                    Console.WriteLine("Creating a workspace and checking in with id " + uniqueId);

                    TfsTeamProjectCollection connection = new TfsTeamProjectCollection(serverUri);
                    VersionControlServer vcs = connection.GetService<VersionControlServer>();

                    string uniqueServerPath = VersionControlPath.Combine(serverPath, uniqueId);
                    string uniqueFolder = Path.Combine(localPath, uniqueId);

                    Workspace workspace = vcs.CreateWorkspace(uniqueId, vcs.AuthorizedUser, "", new WorkingFolder[] {
                        new WorkingFolder(uniqueServerPath, uniqueFolder)
                    });

                    Console.WriteLine("Created TFS workspace " + uniqueId);

                    CheckinLocalFolder(serverUri, localPath, uniqueId);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed: " + e);
            }
        }

        private static void CheckinLocalFolder(Uri serverUri, string localPath, string uniqueId)
        {
            TfsTeamProjectCollection connection = new TfsTeamProjectCollection(serverUri);
            VersionControlServer vcs = connection.GetService<VersionControlServer>();

            string uniqueFolder = Path.Combine(localPath, uniqueId);
            string uniqueFile = Path.Combine(uniqueFolder, uniqueId + ".txt");

            Workspace workspace = vcs.GetWorkspace(uniqueFolder);

            Console.Out.WriteLine("Found workspace " + workspace.Name);

            // Create a local folder with a file in it
            Directory.CreateDirectory(uniqueFolder);
            using (TextWriter output = new StreamWriter(uniqueFile))
            {
                output.WriteLine("This is " + uniqueId);
                output.Close();
            }

            Console.WriteLine("Created file " + uniqueFile);

            workspace.PendAdd(uniqueFolder, true);

            PendingChange[] pendingChanges = workspace.GetPendingChanges();

            Console.WriteLine("Pended changes:");
            foreach (PendingChange pendingChange in pendingChanges)
            {
                Console.WriteLine(" " + pendingChange.LocalItem + " (" + pendingChange.ChangeType + ")");
            }

            int changeset = workspace.CheckIn(pendingChanges, "Test from id " + uniqueId);

            Console.WriteLine("Checked in " + pendingChanges.Length + " as changeset " + changeset);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

所以我建议问题出在代码的其他地方.但是,如果您可以生成一个简短的,自包含的示例,该示例在没有多层抽象的情况下展示问题,那将会很有帮助.

一些有用的提示:

确保您的工作区映射设置正确:否则,PendAdd递归调用实际上不会添加任何内容.

确保文件存在于本地:出于同样的原因.

侦听错误: TFS API有几个可以通知消费者的事件 - 一个特别有用的是"非致命"错误通知.在许多操作中,操作的一部分可能会失败,而不是退出或抛出异常,将引发"非致命"并且操作将继续.

例如,当您添加多个路径PendAdd并且其中一个路径失败时(例如,因为路径已锁定.)如果不收听非致命错误,您将不会知道此路径是从挂起的更改中排除的.(虽然你会知道一个路径被排除,因为看的返回码.)

如果您有VersionControlServer vcs:

public class Example
{
    static void Main(string[] args)
    {
        VersionControlServer vcs = ConnectToServer(); // ... etc ...
        vcs.NonFatalError += Example.OnNonFatalError;
    }

    internal static void OnNonFatalError(Object sender, ExceptionEventArgs e)
    {
        if (e.Exception != null)
        {
            Console.Error.WriteLine("  Non-fatal exception: " + e.Exception.Message);
        }
        else
        {
            Console.Error.WriteLine("  Non-fatal failure: " + e.Failure.Message);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

(请注意,此示例取自Buck Hodges的有用客户端API示例博客文章.)