diff --git a/doc/changes/repo-tree-default-branch.md b/doc/changes/repo-tree-default-branch.md new file mode 100644 index 0000000..0bd9e05 --- /dev/null +++ b/doc/changes/repo-tree-default-branch.md @@ -0,0 +1,18 @@ +# repo +tree 遵循仓库默认分支 + +## 背景 + +`repo +tree` 未指定 `--ref` 时把 ref 硬编码为 `master`,对默认分支是 +`main`(或其他名称)的仓库直接返回 `[-2] 你访问的文件不存在` +(生产实测 `datawhalechina/paper-chart-tutorial`,默认分支 `main`)。 +平台 `sub_entries` API 在不带 `ref` 参数时会自动使用仓库默认分支。 + +## 变更 + +- `--ref` 未指定时不再发送 `ref` 参数(交由平台落到默认分支),flag 也不再 + 声明 `master` 默认值。 + +## 验证 + +- `go test ./shortcuts/repo/`(默认 ref 断言改为「不携带 ref 参数」) +- 生产 gitlink.org.cn 实测:默认分支为 `main` 与 `master` 的仓库均正常列出根目录。 diff --git a/shortcuts/repo/repo.go b/shortcuts/repo/repo.go index 06774a6..ebf6ced 100644 --- a/shortcuts/repo/repo.go +++ b/shortcuts/repo/repo.go @@ -86,21 +86,19 @@ func Shortcuts(translators ...*i18n.Translator) []*common.Shortcut { Description: tr.T("cmd.repo.tree.short"), Flags: []common.Flag{ {Name: "path", Short: "p", Usage: tr.T("flag.repo.tree.path")}, - {Name: "ref", Short: "r", Usage: tr.T("flag.repo.tree.ref"), Default: "master"}, + {Name: "ref", Short: "r", Usage: tr.T("flag.repo.tree.ref")}, }, Run: func(ctx *common.RuntimeContext) error { if err := ctx.ResolveOwnerRepo(); err != nil { return err } q := url.Values{} - ref := ctx.Arg("ref") - if ref == "" { - ref = "master" - } if path := ctx.Arg("path"); path != "" { q.Set("filepath", path) } - q.Set("ref", ref) + if ref := ctx.Arg("ref"); ref != "" { + q.Set("ref", ref) + } env, err := ctx.CallAPIWithQuery("GET", ctx.RepoPath()+"/sub_entries", q) if err != nil { return err diff --git a/shortcuts/repo/repo_test.go b/shortcuts/repo/repo_test.go index 5c5f34b..27312fe 100644 --- a/shortcuts/repo/repo_test.go +++ b/shortcuts/repo/repo_test.go @@ -158,7 +158,9 @@ func TestRepoTreeListsRootOnDefaultRef(t *testing.T) { if _, ok := r.URL.Query()["filepath"]; ok { t.Fatalf("did not expect filepath query for repository root, got %q", r.URL.Query().Get("filepath")) } - assertEqual(t, r.URL.Query().Get("ref"), "master") + if _, ok := r.URL.Query()["ref"]; ok { + t.Fatalf("did not expect ref query for default ref, got %q", r.URL.Query().Get("ref")) + } writeJSON(t, w, map[string]interface{}{ "entries": []map[string]interface{}{ {"name": "README.md", "type": "file"}, @@ -213,7 +215,7 @@ func TestRepoTreeShortcutRegistersHelpFlags(t *testing.T) { if !ok { t.Fatal("tree shortcut missing ref flag") } - if refFlag.Short != "r" || refFlag.Default != "master" || refFlag.Usage == "" { + if refFlag.Short != "r" || refFlag.Default != "" || refFlag.Usage == "" { t.Fatalf("unexpected ref flag: %+v", refFlag) } }