【问题标题】:Azure DevOps REST Api: Does git commit exists in branch?Azure DevOps REST Api:分支中是否存在 git commit?
【发布时间】:2021-02-28 19:31:03
【问题描述】:

我有一个提交 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 具有此功能:

【问题讨论】:

    标签: azure-devops azure-devops-rest-api


    【解决方案1】:

    预览功能中存在“在分支和标签中搜索提交”功能。根据我的测试(用Browser中的后端API检查),恐怕在UI界面之外没有直接的方法来实现这个功能

    还有其他获取信息的方法吗?

    当然。我想分享两种方法:

    1.您可以使用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.你可以使用git命令检查分支是否包含提交。

    git clone --mirror GitURL
    
    git branch --contains Commitid
    

    结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-07
      • 1970-01-01
      • 2021-10-30
      • 2022-07-18
      • 2020-06-12
      • 1970-01-01
      • 2021-05-19
      相关资源
      最近更新 更多