Wednesday, August 1, 2012

TFS Queries: Listing all the branches

For an intro on LinqPad and the TFS API please read this post
For the list of all the posts in this series please read this one

Context


When it comes to branches, using the TFS Web Access or Team Explorer we can navigate around the source code explorer to see branches from various Team Project and related information. But to get the big picture there is an easier way using the TFS API.

Required references


Microsoft.TeamFoundation.Client
Microsoft.TeamFoundation.Common
Microsoft.TeamFoundation.VersionControl.Client

Query (C# Statements)

var tfs = TfsTeamProjectCollectionFactory.
    GetTeamProjectCollection(new Uri("http://localhost:8088/tfs"));

tfs.EnsureAuthenticated();

var versionControl = tfs.GetService<VersionControlServer>();

versionControl.QueryRootBranchObjects(RecursionType.Full)
    .Where(b => !b.Properties.RootItem.IsDeleted)
    .Select(s => new
    {
        Project = s.Properties.RootItem.Item
            .Substring(0, s.Properties.RootItem.Item.IndexOf('/', 2)),
        Properties = s.Properties,
        DateCreated = s.DateCreated,
        ChildBranches = s.ChildBranches
    })
    .Select(s => new 
    {
        s.Project,
        Branch = s.Properties.RootItem.Item.Replace(s.Project, ""),
        Parent = s.Properties.ParentBranch != null ?
            s.Properties.ParentBranch.Item.Replace(s.Project, "") : "",
        Version = (s.Properties.RootItem.Version as ChangesetVersionSpec)
            .ChangesetId,
        DateCreated = s.DateCreated,
        Owner = s.Properties.Owner,
        ChildBranches = s.ChildBranches
            .Where (cb => !cb.IsDeleted)
            .Select(cb => new
            {
                Branch = cb.Item.Replace(s.Project, ""),
                Version = (cb.Version as ChangesetVersionSpec).ChangesetId
            })
    })
    .OrderBy(s => s.Project).ThenByDescending(s => s.Version)
    .Dump();

Result


Here we can see a bit more information on the branches like related parent and child branches in a simple list.


IOrderedEnumerable<> (4 items)
Project Branch Parent Version DateCreated Owner ChildBranches
$/Project1 /Release v1.1.0.0 /Main 14 04/07/2012 9:18:01 PM Domain\Pascal
IEnumerable<> (0 items)
$/Project1 /Main   8 28/06/2012 10:24:05 PM Domain\Pascal

IEnumerable<> (1 item)
Branch Version
/Release v1.1.0.0 14
$/Project2 /Team /Main 13 04/07/2012 9:17:24 PM Domain\Pascal
IEnumerable<> (0 items)
$/Project2 /Main   12 04/07/2012 9:17:02 PM Domain\Pascal

IEnumerable<> (1 item)
Branch Version
/Team 13
47

No comments: