git - 使用 GitHub blob api 获取文件的所有版本

标签 git web-services api github version-control

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

问题是它使用了“内容”API,每个文件的上限为 1 MB(如果您尝试访问大于 1 MB 的文件,您会收到此错误消息:“此 API 返回 blob最大 1 MB。请求的 blob 太大,无法通过 API 获取,但您可以使用 Git 数据 API 请求最大 100 MB 的 blob。")

因此,要获取大于 1 MB(最多 100 MB)的文件,您需要使用“blob”API,但我不知道如何以与内容 API 相同的方式使用它。

即,给定一个文件的特定提交,您如何使用“blob”API 获取该文件的内容?

最佳答案

get content API 确实允许传递 SHA1:

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

注意:GitHub Content 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(<branch_name>:<parent_path>)'表示<branch_name>:<parent_path>需要是url encoded

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

OP buddyroo30 提到 in the comments :

I ended up doing similarly using the tree API.
Specifically, I get all the commits for a file. Then I try to use the contents API to get the file contents for each commit.
If that fails (i.e. over 1 MB in size so I need to use the blob API), I get the tree URL for the file from its commit (i.e. in Perl: $commit_tree_url = $commit_info->{'commit'}->{'tree'}->{'url'}).
then I fetch $commit_tree_url and find the correct tree record in the results for the file --- this will have a 'url' hash value which can be used to get the file contents via the blob API.

关于git - 使用 GitHub blob api 获取文件的所有版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34456176/

相关文章:

web-services - 从部署在 glassfish 上的应用程序访问 ssl webservice

java - 启动前休息功能失败

javascript - AngularJS $http GET 调用获取 "Cannot GET/[object%20Object]"错误

git - 我如何摆脱我的 git repo 中的大包文件?

eclipse - 无法使用 git 提交我的项目

java - 在提供 Maven Artifact 的 Wildfly 部署中使用 CXF 库

visual-studio - ASMX:tempuri.org 应该被什么取代?

git diff 不创建临时文件

git - 可视化项目增长的工具

web-services - 免费的 RESTful API 生成器比较