使用 Azure Devops API 获取 Git 提交(差异)的实际内容

问题描述 投票:0回答:1

我在 azure devops git 存储库中列出 git 提交,以检查是否有任何提交违反了一些简单的原则。

但是,似乎 GitItem.Content 和 GitChange.NewContent 都是 null。

它们从未被我的代码填充,所以我无法获得实际的更改。

以下代码没有输出任何内容。

如何获取变更内容?

另外,我做事的方式是否过于复杂? (我的代码导致大量 API 调用)。

    private void GetGitChanges()
    {
      var organization = "org";
      var project = "proj";
      var repositoryId = "repo";

      var uri = $"https://tfs.foobar.se/tfs/{organization}/";
      var pat = ConfigurationManager.AppSettings["PAT"];
      using (var conn = new VssConnection(new Uri(uri), new VssBasicCredential("", pat)))
      {
        var gitClient = conn.GetClient<GitHttpClient>();
        var repo = gitClient.GetRepositoryAsync(project, repositoryId).WaitForResult();
        var gitCommits = new List<GitCommitRef>();

        var searchCriteria = new GitQueryCommitsCriteria
        {
          IncludeWorkItems = true,
          ItemVersion = new GitVersionDescriptor
          {
            VersionType = GitVersionType.Branch,
            VersionOptions = GitVersionOptions.FirstParent,
            Version = "master"
          }
        };

        var commits = gitClient.GetCommitsAsync(project, repo.Id.ToString(), searchCriteria).WaitForResult();

        foreach(var commit in commits)
        {
          var c2 = gitClient.GetCommitAsync(project: project, commitId: commit.CommitId, repositoryId: repositoryId)
              .WaitForResult();
          if (c2.Parents?.Any() ?? false)
          {
            var parentCommitId = c2.Parents.First();

            var diff = gitClient.GetCommitDiffsAsync(repositoryId: repositoryId, project: project, diffCommonCommit: false,
                targetVersionDescriptor: new GitTargetVersionDescriptor { Version = commit.CommitId, VersionType = GitVersionType.Commit, VersionOptions = GitVersionOptions.None }, 
                baseVersionDescriptor: new GitBaseVersionDescriptor { Version = parentCommitId, VersionType = GitVersionType.Commit, VersionOptions = GitVersionOptions.None }).WaitForResult();

            foreach(var change in diff.Changes)
            {
              if (change.Item.Content != null)
              {
                Debug.WriteLine(change.Item.Content);
              }
              if (change.NewContent != null)
              {
                Debug.WriteLine(change.NewContent);
              }
            }
          }
        }
      }

编辑

ChatGPT 建议我应该将参数

includeContent: true
传递给
GetCommitDiffsAsync
,但是没有这样的参数(或任何看起来像它的参数)

git azure-devops tfs
1个回答
0
投票

你找到相关的东西了吗?我正在尝试类似地获取两次提交之间的文件的 git diff。

© www.soinside.com 2019 - 2024. All rights reserved.