是否可以使用GitHub API从存储库中获取所有文件名?
我目前正在尝试使用PyGithub来修改它,但我完全可以手动执行请求,只要它有效.
到目前为止我的算法是:
获取用户仓库名称
获取与特定描述匹配的用户仓库
??? 获取repo文件名?
Chris.. 22
这必须与特定提交相关,因为某些提交中可能存在某些文件而其他提交中不存在,因此在查看文件之前,您需要在存储库中使用类似List提交的内容:
GET /repos/:owner/:repo/commits
如果您只对分支上的最新提交感兴趣,可以将sha
参数设置为分支名称:
sha
string
SHA或分支开始列出提交.
一旦有了提交哈希,就可以检查该提交
GET /repos/:owner/:repo/git/commits/:sha
应该返回这样的东西(从GitHub的文档中截断):
{ "sha": "...", "...", "tree": { "url": "https://api.github.com/repos/octocat/Hello-World/git/trees/691272480426f78a0138979dd3ce63b77f706feb", "sha": "691272480426f78a0138979dd3ce63b77f706feb" }, "...": "..." }
查看其树的哈希值,它本质上是其目录内容.在这种情况下,691272480426f78a0138979dd3ce63b77f706feb
.现在我们终于可以请求该树的内容:
GET /repos/:owner/:repo/git/trees/:sha
GitHub的例子的输出是
{ "sha": "9fb037999f264ba9a7fc6274d15fa3ae2ab98312", "url": "https://api.github.com/repos/octocat/Hello-World/trees/9fb037999f264ba9a7fc6274d15fa3ae2ab98312", "tree": [ { "path": "file.rb", "mode": "100644", "type": "blob", "size": 30, "sha": "44b4fc6d56897b048c772eb4087f854f46256132", "url": "https://api.github.com/repos/octocat/Hello-World/git/blobs/44b4fc6d56897b048c772eb4087f854f46256132" }, { "path": "subdir", "mode": "040000", "type": "tree", "sha": "f484d249c660418515fb01c2b9662073663c242e", "url": "https://api.github.com/repos/octocat/Hello-World/git/blobs/f484d249c660418515fb01c2b9662073663c242e" }, { "path": "exec_file", "mode": "100755", "type": "blob", "size": 75, "sha": "45b983be36b73c0788dc9cbcb76cbb80fc7bb057", "url": "https://api.github.com/repos/octocat/Hello-World/git/blobs/45b983be36b73c0788dc9cbcb76cbb80fc7bb057" } ] }
如您所见,我们有一些blobs,它们对应于文件,还有一些额外的树,它们对应于子目录.您可能希望以递归方式执行此操作.
这必须与特定提交相关,因为某些提交中可能存在某些文件而其他提交中不存在,因此在查看文件之前,您需要在存储库中使用类似List提交的内容:
GET /repos/:owner/:repo/commits
如果您只对分支上的最新提交感兴趣,可以将sha
参数设置为分支名称:
sha
string
SHA或分支开始列出提交.
一旦有了提交哈希,就可以检查该提交
GET /repos/:owner/:repo/git/commits/:sha
应该返回这样的东西(从GitHub的文档中截断):
{ "sha": "...", "...", "tree": { "url": "https://api.github.com/repos/octocat/Hello-World/git/trees/691272480426f78a0138979dd3ce63b77f706feb", "sha": "691272480426f78a0138979dd3ce63b77f706feb" }, "...": "..." }
查看其树的哈希值,它本质上是其目录内容.在这种情况下,691272480426f78a0138979dd3ce63b77f706feb
.现在我们终于可以请求该树的内容:
GET /repos/:owner/:repo/git/trees/:sha
GitHub的例子的输出是
{ "sha": "9fb037999f264ba9a7fc6274d15fa3ae2ab98312", "url": "https://api.github.com/repos/octocat/Hello-World/trees/9fb037999f264ba9a7fc6274d15fa3ae2ab98312", "tree": [ { "path": "file.rb", "mode": "100644", "type": "blob", "size": 30, "sha": "44b4fc6d56897b048c772eb4087f854f46256132", "url": "https://api.github.com/repos/octocat/Hello-World/git/blobs/44b4fc6d56897b048c772eb4087f854f46256132" }, { "path": "subdir", "mode": "040000", "type": "tree", "sha": "f484d249c660418515fb01c2b9662073663c242e", "url": "https://api.github.com/repos/octocat/Hello-World/git/blobs/f484d249c660418515fb01c2b9662073663c242e" }, { "path": "exec_file", "mode": "100755", "type": "blob", "size": 75, "sha": "45b983be36b73c0788dc9cbcb76cbb80fc7bb057", "url": "https://api.github.com/repos/octocat/Hello-World/git/blobs/45b983be36b73c0788dc9cbcb76cbb80fc7bb057" } ] }
如您所见,我们有一些blobs,它们对应于文件,还有一些额外的树,它们对应于子目录.您可能希望以递归方式执行此操作.