通过 Github API 从 Github 存储库中获取所有文件名 [英] Get all file names from a Github repo through the Github API

查看:113
本文介绍了通过 Github API 从 Github 存储库中获取所有文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 GitHub API 从存储库中获取所有文件名?

Is it possible to get all the file names from repository using the GitHub API?

我目前正在尝试使用 PyGithub 修补这个问题,但我完全可以手动执行请求只要它有效.

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. 获取用户存储库名称
  2. 获取匹配特定描述的用户仓库
  3. ???获取 repo 文件名?

推荐答案

这必须与特定提交相关,因为某些文件可能存在于某些提交中,而在其他提交中不存在,因此在查看文件之前,将需要使用类似列出存储库上的提交:

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:

GET /repos/:owner/:repo/commits

如果您只对分支上的最新提交感兴趣,您可以将 sha 参数设置为分支名称:

If you're just interested in the latest commit on a branch you can set the sha parameter to the branch name:

sha string SHA 或分支开始列出提交.

sha string SHA or branch to start listing commits from.

拥有提交哈希后,您可以检查该提交

Once you have a commit hash, you can inspect that commit

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

应该返回如下内容(从 GitHub 的文档中截取):

which should return something like this (truncated from GitHub's documentation):

{
  "sha": "...",
  "...",
  "tree": {
    "url": "https://api.github.com/repos/octocat/Hello-World/git/trees/691272480426f78a0138979dd3ce63b77f706feb",
    "sha": "691272480426f78a0138979dd3ce63b77f706feb"
  },
  "...": "..."
}

查看它的的哈希值,它本质上是它的目录内容.在这种情况下,691272480426f78a0138979dd3ce63b77f706feb.现在我们终于可以请求那棵树的内容:

Look at the hash of its tree, which is essentially its directory contents. In this case, 691272480426f78a0138979dd3ce63b77f706feb. Now we can finally request the contents of that tree:

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

GitHub 示例的输出是

The output from GitHub's example is

{
  "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"
    }
  ]
}

如您所见,我们有一些 blob,它们对应于文件,还有一些额外的树,它们对应于子目录.您可能希望递归地执行此操作.

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屋!

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