7

For a file in the master branch I can use the following URL - http://tfsserver:8080/tfs/DefaultCollection/TFSProject/_apis/git/repositories/GitRepo/items?path=FilePath&api-version=4.1

But what if I need to download a file from the branch?

P.S.

I know perfectly well that I can clone a git repository. I need REST API specifically.

EDIT 1

So, I tried the following code:

$Url = "http://tfsserver:8080/tfs/DefaultCollection/code/_apis/git/repositories/xyz/itemsbatch?api-version=4.1"
$Body = @"
{
    "itemDescriptors": [
    {
        "path": "/Bootstrap.ps1",
        "version": "shelve/vsts",
        "versionType": "branch"
    }]
}
"@
Invoke-RestMethod -UseDefaultCredentials -Uri $Url -OutFile $PSScriptRoot\AutomationBootstrapImpl.ps1 -Method Post -ContentType "application/json" -Body $Body

It succeeds, but the generated file is not exactly what I expected:

{"count":1,"value":[[{"objectId":"ceb9d83e971abdd3326d67e25b20c2cb1b4943e2","gitObjectType":"blob","commitId":"d4a039914002613e775f6274aee6489b244a42a7","path":"/bootstrap.ps1","url":"http://tfsserver:8080/tfs/DefaultCollection/code/_apis/git/repositories/xyz/items/bootstrap.ps1?versionType=Branch&version=shelve%2Fvsts&versionOptions=None"}]]}

However, it gives the URL I can use to get the file from branch - http://tfsserver:8080/tfs/DefaultCollection/code/_apis/git/repositories/xyz/items/bootstrap.ps1?versionType=Branch&version=shelve%2Fvsts&versionOptions=None

So, here we go:

$Url = "http://tfsserver:8080/tfs/DefaultCollection/code/_apis/git/repositories/xyz/items/bootstrap.ps1?versionType=Branch&version=shelve/vsts"
Invoke-RestMethod -UseDefaultCredentials -Uri $Url -OutFile $PSScriptRoot\AutomationBootstrapImpl.ps1

And it works as expected. But I still do not know what is the name of this API method?

mark
  • 59,016
  • 79
  • 296
  • 580

1 Answers1

9

The GetItems Batch API does include a versionType of type GitVersionType:

Version type (branch, tag, or commit). Determines how Id is interpreted

So if you add to your REST API URL the attributes:

?versionType=Branch&version=myBranch

That should be enough to get the items from a specific branch

As the OP mentions, it gives an intermediate URL which points to:

http://tfsserver:8080/tfs/{organization}/{project}/_apis/git/repositories/{repositoryId}/items/{path}?versionType=Branch&version=myBranch

That means:

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Please, see my edit. Although not really it, I will accept it, since it does lead to the answer. Still, if you could modify it and mention the API method that does it, it would be terrific. – mark Jan 18 '19 at 04:38
  • @mark I have edited the answer accordingly, and added a link to the relevant API. – VonC Jan 18 '19 at 07:27
  • Thank you. I hope nobody will be confused by you mentioning the concrete values that are specific to my particular environment, like `code`, `xyz`, `bootstrap.ps1` and `shelve/vsts` – mark Jan 18 '19 at 16:45
  • @mark Good point. I have edited the question to only use placeholders, instead of the specific values. – VonC Jan 18 '19 at 16:48