1

I am trying to pull commit changes via api and all i get is the path to the file itself, as in the whole file.
What I want to achieve is to see the changes (diff only) for a single file per commit.

E.g: If i query the same using Github i get the diff like so:
"@@ -1 +0,0 @@\n- console.log(\"Blasting!\")"

Is there a similar solution in Azure-devops?
Thanks!

Samion
  • 85
  • 1
  • 5

2 Answers2

0

There is a better way to get the result you are looking for. For each commit you can use the below API Doc

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

The response of the above API will contain an array named changes which will be a list of all the git objects that got changed by this commit.

Each change will look something like this

 {
  item: {
    path: string,
    objectId?: string,
    originalObjectId?: string,
    commitId: string,
    isFolder?: boolean,
    url: string,
  }),
  changeType: 'delete' | 'rename' | 'edit' | 'add' | 'delete, sourceRename' | 'edit, rename',
  sourceServerItem?: string,
}

Filter out all the folders and use the objectId and originalObjectId to fetch the file using the below API Doc

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

note: set Accept header to */*

In this way you will get the previous and the current version of the file

Using the git-diff package Doc you can get the line diff or char diff of the file

-1

What I want to achieve is to see the changes (diff only) for a single file per commit.

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}?api-version=5.0

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

Step3: You could use the Rest API to Get the file path.

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

Step4: 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:

enter image description here

Here is the ticket, you could refer to it.

Kevin Lu-MSFT
  • 20,786
  • 3
  • 19
  • 28