Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically get information about branches in TFS?

Tags:

.net

tfs

tfs-sdk

I need to programmatically find out information about branches in TFS. For instance the main thing i am interested is given root folder $/MyProject/Project1 I neeed to find out what other folders have been branched from it. I am just after the right API methods.

Say i have connection to TFS server and have access to VersionControlServer and Workspace class instances.

like image 838
Captain Comic Avatar asked Jan 14 '11 10:01

Captain Comic


People also ask

How do I create a branch from an existing branch in TFS?

In Source Control Explorer, right-click the folder or file that you want to branch, point to Branching and Merging, and then click Branch. The Branch dialog box appears. In the Target box, modify the location and name for the new branch.


1 Answers

Ok, this was both easier and more difficult than I thought it would be. I was able to pull this together from a few different sources, but this seems to work. I will warn you, there's no error handling here, and if the itemSpec doesn't exist, it bombs with an exception.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;

static void Main(string[] args)
{
    TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(
                                            new Uri("http://tfs:8080"));    
    string srcFolder = "$/ProjectName";    
    var versionControl = tfs.GetService<VersionControlServer>();    
    ItemSpec[] specs = new ItemSpec[]{new ItemSpec(srcFolder, RecursionType.None)};

    System.Console.WriteLine(string.Format("Source folder {0} was branched to:",
                                           srcFolder));    
    BranchHistoryTreeItem[][] branchHistory =
        versionControl.GetBranchHistory(specs, VersionSpec.Latest);

    foreach (BranchHistoryTreeItem item in branchHistory[0][0].Children)
    {
        ShowChildren(item);
    } 

    System.Console.WriteLine();
    System.Console.WriteLine("Hit Enter to continue");
    System.Console.ReadLine();    
}

static void ShowChildren(BranchHistoryTreeItem parent)
{
    foreach (BranchHistoryTreeItem item in parent.Children)
    {
        System.Console.WriteLine(
            string.Format("Branched to {0}", 
                          item.Relative.BranchToItem.ServerItem));
        if (item.Children.Count > 0)
        {
            foreach(BranchHistoryTreeItem child in item.Children)
            {
                ShowChildren(child);
            }                       
        }
    }
}
like image 110
Robaticus Avatar answered Sep 17 '22 13:09

Robaticus