Using GitHub API to retrieve all versions of a specific file

别等时光非礼了梦想. 提交于 2020-01-13 09:13:07

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/16700297/using-github-api-to-retrieve-all-versions-of-a-specific-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!