Lines of Code modified in each Commit in TFS rest API. How do I get?

There isn’t officially released REST API to do that. But you can refer to these steps to achieve your requirements.

  1. Get a list of commits to get a commit’s commitId.
  2. Get a specific commit with its commitId to get parents value and repository id (The value at the end of _links>Repository>href) (Using the URL of _links>Changes>href can get file path if you don’t know)
  3. Get file diff by this POST request https://[account].visualstudio.com/[team project name] /_api/_versioncontrol/fileDiff?__v=5&diffParameters=[data 1]&repositoryId=[repository id]

The [data 1] value should be formatted with JSON and have the following values (remove whitespace):

{
    "originalPath":"/index.html",
    "originalVersion":"GC[a parent value, step 2]",
    "modifiedPath":"/index.html(path: step 2)",
    "modifiedVersion":"GC[commit id]",
    "partialDiff":true,
    "includeCharDiffs":true
}

The result contains this (you need to calculate the items that changeType isn’t equal to 0, 2 means remove, 1 means add):

{
    "changeType": 2,
    "mLine": 9,
    "mLines": [],
    "mLinesCount": 0,
    "oLine": 9,
    "oLines": [
      "    <!-- Polyfill(s) for older browsers -->"
    ],
    "oLinesCount": 1
  },
{
    "changeType": 1,
    "mLine": 22,
    "mLines": [
      "      <div>2</div>"
    ],
    "mLinesCount": 1,
    "oLine": 23,
    "oLines": [],
    "oLinesCount": 0
}

You can capture the request URL of a commit (History > Commits > Select a commit) by using Developer Tools Network capture.

Leave a Comment