Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using GitHub API to retrieve all versions of a specific file

Tags:

github-api

I am currently trying to read through the (GitHub API)[http://developer.github.com/v3/repos/contents/] to figure out how I can programmatically retrieve all versions of a specific file in a single repository.

I see that one can get the list of commits, and the current version of a single file easily. But, is there a way to list all the commits relevant for a specific file and then iterate through all the versions of that file?

like image 948
user1027169 Avatar asked May 22 '13 19:05

user1027169


People also ask

Does GitHub have a REST API?

Learn about GitHub's APIs to extend and customize your GitHub experience. There are two stable versions of the GitHub API: the REST API and the GraphQL API.

How do I get content from GitHub?

To download from GitHub, you should navigate to the top level of the project (SDN in this case) and then a green "Code" download button will be visible on the right. Choose the Download ZIP option from the Code pull-down menu. That ZIP file will contain the entire repository content, including the area you wanted.

How do I find an old GitHub file?

On GitHub.com, navigate to the main page of the repository. Click to open the file whose line history you want to view. In the upper-right corner of the file view, click Blame to open the blame view.


1 Answers

To get the list of commits relevant for a specific file, use this API endpoint and specify the path parameter:

GET https://api.github.com/repos/:owner/:repo/commits?path=FILE_PATH

You'll get back an array of commit objects, each of which has a sha attribute.

Now that you have all the commit SHAs, you can fetch all the different versions of the file using this API endpoint and by specifying the ref query parameter to set the SHA. So, for each commit SHA, make a request to:

GET https://api.github.com/repos/:owner/:repo/contents/:FILE_PATH?ref=SHA

and read the content attribute. Notice that the content is Base64 encoded, but you can also request a raw version by setting the relevant Accept HTTP header.

like image 181
Ivan Zuzak Avatar answered Dec 20 '22 09:12

Ivan Zuzak