gpt4 book ai didi

c# - 调用 `Workspace.PendAdd` 未添加项目

转载 作者:行者123 更新时间:2023-11-30 19:18:33 28 4
gpt4 key购买 nike

我有以下函数在两个新项目的循环中被调用以添加到源代码管理中。循环的每次迭代都获取源代码,将其复制到文件夹,创建一个 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);
}
}

有两个项目。第一个项目工作正常,但第二个项目没有。第二个项目创建项目和工作区,但是在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);
}

workspace.PendAdd(serverPath, true) 总是为第二个项目返回零,无论哪个项目是第二个。第一个项目总是正确完成。哪个项目第二并不重要。第二个项目始终返回零项。我显然希望所有项目都正确添加到源代码管理中。这是怎么回事?

最佳答案

我不知道您的应用程序为什么不工作,但 TFS API 能够创建多个工作区并从中 checkin ,而无需处理任何东西或调用 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);
}
}
}

所以我认为问题出在您的代码中的其他地方。但是,如果您可以生成 short, self-contained example在没有多层抽象的情况下展示问题,这会很有帮助。

一些有用的提示:

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

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

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

这方面的一个例子是,当您向 PendAdd 添加多个路径时,其中一个失败(例如,因为路径被锁定)。如果不监听非 fatal error ,您将不会知道此路径已从未决更改中排除。 (尽管您会知道 a 路径被排除在外,因为查看了返回码。)

如果你有一个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);
}
}
}

(请注意,此示例摘自 Buck Hodges 的有用 client API example 博客文章。)

关于c# - 调用 `Workspace.PendAdd` 未添加项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12355585/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com