简体   繁体   中英

Is there a way to get the raw diff of a commit via the Azure Devops API?

As part of our application, we're building an ability to integrate with Azure DevOps' REST API. One key component that we're interested in is being able to see actual diffs of specific commits, so that we can look at and analyze the line content. We've already created this integration for GitHub, GitLab, and Bitbucket, and each time it was easy: There's a fairly simple diff endpoint for each that takes in a specific commit ID and diffs it (sometimes with a specific parent commit).

I've not had much luck finding this same functionality in Azure DevOps, however: The diffs endpoint has some data related to this, but it is really just an overview of which files changed and the high-level nature of those changes, along with the IDs of specific blobs that represent the files in each state (before and after).

It's theoretically possible to use those blobs to manually construct what I'm after, and indeed I've been able to query for the before and after blobs to get a diff on each file. But that's two separate endpoint queries per file -- take a twenty-file commit, and suddenly we'd need 40 API calls just to construct a reasonable diff. That doesn't really fit our performance needs, unfortunately.

Is there a separate API endpoint or technique that lets us get to the raw diff? It doesn't need to be a raw diff a la git diff directly, just anything that lets us see the before and after state of each line (rather than each file) with minimal API calls (preferably just one). I've done much scouring through the docs and here on StackOverflow, and not found anything that accomplishes this.

There is no existing Rest API to meet your needs. But you could refer to the following steps to get the content of the git diff.

Step1: You could use the Rest API to get the commit id .

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/commits?api-version=5.0

Step2: You could use the Rest API to get the commit by commit id .

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/commits/{commitId}?changeCount={changeCount}&api-version=5.0

In the Rest API Result, you need to record the value of “parentsid”, “path” .

Step3: You could use the following API to get the diff content.

Post https://dev.azure.com/Organization/Project /_api/_versioncontrol/fileDiff?__v=5&diffParameters={value}&repositoryId={repositoryid}

The {value} is Json type.

Here is an example:

{"originalPath":"filepath","originalVersion":"Parentsid","modifiedPath":"filepath","modifiedVersion":"commitid","partialDiff":true,"includeCharDiffs":true}

You could add the value to the API URL.

Then run the API and the result will contains the git diff content. (2 means remove, 1 means add)

Here is a result sample:

API 结果

This is the ticket I refer to, hope it helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM