diff --git a/README.md b/README.md index e5e4318..ddc0dcd 100644 --- a/README.md +++ b/README.md @@ -446,6 +446,10 @@ gitlink-cli branch +create --name feature/new-feature # Delete a branch gitlink-cli branch +delete --name feature/old-feature +# All branch names at once (no paging) and default-branch switch +gitlink-cli branch +all --owner myname --repo myrepo +gitlink-cli branch +set-default --owner myname --repo myrepo -n develop + # Protect a branch gitlink-cli branch +protect --name main diff --git a/internal/i18n/locales/en-US.json b/internal/i18n/locales/en-US.json index 0739395..2faa6d9 100644 --- a/internal/i18n/locales/en-US.json +++ b/internal/i18n/locales/en-US.json @@ -5,10 +5,12 @@ "cmd.auth.logout.short": "Logout from GitLink", "cmd.auth.short": "Authentication commands", "cmd.auth.status.short": "Show authentication status", + "cmd.branch.all.short": "List all branch names (no paging)", "cmd.branch.create.short": "Create a branch", "cmd.branch.delete.short": "Delete a branch", "cmd.branch.list.short": "List branches", "cmd.branch.protect.short": "Set branch protection", + "cmd.branch.set_default.short": "Set the repository default branch", "cmd.branch.short": "Branch operations", "cmd.branch.unprotect.short": "Remove branch protection", "cmd.ci.builds.short": "List CI builds", diff --git a/internal/i18n/locales/zh-CN.json b/internal/i18n/locales/zh-CN.json index 2e6fc4d..0867a75 100644 --- a/internal/i18n/locales/zh-CN.json +++ b/internal/i18n/locales/zh-CN.json @@ -5,10 +5,12 @@ "cmd.auth.logout.short": "退出 GitLink 登录", "cmd.auth.short": "认证命令", "cmd.auth.status.short": "显示认证状态", + "cmd.branch.all.short": "列出全部分支名(不分页)", "cmd.branch.create.short": "创建分支", "cmd.branch.delete.short": "删除分支", "cmd.branch.list.short": "列出分支", "cmd.branch.protect.short": "设置分支保护", + "cmd.branch.set_default.short": "设置仓库默认分支", "cmd.branch.short": "分支操作", "cmd.branch.unprotect.short": "移除分支保护", "cmd.ci.builds.short": "列出 CI 构建", diff --git a/shortcuts/branch/branch.go b/shortcuts/branch/branch.go index 0393ad5..80d3d15 100644 --- a/shortcuts/branch/branch.go +++ b/shortcuts/branch/branch.go @@ -80,6 +80,42 @@ func Shortcuts(translators ...*i18n.Translator) []*common.Shortcut { return ctx.Output(env) }, }, + { + Name: "all", + Description: tr.T("cmd.branch.all.short"), + Flags: []common.Flag{}, + Run: func(ctx *common.RuntimeContext) error { + if err := ctx.ResolveOwnerRepo(); err != nil { + return err + } + env, err := ctx.CallAPI("GET", "/v1"+ctx.RepoPath()+"/branches/all", nil) + if err != nil { + return err + } + return ctx.Output(env) + }, + }, + { + Name: "set-default", + Description: tr.T("cmd.branch.set_default.short"), + Flags: []common.Flag{ + {Name: "name", Short: "n", Usage: tr.T("flag.branch.name"), Required: true}, + }, + Run: func(ctx *common.RuntimeContext) error { + if err := ctx.ResolveOwnerRepo(); err != nil { + return err + } + name, err := ctx.RequireArg("name") + if err != nil { + return err + } + env, err := ctx.CallAPI("PATCH", "/v1"+ctx.RepoPath()+"/branches/update_default_branch", map[string]interface{}{"name": name}) + if err != nil { + return err + } + return ctx.Output(env) + }, + }, { Name: "protect", Description: tr.T("cmd.branch.protect.short"), diff --git a/shortcuts/branch/branch_test.go b/shortcuts/branch/branch_test.go index 1f1908f..c31481d 100644 --- a/shortcuts/branch/branch_test.go +++ b/shortcuts/branch/branch_test.go @@ -216,3 +216,38 @@ func TestBranchUnprotectHTTPError(t *testing.T) { t.Fatal("expected error for HTTP 500") } } + +func TestBranchAllUsesAllEndpoint(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != "GET" || r.URL.Path != "/v1/owner/repo/branches/all.json" { + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path) + } + writeJSON(w, []interface{}{map[string]interface{}{"name": "master"}}) + })) + defer server.Close() + + if err := runShortcut(t, server, "all", nil); err != nil { + t.Fatalf("all failed: %v", err) + } +} + +func TestBranchSetDefaultPatchesName(t *testing.T) { + var payload map[string]interface{} + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != "PATCH" || r.URL.Path != "/v1/owner/repo/branches/update_default_branch.json" { + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path) + } + if err := json.NewDecoder(r.Body).Decode(&payload); err != nil { + t.Fatalf("decode payload: %v", err) + } + writeJSON(w, map[string]interface{}{"status": float64(0), "message": "success"}) + })) + defer server.Close() + + if err := runShortcut(t, server, "set-default", map[string]string{"name": "develop"}); err != nil { + t.Fatalf("set-default failed: %v", err) + } + if payload["name"] != "develop" { + t.Fatalf("expected name=develop, got %v", payload["name"]) + } +}