feat(repo): 增加 repo +file 读取仓库任意文件能力 #302

Merged
wbtiger merged 1 commits from Mengz/gitlink-cli:mengz/repo-file-shortcut into master 2026-07-14 22:25:43 +08:00
Contributor

合并请求描述

当前 gitlink-cli repo 已经支持 +readme+tree,但用户想直接读取 go.mod.gitignore、配置文件、脚本或许可证内容时,仍然只能退回 Raw API。open issue #17 里也反映了这个真实使用缺口:用户需要读取仓库内文件做分析,但现有方式不够直接,raw 路径还容易返回无意义的 HTML 壳页面。

这次新增 repo +file,把“读取仓库任意文件内容”补成高层 Shortcut,基于 GET /{owner}/{repo}/sub_entries 的文件模式实现,不依赖不稳定的 raw 路径。命令支持 --path 指定仓库文件路径,支持 --ref 读取指定分支、标签或提交,也支持 --content-only 只输出文件正文,方便脚本和 Agent 直接消费。

为了让这个命令在真实使用里更顺手,这次还补了两个边界处理:一是把 --path 设为必填,并对空路径或只有 / 的输入直接报错;二是当用户误把目录传进来时,明确提示改用 repo +tree,避免得到难以理解的 API 返回。

变更内容

  1. 新增 gitlink-cli repo +file
  • 读取仓库任意文件内容
  • 支持 --path/-p 指定仓库文件路径
  • 支持 --ref/-r 指定分支、标签或提交,默认 master
  • 支持 --content-only 只输出文件正文
  1. 增强错误提示
  • --path 缺失或无效时直接报错
  • 目录路径误传时提示使用 repo +tree
  • --content-only 场景下若接口未返回内容,直接报错而不是静默返回空值
  1. 文档同步
  • 更新 README.md / README.zh-CN.md
  • 更新 skills/gitlink-repo/SKILL.md
  • 新增 skills/gitlink-repo/references/gitlink-repo-file.md
  • 新增 doc/changes/repo-file-shortcut.md

测试与验证

已新增单元测试覆盖默认分支、显式 --ref、路径归一化、帮助参数注册、目录误传报错、--content-only 缺少内容报错以及结果扁平化等关键分支。

本地验证通过:

go test ./shortcuts/repo/...
go test ./...
go build ./...
git diff --check

另外做了真实命令验证:

go run . repo +file --owner Gitlink --repo gitlink-cli --path go.mod --content-only

能够直接返回 gitlink-cli 仓库 go.mod 的实际内容;当传入目录路径 shortcuts 时,也会按预期提示改用 repo +tree

## 合并请求描述 当前 `gitlink-cli repo` 已经支持 `+readme` 和 `+tree`,但用户想直接读取 `go.mod`、`.gitignore`、配置文件、脚本或许可证内容时,仍然只能退回 Raw API。open issue #17 里也反映了这个真实使用缺口:用户需要读取仓库内文件做分析,但现有方式不够直接,`raw` 路径还容易返回无意义的 HTML 壳页面。 这次新增 `repo +file`,把“读取仓库任意文件内容”补成高层 Shortcut,基于 `GET /{owner}/{repo}/sub_entries` 的文件模式实现,不依赖不稳定的 `raw` 路径。命令支持 `--path` 指定仓库文件路径,支持 `--ref` 读取指定分支、标签或提交,也支持 `--content-only` 只输出文件正文,方便脚本和 Agent 直接消费。 为了让这个命令在真实使用里更顺手,这次还补了两个边界处理:一是把 `--path` 设为必填,并对空路径或只有 `/` 的输入直接报错;二是当用户误把目录传进来时,明确提示改用 `repo +tree`,避免得到难以理解的 API 返回。 ## 变更内容 1. 新增 `gitlink-cli repo +file` - 读取仓库任意文件内容 - 支持 `--path/-p` 指定仓库文件路径 - 支持 `--ref/-r` 指定分支、标签或提交,默认 `master` - 支持 `--content-only` 只输出文件正文 2. 增强错误提示 - `--path` 缺失或无效时直接报错 - 目录路径误传时提示使用 `repo +tree` - `--content-only` 场景下若接口未返回内容,直接报错而不是静默返回空值 3. 文档同步 - 更新 `README.md` / `README.zh-CN.md` - 更新 `skills/gitlink-repo/SKILL.md` - 新增 `skills/gitlink-repo/references/gitlink-repo-file.md` - 新增 `doc/changes/repo-file-shortcut.md` ## 测试与验证 已新增单元测试覆盖默认分支、显式 `--ref`、路径归一化、帮助参数注册、目录误传报错、`--content-only` 缺少内容报错以及结果扁平化等关键分支。 本地验证通过: ```bash go test ./shortcuts/repo/... go test ./... go build ./... git diff --check ``` 另外做了真实命令验证: ```bash go run . repo +file --owner Gitlink --repo gitlink-cli --path go.mod --content-only ``` 能够直接返回 `gitlink-cli` 仓库 `go.mod` 的实际内容;当传入目录路径 `shortcuts` 时,也会按预期提示改用 `repo +tree`。
Mengz added 1 commit 2026-06-26 15:42:03 +08:00
wbtiger merged commit 5bea480990 into master 2026-07-14 22:25:35 +08:00
Sign in to join this conversation.
No reviewers
No Label
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: Gitlink/gitlink-cli#302
No description provided.