首页 > 解决方案 > 如何使用 github api 获取 github 存储库中的所有目录和文件?

问题描述

正如标题所说。我想从 github api 获取所有文件和目录。

我找到了这个https://developer.github.com/v3/repos/contents/#get-contents

GET /repos/:owner/:repo/contents/:path

这个api说Gets the contents of a file or directory in a repository. Specify the file path or directory in :path. If you omit :path, you will receive the contents of all files in the repository.

我调用这个 api,:path它只返回root路径中的文件或目录。

响应数据有git_url字段。它有/repos/:owner/:repo/git/trees/:shaapi 网址。这个api返回文件和目录。

我的问题

  1. 有没有办法通过只调用一次 GitHub api 来获取我的存储库中的所有文件和目录信息?
  2. 如果1.不可能,我是否必须递归调用 api(上述git_url字段)来获取所有文件和目录?

提前致谢。

标签: gitgithub

解决方案


如果您阅读了提供链接的文档,它会说:

要递归地获取存储库的内容,您可以递归地获取 tree

如果您阅读了该文档,则会有一个“递归获取树”标题,其中包含您想要的内容:

GET /repos/:owner/:repo/git/trees/:tree_sha?recursive=1

推荐阅读