Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions backend/plugins/gitextractor/parser/clone_gitcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
31 changes: 29 additions & 2 deletions backend/plugins/gitextractor/parser/clone_gitcli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
5 changes: 4 additions & 1 deletion backend/plugins/gitextractor/parser/gitrunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions backend/plugins/gitextractor/parser/testrepo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down