Search code examples
azure-devops

Is there a way to get the equivalent of a git diff on a pull request in Azure DevOps?


GitHub has and endpoint like this https://api.github.com/repos/{orgName}/{repoName}/pulls/{prId} that will return a git diff on a pull request if you set the Accept request header to application/vnd.github.diff

diff --git a/README b/README
index c57eff55..980a0d5f 100644
--- a/README
+++ b/README
@@ -1 +1 @@
-Hello World!
\ No newline at end of file
+Hello World!

Does Azure DevOps have a similar endpoint that I can request to get the git diff? If not, are there any ideas to how I could possible generate the diff?

I've tried different things without success. For example as suggested here https://techcommunity.microsoft.com/discussions/azure/azure-git-get-the-pr-difference-in-global-diff-format/4294686 but ended up with error messages like "The controller for path '/xxx/_apis/git/repositories/yyy/pullRequests/zzz/diffs' was not found or does not implement IController."

And the MS documentation for the rest API does not seem to have an example that returns the diff, but only a list of changed files https://learn.microsoft.com/en-us/rest/api/azure/devops/git/diffs/get?view=azure-devops-rest-7.1&tabs=HTTP#examples


Solution

  • There is not such an endpoint in ADO.

    Here's is a workaround that I have made.

    If the server, running the code has git, I just use the source and target commit id's to run a git diff

    // Get the commit id's
    var sourceCommitId = pullRequest.LastMergeSourceCommit.CommitId;  
    var targetCommitId = pullRequest.LastMergeTargetCommit.CommitId;
    
    // And then run git diff...
    var commitRange = $"{targetCommitId}..{sourceCommitId}";
    var process = new Process
    {
      StartInfo = new ProcessStartInfo
      {
        FileName = "git",
        Arguments = $"diff {commitRange}",
        RedirectStandardOutput = true,
        RedirectStandardError = true,
        UseShellExecute = false,
        CreateNoWindow = true,
        WorkingDirectory = targetFolder
        }
    };
               
    process.Start();
    var gitDiff = process.StandardOutput.ReadToEnd();
    process.WaitForExit();
    

    The above assumes that git is on the computer and you've first made a git clone on the repository