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
This commit is contained in:
ecuthiell 2026-09-26 19:59:37 -06:00 • committed by GitHub
commit cf683435e7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 62 additions and 1 deletions

View file

@ -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)
}

View file

@ -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")