Azure Devops API: Find a Git commit by Sha1 prefix, branch name or tag name?
Azure DevOps has a Search box on the commits page in the UI. Using that with a partial hash gives the following query criteria. It uses the GET Commit REST method:
{
"fromCommitId": "b86c400000000000000000000000000000000000",
"toCommitId": "b86c4fffffffffffffffffffffffffffffffffff",
"skip": 0,
"maxResultCount": 25
}
Or set the searchCriteria.itemVersion.version
to the exact name and searchCriteria.itemVersion.versionType
to branch
or tag
or commit
to search for a specific commit in case you know the full name (branch, tag or full commit id).
I used the Chrome Dev Tools to capture the request. Worst case the short hash isn't unique and it will return multiple options.
For branches and tags you can also use the refs
API, which has a start-with and a contains option to find the ref you're looking for.
Example:
GET https://dev.azure.com/fabrikam/_apis/git/repositories/{repositoryId}/refs?filter=heads/&filterContains=replacer&api-version=6.0
Search for filter=heads/
for branches and filter=tags/
for tags, or leave the main filter empty to search both.
In theory this API can return multiple objects, since it uses startsWith
for the filter
and contains
for the filterContains
.