Get all TFS Branches programmatically in C'#

2019-03-04 18:06发布

I'm trying to programmatically get a list of all the branches in TFS for Visual Studio 2013. After doing some research I found this blog that details how to get the branches:

Displaying all branch hierarchies in TFS 2010

I modified the code to instead store everything in a list.

private void Setup()
    {
        string serverName = "serverName"; //in the code this is set to the actual server name
        TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri(serverName));
        VersionControlServer vcs = tfs.GetService<VersionControlServer>();

        var bos = vcs.QueryRootBranchObjects(RecursionType.OneLevel);
        Array.ForEach(bos, (bo) => DisplayAllBranches(bo, vcs));
    }

private void DisplayAllBranches(BranchObject bo, VersionControlServer vcs)
    {
        _listOfBranches.Add(bo.Properties.RootItem.Item);
        var childBos = vcs.QueryBranchObjects(bo.Properties.RootItem, RecursionType.OneLevel);
        foreach (var child in childBos)
        {
            if (child.Properties.RootItem.Item == bo.Properties.RootItem.Item)
                continue;

            DisplayAllBranches(child, vcs);
        }
    }

The problem I'm having is that the BranchObjects[] bos is always empty. Is there something I'm missing or is there a better way to get a list of all the branches?

标签: c# tfs
3条回答
姐就是有狂的资本
2楼-- · 2019-03-04 18:14

Can you verify that vcs object is valid? Can you perform some other operation, e.g. getitems? Are you sure you have branches in your repository? Please note that "branch ocjects" were added in tfs 2010 and they arenot equal to just branch operation. In source control explorer they have grey icon, they can be created with context menu

查看更多
欢心
3楼-- · 2019-03-04 18:27

That code should work fine (works for me). I suspect that you are missing your collection name from your server name variable?

If you have a reasonably standard setup, your serverName variable format should be: http://ServerName:Port/tfs/CollectionName so for example: http://tfsServer01:8080/tfs/MyCollection

查看更多
不美不萌又怎样
4楼-- · 2019-03-04 18:36

After doing some more research I figured out the problem. Our TFS is structured similarly to the following:

$/Root
$/Root/Folder
$/Root/Folder/Branch

The first level down would not return any Branches because there aren't any. So Instead I used a simpler approach that gets all of the Branches.

var branchObjects = vcs.QueryRootBranchObjects(RecursionType.Full);

        foreach (var branch in branchObjects)
        {           
            var branchName = branch.Properties.RootItem.Item;
            var parentFolder = branchName.Substring(0, branchName.LastIndexOf('/'));

            if (!_listOfBranches.Contains(parentFolder))
            {
                _listOfBranches.Add(parentFolder);
            }
            _listOfBranches.Add(branchName);
        }
        _listOfBranches.Sort();

It's not the cleanest approach at the moment. But it's able to retrieve all the Branches for the specified project, as well as the Parent Folders (excluding the Root, for now).

查看更多
登录 后发表回答