-1

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

  • 分支(文件夹)
    • 模块 1(文件夹)
      • Branch1(带有父 Dev 的分支)
      • Branch2(带有父 Branch1 的分支)
      • Branch3(带有父 Branch1 的分支)
  • 开发(分公司)

在代码中,我可以访问我的本地工作区以及一个VersionControlServer对象。

我想要一个string GetParentPath(string path) 像下面这样的方法:

GetParentPath("$/Branches/Module1/Branch1"); // $/Dev
GetParentPath("$/Branches/Module1/Branch2"); // $/Branches/Module1/Branch1
GetParentPath("$/Branches/Module1/Branch3"); // $/Branches/Module1/Branch1
GetParentPath("$/Dev"); // throws an exception since there is no parent

我目前有以下内容,认为它有效,但它没有(老实说我也不希望它有效)

private string GetParentPath(string path)
{
    return versionControlServer.QueryMergeRelationships(path)?.LastOrDefault()?.Item;
}
4

2 回答 2

2

您可以使用以下代码获取所有分支层次结构(父/子):(安装 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;
    }
}

在此处输入图像描述

于 2018-08-03T08:54:29.847 回答
1

想通了(感谢Andy Li-MSFT在课堂上戳我的大脑BranchObject):

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, "")}";
}
于 2018-08-03T10:51:41.200 回答