GitLabgitlab-org
cli
main
1package git
3import (
4 "bytes"
5 "os"
6 "os/exec"
7 "strings"
8 "testing"
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
13 "gitlab.com/gitlab-org/cli/internal/run"
14)
16func TestStandardGitCommand_Git_SetsLocale(t *testing.T) {
17 // This test verifies that StandardGitCommand.Git() sets LC_ALL=C
18 // to ensure Git output is always in English, regardless of user's locale.
20 // Use InitGitRepoWithCommit to ensure we have a proper repo with commits
21 // so git status returns predictable output
22 InitGitRepoWithCommit(t)
24 gitCmd := StandardGitCommand{}
26 t.Run("git status outputs English messages", func(t *testing.T) {
27 // Run git status which outputs human-readable messages
28 output, err := gitCmd.Git("status", "--long")
29 require.NoError(t, err)
31 // Verify output contains English messages
32 // These strings would be different in other locales if LC_ALL=C wasn't set:
33 // - German: "Auf Branch" instead of "On branch"
34 // - French: "Sur la branche" instead of "On branch"
35 // - Chinese: "位于分支" instead of "On branch"
36 // Note: Fresh repos may show "No commits yet" but repos with commits show "On branch"
37 require.True(t,
38 strings.Contains(output, "On branch") || strings.Contains(output, "No commits yet"),
39 "Git output should be in English, got: %s", output)
40 })
42 t.Run("git status contains expected English phrases", func(t *testing.T) {
43 // InitGitRepoWithCommit creates a repo with a commit and clean working tree,
44 // so git status should output "nothing to commit" since there are no changes
45 output, err := gitCmd.Git("status", "--long")
46 require.NoError(t, err)
48 // This is the key phrase that stack sync looks for to detect up-to-date branches
49 // Without LC_ALL=C, it would be localized and string matching would fail:
50 // - German: "nichts zu committen"
51 // - French: "rien à valider"
52 // - Chinese: "无文件要提交"
53 require.Contains(t, output, "nothing to commit",
54 "Git status should contain 'nothing to commit' for clean working tree")
56 // Also verify it shows the branch is clean (another English phrase)
57 require.Contains(t, output, "working tree clean",
58 "Git status should contain 'working tree clean' for clean working tree")
59 })
61 t.Run("git commands work regardless of user's LANG setting", func(t *testing.T) {
62 // Even if the user has a non-English LANG variable set in their environment,
63 // the LC_ALL=C setting should override it and force English output.
64 // We can't reliably test locale changes in the test environment,
65 // but we can verify the command succeeds and produces valid output.
66 output, err := gitCmd.Git("branch", "--show-current")
67 require.NoError(t, err)
68 require.NotEmpty(t, output, "Git command should produce output")
69 })
70}
72// TestStandardGitCommand_GitWithIO_WrapsExitError verifies that when the
73// subprocess fails, GitWithIO wraps the *exec.ExitError with git's stderr
74// message instead of returning a bare "exit status N". Without the wrap the
75// CLI prints a cryptic "Error: exit status 128" after git's real message has
76// already been streamed live.
77func TestStandardGitCommand_GitWithIO_WrapsExitError(t *testing.T) {
78 // Cannot call t.Parallel(): we rely on t.Chdir to drop into a non-git
79 // directory, and t.Chdir is incompatible with parallel tests.
81 // Strip inherited GIT_DIR/GIT_WORK_TREE/GIT_INDEX_FILE so this test
82 // really runs outside any git repo (e.g. when invoked via lefthook).
83 unsetGitHookEnv(t)
85 // chdir to a fresh temp dir that is NOT a git repo, so `git rev-parse`
86 // exits with status 128 and prints a recognizable stderr message.
87 tempDir := t.TempDir()
88 t.Chdir(tempDir)
90 var stdout, stderr bytes.Buffer
91 gitc := StandardGitCommand{}
92 err := gitc.GitWithIO(&stdout, &stderr, "rev-parse", "--verify", "HEAD")
94 require.Error(t, err, "git rev-parse outside a repo should fail")
96 // The wrapped error must include git's stderr message — proves GitWithIO
97 // captures the stderr stream alongside teeing it to the live writer.
98 assert.Contains(t, err.Error(), "not a git repository",
99 "wrapped error should include git's stderr message; got: %v", err)
101 // The original *exec.ExitError must remain in the chain so callers using
102 // errors.As / errors.Is keep working.
103 var exitErr *exec.ExitError
104 require.ErrorAs(t, err, &exitErr, "*exec.ExitError should remain in the chain")
105 assert.Equal(t, 128, exitErr.ExitCode(), "git rev-parse outside a repo should exit 128")
107 // The live stderr writer received the same message (io.MultiWriter
108 // guarantees it; we assert it so a future regression dropping the tee
109 // is caught here too).
110 assert.Contains(t, stderr.String(), "not a git repository")
111 assert.Empty(t, stdout.String(), "no stdout expected on this failure")
112}
114func TestStandardGitRunner_LatestCommitPreservesSubjectSpaces(t *testing.T) {
115 InitGitRepo(t)
116 configureGitConfig(t)
117 require.NoError(t, os.WriteFile("randomfile", []byte("test"), 0o600))
118 _, err := run.PrepareCmd(GitCommand("add", "randomfile")).Output()
119 require.NoError(t, err)
121 _, err = run.PrepareCmd(GitCommand("commit", "-m", "fix multiple words")).Output()
122 require.NoError(t, err)
124 commit, err := NewStandardGitRunner("").LatestCommit("HEAD")
126 require.NoError(t, err)
127 assert.Equal(t, "fix multiple words", commit.Title)
128 assert.NotEmpty(t, commit.Sha)
129}
Diagnostics6 git calls · 40 ms
CallStatusSizeTime040 ms
discover200185 B5 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch 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 KiB4 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=4 git.response.bytes=59849 git.response.cache=UPDATING git.response.duration_ms=4 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=4 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=44Open 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:0200510 B · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=29 git.response.bytes=510 git.response.cache=HIT git.response.duration_ms=2 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=4ef234d86a93490a6a3da3549aa13a474a5c9eacOpen pack map
fetch2002.2 KiB · hit29 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2211 git.response.cache=HIT git.response.duration_ms=29 git.fetch.wants=4c26c6bf57c4884c6fd9c8c187972d4a39b68136 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=2211 git.pkt.payload.max=2184 git.sideband.band1.bytes=2184 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
- a5e1259b6d987fd8
- Request
- 692065c1…
- 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