【问题标题】:Getting all versions of a file using GitHub blob api使用 GitHub blob api 获取文件的所有版本
【发布时间】:2022-05-04 15:47:02
【问题描述】:

我想知道如何通过 GitHub API 获取文件的所有提交/版本(即提交/版本的内容)。
我想出了一种方法,它相当于this other question 的答案。

问题在于它使用了“内容”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 获取该文件的内容?

【问题讨论】:

标签: git web-services api github version-control


【解决方案1】:

get content API 确实允许传递 SHA1:

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

注意:GitHub 内容 API 现在(2022 年 5 月)support up to 100MB files

Blob API 也使用 SHA1:

GET /repos/:owner/:repo/git/blobs/:sha

但是你需要先得到你想要的文件的SHA1。

请参阅“How do I get the “sha” parameter from GitHub API without downloading the whole file?”,使用 Get Tree API 作为父文件夹。

GET /repos/<owner>/<repo>/git/trees/url_encode(<branch_name>:<parent_path>)

'url_encode(&lt;branch_name&gt;:&lt;parent_path&gt;)'表示&lt;branch_name&gt;:&lt;parent_path&gt;必须是url encoded

树的结果将为您提供您正在查找的文件的 SHA1。

OP buddyroo30 提到了in the comments

我最终使用树 API 做了类似的事情。
具体来说,我得到了一个文件的所有提交。然后我尝试使用内容 API 来获取每个提交的文件内容。
如果失败(即大小超过 1 MB,因此我需要使用 blob API),我会从其提交中获取文件的树 URL(即在 Perl 中:$commit_tree_url = $commit_info-&gt;{'commit'}-&gt;{'tree'}-&gt;{'url'})。
然后我获取$commit_tree_url 并在文件的结果中找到正确的树记录 --- 这将有一个“url”哈希值,可用于通过 blob API 获取文件内容。

【讨论】:

  • 谢谢,我最终使用树 API 做了类似的事情。具体来说,我得到了一个文件的所有提交。然后我尝试使用内容 API 来获取每个提交的文件内容。如果失败(即大小超过 1 MB,因此我需要使用 blob API),我会从其提交中获取文件的树 URL(即在 Perl 中:$commit_tree_url = $commit_info->{'commit'}-> {'tree'}->{'url'})。然后我获取 $commit_tree_url 并在文件的结果中找到正确的树记录 --- 这将有一个“url”哈希值,可用于通过 blob API 获取文件内容。
  • @buddyroo30 太好了!我已将您的评论包含在答案中以提高知名度。
猜你喜欢
  • 2013-05-18
  • 1970-01-01
  • 2017-07-21
  • 1970-01-01
  • 2016-05-05
  • 2014-09-21
  • 2018-05-06
  • 2021-10-20
相关资源
最近更新 更多