Reputation: 4117
I have a commit id and need to check in which branch(es) the commits contains. I've tried some approaches, but no one fits...
My best idea was:
var existsInBranch = (await gitClient.GetCommitsAsync(projectId, repositoryId, new GitQueryCommitsCriteria
{
Ids = new List<string>
{
commits[0].CommitId
},
CompareVersion = new GitVersionDescriptor()
{
VersionType = GitVersionType.Branch,
VersionOptions = GitVersionOptions.None,
Version = "main"
}
})).Any();
But this will end with an exception: The value specified for the following variable must be null: compareVersion.
.
Are there some other ways to get the information? The Azure DevOps UI has this feature:
Upvotes: 0
Views: 1677
Reputation: 35194
The feature: "Search for commit in branches and tags" exists in the preview features. Based on my test(check with backend API in Browser), I am afraid there is no direct way to achieve this function outside the UI interface
Are there some other ways to get the information?
Of course. I would like to share two methods:
1.You could use PowerShell to Run the RestApis and compare the result:
Here is a PowerShell script example:
$token = "PAT"
$url="https://dev.azure.com/{OrganizationName}/_apis/git/repositories/{RepoId}/refs?api-version=6.0"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$id= "commitid"
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get -ContentType application/json
ForEach($Branch in $response.value.name)
{
$Branch -match "refs/heads/(?<content>.*)"
$BranchName = $matches['content']
echo $BranchName
$url1= "https://dev.azure.com/{OrganizationName}/{ProjectNAME}/_apis/git/repositories/{RepoID}/commits?searchCriteria.itemVersion.version=$($BranchName)&api-version=6.0"
$response2 = Invoke-RestMethod -Uri $url1 -Headers @{Authorization = "Basic $token"} -Method Get -ContentType application/json
if ($response2.value.commitid.contains($id))
{
echo "$BranchName contains the commit"
}
else
{
echo "$BranchName doesn't contains the commit"
}
}
Result:
2.You could use git command to check if the branch contain the commits.
git clone --mirror GitURL
git branch --contains Commitid
Result:
Upvotes: 1