Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting file diff with Github API

Tags:

github

api

net project for which I need to detect and parse changes made to a specific single text file in a repository between different pull requests.

I've been successfully able to access the pull requests and the commits using the Github API but I don't know how to retrieve the lines that changed in the last commit?

Is this possible using the API? What would be the best approach? If not should I try to read the last two file versions and implement a differ algorithm locally? Thanks!

like image 775
Luis Alvarado Day Avatar asked Nov 03 '16 02:11

Luis Alvarado Day


People also ask

How does Git calculate diff?

Computing diffs The diff is dynamically generated from the snapshot data by comparing the root trees of the commit and its parent. Git can compare any two snapshots in time, not just adjacent commits. To compare two commits, start by looking at their root trees, which are almost always different.

How do I fetch API in GitHub?

To get the list of random users, you have to make a request to the /user endpoint, and to get the details of a specific user, you make a request to the /user/{username} endpoint. You can try out the following commands quickly in your terminal with curl to see the results.

What can you do with GitHub API?

Github APIs( or Github ReST APIs) are the APIs that you can use to interact with GitHub. They allow you to create and manage repositories, branches, issues, pull requests, and many more. For fetching publicly available information (like public repositories, user profiles, etc.), you can call the API.


1 Answers

A pull request contains a diff_url entry like

"diff_url": "https://github.com/octocat/Hello-World/pull/1347.diff"

You can do this for any commit. For example, to get the diff of commit 7fd1a60b01f91b3 on octocat's Hello-World it's https://github.com/octocat/Hello-World/commit/7fd1a60b01f91b314f59955a4e4d4e80d8edf11d.diff.

This also works for branches. Here's master on octocat's Hello-World. https://github.com/octocat/Hello-World/commit/master.diff.

The general form is:

https://github.com/<owner>/<repo>/commit/<commit>.diff
like image 198
Schwern Avatar answered Oct 04 '22 15:10

Schwern