You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何对比Azure仓库分支并通过Azure DevOps API或C#获取超前滞后提交

Azure DevOps仓库两分支超前/滞后提交记录获取方案

方案1:基于Azure DevOps REST API实现

  • 前置准备:提前创建拥有Code > Read权限的个人访问令牌(PAT)
  • 调用接口:
GET https://dev.azure.com/{组织名}/{项目名}/_apis/git/repositories/{仓库ID}/diffs/commits?api-version=7.1-preview.1&baseVersionType=branch&baseVersion={基准分支名}&targetVersionType=branch&targetVersion={对比分支名}
  • 请求头要求:添加Authorization字段,值为Basic {Base64编码的任意字符串:PAT},注意需要先将任意字符串:PAT拼接后再做Base64编码
  • 返回参数说明:
    • aheadCount:对比分支相较于基准分支的超前提交数量
    • behindCount:对比分支相较于基准分支的滞后提交数量
    • aheadCommits数组:所有超前的提交详情
    • behindCommits数组:所有滞后的提交详情

方案2:基于C#实现

优先使用微软官方的Azure DevOps .NET客户端SDK实现,步骤如下:

  1. 安装依赖NuGet包
Install-Package Microsoft.TeamFoundationServer.Client
  1. 代码示例
using Microsoft.TeamFoundation.SourceControl.WebApi;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;

namespace AzureDevOpsBranchDiff
{
    class Program
    {
        static async Task Main(string[] args)
        {
            // 替换为自身环境参数
            string orgUrl = "https://dev.azure.com/你的组织名称";
            string projectName = "你的项目名称";
            string repositoryId = "你的仓库ID";
            string baseBranchName = "main"; // 基准分支
            string targetBranchName = "dev"; // 待对比分支
            string pat = "你的个人访问令牌";

            // 初始化Azure DevOps连接
            var credentials = new VssBasicCredential(string.Empty, pat);
            using var connection = new VssConnection(new Uri(orgUrl), credentials);
            var gitClient = connection.GetClient<GitHttpClient>();

            // 构造版本描述
            var baseVersion = new GitVersionDescriptor
            {
                Version = baseBranchName,
                VersionType = GitVersionType.Branch
            };
            var targetVersion = new GitVersionDescriptor
            {
                Version = targetBranchName,
                VersionType = GitVersionType.Branch
            };

            // 查询分支差异
            var diffResult = await gitClient.GetCommitDiffsAsync(
                project: projectName,
                repositoryId: repositoryId,
                baseVersion: baseVersion,
                targetVersion: targetVersion
            );

            // 输出结果
            Console.WriteLine($"分支 {targetBranchName} 相对于 {baseBranchName}:");
            Console.WriteLine($"超前提交数: {diffResult.AheadCount}");
            Console.WriteLine($"滞后提交数: {diffResult.BehindCount}");

            Console.WriteLine("\n超前提交记录:");
            foreach (var commit in diffResult.AheadCommits)
            {
                Console.WriteLine($"- [{commit.CommitId[..8]}] {commit.Comment?.Trim()} 提交人: {commit.Author.Name}");
            }

            Console.WriteLine("\n滞后提交记录:");
            foreach (var commit in diffResult.BehindCommits)
            {
                Console.WriteLine($"- [{commit.CommitId[..8]}] {commit.Comment?.Trim()} 提交人: {commit.Author.Name}");
            }
        }
    }
}

注意事项

  • 个人访问令牌必须授予代码读取权限,否则会触发403无权访问错误
  • 仓库ID可在Azure DevOps仓库的「设置 - 仓库设置」页面直接获取
  • 若仅需要统计超前/滞后数量,无需获取提交详情,可直接取返回结果的AheadCount和BehindCount字段即可,无需遍历提交列表降低接口开销

内容的提问来源于stack exchange,提问作者Nithya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 21:18:02