GitLabgitlab-org

cli

main
ply6 git calls26 ms98 KiB4 cachedprotocol v2
151 lines5.1 KiBRaw
1//go:build !integration
3package config
5import (
6 "os"
7 "os/exec"
8 "path/filepath"
9 "testing"
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
15// An in-memory config (no directory behind it) must not persist local config to
16// the surrounding git repository's .git/glab-cli/config.yml, even when Set()
17// (which writes) is called from inside a git checkout.
18func Test_InMemoryConfig_LocalSetDoesNotPersist(t *testing.T) {
19 unsetGitHookEnv(t)
20 dir := t.TempDir()
21 require.NoError(t, exec.Command("git", "-C", dir, "init").Run())
22 t.Chdir(dir)
24 cfg := NewBlankConfig()
25 local, err := cfg.Local()
26 require.NoError(t, err)
27 require.NoError(t, local.Set("git_protocol", "ssh"))
29 _, statErr := os.Stat(filepath.Join(dir, ".git", "glab-cli", "config.yml"))
30 assert.True(t, os.IsNotExist(statErr), "in-memory config must not persist local config to .git")
33func Test_GitDir(t *testing.T) {
34 gotRelative := filepath.Join(GitDir(true)...)
35 gotAbsolute := filepath.Join(GitDir(false)...)
36 absRelative, err := filepath.Abs(gotRelative)
37 require.NoError(t, err)
38 assert.Equal(t, gotAbsolute, absRelative)
41// chdirTwoLevelsIntoNewRepo keeps the expected relative paths independent of the
42// checkout the tests run from, such as a git worktree whose .git is a file.
43func chdirTwoLevelsIntoNewRepo(t *testing.T) {
44 t.Helper()
45 unsetGitHookEnv(t)
46 dir, err := filepath.EvalSymlinks(t.TempDir())
47 require.NoError(t, err)
48 require.NoError(t, exec.Command("git", "-C", dir, "init").Run())
49 sub := filepath.Join(dir, "a", "b")
50 require.NoError(t, os.MkdirAll(sub, 0o755))
51 t.Chdir(sub)
54func Test_LocalConfigDir(t *testing.T) {
55 chdirTwoLevelsIntoNewRepo(t)
56 got := LocalConfigDir()
57 assert.ElementsMatch(t, []string{filepath.Join("..", "..", ".git"), "glab-cli"}, got)
60func Test_LocalConfigFile(t *testing.T) {
61 chdirTwoLevelsIntoNewRepo(t)
62 expectedPath := filepath.Join("..", "..", ".git", "glab-cli", "config.yml")
63 got := LocalConfigFile()
64 assert.Equal(t, expectedPath, got)
67// unsetGitHookEnv mirrors the helper in internal/git, which this package cannot
68// import without a cycle. Under a git hook these variables point the tests' git
69// commands at the repository running the hook instead of the temporary one.
70func unsetGitHookEnv(t *testing.T) {
71 t.Helper()
72 for _, key := range []string{"GIT_DIR", "GIT_WORK_TREE", "GIT_INDEX_FILE"} {
73 t.Setenv(key, "")
74 require.NoError(t, os.Unsetenv(key))
75 }
78// newRepoWithWorktree creates a repository with one commit and a linked worktree,
79// returning the absolute path of each. A worktree needs a commit to check out.
80func newRepoWithWorktree(t *testing.T) (string, string) {
81 t.Helper()
82 unsetGitHookEnv(t)
84 base, err := filepath.EvalSymlinks(t.TempDir())
85 require.NoError(t, err)
86 repo := filepath.Join(base, "repo")
87 require.NoError(t, os.MkdirAll(repo, 0o755))
89 git := func(args ...string) {
90 t.Helper()
91 full := append([]string{"-C", repo}, args...)
92 out, cmdErr := exec.Command("git", full...).CombinedOutput()
93 require.NoErrorf(t, cmdErr, "git %v: %s", args, out)
94 }
96 git("init")
97 git("config", "user.email", "test@example.com")
98 git("config", "user.name", "test")
99 git("config", "commit.gpgsign", "false")
100 git("commit", "--allow-empty", "-m", "initial")
102 worktree := filepath.Join(base, "wt")
103 git("worktree", "add", "--detach", worktree)
105 return repo, worktree
108func Test_GitCommonDir_MatchesGitDirInANormalCheckout(t *testing.T) {
109 chdirTwoLevelsIntoNewRepo(t)
111 assert.Equal(t, filepath.Join(GitDir(true)...), filepath.Join(GitCommonDir(true)...),
112 "outside a worktree the two directories are the same")
115// Git shares .git/config across every linked worktree and makes per-worktree
116// config opt-in. Local glab config describes the repository, not the checkout, so
117// it is shared the same way. Before this, a worktree resolved to
118// .git/worktrees/<name>/glab-cli and silently saw none of the repository's config.
119func Test_LocalConfigFile_IsSharedWithLinkedWorktrees(t *testing.T) {
120 repo, worktree := newRepoWithWorktree(t)
121 shared := filepath.Join(repo, ".git", "glab-cli", "config.yml")
123 t.Chdir(repo)
124 fromRepo, err := filepath.Abs(LocalConfigFile())
125 require.NoError(t, err)
126 assert.Equal(t, shared, fromRepo)
128 t.Chdir(worktree)
129 fromWorktree, err := filepath.Abs(LocalConfigFile())
130 require.NoError(t, err)
131 assert.Equal(t, shared, fromWorktree, "a linked worktree reads the repository's config")
134// A worktree that already carries its own file keeps using it, so a config
135// written before the shared default still applies where it was written.
136func Test_LocalConfigFile_HonoursAnExistingPerWorktreeFile(t *testing.T) {
137 repo, worktree := newRepoWithWorktree(t)
139 t.Chdir(worktree)
140 perWorktreeDir := filepath.Join(GitDir(false)...)
141 require.NotEqual(t, filepath.Join(repo, ".git"), perWorktreeDir,
142 "a linked worktree must have its own git dir for this case to mean anything")
144 own := filepath.Join(perWorktreeDir, "glab-cli", "config.yml")
145 require.NoError(t, os.MkdirAll(filepath.Dir(own), 0o755))
146 require.NoError(t, os.WriteFile(own, []byte("editor: worktree-only\n"), 0o600))
148 got, err := filepath.Abs(LocalConfigFile())
149 require.NoError(t, err)
150 assert.Equal(t, own, got)
Diagnostics6 git calls · 26 ms
CallStatusSizeTime026 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 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 · hit1 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=1
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 · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=1
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
fetchtree:02001.4 KiB · hit1 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=1
git.response.bytes=1391
git.response.cache=HIT
git.response.duration_ms=1
git.fetch.filter=tree:0
git.fetch.wants=e9da9edfdcf44933ee5fa121d411de326220161c
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=1391
git.pkt.payload.max=1364
git.sideband.band1.bytes=1364
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1364
git.pack.trailer=bf750bd385ed98d15e1fea1289d3b00caa61d64c
Open pack map
fetch2001.9 KiB · hit21 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=21
git.response.bytes=1902
git.response.cache=HIT
git.response.duration_ms=21
git.fetch.wants=40267c2b0e63b68c923aedbe9b3cc47c802e423d
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=1902
git.pkt.payload.max=1875
git.sideband.band1.bytes=1875
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
a5e15f5a98ab725b
Request
5f916750…
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