我需要以編程方式查找有關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