使用GitHub API检索特定文件的所有版本
我目前正在尝试通读(GitHub API)[http://developer.github.com/v3/repos/contents/],以了解如何以编程方式在单个目录中检索特定文件的所有版本存储库.
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?
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
您将获得一个提交对象数组,每个提交对象都有一个sha
属性.
You'll get back an array of commit objects, each of which has a sha
attribute.
现在您已经拥有所有提交SHA,可以使用此API端点,并指定ref
查询参数以设置SHA.因此,对于每个提交SHA,都要求:
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
并读取content
属性.请注意,内容是Base64编码的,但是您也可以通过设置相关的Accept
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.