7

In TFS API only allow getting the modified file count only. But needed to get each file modified line count (added/deleted) for each commit in the rest call.

fatihyildizhan
  • 8,614
  • 7
  • 64
  • 88

1 Answers1

9

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.

fatihyildizhan
  • 8,614
  • 7
  • 64
  • 88
starian chen-MSFT
  • 33,174
  • 2
  • 29
  • 53
  • I coundn't understand the 3rd step.Can u pls explain what is the url for POST request and Request body and query parameter if it have. – bhuvaneswari kalimuthu Jan 24 '17 at 10:09
  • @bhuvaneswarikalimuthu There isn't the request body, just in query parameter. I have already explain the parameters (?__v=5&diffParameters=[data 1]&repositoryId=[repository id]), the first one (data 1) is the JSON data, there is the sample in my answer, and the other parameter is repository id. What's parameter you unclear? – starian chen-MSFT Jan 25 '17 at 03:32
  • Thanks..Working...But this api is not mentioned anywhere in the TFS rest api reference. – bhuvaneswari kalimuthu Jan 31 '17 at 05:05
  • can you suggest me the "scope" to enable for this api. I tried with **vso.code_manage vso.code_status vso.project vso.work**. But have 401 authorized error by using token with this scope – bhuvaneswari kalimuthu Jan 31 '17 at 09:16
  • 1
    @bhuvaneswarikalimuthu Yes, it isn't a released API and not mentioned anywhere, I captured the request through Developer Tools. I don't understand clearly about 'scope', how did you try it? – starian chen-MSFT Feb 01 '17 at 01:13
  • I create oauth application with code_manage(read,write) and code_status(Manage) permission in my TFS account.and using this appid and secret id I created oauth token.By using that token access this api I get 401 unauthorized error.But by using this token I can able to get other api calls like get pullRequests. – bhuvaneswari kalimuthu Feb 01 '17 at 05:41
  • @bhuvaneswarikalimuthu I am afraid that you can use oauth token. For general REST API, it has api-version, but this request is different. – starian chen-MSFT Feb 01 '17 at 06:08
  • Thanks @starain-MSFT. Any other way to get this count by rest call? – bhuvaneswari kalimuthu Feb 02 '17 at 05:09
  • @bhuvaneswarikalimuthu There isn't the way to get count directly, you can calculate the items (e.g count items that changetype is 1) – starian chen-MSFT Feb 02 '17 at 05:27
  • Count items means count the files which edited or added/deleted – bhuvaneswari kalimuthu Feb 02 '17 at 05:32
  • 1
    @starianchen-MSFT hi I tried it today and I get 404. Maybe the endpoint in step 3 has changed. Do you know what's the current endpoint is? – Motoko Jul 25 '20 at 01:15
  • Do note that you will get 404 if the file was created in the last commit: there is no diff to fetch prior to this commit because it didn't exist. – Martin Faucheux Jan 03 '23 at 11:09
  • @starianchen-MSFT if the commit has changes in several files, it means we need to make a call for each file. Any known way to make 1 call for the whole commit? – Martin Faucheux Jan 03 '23 at 14:16