bud*_*o30 6 git api version-control web-services github
我想知道如何通过 GitHub API 获取文件的所有提交/版本(即提交/版本的内容)。
我想出了一种方法,相当于另一个问题的答案。
问题是这使用了“内容”API,每个文件的上限为 1 MB(如果您尝试访问大于 1 MB 的文件,则会收到此错误消息:“ This API returns blobs up to 1 MB in size. The requested blob is too large to fetch via the API, but you can use the Git Data API to request blobs up to 100 MB in size.”)
因此,要获取大于 1 MB(最多 100 MB)的文件,您需要使用“blob”API,但我不知道如何以与内容 API 相同的方式使用它。
即,给定文件的特定提交,您如何使用“blob”API 获取该文件的内容?
在获取内容API确实允许通过一个SHA1:
GET https://api.github.com/repos/:owner/:repo/contents/:FILE_PATH?ref=SHA
Run Code Online (Sandbox Code Playgroud)
该斑点API还使用SHA1:
GET /repos/:owner/:repo/git/blobs/:sha
Run Code Online (Sandbox Code Playgroud)
但是您需要先获取所需文件的 SHA1。
请参阅“如何在不下载整个文件的情况下从 GitHub API 获取“sha”参数? ”,使用父文件夹的Get Tree API。
GET /repos/<owner>/<repo>/git/trees/url_encode(<branch_name>:<parent_path>)
Run Code Online (Sandbox Code Playgroud)
' url_encode(<branch_name>:<parent_path>)' 表示<branch_name>:<parent_path>需要进行url 编码
树的结果将为您提供要查找的文件的 SHA1。
该buddyroo30 OP提到的评论:
我最终使用树 API 做了类似的事情。
具体来说,我获得了一个文件的所有提交。然后我尝试使用内容 API 来获取每次提交的文件内容。
如果失败(即大小超过 1 MB,所以我需要使用 blob API),我会从文件的提交中获取该文件的树 URL(即在 Perl: 中$commit_tree_url = $commit_info->{'commit'}->{'tree'}->{'url'})。
然后我$commit_tree_url在文件的结果中获取并找到正确的树记录——这将有一个“url”哈希值,可用于通过 blob API 获取文件内容。
| 归档时间: |
|
| 查看次数: |
1594 次 |
| 最近记录: |