From cf683435e725cf0a2fc619cafe173abbcde33fa1 Mon Sep 17 00:00:00 2001 From: ecuthiell Date: Sat, 26 Sep 2026 19:59:37 -0600 Subject: [PATCH] fix(git): preserve config read errors instead of treating them as absent (#6477) * fix(git): preserve config read errors instead of treating them as absent (saa-cgh.120.20) Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell * fix(git): preserve config read errors instead of treating them as absent (saa-cgh.120.20) Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell * test(git): assert config failures without English diagnostics (saa-cgh.120.20) Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell --- internal/storage/domain/git/git.go | 6 ++- internal/storage/domain/git/git_test.go | 57 +++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/internal/storage/domain/git/git.go b/internal/storage/domain/git/git.go index d960d0cbf..1fdb6eb14 100644 --- a/internal/storage/domain/git/git.go +++ b/internal/storage/domain/git/git.go @@ -102,7 +102,11 @@ func (r *gitRepositoryImpl) GetConfig(ctx context.Context, key string) (string, if err != nil { var exitErr *exec.ExitError if errors.As(err, &exitErr) { - return "", false, nil + // Git also uses exit 1 for invalid keys, with a diagnostic. + if exitErr.ExitCode() == 1 && len(bytes.TrimSpace(exitErr.Stderr)) == 0 { + return "", false, nil + } + return "", false, fmt.Errorf("git: GetConfig %s: %w: %s", key, err, bytes.TrimSpace(exitErr.Stderr)) } return "", false, fmt.Errorf("git: GetConfig %s: %w", key, err) } diff --git a/internal/storage/domain/git/git_test.go b/internal/storage/domain/git/git_test.go index 25cd8625f..5cf4e3f4a 100644 --- a/internal/storage/domain/git/git_test.go +++ b/internal/storage/domain/git/git_test.go @@ -1,7 +1,9 @@ package git import ( + "context" "os" + "os/exec" "path/filepath" "strings" @@ -59,6 +61,61 @@ func (s *testSuite) TestConfig_RoundTrip() { s.Equal("maintainer", value) } +func (s *testSuite) TestConfig_ReadFailuresAreNotMissing() { + s.gitInit() + configPath := filepath.Join(s.tmpDir, ".git", "config") + original, err := os.ReadFile(configPath) + s.Require().NoError(err) + for _, tc := range []struct { + name, key string + exitCode int + }{ + {"malformed_config", "beads.role", 128}, + {"invalid_key", "invalid", 1}, + {"invalid_routing_boolean", "beads.role", 128}, + } { + s.Run(tc.name, func() { + switch tc.name { + case "malformed_config": + s.Require().NoError(os.WriteFile(configPath, []byte("[broken\n"), 0600)) + t := s.T() + t.Cleanup(func() { + if err := os.WriteFile(configPath, original, 0600); err != nil { + t.Errorf("restore repository config: %v", err) + } + }) + case "invalid_routing_boolean": + s.T().Setenv("GIT_CONFIG_NOSYSTEM", "not-a-boolean") + } + value, found, err := s.repo.GetConfig(s.Ctx(), tc.key) + s.Require().Error(err) + s.False(found) + s.Empty(value) + var exitErr *exec.ExitError + s.Require().ErrorAs(err, &exitErr) + s.Equal(tc.exitCode, exitErr.ExitCode()) + diagnostic := strings.TrimSpace(string(exitErr.Stderr)) + s.Require().NotEmpty(diagnostic) + s.Contains(err.Error(), diagnostic) + if tc.name != "invalid_key" { + _, _, roleErr := domain.NewGitUseCase(s.tmpDir, s.repo).BeadsRole(s.Ctx()) + s.Require().Error(roleErr) + s.ErrorAs(roleErr, &exitErr) + } + }) + } +} + +// Regression guard: pre-canceled contexts already propagated before the config fix. +func (s *testSuite) TestConfig_CancellationIsNotMissing() { + ctx, cancel := context.WithCancel(s.Ctx()) + cancel() + value, found, err := s.repo.GetConfig(ctx, "beads.role") + s.ErrorIs(err, context.Canceled) + s.False(found) + s.Empty(value) +} + func (s *testSuite) TestConfig_SetEmptyKeyErrors() { s.gitInit() err := s.repo.SetConfig(s.Ctx(), "", "x")