GitLabgitlab-org
cli
main
1package navigate
3import (
4 "errors"
5 "fmt"
7 "charm.land/huh/v2"
8 "github.com/MakeNowJust/heredoc/v2"
9 "github.com/spf13/cobra"
11 "gitlab.com/gitlab-org/cli/internal/cmdutils"
12 "gitlab.com/gitlab-org/cli/internal/git"
13 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
14 "gitlab.com/gitlab-org/cli/internal/text"
15)
17func baseCommand() (git.Stack, error) {
18 title, err := git.GetCurrentStackTitle()
19 if err != nil {
20 return git.Stack{}, err
21 }
23 stack, err := git.GatherStackRefs(title)
24 if err != nil {
25 return git.Stack{}, err
26 }
28 return stack, nil
29}
31func NewCmdStackFirst(f cmdutils.Factory, gr git.GitRunner) *cobra.Command {
32 return &cobra.Command{
33 Use: "first",
34 Short: "Move to the first diff in the stack. (EXPERIMENTAL)",
35 Long: "Checks out the branch of the first diff in the stack.\n" + text.ExperimentalString,
36 Example: heredoc.Doc(`
37 glab stack first`),
38 Args: cobra.NoArgs,
39 ValidArgsFunction: cobra.NoFileCompletions,
40 Annotations: map[string]string{
41 mcpannotations.Destructive: "true",
42 },
43 RunE: func(cmd *cobra.Command, args []string) error {
44 stack, err := baseCommand()
45 if err != nil {
46 return err
47 }
49 if stack.Empty() {
50 return errors.New("you are on an empty stack; to use a stack, first save a diff")
51 }
53 ref := stack.First()
54 err = git.CheckoutBranch(ref.Branch, gr)
55 if err != nil {
56 return err
57 }
59 switchMessage(f, &ref)
61 return nil
62 },
63 }
64}
66func NewCmdStackNext(f cmdutils.Factory, gr git.GitRunner) *cobra.Command {
67 return &cobra.Command{
68 Use: "next",
69 Short: "Move to the next diff in the stack. (EXPERIMENTAL)",
70 Long: "Checks out the branch of the next diff in the stack.\n" + text.ExperimentalString,
71 Example: heredoc.Doc(`
72 glab stack next`),
73 Args: cobra.NoArgs,
74 ValidArgsFunction: cobra.NoFileCompletions,
75 Annotations: map[string]string{
76 mcpannotations.Destructive: "true",
77 },
78 RunE: func(cmd *cobra.Command, args []string) error {
79 stack, err := baseCommand()
80 if err != nil {
81 return err
82 }
84 ref, err := git.CurrentStackRefFromCurrentBranch(stack.Title)
85 if err != nil {
86 return err
87 }
89 if ref.IsLast() {
90 return errors.New("you are already at the last diff; use `glab stack list` to see the complete list")
91 }
93 err = git.CheckoutBranch(stack.Refs[ref.Next].Branch, gr)
94 if err != nil {
95 return err
96 }
98 next := stack.Refs[ref.Next]
99 switchMessage(f, &next)
101 return nil
102 },
103 }
104}
106func NewCmdStackPrev(f cmdutils.Factory, gr git.GitRunner) *cobra.Command {
107 return &cobra.Command{
108 Use: "prev",
109 Short: "Move to the previous diff in the stack. (EXPERIMENTAL)",
110 Long: "Checks out the branch of the previous diff in the stack.\n" + text.ExperimentalString,
111 Example: heredoc.Doc(`
112 glab stack prev`),
113 Args: cobra.NoArgs,
114 ValidArgsFunction: cobra.NoFileCompletions,
115 Annotations: map[string]string{
116 mcpannotations.Destructive: "true",
117 },
118 RunE: func(cmd *cobra.Command, args []string) error {
119 stack, err := baseCommand()
120 if err != nil {
121 return err
122 }
124 ref, err := git.CurrentStackRefFromCurrentBranch(stack.Title)
125 if err != nil {
126 return err
127 }
129 if ref.IsFirst() {
130 return errors.New("you are already at the first diff; use `glab stack list` to see the complete list")
131 }
133 err = git.CheckoutBranch(stack.Refs[ref.Prev].Branch, gr)
134 if err != nil {
135 return err
136 }
138 prev := stack.Refs[ref.Prev]
139 switchMessage(f, &prev)
141 return nil
142 },
143 }
144}
146func NewCmdStackLast(f cmdutils.Factory, gr git.GitRunner) *cobra.Command {
147 return &cobra.Command{
148 Use: "last",
149 Short: "Move to the last diff in the stack. (EXPERIMENTAL)",
150 Long: "Checks out the branch of the last diff in the stack.\n" + text.ExperimentalString,
151 Example: heredoc.Doc(`
152 glab stack last`),
153 Args: cobra.NoArgs,
154 ValidArgsFunction: cobra.NoFileCompletions,
155 Annotations: map[string]string{
156 mcpannotations.Destructive: "true",
157 },
158 RunE: func(cmd *cobra.Command, args []string) error {
159 stack, err := baseCommand()
160 if err != nil {
161 return err
162 }
164 if stack.Empty() {
165 return errors.New("stack is empty until you save a diff")
166 }
168 ref := stack.Last()
170 err = git.CheckoutBranch(ref.Branch, gr)
171 if err != nil {
172 return err
173 }
175 switchMessage(f, &ref)
177 return nil
178 },
179 }
180}
182func NewCmdStackMove(f cmdutils.Factory, gr git.GitRunner) *cobra.Command {
183 return &cobra.Command{
184 Use: "move",
185 Short: "Move to a specific diff in the stack. (EXPERIMENTAL)",
186 Long: heredoc.Docf(`
187 Shows a list of the diffs in the stack, and checks out the branch of the diff you select.
189 To work on a different stack, run %[1]sglab stack switch%[1]s first.
190 `, "`") + text.ExperimentalString,
191 Example: heredoc.Doc(`
192 glab stack move`),
193 Args: cobra.NoArgs,
194 ValidArgsFunction: cobra.NoFileCompletions,
195 Annotations: map[string]string{
196 mcpannotations.Destructive: "true",
197 },
198 RunE: func(cmd *cobra.Command, args []string) error {
199 stack, err := baseCommand()
200 if err != nil {
201 return err
202 }
204 options := make([]huh.Option[string], 0)
205 i := 1
206 for ref := range stack.Iter() {
207 label := fmt.Sprintf("%s - %d: %s", ref.Branch, i, ref.Description)
208 options = append(options, huh.NewOption(label, ref.Branch))
209 i++
210 }
212 var branch string
213 selector := huh.NewSelect[string]().
214 Title("Choose a diff to be checked out:").
215 Options(options...).
216 Value(&branch)
218 err = f.IO().Run(cmd.Context(), selector)
219 if err != nil {
220 return err
221 }
223 err = git.CheckoutBranch(branch, gr)
224 if err != nil {
225 return err
226 }
228 return nil
229 },
230 }
231}
233func switchMessage(f cmdutils.Factory, ref *git.StackRef) {
234 color := f.IO().Color()
235 fmt.Printf(
236 "%v Switched to branch: %v - %v\n",
237 color.ProgressIcon(),
238 color.Blue(ref.Branch),
239 color.Bold(ref.Description),
240 )
241}
Diagnostics8 git calls · 275 ms
CallStatusSizeTime0275 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 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 KiB3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=59849 git.response.cache=UPDATING 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
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit2 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=4273 git.response.cache=HIT git.response.duration_ms=2 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:0 · deepen 1020032 KiB · hit3 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=3 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=44Open pack map
fetchtree:02001.8 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=1857 git.response.cache=HIT git.response.duration_ms=2 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=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200432 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=432 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=ff2d1e7abc74964e5679ebdb51f1ac0d0dc3aff0 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=432 git.pkt.payload.max=405 git.sideband.band1.bytes=405 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=405 git.pack.trailer=8f3029a269217d94d12d0671e0490fe3d5506303Open pack map
fetchtree:0200115 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=115 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=0fcb0fe4bbe367c5c938e2a3f95a5d5b3f1c4aae 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=115 git.pkt.payload.max=88 git.sideband.band1.bytes=88 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=88 git.pack.trailer=22f300a48c4fb4a00108055be6ecf79c3ca8cd97Open pack map
fetch2001.4 KiB263 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=263 git.response.bytes=1386 git.response.cache=MISS git.response.duration_ms=263 git.fetch.wants=bfbdc3079d8c3d31c98570224dd94ca4dfbbec8b 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=1386 git.pkt.payload.max=1359 git.sideband.band1.bytes=1359 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
- a5e18ce49cea5e37
- Request
- 468e6f56…
- 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