GitLabgitlab-org

cli

main
ply8 git calls226 ms99 KiB6 cachedprotocol v2
200 lines5.1 KiBRaw
1package semantic
3import (
4 "context"
5 "fmt"
6 "net/http"
7 "net/url"
8 "strconv"
9 "strings"
11 "github.com/MakeNowJust/heredoc/v2"
12 "github.com/spf13/cobra"
14 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
16 "gitlab.com/gitlab-org/cli/internal/cmdutils"
17 "gitlab.com/gitlab-org/cli/internal/glrepo"
18 "gitlab.com/gitlab-org/cli/internal/iostreams"
19 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
20 "gitlab.com/gitlab-org/cli/internal/text"
23type options struct {
24 io *iostreams.IOStreams
25 gitlabClient func() (*gitlab.Client, error)
26 baseRepoFunc func() (glrepo.Interface, error)
28 client *gitlab.Client
29 projectID string
31 query string
32 directoryPath string
33 knn int
34 limit int
35 outputFormat string
38type semanticSearchResponse struct {
39 Confidence string `json:"confidence"`
40 Results []searchResult `json:"results"`
43type searchResult struct {
44 Path string `json:"path"`
45 FileURL string `json:"file_url"`
46 Score float64 `json:"score"`
47 SnippetRanges []codeChunk `json:"snippet_ranges"`
50type codeChunk struct {
51 StartLine int `json:"start_line"`
52 EndLine int `json:"end_line"`
53 Content string `json:"content"`
56func NewCmd(f cmdutils.Factory) *cobra.Command {
57 opts := &options{
58 io: f.IO(),
59 gitlabClient: f.GitLabClient,
60 baseRepoFunc: f.BaseRepo,
61 }
63 cmd := &cobra.Command{
64 Use: "semantic [flags]",
65 Short: `Search project code using natural language.`,
66 Long: heredoc.Doc(`
67 Search project code using natural language (semantic similarity).
69 Requires the project to have semantic code search enabled via GitLab Duo.
70 `) + text.BetaString,
71 Example: heredoc.Doc(`
72 # Search for authentication-related code in the current project
73 glab search semantic -q "authentication middleware"
75 # Search within a specific directory
76 glab search semantic -q "rate limiting" -d app/services/
78 # Search in a specific project with JSON output
79 glab search semantic -q "CI pipeline triggers" -R gitlab-org/gitlab --output json
81 # Limit results
82 glab search semantic -q "database migrations" --limit 5
83 `),
84 Args: cobra.NoArgs,
85 ValidArgsFunction: cobra.NoFileCompletions,
86 Annotations: map[string]string{
87 mcpannotations.Safe: "true",
88 },
89 RunE: func(cmd *cobra.Command, args []string) error {
90 if err := opts.validate(); err != nil {
91 return err
92 }
93 if err := opts.complete(); err != nil {
94 return err
95 }
96 return opts.run(cmd.Context())
97 },
98 }
100 fl := cmd.Flags()
101 fl.StringVarP(&opts.query, "query", "q", "", "Natural language search query.")
102 fl.StringVarP(&opts.directoryPath, "directory-path", "d", "", "Restrict search to files under this path (e.g. app/services/).")
103 fl.IntVar(&opts.knn, "knn", 0, "Nearest neighbours to retrieve (1–100). Defaults to 64 server-side.")
104 fl.IntVarP(&opts.limit, "limit", "l", 0, "Maximum number of results (1–100). Defaults to 20 server-side.")
105 cmdutils.EnableJSONOutput(cmd, opts.io, &opts.outputFormat)
107 cobra.CheckErr(cmd.MarkFlagRequired("query"))
109 return cmd
112func (o *options) validate() error {
113 if o.knn != 0 && (o.knn < 1 || o.knn > 100) {
114 return cmdutils.FlagError{Err: fmt.Errorf("--knn must be between 1 and 100, got %d", o.knn)}
115 }
116 if o.limit != 0 && (o.limit < 1 || o.limit > 100) {
117 return cmdutils.FlagError{Err: fmt.Errorf("--limit must be between 1 and 100, got %d", o.limit)}
118 }
119 return nil
122func (o *options) complete() error {
123 var err error
124 o.client, err = o.gitlabClient()
125 if err != nil {
126 return err
127 }
129 baseRepo, err := o.baseRepoFunc()
130 if err != nil {
131 return err
132 }
133 o.projectID = baseRepo.FullName()
135 return nil
138func (o *options) run(ctx context.Context) error {
139 path := fmt.Sprintf("projects/%s/search/semantic", url.PathEscape(o.projectID))
140 req, err := o.client.NewRequest(http.MethodGet, path, nil, []gitlab.RequestOptionFunc{
141 gitlab.WithContext(ctx),
142 })
143 if err != nil {
144 return fmt.Errorf("creating request: %w", err)
145 }
147 q := req.URL.Query()
148 q.Set("q", o.query)
149 if o.directoryPath != "" {
150 q.Set("directory_path", o.directoryPath)
151 }
152 if o.knn != 0 {
153 q.Set("knn", strconv.Itoa(o.knn))
154 }
155 if o.limit != 0 {
156 q.Set("limit", strconv.Itoa(o.limit))
157 }
158 req.URL.RawQuery = q.Encode()
160 if o.outputFormat != "json" {
161 o.io.LogInfof("Searching for %q in %s...\n", o.query, o.projectID)
162 }
164 var result semanticSearchResponse
165 _, err = o.client.Do(req, &result)
166 if err != nil {
167 return fmt.Errorf("semantic search request failed: %w", err)
168 }
170 if o.outputFormat == "json" {
171 return o.io.PrintJSON(result)
172 }
174 return o.printText(&result)
177func (o *options) printText(result *semanticSearchResponse) error {
178 c := o.io.Color()
179 o.io.LogInfof("Confidence: %s\n", result.Confidence)
181 if len(result.Results) == 0 {
182 o.io.LogInfo("\nNo results found.")
183 return nil
184 }
186 o.io.LogInfo()
187 for _, r := range result.Results {
188 o.io.LogInfof("%s (score: %.2f)\n",
189 c.Bold(r.Path), r.Score)
190 for _, chunk := range r.SnippetRanges {
191 o.io.LogInfof(" Lines %d–%d:\n", chunk.StartLine, chunk.EndLine)
192 for line := range strings.SplitSeq(chunk.Content, "\n") {
193 o.io.LogInfof(" %s\n", line)
194 }
195 }
196 o.io.LogInfo()
197 }
199 return nil
Diagnostics8 git calls · 226 ms
CallStatusSizeTime0226 ms
discover200185 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=185
git.server.agent=git/2.56.g1891de4-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=185
git.pkt.payload.max=35
ls-refs20058 KiB · hit3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=59849
git.response.cache=HIT
git.response.duration_ms=3
git.protocol.version=2
git.pkt.frames=731
git.pkt.data=730
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=59849
git.pkt.payload.max=143
git.ls_refs.count=730
git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 1020032 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=32926
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=22
git.pkt.data=20
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=32926
git.pkt.payload.max=32046
git.sideband.band1.bytes=32046
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=80
git.pack.bytes=32046
git.pack.ofs_delta=36
git.pack.ref_delta=0
git.pack.chain.max=2
git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91
git.pack.full=44
Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
git.response.bytes=4273
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=combine:blob:none+tree:2
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=1
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=4273
git.pkt.payload.max=4173
git.sideband.band1.bytes=4173
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=11
git.pack.bytes=4173
git.fetch.wide.objects=11
git.fetch.wide.extra_bytes=3930
git.fetch.wide.blobs=0
fetchtree:02001.8 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=1857
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=dce174ff8c632a19d17eea99c8a9b7c37fb2b592
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=1857
git.pkt.payload.max=1830
git.sideband.band1.bytes=1830
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1830
git.pack.trailer=fb03183b7909876778f740d196d7516811003517
Open pack map
fetchtree:0200144 B · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=144
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=1884ac3ced38987dfbc036c31558ff615a88ec05
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=144
git.pkt.payload.max=117
git.sideband.band1.bytes=117
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=117
git.pack.trailer=b2a0477ed58eb03d308b0682ea31444d887c3f4f
Open pack map
fetchtree:0200140 B · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=140
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=fd5b978a9df3e535b3cb674347bc718a7d5f1dfd
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=140
git.pkt.payload.max=113
git.sideband.band1.bytes=113
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=113
git.pack.trailer=1bd24f5093bc9133b8023c8b1c003692fdbf56bb
Open pack map
fetch2002.1 KiB211 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=211
git.response.bytes=2119
git.response.cache=MISS
git.response.duration_ms=211
git.fetch.wants=0c30d0484c3bbfce856bf94350b7b6d194acfe81
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=2119
git.pkt.payload.max=2092
git.sideband.band1.bytes=2092
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://gitlab.com/gitlab-org/cli
Ref
refs/heads/main
Commit
0dd7574dce9e385c5c30540e6c15657adc6bdce9
cf-ray
a5e2e089ab352308
Request
e4af26ad…
Protocol
v2 · sha1
Agent
git/2.56.g1891de4-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format