使用 GitHub CLI 中的 `gh` 列出存储库中的文件?

huangapple go评论61阅读模式
英文:

List files in repo using `gh` from GitHub CLI?

问题

有没有一个 gh 命令可以列出目录中的文件而不需要一个工作副本的克隆?

我找到了一个 git 命令可以做到,但我认为这仅适用于本地工作副本。

git ls-tree --name-only HEAD
git ls-tree --name-only -r HEAD
英文:

Is there a gh command to list files in a directory without having a working copy clone?

I have found a git command that will do it, but I think this is working only on the local working copy.

git ls-tree --name-only HEAD
git ls-tree --name-only -r HEAD

答案1

得分: 1

gh api 是一个 gh 命令,可以帮助您调用 GitHub API。您可以使用 -q 选项来使用 jq 来排序。

例如,以下是一种获取内容列表的方式,基于 trees API 的一个命令示例:

gh api '/repos/{owner}/{repo}/git/trees/{branch}?recursive=true' -q '.tree[]|.path'

这里,owner 是您的 GitHub 用户名(或组织名),repo 是仓库名称,branch 是分支名称。然后,.tree[]|.path 将原始的 JSON 输出转换为仓库中路径的列表。

recursive 选项将帮助您避免手动递归目录结构,但有一个大小限制

注意:使用递归参数时,树数组的限制为 100,000 条记录,最大大小为 7 MB。

如果您担心这个限制,您将需要自己进行递归。

还有一个带有相同 100,000 限制的 contents API,以及一个用于下载 tarballs 或 zipfiles 的API,如果您对文件的内容而不仅仅是它们的名称感兴趣。

我鼓励您深入探索 GitHub API - GitHub 拥有一些非常有趣的 API 并且在其实施方面做得非常出色。

英文:

gh api is a gh command that can help you call the github apis. You can use the -q option to use jq to sort

For example, here's one way to get a list of contents in one command, based on the trees api:

gh api '/repos/{owner}/{repo}/git/trees/{branch}?recursive=true' -q '.tree[]|.path'

Here, owner is your github user (or organization) name, repo is the repo name, branch is the branch name. Then .tree[]|.path turns the raw json output into a list of just paths in your repo.

recursive option will save you from having to recurse the directory structure yourself, but has a size limit

> Note: The limit for the tree array is 100,000 entries with a maximum size of 7 MB when using the recursive parameter.

If you're concerned about that limit, you'll have to do your own recursion.

There is also a contents api (with the same 100k limit) as well as one for tarballs or zipfiles if you're interested in the contents of those files and not just their names.

I encourage you to explore the github apis in depth - github has some really interesting APIs and did a very good job implementing them.

huangapple
  • 本文由 发表于 2023年2月18日 01:26:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487452.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定