Dot-Net

如何以程式方式獲取有關 TFS 中分支的資訊?

  • January 14, 2011

我需要以程式方式找出有關 TFS 中分支的資訊。例如,我感興趣的主要內容是根文件夾**$/MyProject/Project1**我需要找出從它分支的其他文件夾。我只是在使用正確的 API 方法。

假設我連接到 TFS 伺服器並且可以訪問VersionControlServerWorkspace類實例。

好吧,這比我想像的更容易也更困難。我能夠從幾個不同的來源將其整合在一起,但這似乎有效。我會警告你,這裡沒有錯誤處理,如果 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);
           }                       
       }
   }
}

引用自:https://stackoverflow.com/questions/4690217