【问题标题】:How to programmatically get information about branches in TFS?如何以编程方式获取有关 TFS 中分支的信息?
【发布时间】:2011-06-09 01:55:15
【问题描述】:

我需要以编程方式找出有关 TFS 中分支的信息。例如,我感兴趣的主要事情是给定根文件夹 $/MyProject/Project1 我需要找出从它分支的其他文件夹。我只是追求正确的 API 方法。

假设我已连接到 TFS 服务器并可以访问 VersionControlServerWorkspace 类实例。

【问题讨论】:

    标签: .net tfs tfs-sdk


    【解决方案1】:

    好的,这比我想象的更容易也更困难。我能够从几个不同的来源将其整合在一起,但这似乎有效。我会警告你,这里没有错误处理,如果 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);
                }                       
            }
        }
    }
    

    【讨论】:

    • 请注意,在 ShowChildren 中还需要显示父项的相关项。因此,将其添加到 Foreach 循环之上。 System.Console.WriteLine(string.Format("分支到 {0}", parent.Relative.BranchToItem.ServerItem));
    【解决方案2】:

    主要答案中的代码并不总是返回所有目标分支。在我的测试中,它返回的分支比 Visual Studio 合并对话框少一个。

    有一种更简单、更安全的方法来获取目标分支列表。这与 Visual Studio 获取 Merge 对话框列表的方式相同:

    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 似乎返回了一个包含任何关系的项目列表,包括父关系,这与您的变量命名相反;您的代码假定您正在使用根分支,然后将提供所有子分支的列表,因为它是根。但是如果你传入一个子分支,它也会返回父分支,这不符合漫画队长的问题“我需要找出从它分支出的其他文件夹”的要求,隐含的意思是只有孩子分支是必需的。
    • 是的。我修改了变量和方法名称。 API 中可能有一种方法可以识别哪个路径是父路径并排除它。
    猜你喜欢
    • 1970-01-01
    • 2017-01-13
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 2012-11-28
    • 2011-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多