通过Github API从Github仓库获取所有文件名 [英] Get all file names from a Github repo through the Github API
问题描述
是否有可能使用github api从存储库获取所有文件名?
我正在尝试使用 PyGithub ,但我完全可以手动完成请求,只要它有效。
到目前为止,我的算法是:
1)获取用户回购名称
2)获取匹配特定描述的用户回购
3)???获取repo文件名?
这必须与特定提交相关,因为某些提交中可能存在某些文件并在其他人缺席,所以在你看文件之前,你需要使用像 如果您只是对分支上的最新提交感兴趣,您可以将 提交哈希,你可以检查提交 应该返回类似这样的内容(从GitHub的文档中截断): 查看它的树的散列,它实际上是它的目录内容。在这种情况下, GitHub示例的输出是 正如您所看到的,我们有一些 blobs ,它们对应于文件以及一些与子目录对应的附加树。您可能需要递归执行此操作。 Is it possible to get all the file names from repository using the github api? I'm currently trying to tinker this using PyGithub, but I'm totally ok with manually doing the request as long as it works. My algorithm so far is:
1) Get the user repo names
2) Get the user repo that matches a certain description
3) ??? get repo file names? This will have to be relative to a particular commit, as some files may be present in some commits and absent in others, so before you can look at files you'll need to use something like List commits on a repository: If you're just interested in the latest commit on a branch you can set the Once you have a commit hash, you can inspect that commit which should return something like this (truncated from GitHub's documentation): Look at the hash of its tree, which is essentially its directory contents. In this case, The output from GitHub's example is As you can see, we have some blobs, which correspond to files, and some additional trees, which correspond to subdirectories. You may want to do this recursively. 这篇关于通过Github API从Github仓库获取所有文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
GET / repos /:owner /:repo / commitits
sha
参数到分支名称:
sha
字符串
SHA或分支开始列出提交。
GET / repos /:owner /:repo / git / commits /:sha
$ $ $ $ $ $ $ $ $ $ $ $ $
...,
tree:{
url:https://api.github.com/repos/octocat/Hello-World/git/trees/ 691272480426f78a0138979dd3ce63b77f706feb,
sha:691272480426f78a0138979dd3ce63b77f706feb
},
...:...
}
691272480426f78a0138979dd3ce63b77f706feb
。现在我们终于可以请求该树的内容:
GET / repos /:owner /:repo / git / trees /:sha
{
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/oct ocat / 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
}
]
}
GET /repos/:owner/:repo/commits
sha
parameter to the branch name:
sha
string
SHA or branch to start listing commits from.GET /repos/:owner/:repo/git/commits/:sha
{
"sha": "...",
"...",
"tree": {
"url": "https://api.github.com/repos/octocat/Hello-World/git/trees/691272480426f78a0138979dd3ce63b77f706feb",
"sha": "691272480426f78a0138979dd3ce63b77f706feb"
},
"...": "..."
}
691272480426f78a0138979dd3ce63b77f706feb
. Now we can finally request the contents of that tree:GET /repos/:owner/:repo/git/trees/:sha
{
"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"
}
]
}