GitLabgitlab-org

cli

main
ply8 git calls211 ms99 KiB5 cachedprotocol v2
150 lines4.2 KiBRaw
1package set
3import (
4 "fmt"
5 "strings"
7 "github.com/MakeNowJust/heredoc/v2"
8 "github.com/google/shlex"
9 "github.com/spf13/cobra"
11 "gitlab.com/gitlab-org/cli/internal/cmdutils"
12 "gitlab.com/gitlab-org/cli/internal/config"
13 "gitlab.com/gitlab-org/cli/internal/iostreams"
14 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
17type options struct {
18 io *iostreams.IOStreams
19 config func() config.Config
21 name string
22 expansion string
23 isShell bool
24 rootCmd *cobra.Command
27func NewCmdSet(f cmdutils.Factory) *cobra.Command {
28 opts := &options{
29 config: f.Config,
30 io: f.IO(),
31 }
33 aliasSetCmd := &cobra.Command{
34 Use: "set <alias name> '<command>' [flags]",
35 Short: `Set an alias for a longer command.`,
36 Long: heredoc.Docf(`
37 Declare a word as an alias for a longer command.
39 Use quotation marks when you define a command, as shown in the examples.
41 Your expansion can include arguments and flags. If your expansion
42 includes positional placeholders such as %[1]s$1%[1]s or %[1]s$2%[1]s, any extra
43 arguments that follow the invocation of the alias are inserted into
44 those placeholders.
46 To run an alias through %[1]ssh%[1]s, a shell converter, specify the
47 %[1]s--shell%[1]s flag. With shell conversion, you can compose commands
48 with %[1]s|%[1]s or redirect with %[1]s>%[1]s. Shell aliases have these caveats:
50 - Extra arguments that follow the alias are not passed to the expansion.
51 To accept arguments, use %[1]s$1%[1]s, %[1]s$2%[1]s, and so on.
52 - To accept all arguments, use %[1]s$@%[1]s.
54 On Windows, shell aliases run through %[1]ssh%[1]s as installed by Git for
55 Windows. If you installed Git in another way on Windows, shell aliases
56 might not work.
57 `, "`"),
58 Example: heredoc.Doc(`
59 # Define an alias for "mr view"
60 glab alias set mrv 'mr view'
61 # Run the alias; it expands to "glab mr view -w 123"
62 glab mrv -w 123
64 # Define an alias with a positional placeholder
65 glab alias set createissue 'issue create --title "$1"'
66 # Run the alias with an argument and an extra flag
67 glab createissue "My Issue" --description "Something is broken."
69 # Define a shell alias that pipes glab output to grep
70 glab alias set --shell igrep 'glab issue list --assignee="$1" | grep $2'
71 # Run the shell alias with two arguments
72 glab igrep user foo
73 `),
74 Args: cobra.ExactArgs(2),
75 ValidArgsFunction: cobra.NoFileCompletions,
76 Annotations: map[string]string{
77 mcpannotations.Destructive: "true",
78 },
79 RunE: func(cmd *cobra.Command, args []string) error {
80 opts.complete(cmd, args)
82 return opts.run()
83 },
84 }
85 aliasSetCmd.Flags().BoolVarP(&opts.isShell, "shell", "s", false, "Declare an alias to be passed through a shell interpreter.")
86 return aliasSetCmd
89func (o *options) complete(cmd *cobra.Command, args []string) {
90 o.rootCmd = cmd.Root()
91 o.name = args[0]
92 o.expansion = args[1]
95func (o *options) run() error {
96 c := o.io.Color()
97 cfg := o.config()
99 aliasCfg, err := cfg.Aliases()
100 if err != nil {
101 return err
102 }
104 if o.io.IsaTTY && o.io.IsErrTTY {
105 o.io.LogErrorf("- Adding alias for %s: %s.\n", c.Bold(o.name), c.Bold(o.expansion))
106 }
108 expansion := o.expansion
109 isShell := o.isShell
110 if isShell && !strings.HasPrefix(expansion, "!") {
111 expansion = "!" + expansion
112 }
113 isShell = strings.HasPrefix(expansion, "!")
115 if validCommand(o.rootCmd, o.name) {
116 return fmt.Errorf("could not create alias: %q is already a glab command", o.name)
117 }
119 if !isShell && !validCommand(o.rootCmd, expansion) {
120 return fmt.Errorf("could not create alias: %s does not correspond to a glab command", expansion)
121 }
123 successMsg := fmt.Sprintf("%s Added alias.", c.Green("✓"))
124 if oldExpansion, ok := aliasCfg.Get(o.name); ok {
125 successMsg = fmt.Sprintf("%s Changed alias %s from %s to %s.",
126 c.Green("✓"),
127 c.Bold(o.name),
128 c.Bold(oldExpansion),
129 c.Bold(expansion),
130 )
131 }
133 err = aliasCfg.Set(o.name, expansion)
134 if err != nil {
135 return fmt.Errorf("could not create alias: %w", err)
136 }
138 o.io.LogError(successMsg)
139 return nil
142func validCommand(rootCmd *cobra.Command, expansion string) bool {
143 split, err := shlex.Split(expansion)
144 if err != nil {
145 return false
146 }
148 cmd, _, err := rootCmd.Traverse(split)
149 return err == nil && cmd != rootCmd
Diagnostics8 git calls · 211 ms
CallStatusSizeTime0211 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 KiB8 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=8
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 · 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:0 · deepen 1020032 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=32926
git.response.cache=HIT
git.response.duration_ms=4
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
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=fb03183b7909876778f740d196d7516811003517
Open pack map
fetchtree:0200256 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=256
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=9b63d874f7ad9ca8ff5a684fa9f649afab76eddc
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=256
git.pkt.payload.max=229
git.sideband.band1.bytes=229
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=229
git.pack.trailer=21020800d0a123456c692912cb576372452d51ba
Open pack map
fetchtree:0200140 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=140
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=b47d2886ac22d7dc6502e1d43cef71c40abf4fcd
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=a6def49bfc6de675d5709a7ab0a9f413d249fa57
Open pack map
fetch2001.8 KiB191 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=191
git.response.bytes=1821
git.response.cache=MISS
git.response.duration_ms=191
git.fetch.wants=51e61fc7fa243a323b85bb933c2d1aa25c23de2b
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=1821
git.pkt.payload.max=1794
git.sideband.band1.bytes=1794
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
a5e2a626ff261293
Request
64474efe…
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