如何以编程方式获取有关 TFS 中分支的信息? [英] How to programmatically get information about branches in TFS?

查看:26
本文介绍了如何以编程方式获取有关 TFS 中分支的信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to programmatically find out information about branches in TFS. For instance the main thing i am interested is given root folder $/MyProject/Project1 I neeed to find out what other folders have been branched from it. I am just after the right API methods.

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

Say i have connection to TFS server and have access to VersionControlServer and Workspace class instances.

推荐答案

好吧,这比我想象的更容易也更困难.我能够从几个不同的来源将其汇总在一起,但这似乎有效.我会警告你,这里没有错误处理,如果 itemSpec 不存在,它会抛出异常.

Ok, this was both easier and more difficult than I thought it would be. I was able to pull this together from a few different sources, but this seems to work. I will warn you, there's no error handling here, and if the itemSpec doesn't exist, it bombs with an exception.

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);
            }                       
        }
    }
}

这篇关于如何以编程方式获取有关 TFS 中分支的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆