Azure DevOps REST Api:分支中是否存在 git 提交?

Azure DevOps REST Api: Does git commit exists in branch?

我有一个提交 ID,需要检查提交包含哪个分支。我尝试了一些方法,但没有一个适合...

我最好的主意是:

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();

但这将以异常结束:The value specified for the following variable must be null: compareVersion.

还有其他获取信息的方法吗? Azure DevOps UI 具有以下功能:

预览功能中存在“在分支和标签中搜索提交”功能。根据我的测试(在浏览器中检查后端API),恐怕没有直接的方法可以在UI接口

之外实现这个功能

Are there some other ways to get the information?

当然可以。我想分享两个方法:

1.You 可以使用 PowerShell 运行 RestApis 并比较结果:

Refs - List

Commits - Get Commits

这是一个 PowerShell 脚本示例:

$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"
   
   }


}

结果:

2.You 可以使用 git 命令来检查分支是否包含提交。

git clone --mirror GitURL

git branch --contains Commitid

结果: