gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 14:05:28 25 4
gpt4 key购买 nike

我想知道如何通过 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com