首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何以编程方式获取TFS中分支的相关信息?

如何以编程方式获取TFS中分支的相关信息?
EN

Stack Overflow用户
提问于 2011-01-14 18:46:06
回答 2查看 13.7K关注 0票数 19

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

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-01-14 22:53:31

好吧,这比我想象的要容易,也比我想象的要难。我能够从几个不同的来源收集这些信息,但这似乎是有效的。我要警告你,这里没有错误处理,如果itemSpec不存在,它会抛出一个异常。

代码语言:javascript
复制
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);
            }                       
        }
    }
}
票数 22
EN

Stack Overflow用户

发布于 2014-04-02 03:28:01

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

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

代码语言:javascript
复制
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;
    }
}

此代码始终返回与“合并”对话框相同的列表。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4690217

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档