GitLabgitlab-org

cli

main
ply9 git calls239 ms99 KiB6 cachedprotocol v2
175 lines4.8 KiBRaw
1// Package opencodeplugin manages the OpenCode plugin that glab installs to
2// sync OpenCode sessions.
3package opencodeplugin
5import (
6 "bytes"
7 _ "embed"
8 "errors"
9 "fmt"
10 "io/fs"
11 "os"
12 "path/filepath"
13 "slices"
16// FileName is the plugin's file name in the OpenCode plugin directory.
17const FileName = "glab-govern.js"
19// Source is the plugin glab installs. OpenCode sets no environment variable
20// with the session ID, so the plugin passes it to glab as
21// OPENCODE_SESSION_ID.
22//
23//go:embed glab-govern.js
24var Source []byte
26// markers start every version of the plugin, so that glab only removes a file
27// it installed. Versions before the rename to glab minder start with the
28// second.
29var markers = [][]byte{
30 []byte("// Installed by glab minder setup."),
31 []byte("// Installed by glab govern setup."),
34func hasMarker(data []byte) bool {
35 return slices.ContainsFunc(markers, func(m []byte) bool { return bytes.HasPrefix(data, m) })
38// ConfigDir returns OpenCode's global configuration directory, which OpenCode
39// creates when it first runs.
40func ConfigDir() (string, error) {
41 if dir := os.Getenv("XDG_CONFIG_HOME"); dir != "" {
42 return filepath.Join(dir, "opencode"), nil
43 }
44 home, err := os.UserHomeDir()
45 if err != nil {
46 return "", fmt.Errorf("could not find home directory: %w", err)
47 }
48 return filepath.Join(home, ".config", "opencode"), nil
51// Path returns where the plugin is installed. OpenCode loads every plugin in
52// this directory, for all projects.
53func Path() (string, error) {
54 dir, err := ConfigDir()
55 if err != nil {
56 return "", err
57 }
58 return filepath.Join(dir, "plugins", FileName), nil
61// Display shortens path for messages, writing the home directory as ~.
62func Display(path string) string {
63 home, err := os.UserHomeDir()
64 if err != nil {
65 return path
66 }
67 if rel, err := filepath.Rel(home, path); err == nil && filepath.IsLocal(rel) {
68 return filepath.Join("~", rel)
69 }
70 return path
73// Detected reports whether OpenCode has run for this user.
74func Detected() (bool, error) {
75 dir, err := ConfigDir()
76 if err != nil {
77 return false, err
78 }
79 switch _, err := os.Stat(dir); {
80 case err == nil:
81 return true, nil
82 case errors.Is(err, fs.ErrNotExist):
83 return false, nil
84 default:
85 return false, err
86 }
89// State is what is installed at the plugin path.
90type State int
92const (
93 // NotInstalled means there is no file at the plugin path.
94 NotInstalled State = iota
95 // Current means the plugin is this glab's version.
96 Current
97 // Outdated means glab installed a different version of the plugin.
98 Outdated
99 // Foreign means the file at the plugin path was not installed by glab.
100 Foreign
103// Check reports what is installed at path.
104func Check(path string) (State, error) {
105 data, err := os.ReadFile(path)
106 switch {
107 case errors.Is(err, fs.ErrNotExist):
108 return NotInstalled, nil
109 case err != nil:
110 return NotInstalled, err
111 case bytes.Equal(data, Source):
112 return Current, nil
113 case hasMarker(data):
114 return Outdated, nil
115 default:
116 return Foreign, nil
117 }
120// ForeignError reports that the file at path, where the plugin goes, was not
121// installed by glab.
122func ForeignError(path string) error {
123 return fmt.Errorf("%s was not installed by glab; remove or rename it, then run 'glab minder setup --agents opencode'", Display(path))
126// Install writes the plugin to path, replacing an earlier version. It
127// returns whether anything changed, and refuses to replace a file that glab
128// did not install.
129func Install(path string) (bool, error) {
130 state, err := Check(path)
131 switch {
132 case err != nil:
133 return false, err
134 case state == Current:
135 return false, nil
136 case state == Foreign:
137 return false, ForeignError(path)
138 }
139 if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
140 return false, fmt.Errorf("could not create directory: %w", err)
141 }
142 tmp, err := os.CreateTemp(filepath.Dir(path), ".glab-govern-*.js.tmp")
143 if err != nil {
144 return false, fmt.Errorf("could not create temp file: %w", err)
145 }
146 tmpName := tmp.Name()
147 defer func() { _ = os.Remove(tmpName) }()
149 if _, err := tmp.Write(Source); err != nil {
150 return false, errors.Join(fmt.Errorf("could not write temp file: %w", err), tmp.Close())
151 }
152 if err := tmp.Close(); err != nil {
153 return false, fmt.Errorf("could not close temp file: %w", err)
154 }
155 if err := os.Chmod(tmpName, 0o644); err != nil {
156 return false, fmt.Errorf("could not set file permissions: %w", err)
157 }
158 if err := os.Rename(tmpName, path); err != nil {
159 return false, fmt.Errorf("could not rename temp file: %w", err)
160 }
161 return true, nil
164// Remove deletes the plugin at path if glab installed it, and returns whether
165// it did.
166func Remove(path string) (bool, error) {
167 state, err := Check(path)
168 if err != nil || state == NotInstalled || state == Foreign {
169 return false, err
170 }
171 if err := os.Remove(path); err != nil {
172 return false, err
173 }
174 return true, nil
Diagnostics9 git calls · 239 ms
CallStatusSizeTime0239 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=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 KiB2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=59849
git.response.cache=UPDATING
git.response.duration_ms=2
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=4
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=44
Open pack map
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=3
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:02001.8 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=1857
git.response.cache=HIT
git.response.duration_ms=3
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:0200261 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=261
git.response.cache=HIT
git.response.duration_ms=3
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=e0a52f164f79506da6b0dcc275ed62911f5383bf
Open pack map
fetchtree:0200273 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=273
git.response.cache=HIT
git.response.duration_ms=3
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=06dd39b7545811625e04a7645a0a64c9a62c99c6
Open pack map
fetchtree:0200185 B · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=220
git.response.bytes=185
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=b97e092ad09f2ba787b9764e83b6dbf8477ee2a1
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=185
git.pkt.payload.max=158
git.sideband.band1.bytes=158
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=158
git.pack.trailer=4d24c4711408fe0a3a34dd470aa37740bf7101b2
Open pack map
fetch2001.7 KiB220 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=1722
git.response.cache=MISS
git.response.duration_ms=220
git.fetch.wants=58cc6005e3464ba01c3929b03e0f50b4aaceac7f
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=1722
git.pkt.payload.max=1695
git.sideband.band1.bytes=1695
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
a5e23a525d174bc2
Request
ee27ab21…
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