我正在开发一个实用程序,使用 TFS 2010 处理受源代码控制的文件。
如果某个项目尚未签出进行编辑,我会收到一个异常,这绝对是可以预见的,因为文件处于只读模式。
有哪些方法可以检出文件?
附注我想要一些程序化的东西,而不是
Process.Start("tf.exe", "...");
(如果适用的话)。
OpenAI 和 Stack Overflow 通过 OverflowAPI 访问结合在一起,为 OpenAI 用户和客户提供准确且经过审查的数据基础,AI 工具需要快速找到问题的解决方案,以便技术人员能够专注于优先任务。 OpenAI 还将把来自 Stack Overflow 的经过验证的技术知识直接呈现到 ChatGPT 中,让用户轻松访问可信、归属、准确且高度技术性的知识和代码,这些知识和代码由 15 年来为 Stack Overflow 平台做出贡献的数百万开发人员提供支持。此次合作的一部分:
private const string tfsServer = @"http://tfsserver.org:8080/tfs";
public void CheckOutFromTFS(string fileName)
{
using (TfsTeamProjectCollection pc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(tfsServer)))
{
if (pc != null)
{
WorkspaceInfo workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(fileName);
if (null != workspaceInfo)
{
Workspace workspace = workspaceInfo.GetWorkspace(pc);
workspace.PendEdit(fileName);
}
}
}
FileInfo fi = new FileInfo(fileName);
}
请注意,
Microsoft.TeamFoundation.Client.TeamFoundationServerFactory
已过时:TeamFoundationServer
类已过时。使用 TeamFoundationProjectCollection
或 TfsConfigurationServer
类与 2010 Team Foundation Server 对话。
为了与 2005 或 2008 Team Foundation Server 对话,请使用 TeamFoundationProjectCollection
类。相应的工厂类是 TfsTeamProjectCollectionFactory
。
您可以使用 Team Foundation 版本控制客户端 API。 方法是PendEdit()
workspace.PendEdit(fileName);
查看 MSDN 上的详细示例 http://blogs.msdn.com/b/buckh/archive/2006/03/15/552288.aspx
首先获取工作空间
var tfs = new TeamFoundationServer("http://server:8080/tfs/collection");
var version = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));
var workspace = version.GetWorkspace("WORKSPACE-NAME", version.AuthorizedUser);
使用工作区,您可以检出文件
workspace.PendEdit(fileName);
var registerdCollection = RegisteredTfsConnections.GetProjectCollections().First();
var projectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(registerdCollection);
var versionControl = projectCollection.GetService<VersionControlServer>();
var workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(_fileName);
var server = new TeamFoundationServer(workspaceInfo.ServerUri.ToString());
var workspace = workspaceInfo.GetWorkspace(server);
workspace.PendEdit(fileName);
我有两种方法可以做到这一点:简单和高级。
1)。简单:
#region Check Out
public bool CheckOut(string path)
{
using (TfsTeamProjectCollection pc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(ConstTfsServerUri)))
{
if (pc == null) return false;
WorkspaceInfo workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(path);
Workspace workspace = workspaceInfo?.GetWorkspace(pc);
return workspace?.PendEdit(path, RecursionType.Full) == 1;
}
}
public async Task<bool> CheckoutAsync(string path)
{
return await Task.Run(() => CheckOut(path));
}
#endregion
2)。高级(有接收状态):
private static string GetOwnerDisplayName(PendingSet[] pending)
{
var result = pending.FirstOrDefault(pendingSet => pendingSet.Computer != Environment.MachineName) ?? pending[0];
return result.OwnerDisplayName;
}
private string CheckoutFileInternal(string[] wsFiles, string folder = null)
{
try
{
var workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(folder);
var server = new TfsTeamProjectCollection(workspaceInfo.ServerUri);
var workspace = workspaceInfo.GetWorkspace(server);
var request = new GetRequest(folder, RecursionType.Full, VersionSpec.Latest);
GetStatus status = workspace.Get(request, GetOptions.None);
int result = workspace.PendEdit(wsFiles, RecursionType.Full, null, LockLevel.None);
if (result == wsFiles.Length)
{
//TODO: write info (succeed) to log here - messageText
return null;
}
var pending = server.GetService<VersionControlServer>().QueryPendingSets(wsFiles, RecursionType.None, null, null);
var messageText = "Failed to checkout !.";
if (pending.Any())
{
messageText = string.Format("{0}\nFile is locked by {1}", messageText, GetOwnerDisplayName(pending));
}
//TODO: write error to log here - messageText
return messageText;
}
catch (Exception ex)
{
UIHelper.Instance.RunOnUiThread(() =>
{
MessageBox.Show(Application.Current.MainWindow, string.Format("Failed checking out TFS files : {0}", ex.Message), "Check-out from TFS",
MessageBoxButton.OK, MessageBoxImage.Error);
});
return null;
}
}
public async Task<string> CheckoutFileInternalAsync(string[] wsFiles, string folder)
{
return await Task.Run(() => CheckoutFileInternal(wsFiles, folder));
}