feat(branch): add +all and keyword filtering for +list

- branch +list -k/--keyword: filter branches by name keyword (passed
  through to the v1 branches index)
- branch +all: list all branch names via /branches/all, no pagination

Production-verified on gitlink.org.cn (keyword narrows to matching
branches; /all returns the full set). 2 new unit tests, README
examples, and en-US/zh-CN i18n keys.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
1os21ka23r9navae6mrro 2026-07-08 14:03:05 +00:00
parent c09645da62
commit 2e92db38d7
5 changed files with 65 additions and 0 deletions

View File

@ -440,6 +440,12 @@ gitlink-cli pr +review --owner Gitlink --repo forgeplus -i 42 --status approved
# List branches
gitlink-cli branch +list --owner Gitlink --repo forgeplus
# Filter branches by name keyword
gitlink-cli branch +list --owner Gitlink --repo forgeplus -k feature
# List all branch names without pagination
gitlink-cli branch +all --owner Gitlink --repo forgeplus
# Create a branch
gitlink-cli branch +create --name feature/new-feature

View File

@ -5,6 +5,7 @@
"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 pagination)",
"cmd.branch.create.short": "Create a branch",
"cmd.branch.delete.short": "Delete a branch",
"cmd.branch.list.short": "List branches",
@ -125,6 +126,7 @@
"flag.api.query": "Query parameters (key=val&key2=val2)",
"flag.auth.token": "Login by pasting an existing token",
"flag.branch.from": "Source branch or commit",
"flag.branch.keyword": "Filter branches by name keyword",
"flag.branch.name": "Branch name",
"flag.ci.build": "Build number",
"flag.ci.stage": "Stage number",

View File

@ -5,6 +5,7 @@
"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": "列出分支",
@ -125,6 +126,7 @@
"flag.api.query": "查询参数key=val&key2=val2",
"flag.auth.token": "通过粘贴已有 Token 登录",
"flag.branch.from": "源分支或 Commit",
"flag.branch.keyword": "按名称关键字过滤分支",
"flag.branch.name": "分支名称",
"flag.ci.build": "构建编号",
"flag.ci.stage": "阶段编号",

View File

@ -15,6 +15,7 @@ func Shortcuts(translators ...*i18n.Translator) []*common.Shortcut {
Name: "list",
Description: tr.T("cmd.branch.list.short"),
Flags: []common.Flag{
{Name: "keyword", Short: "k", Usage: tr.T("flag.branch.keyword")},
{Name: "page", Short: "p", Usage: tr.T("flag.page"), Default: "1"},
{Name: "limit", Short: "l", Usage: tr.T("flag.limit"), Default: "20"},
},
@ -25,6 +26,9 @@ func Shortcuts(translators ...*i18n.Translator) []*common.Shortcut {
q := url.Values{}
q.Set("page", ctx.Arg("page"))
q.Set("limit", ctx.Arg("limit"))
if keyword := ctx.Arg("keyword"); keyword != "" {
q.Set("keyword", keyword)
}
env, err := ctx.CallAPIWithQuery("GET", "/v1"+ctx.RepoPath()+"/branches", q)
if err != nil {
return err
@ -32,6 +36,21 @@ 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: "create",
Description: tr.T("cmd.branch.create.short"),

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gitlink-org/gitlink-cli/internal/client"
@ -216,3 +217,38 @@ func TestBranchUnprotectHTTPError(t *testing.T) {
t.Fatal("expected error for HTTP 500")
}
}
func TestBranchListPassesKeyword(t *testing.T) {
var query string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v1/owner/repo/branches.json" {
t.Fatalf("unexpected path: %s", r.URL.Path)
}
query = r.URL.RawQuery
writeJSON(w, map[string]interface{}{"total_count": 1, "branches": []interface{}{}})
}))
defer server.Close()
err := runShortcut(t, server, "list", map[string]string{"page": "1", "limit": "20", "keyword": "feat"})
if err != nil {
t.Fatalf("list failed: %v", err)
}
if !strings.Contains(query, "keyword=feat") {
t.Fatalf("expected keyword in query, got %q", query)
}
}
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()
err := runShortcut(t, server, "all", map[string]string{})
if err != nil {
t.Fatalf("all failed: %v", err)
}
}