我需要以编程方式查找有关TFS中分支机构的信息。例如,我感兴趣的主要事情是给根文件夹$/MyProject/Project1我需要找出其他文件夹已从其分支。我只是在正确的API方法之后。如何以编程方式获取有关TFS分支机构的信息?
假设我有连接到TFS服务器并有权访问VersionControlServer
和Workspace
类实例。
我需要以编程方式查找有关TFS中分支机构的信息。例如,我感兴趣的主要事情是给根文件夹$/MyProject/Project1我需要找出其他文件夹已从其分支。我只是在正确的API方法之后。如何以编程方式获取有关TFS分支机构的信息?
假设我有连接到TFS服务器并有权访问VersionControlServer
和Workspace
类实例。
好吧,这比我想象的要容易和困难。我能够从几个不同的来源一起拉动这一切,但这似乎工作。我会警告你,这里没有错误处理,如果itemSpec不存在,它会抛出异常。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
static void Main(string[] args)
{
TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(
new Uri("http://tfs:8080"));
string srcFolder = "$/ProjectName";
var versionControl = tfs.GetService<VersionControlServer>();
ItemSpec[] specs = new ItemSpec[]{new ItemSpec(srcFolder, RecursionType.None)};
System.Console.WriteLine(string.Format("Source folder {0} was branched to:",
srcFolder));
BranchHistoryTreeItem[][] branchHistory =
versionControl.GetBranchHistory(specs, VersionSpec.Latest);
foreach (BranchHistoryTreeItem item in branchHistory[0][0].Children)
{
ShowChildren(item);
}
System.Console.WriteLine();
System.Console.WriteLine("Hit Enter to continue");
System.Console.ReadLine();
}
static void ShowChildren(BranchHistoryTreeItem parent)
{
foreach (BranchHistoryTreeItem item in parent.Children)
{
System.Console.WriteLine(
string.Format("Branched to {0}",
item.Relative.BranchToItem.ServerItem));
if (item.Children.Count > 0)
{
foreach(BranchHistoryTreeItem child in item.Children)
{
ShowChildren(child);
}
}
}
}
初级答案的代码并不总是返回所有目标分支。在我的测试中,它比Visual Studio合并对话框返回的分支少了一个。
有一个更简单和更安全的方式来获取目标分支的列表。这是同样的方式,Visual Studio中获得的列表中合并对话框:
using System;
using System.Collections.Generic;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
class Program
{
static void Main(string[] args)
{
string tfsUri = "http://tfs:8080/tfs/MyCollection";
string tfsItemSpec = "$/MyTeamProject/Folder";
List<string> branches = GetPathsEligibleForMerge(tfsUri, tfsItemSpec);
foreach (string branch in branches)
{
Console.WriteLine(branch);
}
}
public static List<string> GetPathsEligibleForMerge(
string tfsUri, string tfsBranchPath)
{
List<string> tfsEligibleMergePaths = new List<string>();
using (TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri(tfsUri)))
{
VersionControlServer vcs =
(VersionControlServer)tfs.GetService(typeof(VersionControlServer));
foreach (ItemIdentifier mergeItem in vcs.QueryMergeRelationships(tfsBranchPath))
{
if (!mergeItem.IsDeleted && !string.IsNullOrWhiteSpace(mergeItem.Item))
{
tfsEligibleMergePaths.Add(mergeItem.Item);
}
}
}
tfsEligibleMergePaths.Sort(StringComparer.OrdinalIgnoreCase);
return tfsEligibleMergePaths;
}
}
该代码总是返回相同的列表作为合并对话框。
我可能会在这里丢失一些东西,但似乎QueryMergeRelationships返回的是一个项目列表,其中包含父项关系,与您的变量命名相反;你的代码假定你正在使用根分支,然后会提供一个所有孩子的列表,因为它是根。但是如果你传入一个子分支,它也会返回父分支,这不符合Captain Comic问题的要求,“我需要找出其他文件夹已经被分支了”,这隐含地意味着只有孩子需要分支机构。 – paulyphonic 2014-04-19 20:25:52