如何从TFS中的路径获取分支父级

时间:2018-08-02 11:28:39

标签: c# tfs team-project-collection

可以说以下是我的TFS结构:

  • 分支机构(文件夹)
    • Module1(文件夹)
      • Branch1(与父级Dev分支)
      • Branch2(与父Branch1分支)
      • Branch3(与父Branch1分支)
  • 开发(分支)

在代码中,我可以访问我的本地工作区以及一个Map[1][0]对象。

我想要一个OpenFileDialog OptimaExcel = new OpenFileDialog(); OptimaExcel.Title = "Optimas Excel wählen"; OptimaExcel.Filter = "Excel file|*.xlsx"; if (OptimaExcel.ShowDialog() == true) { Microsoft.Office.Interop.Excel.Application OptimaExcelApp; OptimaExcelApp = new Excel.Application(); OptimaExcelApp.Visible = true; //Später auf false object missing = System.Reflection.Missing.Value; string filepathname = myPublicPath + OptimaExcel.SafeFileName; Workbook OptimaWorkbook; OptimaWorkbook = OptimaExcelApp.Workbooks.Open(filepathname, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); Worksheet myBlockWorksheet; myBlockWorksheet = (Worksheet)OptimaWorkbook.Worksheets.Add(); myBlockWorksheet.Move(Missing.Value, OptimaWorkbook.Sheets[OptimaWorkbook.Sheets.Count]); myBlockWorksheet.Name = "Projektdaten"; 之类的方法 如下所示:

VersionControlServer

我目前有以下内容,认为它可以工作,但是没有(而且我也不诚实地期望它也可以工作)

string GetParentPath(string path)

2 个答案:

答案 0 :(得分:1)

您可以使用以下代码获取所有分支层次结构(父/子):(安装Nuget软件包Microsoft.TeamFoundationServer.ExtendedClient

using System;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace DisplayAllBranches
{
    class Program
    {
        static void Main(string[] args)
        {
            string serverName = @"http://ictfs2015:8080/tfs/DefaultCollection";

            //1.Construct the server object
            TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri(serverName));
            VersionControlServer vcs = tfs.GetService<VersionControlServer>();

            //2.Query all root branches
            BranchObject[] bos = vcs.QueryRootBranchObjects(RecursionType.OneLevel);

            //3.Display all the root branches
            Array.ForEach(bos, (bo) => DisplayAllBranches(bo, vcs));
            Console.ReadKey();
        }

        private static void DisplayAllBranches(BranchObject bo, VersionControlServer vcs)
        {
            //0.Prepare display indentation
            for (int tabcounter = 0; tabcounter < recursionlevel; tabcounter++)
                Console.Write("\t");

            //1.Display the current branch
            Console.WriteLine(string.Format("{0}", bo.Properties.RootItem.Item));

            //2.Query all child branches (one level deep)
            BranchObject[] childBos = vcs.QueryBranchObjects(bo.Properties.RootItem, RecursionType.OneLevel);

            //3.Display all children recursively
            recursionlevel++;
            foreach (BranchObject child in childBos)
            {
                if (child.Properties.RootItem.Item == bo.Properties.RootItem.Item)
                    continue;

                DisplayAllBranches(child, vcs);
            }
            recursionlevel--;
        }

        private static int recursionlevel = 0;
    }
}

enter image description here

答案 1 :(得分:0)

弄清楚了(感谢Andy Li-MSFTBranchObject类的帮助)

string GetParentPath(string path)
{
    BranchObject branchObject = versionControlServer.QueryBranchObjects(new ItemIdentifier(path), RecursionType.None).Single();
    if (branchObject.Properties.ParentBranch != null)
        return branchObject.Properties.ParentBranch.Item;
    else
        throw new Exception($"Branch '{path}' does not have a parent");
}

此外,如果要获取位于该分支内的文件/文件夹的父分支,则可以使用以下代码来获得该功能:

private string GetParentPath(string path)
{
    string modifyingPath = path;
    BranchObject branchObject = versionControlServer.QueryBranchObjects(new ItemIdentifier(modifyingPath), RecursionType.None).FirstOrDefault();
    while (branchObject == null && !string.IsNullOrWhiteSpace(modifyingPath))
    {
        modifyingPath = modifyingPath.Substring(0, modifyingPath.LastIndexOf("/"));
        branchObject = versionControlServer.QueryBranchObjects(new ItemIdentifier(modifyingPath), RecursionType.None).FirstOrDefault();
    }

    string root = branchObject?.Properties?.ParentBranch?.Item;
    return root == null ? null : $"{root}{path.Replace(modifyingPath, "")}";
}