使用 GitHub blob api 获取文件的所有版本 [英] Getting all versions of a file using GitHub blob api

查看:85
本文介绍了使用 GitHub blob api 获取文件的所有版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何通过 GitHub API 获取文件的所有提交/版本(即提交/版本的内容).
我想出了一种方法,它相当于 另一个问题.

I want to know how to get all commits/versions of a file (i.e. the contents of the commits/versions) via the GitHub API.
I figured one way to do it which is equivalent to the answer to this other question.

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

The problem is this uses the "contents" API, which has an upper limit of 1 MB per file (you get this error message if you try to access a file larger than 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 相同的方式使用它.

So to get files larger than 1 MB (up to 100 MB) you need to use the "blob" API, but I don't know how to use it in the same way as the contents API.

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

I.e., given a specific commit of a file, how do you get the contents of that file using the "blob" API?

推荐答案

获取内容 API 确实允许传递 SHA1:

The get content API indeed allows to pass a SHA1:

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

Blob API 也使用 SHA1:

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

但是您需要先获取所需文件的 SHA1.

But you need to get the SHA1 of the file you want first.

参见如何在不下载整个文件的情况下从 GitHub API 获取sha"参数?",使用父文件夹的 Get Tree API.

See "How do I get the "sha" parameter from GitHub API without downloading the whole file?", using the Get Tree API for the parent folder.

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

'url_encode(:)' 表示 : 需要 url 编码

树的结果将为您提供您要查找的文件的 SHA1.

The result from the tree will give you the SHA1 of the file you are looking for.

OP buddyroo30 提到了 在评论中:

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

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.

这篇关于使用 GitHub blob api 获取文件的所有版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆