GitLabgitlab-org

cli

main
ply7 git calls361 ms97 KiB4 cachedprotocol v2
127 lines3.4 KiBRaw
1package cmdtest
3import (
4 "bytes"
5 "log"
6 "math/rand"
7 "os"
8 "os/exec"
9 "path/filepath"
10 "strconv"
11 "strings"
12 "testing"
14 "github.com/otiai10/copy"
16 "gitlab.com/gitlab-org/cli/internal/config"
17 "gitlab.com/gitlab-org/cli/internal/git"
20var (
21 ProjectPath string
22 GlabBinaryPath string
25type fatalLogger interface {
26 Fatal(...any)
29func init() {
30 // Unset git hook environment variables that leak when tests run inside
31 // a git hook (e.g., pre-push via lefthook). These cause rev-parse and
32 // child go-test builds (with -coverpkg=./...) to fail in worktrees.
33 // Note: this permanently unsets these vars for the process since init()
34 // has no testing.T for cleanup. This is intentional — any test importing
35 // cmdtest needs a clean git environment for binary builds.
36 for _, key := range []string{"GIT_DIR", "GIT_WORK_TREE", "GIT_INDEX_FILE"} {
37 os.Unsetenv(key)
38 }
40 path := &bytes.Buffer{}
41 // get root dir via git
42 gitCmd := git.GitCommand("rev-parse", "--show-toplevel")
43 gitCmd.Stdout = path
44 err := gitCmd.Run()
45 if err != nil {
46 log.Fatalln("Failed to get root directory: ", err)
47 }
48 ProjectPath = strings.TrimSuffix(path.String(), "\n")
49 if !strings.HasSuffix(ProjectPath, "/") {
50 ProjectPath += "/"
51 }
54func InitTest(m *testing.M, suffix string) {
55 // Build a glab binary with test symbols. If the parent test binary was run
56 // with coverage enabled, enable coverage on the child binary, too.
57 var err error
58 GlabBinaryPath, err = filepath.Abs(os.ExpandEnv(ProjectPath + "testdata/glab.test"))
59 if err != nil {
60 log.Fatal(err)
61 }
62 testCmd := []string{"test", "-c", "-o", GlabBinaryPath, ProjectPath + "cmd/glab"}
63 if coverMode := testing.CoverMode(); coverMode != "" {
64 testCmd = append(testCmd, "-covermode", coverMode, "-coverpkg", "./...")
65 }
66 if out, err := exec.Command("go", testCmd...).CombinedOutput(); err != nil {
67 log.Fatalf("Error building glab test binary: %s (%s)", string(out), err)
68 }
70 originalWd, err := os.Getwd()
71 if err != nil {
72 log.Fatal(err)
73 }
74 var repo string = CopyTestRepo(log.New(os.Stderr, "", log.LstdFlags), suffix)
76 if err := os.Chdir(repo); err != nil {
77 log.Fatalf("Error chdir to test/testdata: %s", err)
78 }
79 code := m.Run()
81 if err := os.Chdir(originalWd); err != nil {
82 log.Fatalf("Error chdir to original working dir: %s", err)
83 }
85 testdirs, err := filepath.Glob(os.ExpandEnv(repo))
86 if err != nil {
87 log.Printf("Error listing glob test/testdata-*: %s", err)
88 }
89 for _, dir := range testdirs {
90 err := os.RemoveAll(dir)
91 if err != nil {
92 log.Printf("Error removing dir %s: %s", dir, err)
93 }
94 }
96 os.Exit(code)
99func CopyTestRepo(log fatalLogger, name string) string {
100 if name == "" {
101 name = strconv.Itoa(int(rand.Uint64()))
102 }
103 dest, err := filepath.Abs(os.ExpandEnv(ProjectPath + "test/testdata-" + name))
104 if err != nil {
105 log.Fatal(err)
106 }
107 src, err := filepath.Abs(os.ExpandEnv(ProjectPath + "test/testdata"))
108 if err != nil {
109 log.Fatal(err)
110 }
111 if err := copy.Copy(src, dest); err != nil {
112 log.Fatal(err)
113 }
114 // Move the test.git dir into the expected path at .git
115 if !config.CheckPathExists(dest + "/.git") {
116 if err := os.Rename(dest+"/test.git", dest+"/.git"); err != nil {
117 log.Fatal(err)
118 }
119 }
120 // Move the test.glab-cli dir into the expected path at .glab-cli
121 if !config.CheckPathExists(dest + "/.glab-cli") {
122 if err := os.Rename(dest+"/test.glab-cli", dest+"/.glab-cli"); err != nil {
123 log.Fatal(err)
124 }
125 }
126 return dest
Diagnostics7 git calls · 361 ms
CallStatusSizeTime0361 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=2
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:0200233 B · 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=233
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=be3b2f46264fd78e3aeec243c47ee7f44f4b940d
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=233
git.pkt.payload.max=206
git.sideband.band1.bytes=206
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=206
git.pack.trailer=60d385382f0daa4bf5b182241b9c3f6f6404da58
Open pack map
fetchtree:0200425 B · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=348
git.response.bytes=425
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=a60e63d01319c0073dd508088207ea8a30fdb705
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=425
git.pkt.payload.max=398
git.sideband.band1.bytes=398
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=398
git.pack.trailer=a85ea41c4277ec613536d00d6cd4a6b2898770d0
Open pack map
fetch2001.4 KiB348 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=1482
git.response.cache=MISS
git.response.duration_ms=348
git.fetch.wants=7fb1f787afe4973918ec3aa1682a1fbdb9d4135d
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=1482
git.pkt.payload.max=1455
git.sideband.band1.bytes=1455
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
a5e21bb8cc37ecd4
Request
2ab94eab…
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