GitLabgitlab-org

cli

main
ply7 git calls33 ms99 KiB6 cachedprotocol v2
406 lines8.8 KiBRaw
1package git
3import (
4 "encoding/json"
5 "errors"
6 "fmt"
7 "io/fs"
8 "iter"
9 "os"
10 "path/filepath"
11 "strings"
13 "gitlab.com/gitlab-org/cli/internal/config"
16type StackRef struct {
17 Prev string `json:"prev"`
18 Branch string `json:"branch"`
19 SHA string `json:"sha"`
20 Next string `json:"next"`
21 MR string `json:"mr"`
22 Description string `json:"description"`
25// Stack represents a stacked diff data structure.
26// Refs are structured as a doubly linked list where
27// the links are identified with the StackRef.Prev
28// and StackRef.Next fields.
29// The StackRef.SHA is the id that the former two
30// fields can point to.
31// All stacks must be created with GatherStackRefs
32// which validates the stack for consistency.
33type Stack struct {
34 Title string
35 Refs map[string]StackRef
38func (s Stack) Empty() bool { return len(s.Refs) == 0 }
40func (s *Stack) RemoveRef(ref StackRef, gr GitRunner) error {
41 if ref.IsFirst() && ref.IsLast() {
42 // this is the only ref, so just remove it
43 err := DeleteStackRefFile(s.Title, ref)
44 delete(s.Refs, ref.SHA)
45 if err != nil {
46 return fmt.Errorf("could not delete reference file: %w", err)
47 }
49 return nil
50 }
52 err := s.adjustAdjacentRefs(ref)
53 if err != nil {
54 return fmt.Errorf("error adjusting next reference: %w", err)
55 }
57 err = DeleteStackRefFile(s.Title, ref)
58 if err != nil {
59 return fmt.Errorf("could not delete reference file: %w", err)
60 }
62 err = s.RemoveBranch(ref, gr)
63 if err != nil {
64 return fmt.Errorf("could not remove branch: %w", err)
65 }
67 delete(s.Refs, ref.SHA)
69 return nil
72func (s *Stack) RemoveBranch(ref StackRef, gr GitRunner) error {
73 var branch string
74 var err error
76 if ref.IsFirst() {
77 branch, err = s.BaseBranch(gr)
78 if err != nil {
79 return err
80 }
82 } else {
83 branch = s.Refs[ref.Prev].Branch
84 }
86 err = CheckoutBranch(branch, gr)
87 if err != nil {
88 return err
89 }
91 err = DeleteLocalBranch(ref.Branch, gr)
92 if err != nil {
93 return err
94 }
96 return nil
99func (s *Stack) adjustAdjacentRefs(ref StackRef) error {
100 refs := s.Refs
102 if ref.Prev != "" {
103 prev := refs[ref.Prev]
104 delete(refs, ref.Prev)
106 prev.Next = ref.Next
107 refs[ref.Prev] = prev
109 err := UpdateStackRefFile(s.Title, prev)
110 if err != nil {
111 return fmt.Errorf("could not update reference file: %w", err)
112 }
113 }
115 if ref.Next != "" {
116 next := refs[ref.Next]
117 delete(refs, ref.Next)
119 next.Prev = ref.Prev
120 refs[ref.Next] = next
122 err := UpdateStackRefFile(s.Title, next)
123 if err != nil {
124 return fmt.Errorf("could not update reference file: %w", err)
125 }
126 }
128 return nil
131func (s *Stack) IndexAt(ref StackRef) int {
132 for i, r := range s.Iter2() {
133 if r == ref {
134 return i
135 }
136 }
138 return -1
141func (s *Stack) Last() StackRef {
142 if s.Empty() {
143 return StackRef{}
144 }
146 for _, ref := range s.Refs {
147 if ref.IsLast() {
148 return ref
149 }
150 }
152 // All Stacks should be created with GatherStackRefs which validates the Stack consistency.
153 panic(errors.New("can't find the last ref in the chain; data might be corrupted"))
156func (s *Stack) First() StackRef {
157 if s.Empty() {
158 return StackRef{}
159 }
161 for _, ref := range s.Refs {
162 if ref.IsFirst() {
163 return ref
164 }
165 }
167 // All Stacks should be created with GatherStackRefs which validates the Stack consistency.
168 panic(errors.New("can't find the first ref in the chain; data might be corrupted"))
171// Iter returns an iterator to range from the first to the last ref in the stack.
172func (s *Stack) Iter() iter.Seq[StackRef] {
173 return func(yield func(StackRef) bool) {
174 ref := s.First()
175 for !ref.Empty() {
176 if !yield(ref) {
177 return
178 }
180 ref = s.Refs[ref.Next]
181 }
182 }
185func (s *Stack) Branches() []string {
186 branches := []string{}
187 for ref := range s.Iter() {
188 branches = append(branches, ref.Branch)
189 }
191 return branches
194// Iter2 returns an iterator like Iter, but includes an index
195func (s *Stack) Iter2() iter.Seq2[int, StackRef] {
196 return func(yield func(int, StackRef) bool) {
197 ref := s.First()
198 i := 0
200 for !ref.Empty() {
201 if !yield(i, ref) {
202 return
203 }
205 i++
206 ref = s.Refs[ref.Next]
207 }
208 }
211func (s *Stack) BaseBranch(gr GitRunner) (string, error) {
212 root, err := StackRootDir(s.Title)
213 if err != nil {
214 return "", fmt.Errorf("could not determine stack root: %w", err)
215 }
217 filename := filepath.Join(root, BaseBranchFile)
219 // we do have a base branch in the metadata
220 fileInfo, err := os.Stat(filename)
221 if err == nil && !fileInfo.IsDir() {
222 trimmed, err := config.TrimmedFileContents(filename)
223 if err != nil {
224 return "", fmt.Errorf("could not read base branch file: %w", err)
225 }
227 return trimmed, nil
228 }
230 // if there's an error reading the file, show that.
231 // it's ok if doesn't exist yet, however.
232 if err != nil && !errors.Is(err, os.ErrNotExist) {
233 return "", fmt.Errorf("could not access base branch file: %w", err)
234 }
236 // no metadata file - lets try to get it from git
237 defBranchOutput, err := gr.Git("remote", "show", DefaultRemote)
238 if err != nil {
239 return "", fmt.Errorf("could not get remote data: %w", err)
240 }
242 branch, err := ParseDefaultBranch([]byte(defBranchOutput))
243 if err != nil {
244 return "", fmt.Errorf("could not parse default branch from remote data: %w", err)
245 }
247 return branch, nil
250func AddStackBaseBranch(title string, branch string) error {
251 root, err := StackRootDir(title)
252 if err != nil {
253 return fmt.Errorf("could not determine stack root: %w", err)
254 }
256 filename := filepath.Join(root, BaseBranchFile)
258 // os.WriteFile creates the file itself; the previous os.Create left an open
259 // file handle that was never closed, which on Windows blocked the file from
260 // being removed afterwards.
261 if err := os.WriteFile(filename, []byte(branch), 0o644); err != nil {
262 return fmt.Errorf("error adding branch metadata file %v: %w", filename, err)
263 }
265 return nil
268func GatherStackRefs(title string) (Stack, error) {
269 stack := Stack{Title: title}
270 stack.Refs = make(map[string]StackRef)
272 root, err := StackRootDir(title)
273 if err != nil {
274 return stack, err
275 }
277 err = filepath.WalkDir(root, func(dir string, d fs.DirEntry, err error) error {
278 if err != nil {
279 return err
280 }
282 if d.IsDir() {
283 return nil
284 }
285 // read files in the stacked ref directory
286 // TODO: this may be quicker if we introduce a package
287 // https://github.com/bmatcuk/doublestar
288 if filepath.Ext(d.Name()) == ".json" {
289 data, err := os.ReadFile(dir)
290 if err != nil {
291 return err
292 }
294 // marshal them into our StackRef type
295 stackRef := StackRef{}
296 err = json.Unmarshal(data, &stackRef)
297 if err != nil {
298 return err
299 }
301 stack.Refs[stackRef.SHA] = stackRef
302 }
304 return nil
305 })
306 if err != nil {
307 if os.IsNotExist(err) { // there might not be any refs yet, this is ok.
308 return stack, nil
309 } else {
310 return stack, err
311 }
312 }
314 err = validateStackRefs(stack)
315 if err != nil {
316 return Stack{}, err
317 }
319 return stack, nil
322func validateStackRefs(s Stack) error {
323 endRefs := 0
324 startRefs := 0
326 if s.Empty() {
327 // empty stacks are okay.
328 return nil
329 }
331 for _, ref := range s.Refs {
332 if ref.IsFirst() {
333 startRefs++
334 }
336 if ref.IsLast() {
337 endRefs++
338 }
340 if endRefs > 1 || startRefs > 1 {
341 return errors.New("more than one end or start ref detected; data might be corrupted")
342 }
343 }
345 if startRefs != 1 {
346 return errors.New("expected exactly one start ref; data might be corrupted")
347 }
348 if endRefs != 1 {
349 return errors.New("expected exactly one end ref; data might be corrupted")
350 }
351 return nil
354func CurrentStackRefFromCurrentBranch(title string) (StackRef, error) {
355 stack, err := GatherStackRefs(title)
356 if err != nil {
357 return StackRef{}, err
358 }
360 branch, err := CurrentBranch()
361 if err != nil {
362 return StackRef{}, err
363 }
365 return stack.RefFromBranch(branch)
368func (s Stack) RefFromBranch(branch string) (StackRef, error) {
369 for ref := range s.Iter() {
370 if ref.Branch == branch {
371 return ref, nil
372 }
373 }
375 return StackRef{}, errors.New("Could not find stack ref for branch: " + branch)
378// Empty returns true if the stack ref does not have an associated SHA (commit).
379// This indicates that the StackRef is invalid.
380func (r StackRef) Empty() bool { return r.SHA == "" }
382// IsFirst returns true if the stack ref is the first of the stack.
383// A stack ref is considered the first if it does not reference any previous ref.
384func (r StackRef) IsFirst() bool { return r.Prev == "" }
386// IsLast returns true if the stack ref is the last of the stack.
387// A stack ref is considered the last if it does not reference any next ref.
388func (r StackRef) IsLast() bool { return r.Next == "" }
390// Subject returns the stack ref description suitable as commit Subject
391// and for other in space limited places.
392// It only takes the first line of the description into account
393// and truncates it to 72 characters.
394func (r StackRef) Subject() string {
395 ls := strings.SplitN(r.Description, "\n", 2)
396 if len(ls[0]) <= 72 {
397 return ls[0]
398 }
400 return ls[0][:69] + "..."
403func (r StackRef) Body() string {
404 _, body, _ := strings.Cut(r.Description, "\n")
405 return strings.TrimSpace(body)
Diagnostics7 git calls · 33 ms
CallStatusSizeTime033 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 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=3
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 · 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=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=44
Open pack map
fetchtree:0 · deepen 1200706 B · hit5 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=706
git.response.cache=HIT
git.response.duration_ms=5
git.fetch.filter=tree:0
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=706
git.pkt.payload.max=606
git.sideband.band1.bytes=606
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=606
git.pack.trailer=5b8940d8a309b9f86f08953120610ba63124325e
Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit5 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=4273
git.response.cache=HIT
git.response.duration_ms=5
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:0200510 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=510
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=a3147172443266d011121808a125248c5c915972
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=510
git.pkt.payload.max=483
git.sideband.band1.bytes=483
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=483
git.pack.trailer=4ef234d86a93490a6a3da3549aa13a474a5c9eac
Open pack map
fetch2002.8 KiB · hit22 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=22
git.response.bytes=2841
git.response.cache=HIT
git.response.duration_ms=22
git.fetch.wants=e75d97d76e62d5f445610166e98449abd6f2e331
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=2841
git.pkt.payload.max=2814
git.sideband.band1.bytes=2814
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
a5e11e47ce757fd8
Request
2eccfb69…
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