GitLabgitlab-org
cli
main
1// Package syncstate records how far glab has synced each agent session: its
2// sync position, which is a byte offset or a count of entries, depending on
3// the agent.
4package syncstate
6import (
7 "errors"
8 "fmt"
9 "io/fs"
10 "os"
11 "path/filepath"
12 "strconv"
13 "strings"
15 "gitlab.com/gitlab-org/cli/internal/commands/govern/internal/gaig"
16)
18// PositionPath returns the path of the file that holds a session's sync
19// position. The files are kept in the cursors directory, as they always were.
20func PositionPath(sessionID string) (string, error) {
21 if strings.ContainsAny(sessionID, "/\\..") {
22 return "", fmt.Errorf("invalid session ID: %q", sessionID)
23 }
24 return filepath.Join(gaig.StateDir(), "cursors", sessionID), nil
25}
27// ReadPosition returns the position a session was last synced to, or 0 if
28// it has none.
29func ReadPosition(sessionID string) (int64, error) {
30 path, err := PositionPath(sessionID)
31 if err != nil {
32 return 0, err
33 }
34 position, err := readOffset(path)
35 if err != nil {
36 return 0, fmt.Errorf("could not read the sync position: %w", err)
37 }
38 return position, nil
39}
41// WritePosition saves the position a session was synced to. It does not move
42// the position back: a hook and the fallback sync can work on the same
43// session at once, and the one that finishes last may have read less.
44func WritePosition(sessionID string, position int64) error {
45 path, err := PositionPath(sessionID)
46 if err != nil {
47 return err
48 }
50 if current, err := ReadPosition(sessionID); err == nil && current > position {
51 return nil
52 }
53 if err := writeOffset(path, position); err != nil {
54 return fmt.Errorf("could not write the sync position: %w", err)
55 }
56 return nil
57}
59// StartPath returns the path of the file that records the position a
60// session's syncing started from, when it did not start at the beginning of
61// the transcript.
62func StartPath(sessionID string) (string, error) {
63 path, err := PositionPath(sessionID)
64 if err != nil {
65 return "", err
66 }
67 return path + ".start", nil
68}
70// HasUploads reports whether glab has uploaded any of the session, which it
71// has once the sync position has moved past where syncing started.
72func HasUploads(sessionID string, position int64) (bool, error) {
73 path, err := StartPath(sessionID)
74 if err != nil {
75 return false, err
76 }
77 start, err := readOffset(path)
78 if err != nil {
79 return false, fmt.Errorf("could not read where syncing started: %w", err)
80 }
81 return position > start, nil
82}
84// StartAt moves the session's sync position to offset, and makes its syncing
85// start there if glab has uploaded none of it yet.
86func StartAt(sessionID string, offset int64) error {
87 position, err := ReadPosition(sessionID)
88 if err != nil {
89 return err
90 }
91 uploaded, err := HasUploads(sessionID, position)
92 if err != nil {
93 return err
94 }
95 offset = max(offset, position)
96 if !uploaded {
97 start, err := StartPath(sessionID)
98 if err != nil {
99 return err
100 }
101 // Written before the position, so that the position never passes
102 // the start without anything uploaded.
103 if err := writeOffset(start, offset); err != nil {
104 return fmt.Errorf("could not record where syncing starts: %w", err)
105 }
106 }
107 return WritePosition(sessionID, offset)
108}
110func readOffset(path string) (int64, error) {
111 data, err := os.ReadFile(path)
112 if errors.Is(err, fs.ErrNotExist) {
113 return 0, nil
114 }
115 if err != nil {
116 return 0, err
117 }
118 offset, err := strconv.ParseInt(strings.TrimSpace(string(data)), 10, 64)
119 if err != nil {
120 return 0, fmt.Errorf("could not parse %s: %w", filepath.Base(path), err)
121 }
122 return offset, nil
123}
125func writeOffset(path string, offset int64) error {
126 if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
127 return err
128 }
129 return gaig.WriteFileAtomic(path, []byte(strconv.FormatInt(offset, 10)))
130}
Diagnostics9 git calls · 226 ms
CallStatusSizeTime0226 ms
discover200185 B4 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch 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 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 · 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=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
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit3 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=3 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: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=4 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=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200261 B · hit4 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=261 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=3bb9946301aa8c0dc8d202b83bc9f84bf0400f6d 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=261 git.pkt.payload.max=234 git.sideband.band1.bytes=234 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=234 git.pack.trailer=e0a52f164f79506da6b0dcc275ed62911f5383bfOpen pack map
fetchtree:0200273 B · hit2 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=273 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=5fa160737ad30857ff606d64de1d596bb211ac3d 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=273 git.pkt.payload.max=246 git.sideband.band1.bytes=246 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=246 git.pack.trailer=06dd39b7545811625e04a7645a0a64c9a62c99c6Open pack map
fetchtree:0200141 B · hit5 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=204 git.response.bytes=141 git.response.cache=HIT git.response.duration_ms=5 git.fetch.filter=tree:0 git.fetch.wants=76f08950527546a73952f5c491a3f572ff9b86c8 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=141 git.pkt.payload.max=114 git.sideband.band1.bytes=114 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=114 git.pack.trailer=d4cf757339705617fab372ce81d8f59ac807f993Open pack map
fetch2001.3 KiB204 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1322 git.response.cache=MISS git.response.duration_ms=204 git.fetch.wants=b6f586d67a0520f94df0f93d68ea64393fe48c40 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=1322 git.pkt.payload.max=1295 git.sideband.band1.bytes=1295 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
- a5e2c11c7af689da
- Request
- 24f98a92…
- 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