diff --git a/cmd/bd/BUILD.bazel b/cmd/bd/BUILD.bazel index 88ea7915a..b22dcd48b 100644 --- a/cmd/bd/BUILD.bazel +++ b/cmd/bd/BUILD.bazel @@ -754,6 +754,7 @@ go_test( "history_full_hash_test.go", "history_proxied_integration_test.go", "history_remote_frontdoor_matrix_test.go", + "hook_env_test.go", "hooks_embedded_test.go", "hooks_exec_composition_test.go", "hooks_export_staged_deletion_test.go", diff --git a/cmd/bd/export_auto.go b/cmd/bd/export_auto.go index 54a6b6921..d366f8d52 100644 --- a/cmd/bd/export_auto.go +++ b/cmd/bd/export_auto.go @@ -9,6 +9,7 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "sort" "strings" "sync" @@ -18,6 +19,7 @@ import ( "github.com/steveyegge/beads/internal/beads" "github.com/steveyegge/beads/internal/config" "github.com/steveyegge/beads/internal/debug" + "github.com/steveyegge/beads/internal/execenv" "github.com/steveyegge/beads/internal/storage" "github.com/steveyegge/beads/internal/storage/dolt" "github.com/steveyegge/beads/internal/types" @@ -1072,6 +1074,10 @@ func gitIndexLockPath(path string, env []string) (string, error) { return filepath.Join(gitDir, "index.lock"), nil } +// hookEnvGOOS lets both hook filters exercise either key policy on every test host. +// Process-wide seam: tests that swap it must not run in parallel. +var hookEnvGOOS = runtime.GOOS + // scrubGitHookEnv returns env with the GIT_* variables that can poison // git's repo/worktree auto-discovery or object-store resolution removed, // so git falls back to auto-discovery from cwd. The scrub is @@ -1090,34 +1096,28 @@ func gitIndexLockPath(path string, env []string) (string, error) { // GIT_CONFIG namespace, which includes _COUNT, _KEY_n, _VALUE_n, // _GLOBAL, _SYSTEM, _NOSYSTEM, and the legacy GIT_CONFIG itself. func scrubGitHookEnv(env []string) []string { + return scrubGitHookEnvForOS(env, hookEnvGOOS) +} + +func scrubGitHookEnvForOS(env []string, goos string) []string { // The GIT_CONFIG prefix (no trailing "=") is intentional: it matches // GIT_CONFIG=, GIT_CONFIG_COUNT=, GIT_CONFIG_KEY_n=, GIT_CONFIG_VALUE_n=, // GIT_CONFIG_PARAMETERS=, GIT_CONFIG_GLOBAL=, GIT_CONFIG_SYSTEM=, and // GIT_CONFIG_NOSYSTEM= — the whole family — in one entry. No standard // git env var starts with GIT_CONFIG that we want to preserve. - prefixes := []string{ - "GIT_DIR=", - "GIT_WORK_TREE=", - "GIT_INDEX_FILE=", - "GIT_COMMON_DIR=", - "GIT_PREFIX=", - "GIT_OBJECT_DIRECTORY=", - "GIT_ALTERNATE_OBJECT_DIRECTORIES=", - "GIT_CEILING_DIRECTORIES=", - "GIT_DISCOVERY_ACROSS_FILESYSTEM=", - "GIT_CONFIG", - } - out := make([]string, 0, len(env)) - for _, e := range env { - skip := false - for _, p := range prefixes { - if strings.HasPrefix(e, p) { - skip = true - break - } - } - if !skip { - out = append(out, e) + cleaned := execenv.WithoutForOS(env, goos, + "GIT_DIR", "GIT_WORK_TREE", "GIT_INDEX_FILE", "GIT_COMMON_DIR", + "GIT_PREFIX", "GIT_OBJECT_DIRECTORY", "GIT_ALTERNATE_OBJECT_DIRECTORIES", + "GIT_CEILING_DIRECTORIES", "GIT_DISCOVERY_ACROSS_FILESYSTEM", + ) + // Without owns a fresh slice, so this second filter cannot mutate env. + out := cleaned[:0] + for _, entry := range cleaned { + // Prefix policy also drops valueless GIT_CONFIG entries. Without + // intentionally preserves valueless exact keys such as GIT_DIR. + key := execenv.EntryKey(entry) + if !execenv.KeyHasPrefixForOS(key, "GIT_CONFIG", goos) { + out = append(out, entry) } } return out diff --git a/cmd/bd/hook_env_test.go b/cmd/bd/hook_env_test.go new file mode 100644 index 000000000..0420921ec --- /dev/null +++ b/cmd/bd/hook_env_test.go @@ -0,0 +1,76 @@ +package main + +import ( + "slices" + "testing" +) + +func TestFilterChainedHookEnvUsesHostKeyIdentity(t *testing.T) { + checkHookEnvForBothHosts(t, checkChainedHookEnv) +} + +// Callers and subtests must remain serial while they swap the process-wide seam. +func checkHookEnvForBothHosts(t *testing.T, check func(*testing.T)) { + t.Helper() + for _, goos := range []string{"linux", "windows"} { + t.Run(goos, func(t *testing.T) { + previous := hookEnvGOOS + hookEnvGOOS = goos + t.Cleanup(func() { hookEnvGOOS = previous }) + check(t) + }) + } +} + +func checkChainedHookEnv(t *testing.T) { + t.Helper() + env := []string{ + "BD_GIT_HOOK=canonical", "bd_git_hook=mixed", "BD_GİT_HOOK=unicode-case", + "BD_GIT_HOOK=unicode-case", "BD_GIT_HOOK=", + "BD_GıT_HOOK=distinct", "KEEP=first", "KEEP=BD_GIT_HOOK=1", + "BD_GIT_HOOK", `=C:=C:\work`, + } + original := slices.Clone(env) + want := []string{"BD_GıT_HOOK=distinct", "KEEP=first", "KEEP=BD_GIT_HOOK=1", "BD_GIT_HOOK", `=C:=C:\work`} + if hookEnvGOOS != "windows" { + want = append([]string{"bd_git_hook=mixed", "BD_GİT_HOOK=unicode-case", "BD_GIT_HOOK=unicode-case"}, want...) + } + if got := filterEnv(env, "BD_GIT_HOOK"); !slices.Equal(got, want) { + t.Errorf("chained-hook environment = %q, want %q", got, want) + } + if !slices.Equal(env, original) { + t.Errorf("input environment mutated: %q", env) + } +} + +func TestScrubGitHookEnvUsesHostKeyIdentity(t *testing.T) { + checkHookEnvForBothHosts(t, checkGitHookEnv) +} + +func checkGitHookEnv(t *testing.T) { + t.Helper() + env := []string{ + "GIT_DIR=canonical", "git_dir=mixed", "GİT_DİR=unicode-case", "GIT_DıR=distinct", + "GIT_WORK_TREE=unicode-case", "GIT_DIſCOVERY_ACROSS_FILESYSTEM=distinct", "GIT_CONFIG=", + "GIT_CONFIG_COUNT=1", "git_config_parameters=mixed", "gİt_config_count=unicode-case", + "GIT_CONFIG_UNSET", "git_config_unset", "KEEP=first", "KEEP=GIT_CONFIG_COUNT=1", + "GIT_AUTHOR_NAME=author", "GIT_OPTIONAL_LOCKS=1", "GIT_DIR", "MALFORMED", `=C:=C:\work`, + } + original := slices.Clone(env) + want := []string{"GIT_DıR=distinct"} + if hookEnvGOOS != "windows" { + want = append([]string{"git_dir=mixed", "GİT_DİR=unicode-case"}, want...) + want = append(want, "GIT_WORK_TREE=unicode-case") + } + want = append(want, "GIT_DIſCOVERY_ACROSS_FILESYSTEM=distinct") + if hookEnvGOOS != "windows" { + want = append(want, "git_config_parameters=mixed", "gİt_config_count=unicode-case", "git_config_unset") + } + want = append(want, "KEEP=first", "KEEP=GIT_CONFIG_COUNT=1", "GIT_AUTHOR_NAME=author", "GIT_OPTIONAL_LOCKS=1", "GIT_DIR", "MALFORMED", `=C:=C:\work`) + if got := scrubGitHookEnv(env); !slices.Equal(got, want) { + t.Errorf("Git-hook environment = %q, want %q", got, want) + } + if !slices.Equal(env, original) { + t.Errorf("input environment mutated: %q", env) + } +} diff --git a/cmd/bd/hooks.go b/cmd/bd/hooks.go index 63a3e42c4..b48d5c75f 100644 --- a/cmd/bd/hooks.go +++ b/cmd/bd/hooks.go @@ -14,6 +14,7 @@ import ( "github.com/steveyegge/beads/internal/beads" "github.com/steveyegge/beads/internal/config" "github.com/steveyegge/beads/internal/debug" + "github.com/steveyegge/beads/internal/execenv" "github.com/steveyegge/beads/internal/git" "github.com/steveyegge/beads/internal/gitenv" "github.com/steveyegge/beads/internal/metrics" @@ -2100,14 +2101,11 @@ func warnJSONLWithoutDoltRemote(reason string) { // filterEnv returns a copy of env with entries matching the given key removed. func filterEnv(env []string, key string) []string { - prefix := key + "=" - out := make([]string, 0, len(env)) - for _, e := range env { - if !strings.HasPrefix(e, prefix) { - out = append(out, e) - } - } - return out + return filterEnvForOS(env, key, hookEnvGOOS) +} + +func filterEnvForOS(env []string, key, goos string) []string { + return execenv.WithoutForOS(env, goos, key) } // runPostMergeHook runs chained hooks after merge, then runs the legacy