2012-11-15 54 views
0

我想要做的是获得可合并的可用分支。与TFS2008中的合并并选择目标分支时,几乎与下拉菜单相同。TFS SDK:获取子分支

然而,它很难找到如何。

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

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

我的资料库是这样的:

Development 
    Version1 
    Code 
    Version2 
    Code 
    Version3 
    Code 
Main 
    Code 

,通常我汊主>版本X.因此,合并可以做到主营>版本X和版本X主。

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

这是因为也许我使用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(); 
} 

回答

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