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

How do I get a branches parent from a path in TFS

假设以下是我的 TFS 结构:

在代码中,我可以访问我的本地工作区以及一个 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;
}

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

想出来了(感谢 Andy Li-MSFTBranchObject class 戳我的脑袋):

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

此外,如果您想获取位于该分支内的 file/folder 的父分支,您可以使用以下代码来获取该功能:

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