如何以编程方式获取 TFS 项目下的分支列表?

How to get list of branches under TFS project programatically?

需要获取一个Team project collection或Team project下的分支列表,需要写在一些txt或csv文件中。我正在使用 TFS 2015。

有没有可用的脚本?最好是 Powershell 脚本

提前致谢, 迪内什。

RESTAPI和ClientAPI都可以实现你所需要的。您也可以在 Powershell 脚本中使用它们。

使用休息API-Get a branch

 GET https://{instance}/DefaultCollection/_apis/tfvc/branches/{path}?api-version={version}[&includeChildren={bool}&includeParent={bool}&includeDeleted={bool}]

使用客户端API

通过 C# 使用 Powershell

# Define parameters 
$tfsCollectionUrl = New-Object System.URI("http://ditfs.cloudapp.net:8080/tfs/disample");

# Load Client Assembly 
[Reflection.Assembly]::Load(“Microsoft.TeamFoundation.Client, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”); 
[Reflection.Assembly]::Load(“Microsoft.TeamFoundation.VersionControl.Client, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”); 
[Reflection.Assembly]::Load(“Microsoft.TeamFoundation.Common, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”); 
[Reflection.Assembly]::Load(“Microsoft.TeamFoundation.Lab.Client, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”); 
[Reflection.Assembly]::Load(“Microsoft.TeamFoundation.Lab.Common, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”);  

# Connect to tfs 
$tfsCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($tfsCollectionUrl); 
$projectService = $tfsCollection.GetService([Microsoft.TeamFoundation.Server.ICommonStructureService]);
$versionControl = $tfsCollection.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer]);

# Query all the projects and branches
$projects = $projectService.ListAllProjects();
$objallbranch = $versionControl.QueryRootBranchObjects("full")

foreach ($project in $projects) 
{ 
  Write-Host Finding environments for project $project.Name
   foreach ($branchObject in $objallbranch)
               {
                   if ($branchObject.Properties.RootItem.Item.ToUpper().Contains($project.Name.ToUpper()))
                   {
                       write-host $branchObject
                   }
                   }

}

Source code 来自 GitHub