GitLabgitlab-org

cli

main
ply8 git calls293 ms99 KiB5 cachedprotocol v2
166 lines5.2 KiBRaw
1package save
3import (
4 "context"
5 "fmt"
6 "time"
8 "github.com/MakeNowJust/heredoc/v2"
9 "github.com/briandowns/spinner"
10 "github.com/spf13/cobra"
12 "gitlab.com/gitlab-org/cli/internal/cmdutils"
13 "gitlab.com/gitlab-org/cli/internal/commands/stack/stackutils"
14 "gitlab.com/gitlab-org/cli/internal/git"
15 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
16 "gitlab.com/gitlab-org/cli/internal/run"
17 "gitlab.com/gitlab-org/cli/internal/text"
20func NewCmdAmendStack(f cmdutils.Factory, gr git.GitRunner, getText cmdutils.GetTextUsingEditor) *cobra.Command {
21 var amendStageAll bool
22 var noVerify bool
23 var reword bool
24 stackSaveCmd := &cobra.Command{
25 Use: "amend",
26 Short: `Save your changes to an existing diff. (EXPERIMENTAL)`,
27 Long: heredoc.Docf(`
28 Adds your changes to the diff you have checked out. Its merge request updates the next time you run %[1]sglab stack sync%[1]s, which also rebases the diffs after it.
30 To create a new diff from your changes instead, use %[1]sglab stack save%[1]s.
31 `, "`") + text.ExperimentalString,
32 Example: heredoc.Doc(`
33 # Amend diff with currently staged changes
34 glab stack amend -m "Fix a function"
36 # Add specified file to staged changes and amend diff
37 glab stack amend newfile -m "forgot to add this"
39 # Add all tracked files to staged changes and amend diff
40 glab stack amend -a -m "fixed a function in exisiting file"
42 # Add all tracked and untracked files to staged changes and amend diff
43 glab stack amend . -m "refactored file into new files"
45 # Reword the commit message without adding any files
46 glab stack amend --reword -m "updated commit message"`),
47 Args: cobra.ArbitraryArgs,
48 Annotations: map[string]string{
49 mcpannotations.Destructive: "true",
50 },
51 RunE: func(cmd *cobra.Command, args []string) error {
52 output, err := amendFunc(cmd.Context(), f, args, getText, description, amendStageAll, noVerify, reword)
53 if err != nil {
54 return fmt.Errorf("could not run stack amend: %w", err)
55 }
57 if f.IO().IsOutputTTY() {
58 f.IO().LogInfof("%s", output)
59 }
61 return nil
62 },
63 }
64 stackSaveCmd.Flags().StringVarP(&description, "description", "d", "", "A description of the change.")
65 stackSaveCmd.Flags().StringVarP(&description, "message", "m", "", "Alias for the description flag.")
66 stackSaveCmd.Flags().BoolVarP(&amendStageAll, "all", "a", false, "Automatically stage modified and deleted tracked files.")
67 stackSaveCmd.Flags().BoolVar(&noVerify, "no-verify", false, "Bypass the pre-commit and commit-msg hooks of git-commit(1).")
68 stackSaveCmd.Flags().BoolVar(&reword, "reword", false, "Only update the commit message without staging any files.")
69 stackSaveCmd.MarkFlagsMutuallyExclusive("message", "description")
70 stackSaveCmd.MarkFlagsMutuallyExclusive("all", "reword")
72 return stackSaveCmd
75func amendFunc(ctx context.Context, f cmdutils.Factory, args []string, getText cmdutils.GetTextUsingEditor, description string, stageAll, noVerify, reword bool) (string, error) {
76 if reword && len(args) > 0 {
77 return "", fmt.Errorf("--reword cannot be used with file arguments")
78 }
80 if err := stackutils.CheckNoRebaseInProgress(); err != nil {
81 return "", err
82 }
84 if !reword {
85 // check if there are even any changes before we start
86 err := checkForChanges()
87 if err != nil {
88 return "", fmt.Errorf("could not save: %w", err)
89 }
90 }
92 title, err := git.GetCurrentStackTitle()
93 if err != nil {
94 return "", fmt.Errorf("error running Git command: %w", err)
95 }
97 ref, err := git.CurrentStackRefFromCurrentBranch(title)
98 if err != nil {
99 return "", fmt.Errorf("error checking for stack: %w", err)
100 }
102 if ref.Branch == "" {
103 return "", fmt.Errorf("not currently in a stack; change to the branch you want to amend")
104 }
106 // a description is required, so ask if one is not provided
107 if description == "" {
108 description, err = promptForCommit(ctx, f, getText, ref.Description)
109 if err != nil {
110 return "", fmt.Errorf("error getting commit message: %w", err)
111 }
112 }
114 s := spinner.New(spinner.CharSets[11], 100*time.Millisecond)
116 if !reword {
117 // git add files
118 err = addFiles(args[0:], stageAll)
119 if err != nil {
120 return "", fmt.Errorf("error adding files: %w", err)
121 }
122 }
124 // run the amend commit
125 err = gitAmend(description, noVerify, reword)
126 if err != nil {
127 return "", fmt.Errorf("error amending commit with Git: %w", err)
128 }
130 // update the stack ref description to match the new commit message
131 ref.Description = description
132 err = git.UpdateStackRefFile(title, ref)
133 if err != nil {
134 return "", fmt.Errorf("error updating stack ref: %w", err)
135 }
137 var output string
138 if f.IO().IsOutputTTY() {
139 output = fmt.Sprintf("Amended diff with description: %q.\n", description)
140 }
142 s.Stop()
144 return output, nil
147func gitAmend(description string, noVerify, reword bool) error {
148 args := []string{"commit", "--amend", "-m", description}
149 if noVerify {
150 args = append(args, "--no-verify")
151 }
152 if reword {
153 // --only rewords the commit message without committing any
154 // changes that may already be staged in the index.
155 args = append(args, "--only")
156 }
157 amendCmd := git.GitCommand(args...)
158 output, err := run.PrepareCmd(amendCmd).Output()
159 if err != nil {
160 return fmt.Errorf("error running Git command: %w", err)
161 }
163 fmt.Println("Amend commit: ", string(output))
165 return nil
Diagnostics8 git calls · 293 ms
CallStatusSizeTime0293 ms
discover200185 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
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=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 · 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 · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=5
git.response.bytes=1857
git.response.cache=HIT
git.response.duration_ms=4
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:0200432 B · hit5 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=5
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=8f3029a269217d94d12d0671e0490fe3d5506303
Open pack map
fetchtree:0200254 B · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=276
git.response.bytes=254
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=1bddb68dc447e75c7fc2f3cd02e93e572448d630
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=254
git.pkt.payload.max=227
git.sideband.band1.bytes=227
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=227
git.pack.trailer=5113828f2359f89a3269ef1119ad801c584c57a8
Open pack map
fetch2001.9 KiB276 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=1983
git.response.cache=MISS
git.response.duration_ms=276
git.fetch.wants=c0e09d7800fc7afe78f5fd445fad2e375bd9afca
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=1983
git.pkt.payload.max=1956
git.sideband.band1.bytes=1956
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
a5e1dc9e9e8fecfa
Request
0b5a19b4…
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