问题
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?
回答1:
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:
- it uses as expected
?versionType=Branch&version=myBranch
- But it uses _apis/git/repositories/{repositoryId}/items Items Get API, not
_apis/git/repositories/{repositoryId}/itemsbatch
来源:https://stackoverflow.com/questions/54228312/how-to-download-a-file-in-a-branch-from-a-git-repo-using-azure-devops-rest-api