GitLabgitlab-org
cli
main
1package delete
3import (
4 "context"
5 "errors"
6 "fmt"
7 "os"
8 "slices"
10 "github.com/MakeNowJust/heredoc/v2"
11 "github.com/spf13/cobra"
13 "gitlab.com/gitlab-org/cli/internal/cmdutils"
14 "gitlab.com/gitlab-org/cli/internal/git"
15 "gitlab.com/gitlab-org/cli/internal/iostreams"
16 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
17 "gitlab.com/gitlab-org/cli/internal/text"
18 "gitlab.com/gitlab-org/cli/internal/utils"
19)
21var errNoStacksFound = errors.New("no stacks found; create one with \"glab stack create\"")
23type options struct {
24 io *iostreams.IOStreams
26 name string
27 skipConfirmation bool
28}
30func NewCmdDeleteStack(f cmdutils.Factory) *cobra.Command {
31 opts := &options{
32 io: f.IO(),
33 }
35 stackDeleteCmd := &cobra.Command{
36 Use: "delete [<stack-name>]",
37 Short: "Delete a stack. (EXPERIMENTAL)",
38 Long: heredoc.Docf(`
39 Removes the stack's local metadata from the %[1]s.git/stacked%[1]s directory.
40 Use this command to clean up stacks for merged or abandoned merge requests.
41 Branches, commits, and merge requests are not affected.
43 If you do not provide a stack name, the command shows a list of stacks for you to choose from.
44 `, "`") + text.ExperimentalString,
45 Example: heredoc.Doc(`
46 # Interactively pick from the list of available stacks
47 glab stack delete
49 # Delete a specific stack by name
50 glab stack delete <stack-name>
52 # Delete a specific stack without the confirmation prompt
53 glab stack delete <stack-name> -y`),
54 Annotations: map[string]string{
55 mcpannotations.Destructive: "true",
56 },
57 Args: cobra.MaximumNArgs(1),
58 RunE: func(cmd *cobra.Command, args []string) error {
59 opts.complete(args)
61 if err := opts.validate(); err != nil {
62 return err
63 }
65 if err := opts.run(cmd.Context()); err != nil {
66 if errors.Is(err, iostreams.ErrUserCancelled) {
67 return err
68 }
69 return fmt.Errorf("deleting stack failed: %w", err)
70 }
72 return nil
73 },
74 }
76 stackDeleteCmd.Flags().BoolVarP(&opts.skipConfirmation, "yes", "y", false, "Skip the confirmation prompt.")
78 return stackDeleteCmd
79}
81func (o *options) complete(args []string) {
82 if len(args) > 0 {
83 o.name = args[0]
84 }
85}
87func (o *options) validate() error {
88 if o.io.PromptEnabled() {
89 return nil
90 }
92 if o.name == "" {
93 return cmdutils.FlagError{Err: errors.New("the <stack-name> argument is required when prompts are disabled")}
94 }
96 if !o.skipConfirmation {
97 return cmdutils.FlagError{Err: errors.New("--yes or -y flag is required when not running interactively")}
98 }
100 return nil
101}
103func (o *options) run(ctx context.Context) error {
104 stacks, err := git.GetStacks()
105 if err != nil {
106 // The stacked directory does not exist until the first "glab stack create".
107 if errors.Is(err, os.ErrNotExist) {
108 return errNoStacksFound
109 }
110 return fmt.Errorf("getting stacks: %w", err)
111 }
113 if len(stacks) == 0 {
114 return errNoStacksFound
115 }
117 if o.name == "" {
118 names := make([]string, 0, len(stacks))
119 for _, s := range stacks {
120 names = append(names, s.Title)
121 }
123 if err := o.io.Select(ctx, &o.name, "Choose a stack to delete:", names); err != nil {
124 return err
125 }
126 }
128 if !slices.ContainsFunc(stacks, func(s git.Stack) bool { return s.Title == o.name }) {
129 return fmt.Errorf("no stack named %q found", o.name)
130 }
132 if !o.skipConfirmation {
133 if err := o.confirmDeletion(ctx); err != nil {
134 return err
135 }
136 }
138 currentStack, err := git.GetCurrentStackTitle()
139 if err != nil && !errors.Is(err, git.ErrNoCurrentStack) {
140 return err
141 }
143 // Clear the current stack before removing the directory so a failure leaves the stack intact.
144 clearedCurrent := false
145 if err == nil && currentStack == o.name {
146 if err := git.ClearCurrentStackTitle(o.name); err != nil {
147 return err
148 }
149 clearedCurrent = true
150 }
152 if err := git.RemoveStackRefDir(o.name); err != nil {
153 if clearedCurrent {
154 if restoreErr := git.SetCurrentStackTitle(o.name); restoreErr != nil {
155 return errors.Join(err, restoreErr)
156 }
157 }
158 return err
159 }
161 o.io.LogInfof("Deleted stack %s.\n", o.name)
163 return nil
164}
166func (o *options) confirmDeletion(ctx context.Context) error {
167 // Corrupted refs should not block deletion, so a failure only skips the count.
168 if stack, err := git.GatherStackRefs(o.name); err == nil && !stack.Empty() {
169 o.io.LogInfof("Stack %s still has %s.\n", o.name, utils.Pluralize(len(stack.Refs), "diff"))
170 }
172 o.io.LogInfof("Deleting a stack removes its local metadata. Branches and merge requests are not affected.\n\n")
174 var confirmed bool
175 if err := o.io.Confirm(ctx, &confirmed, fmt.Sprintf("Delete stack %s?", o.name)); err != nil {
176 return cmdutils.WrapError(err, "could not prompt")
177 }
179 if !confirmed {
180 return cmdutils.CancelError()
181 }
183 return nil
184}
Diagnostics8 git calls · 247 ms
CallStatusSizeTime0247 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 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=2 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 · 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=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:0200144 B · 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=144 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=a9671173857fdf27e1a1fe66cc3b47ec8aaea508 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=8258e60188cdbd99cac86c39f849cee8a2cf4d7eOpen pack map
fetch2001.8 KiB233 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=233 git.response.bytes=1845 git.response.cache=MISS git.response.duration_ms=233 git.fetch.wants=d94fcb0d840e89179838eec51f5c0ae3a76fce81 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=1845 git.pkt.payload.max=1818 git.sideband.band1.bytes=1818 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
- a5e16d5afbf74b2a
- Request
- 2049d028…
- 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