From c24e15389a2095cfc9b922927793dedc03f2339b Mon Sep 17 00:00:00 2001 From: DoDiODev Date: Mon, 16 Nov 2026 10:09:44 +0100 Subject: [PATCH] fix(gitextractor): pass --git-dir so bare clones work with safe.bareRepository=explicit With safe.bareRepository=explicit (git 2.38+), git refuses to use a bare repository found from the working directory. The cloner and the collector ran git with cmd.Dir set to the bare clone, so the clone failed at `git repack -d` with "cannot use bare repository". Pass --git-dir explicitly, and do the same in the test helpers that run git in bare repos. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../gitextractor/parser/clone_gitcli.go | 4 +++ .../gitextractor/parser/clone_gitcli_test.go | 31 +++++++++++++++++-- .../plugins/gitextractor/parser/gitrunner.go | 5 ++- .../gitextractor/parser/testrepo_test.go | 4 +-- 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/backend/plugins/gitextractor/parser/clone_gitcli.go b/backend/plugins/gitextractor/parser/clone_gitcli.go index 06f1a7acb00..ed3aa00c1fb 100644 --- a/backend/plugins/gitextractor/parser/clone_gitcli.go +++ b/backend/plugins/gitextractor/parser/clone_gitcli.go @@ -408,6 +408,10 @@ func (g *GitcliCloner) git(env []string, dir string, gitcmd string, args ...stri func (g *GitcliCloner) gitCommand(env []string, dir string, gitcmd string, args ...string) *exec.Cmd { g.logger.Debug("git %s %v", gitcmd, sanitizeArgs(args)) // CWE-532: sanitize before logging cmdArgs := append([]string{}, g.globalConfigArgs...) + if dir != "" { + // dir is always a bare clone; see gitRunner.command for why --git-dir is passed + cmdArgs = append(cmdArgs, "--git-dir="+dir) + } cmdArgs = append(cmdArgs, gitcmd) cmdArgs = append(cmdArgs, args...) cmd := exec.CommandContext(g.ctx.GetContext(), "git", cmdArgs...) diff --git a/backend/plugins/gitextractor/parser/clone_gitcli_test.go b/backend/plugins/gitextractor/parser/clone_gitcli_test.go index 83636b97e90..732c4c4cae1 100644 --- a/backend/plugins/gitextractor/parser/clone_gitcli_test.go +++ b/backend/plugins/gitextractor/parser/clone_gitcli_test.go @@ -32,9 +32,13 @@ import ( "github.com/stretchr/testify/require" ) -// runGit runs git in dir with a fixed identity (and date, if given), and returns its trimmed stdout. +// runGit runs git in the bare repository dir (if given) with a fixed identity +// (and date, if given), and returns its trimmed stdout. func runGit(t *testing.T, dir, date string, args ...string) string { t.Helper() + if dir != "" { + args = append([]string{"--git-dir=" + dir}, args...) + } cmd := exec.Command("git", args...) cmd.Dir = dir cmd.Env = append(os.Environ(), @@ -101,7 +105,7 @@ func newTestCloner(t *testing.T, remoteDir string, since time.Time) *GitcliClone } func hasObject(dir, sha string) bool { - cmd := exec.Command("git", "cat-file", "-e", sha) + cmd := exec.Command("git", "--git-dir="+dir, "cat-file", "-e", sha) cmd.Dir = dir return cmd.Run() == nil } @@ -184,3 +188,26 @@ func TestNewestShallowCommitTimeIgnoresMissingCommits(t *testing.T) { require.NoError(t, err) assert.True(t, newest.IsZero()) } + +// safe.bareRepository=explicit (git 2.38+) makes git refuse to use a bare +// repository found from the working directory. Hardened hosts and some CI +// runners set it; the cloner and the collector must still work on bare clones. +func TestCloneAndCollectWithSafeBareRepositoryExplicit(t *testing.T) { + if _, err := exec.LookPath("git"); err != nil { + t.Skip("git is not installed") + } + origin, commits := buildMergeBoundaryRepo(t) + t.Setenv("GIT_CONFIG_COUNT", "1") + t.Setenv("GIT_CONFIG_KEY_0", "safe.bareRepository") + t.Setenv("GIT_CONFIG_VALUE_0", "explicit") + cloner := newTestCloner(t, origin, time.Date(2024, 1, 2, 12, 0, 0, 0, time.UTC)) + + require.NoError(t, cloner.CloneRepo()) + assert.True(t, hasObject(cloner.localDir, commits["M"])) + + collector, err := NewGitcliRepoCollector(cloner.localDir, "test-repo", nil, unithelper.DummyLogger()) + require.Nil(t, err) + branches, e := collector.CountBranches(context.Background()) + require.NoError(t, e) + assert.Positive(t, branches) +} diff --git a/backend/plugins/gitextractor/parser/gitrunner.go b/backend/plugins/gitextractor/parser/gitrunner.go index 4d547f8a570..ba8aed28416 100644 --- a/backend/plugins/gitextractor/parser/gitrunner.go +++ b/backend/plugins/gitextractor/parser/gitrunner.go @@ -114,7 +114,10 @@ func (r *gitRunner) Pipe(ctx context.Context, stdin []byte, args ...string) (io. } func (r *gitRunner) command(ctx context.Context, args ...string) *exec.Cmd { - fullArgs := append([]string{"-c", "core.quotePath=false", "-c", "gc.auto=0", "-c", "maintenance.auto=false"}, args...) + // Clones are bare. Passing --git-dir keeps git working when + // safe.bareRepository=explicit is set, which forbids discovering a bare + // repository from the working directory. + fullArgs := append([]string{"--git-dir=" + r.dir, "-c", "core.quotePath=false", "-c", "gc.auto=0", "-c", "maintenance.auto=false"}, args...) cmd := exec.CommandContext(ctx, "git", fullArgs...) cmd.Dir = r.dir cmd.Env = append(cmd.Environ(), diff --git a/backend/plugins/gitextractor/parser/testrepo_test.go b/backend/plugins/gitextractor/parser/testrepo_test.go index 400acd63730..c754c1bd6ca 100644 --- a/backend/plugins/gitextractor/parser/testrepo_test.go +++ b/backend/plugins/gitextractor/parser/testrepo_test.go @@ -287,12 +287,12 @@ func blameAtHEAD(t *testing.T, dir string) map[string]map[int]string { t.Helper() repo := newGitFixtureAt(t, dir) blames := make(map[string]map[int]string) - files := strings.Split(strings.TrimRight(repo.git("ls-tree", "-r", "--name-only", "-z", "HEAD"), "\x00"), "\x00") + files := strings.Split(strings.TrimRight(repo.git("--git-dir="+dir, "ls-tree", "-r", "--name-only", "-z", "HEAD"), "\x00"), "\x00") for _, file := range files { if file == "" { continue } - out := repo.git("blame", "-l", "--line-porcelain", "HEAD", "--", file) + out := repo.git("--git-dir="+dir, "blame", "-l", "--line-porcelain", "HEAD", "--", file) lines := make(map[int]string) for _, line := range strings.Split(out, "\n") { // The header of the first line of each group carries the group