0

我想要做的是获得可以合并到的可用分支。与您在 TFS2008 中说合并并选择目标分支时的下拉列表几乎相同。

但是,很难找到方法。

下面的代码是我在网络上找到的一些资源之间的合并,但似乎没有一个有效。我的猜测是,如果 VS2008 可以做到,我可以通过 SDK 做到,对吧?

下面的代码总是给我同样的结果。

我的存储库是这样的:

Development
  Version1
    Code
  Version2
    Code
  Version3
    Code
Main
  Code

通常我会分支 Main > Version X。因此可以将 Main > Version X 和 Version X 合并到 Main。

下面的代码总是给我 Main 的孩子,即使我使用 Version3 文件夹查询 (tfsParentBranchPath)。

这是因为也许我使用了 TFS2010 Web 服务但指向 TFS2008(这就是为什么我标记了一些在代码中不起作用的方法)?

好吧,如果有人知道答案,请告诉我。

谢谢!

 public string[] GetChildBranchesToMerge(string tfsParentBranchPath)
    {
    var versionControl = teamFoundationServer.GetService<VersionControlServer>();

    //not supported by tfs2008
    //ItemIdentifier[] identifiers = versionControl.QueryMergeRelationships(tfsParentBranchPath);
    //var allBranches = versionControl.QueryBranchObjects(new ItemIdentifier(tfsParentBranchPath), RecursionType.OneLevel);

    List<string> childs = new List<string>();
    ItemSpec[] specs = new ItemSpec[] { new ItemSpec(tfsParentBranchPath, RecursionType.OneLevel) };
    BranchHistoryTreeItem[][] branchHistoryTree = versionControl.GetBranchHistory(specs, VersionSpec.Latest);

    if (branchHistoryTree.Length > 0 && branchHistoryTree[0].Length > 0)
    {
        var treeItem = branchHistoryTree[0][0];

        if (treeItem.Children.Count > 0)
        {
            foreach (BranchHistoryTreeItem tia in treeItem.Children)
            {
                childs.Add(tia.Relative.BranchToItem.ServerItem);
            }
        }
    }

    return childs.OrderBy((s) =>
    {
        return s;
    }).ToArray();
}
4

1 回答 1

0

最后,这是最后一个有效的版本!

首先,我停止使用 VS2010 程序集,现在它更快了。

其次,最大的区别是这一行:

var treeItem = branchHistoryTree[0][0].GetRequestedItem();

我真的不知道为什么会有区别,但似乎没有那个方法,返回的项目是一个通用的。

public string[] GetChildBranchesToMerge(string tfsParentBranchPath)
{
    DoLog("Getting child branches for {0} ...", tfsParentBranchPath);
    VersionControlServer vcs = (VersionControlServer)teamFoundationServer.GetService(typeof(VersionControlServer));

    List<string> childs = new List<string>();
    ItemSpec[] specs = new ItemSpec[] { new ItemSpec(tfsParentBranchPath, RecursionType.OneLevel) };
    BranchHistoryTreeItem[][] branchHistoryTree = vcs.GetBranchHistory(specs, VersionSpec.Latest);

    if (branchHistoryTree.Length > 0 && branchHistoryTree[0].Length > 0)
    {
        var treeItem = branchHistoryTree[0][0].GetRequestedItem();
        AddChildBranch(childs, treeItem, tfsParentBranchPath);

        if (treeItem.Children != null && treeItem.Children.Count > 0)
        {
            foreach (BranchHistoryTreeItem tia in treeItem.Children)
            {
                AddChildBranch(childs, tia, tfsParentBranchPath);
            }
        }
    }

    DoLog("{0} child branches found", childs.Count);

    return childs.OrderBy((s) =>
    {
        return s;
    }).ToArray();
}

private void AddChildBranch(List<string> list, BranchHistoryTreeItem itemToCheck, string tfsParentBranchPath)
{
    if (itemToCheck.Relative.BranchFromItem != null && itemToCheck.Relative.BranchFromItem.DeletionId == 0 && itemToCheck.Relative.BranchFromItem.ServerItem != tfsParentBranchPath)
        list.Add(itemToCheck.Relative.BranchFromItem.ServerItem);

    if (itemToCheck.Relative.BranchToItem != null && itemToCheck.Relative.BranchToItem.DeletionId == 0 && itemToCheck.Relative.BranchToItem.ServerItem != tfsParentBranchPath)
        list.Add(itemToCheck.Relative.BranchToItem.ServerItem);
}
于 2012-11-16T18:25:51.740 回答