* fix(exec): match Windows environment key identity (#5618) Centralize os/exec-compatible environment-key comparison and use it for credential, metrics, Git-hook, trace, and repro subprocess filters. Preserve exact Unix semantics and Unicode near-collisions. Agent-Signature: codex-gpt-5-unknown-reasoning on behalf of Ewen Cuthiell Signed-off-by: Ewen Cuthiell <ewencuthiell@gmail.com> * fix(worktree): scrub inherited Git routing state Establish CWD as the worktree repository boundary before discovery can consume inherited Git routing. Fence the startup config probe, clear routing in-process before root pre-run discovery, and retain command-owned subprocess scrubbing as defense in depth without changing PID or signal semantics. Fixes #5531 Agent-Signature: codex-gpt-5-unknown-reasoning on behalf of Ewen Cuthiell * refactor(worktree): explain owned filtering and remove an unused shim Document why filtering the fresh routing slice in place is safe and call the shared routing-key classifier directly from tests. Retain the two forwarders used by production worktree subprocess/removal paths. Review: https://github.com/gastownhall/beads/pull/5623#pullrequestreview-5098421671 Bead: saa-cgh.23 Agent-Signature: unknown-model/unknown-reasoning * fix(config): isolate identity and tracked-file Git probes from routing Reuse the worktree routing owner for GetIdentity and isGitTracked so explicit cwd/path selection is not displaced by inherited repository, index or injected config state. Preserve identity precedence and the tracked-secret refusal contract, with real target/decoy regressions. Follow-up: https://github.com/gastownhall/beads/issues/6222 Dependency: https://github.com/gastownhall/beads/pull/5623 Reported-by: bee-ghosttrack Bead: saa-cgh.93 Agent-Signature: unknown-model/unknown-reasoning * test(execenv): exercise every environment operation under both host rules Thread the host choice through narrow private helpers behind the existing exported functions. Keep one normalization owner and retain native wrapper tests; the additional table covers both host branches for equality, prefix matching, last-wins lookup, filtering and input preservation. Review: https://github.com/gastownhall/beads/pull/5672#pullrequestreview-5096948965 Bead: saa-cgh.78 Agent-Signature: unknown-model/unknown-reasoning * docs(worktree): explain startup Git environment scope (saa-cgh.23) Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell * test(execenv): exercise exported host policy wiring (saa-cgh.78) Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell * fix(config): share subprocess key identity for Git routing (saa-cgh.93) Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell * fix(hooks): isolate tracked-file guard from Git routing (saa-cgh.98) Follow-up to bee-ghosttrack's review of gastownhall/beads#6322; refs #6356. Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell * fix(config): isolate role commands from Git routing (saa-cgh.99) Follow-up to bee-ghosttrack's review of gastownhall/beads#6322; refs #6357. Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell * test(env): exercise shared key helpers on native Windows (saa-cgh.78) Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell * refactor(gitenv): share key parsing and normalize routing membership once (saa-cgh.93) Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell * docs(config): explain role command routing policy (saa-cgh.99) Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell * fix(hooks): retain legitimate inherited tracking context (saa-cgh.98) Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell * test(hooks): isolate trusted global configuration fixture (saa-cgh.98) Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell * fix(hooks): isolate role cleanup in the selected repository (saa-cgh.109.4) Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell * fix(hooks): reset only common local role configuration (saa-cgh.109.10) Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell * fix(hooks): scope hooks-path cleanup to selected common config (saa-cgh.109.11) Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell * test(hooks): make worktree role preservation independent (saa-cgh.120) Agent-Signature: Codex-unknown-model-unknown-reasoning on behalf of Ewen Cuthiell * Report selected config read failures during hook reset (saa-cgh.120) Agent-Signature: Codex-unknown-model-unknown-reasoning on behalf of Ewen Cuthiell * fix(hooks): keep successful config stdout separate from diagnostics (saa-cgh.120) Agent-Signature: Codex-unknown-model-unknown-reasoning on behalf of Ewen Cuthiell * fix(config): pin role writes to fresh common context (GH#6384) (saa-cgh.120.3) Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell * fix(config): retain Git diagnostics from role writes (saa-cgh.120.3) Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell * test(config): assert the actual Git lock diagnostic (saa-cgh.120.3) Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell * fix(config,hooks): restore the inherited-tracking fallback in the secret guard Maintainer review fixes for PR #6454 (adopt-pr iteration 1). The scorecard approved at 913/1000 with no blocker or major surviving synthesis; these are the non-gating findings that were cheap to close while the fixup was open. Finding 1 (minor, security) is the only in-range regression in the report. The range switched internal/config.isGitTracked from the inherited environment to gitenv.ScrubRouting only, so a config.yaml tracked solely through legitimate routing -- a bare repository whose work tree is named by GIT_DIR/GIT_WORK_TREE -- reported untracked, and a secret-key write that upstream-base refused was silently allowed. Restores the scrubbed-then-inherited union the same stack already gave cmd/bd.isGitTrackedFile. Finding 4 (nit) composes with it rather than conflicting, so both land as one change: the inherited probe now runs only when the scrubbed probe failed for a configuration reason. git's exit codes discriminate exactly -- 0 tracked, 1 repository reached but path untracked (final), 128 no reachable repository -- so the ordinary untracked-hook miss no longer spawns a second `git ls-files` while the bare-work-tree fallback is preserved. Finding 2 (minor). scrubWorktreeRemovalGitEnvForOS keyed its stricter no-optional-locks policy on strings.ToUpper while the rest of the PR uses execenv key identity. The two disagree in both directions on Windows: GIT_OPTIONAL_LOCKS with a dotted capital I (U+0130) survived the scrub although Git honors it, and GIT_OPTIONAL_LOCK with a long s (U+017F) was dropped although os/exec keeps it distinct. normalizeWorktreeGitEnvKey is deleted in favour of execenv.KeyEqualForOS. Base applied ToUpper on all platforms, so this is a consistency fix in newly rewritten code, not a regression. Finding 5 (nit). The comment above resetHooksPathIfBeadsManaged's probe asserted that repoRoot and commonDir share the cached Git context. Probed false: with cwd A and GIT_DIR=B/.git, `git rev-parse --git-common-dir --show-toplevel` prints B/.git and A. Restated as intent. Finding 3 (scrubGitHookEnv is a fifth, strictly weaker routing scrubber) is deliberately not taken here: cmd/bd/export_auto.go is untouched by this range, so the hazard is byte-identical at base, and the synthesis scoped it to a follow-up bead rather than growing this stack. Each fix is mutation-proved. Reverting isGitTracked reds only the new TestSecretGitTrackingHonorsInheritedBareWorkTree -- including "refusal = <nil>", the actual security consequence -- while the incumbent decoy test stays green, so the pin adds coverage rather than duplicating it. Restoring ToUpper reds only the new Windows row, showing both directions at once. TestGuardHookWritePathHonorsInheritedRepository gains a fixture guard asserting its scrubbed probe fails for a configuration reason rather than exit 1, so a future fixture change cannot silently make the fallback unreachable. scrubWorktreeRemovalGitEnvForOS had no test at all before this. Green locally: ./internal/config, ./internal/execenv, ./internal/gitenv, ./internal/githooksenv, ./internal/gittraceenv, ./internal/metrics, ./scripts, ./internal/beads (the GIT_CONFIG_GLOBAL blast radius neither reviewer ran), and a blast-radius cmd/bd run over the hooks, worktree, role and uninstall suites. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> --------- Signed-off-by: Ewen Cuthiell <ewencuthiell@gmail.com> Co-authored-by: Bee <bee@ghosttrack.com> Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local> Co-authored-by: Julian Knutsen <julianknutsen@users.noreply.github.com> Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
487 lines
16 KiB
Go
487 lines
16 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/steveyegge/beads/internal/gitenv"
|
|
)
|
|
|
|
func TestTruncateForBox(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
path string
|
|
maxLen int
|
|
want string
|
|
}{
|
|
{
|
|
name: "short path no truncate",
|
|
path: "/home/user",
|
|
maxLen: 20,
|
|
want: "/home/user",
|
|
},
|
|
{
|
|
name: "exact length",
|
|
path: "12345",
|
|
maxLen: 5,
|
|
want: "12345",
|
|
},
|
|
{
|
|
name: "needs truncate",
|
|
path: "/very/long/path/to/somewhere/deep",
|
|
maxLen: 15,
|
|
want: "...mewhere/deep",
|
|
},
|
|
{
|
|
name: "truncate to minimum",
|
|
path: "abcdefghij",
|
|
maxLen: 5,
|
|
want: "...ij",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := truncateForBox(tt.path, tt.maxLen)
|
|
if got != tt.want {
|
|
t.Errorf("truncateForBox(%q, %d) = %q, want %q", tt.path, tt.maxLen, got, tt.want)
|
|
}
|
|
if len(got) > tt.maxLen {
|
|
t.Errorf("truncateForBox(%q, %d) returned %q with length %d > maxLen %d",
|
|
tt.path, tt.maxLen, got, len(got), tt.maxLen)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGitRevParse(t *testing.T) {
|
|
// Basic test - should either return a value or empty string (if not in git repo)
|
|
result := gitRevParse("--git-dir")
|
|
// Just verify it doesn't panic and returns a string
|
|
if result != "" {
|
|
// In a git repo
|
|
t.Logf("Git dir: %s", result)
|
|
} else {
|
|
// Not in a git repo or error
|
|
t.Logf("Not in git repo or error")
|
|
}
|
|
}
|
|
|
|
func TestGitCmdInDirSuppressesHooksViaGitConfig(t *testing.T) {
|
|
t.Setenv("GIT_DIR", "/wrong")
|
|
t.Setenv("GIT_OPTIONAL_LOCKS", "1")
|
|
cmd := gitCmdInDir(t.Context(), "/tmp/example", "status", "--porcelain")
|
|
if cmd.Dir != "/tmp/example" {
|
|
t.Fatalf("cmd.Dir = %q, want /tmp/example", cmd.Dir)
|
|
}
|
|
wantArgs := []string{"-c", "core.hooksPath=", "status", "--porcelain"}
|
|
if len(cmd.Args) != len(wantArgs)+1 {
|
|
t.Fatalf("cmd.Args = %#v, want git plus %#v", cmd.Args, wantArgs)
|
|
}
|
|
for i, want := range wantArgs {
|
|
if got := cmd.Args[i+1]; got != want {
|
|
t.Fatalf("cmd.Args[%d] = %q, want %q; full args: %#v", i+1, got, want, cmd.Args)
|
|
}
|
|
}
|
|
hasTemplateSuppression := false
|
|
hasOptionalLocks := false
|
|
for _, env := range cmd.Env {
|
|
if env == "GIT_HOOKS_PATH=" {
|
|
t.Fatal("cmd.Env contains dead GIT_HOOKS_PATH hook suppression")
|
|
}
|
|
if env == "GIT_TEMPLATE_DIR=" {
|
|
hasTemplateSuppression = true
|
|
}
|
|
if env == "GIT_OPTIONAL_LOCKS=1" {
|
|
hasOptionalLocks = true
|
|
}
|
|
if gitenv.IsRoutingKeyForOS(worktreeGitEnvKey(env), runtime.GOOS) && env != "GIT_TEMPLATE_DIR=" {
|
|
t.Fatalf("cmd.Env retains Git routing state: %q", env)
|
|
}
|
|
}
|
|
if !hasTemplateSuppression {
|
|
t.Fatal("cmd.Env missing GIT_TEMPLATE_DIR suppression")
|
|
}
|
|
if !hasOptionalLocks {
|
|
t.Fatal("cmd.Env did not preserve GIT_OPTIONAL_LOCKS")
|
|
}
|
|
}
|
|
|
|
func TestClearWorktreeGitRoutingEnvPreservesNonRoutingVariables(t *testing.T) {
|
|
// ClearRouting is deliberately process-lifetime state for the real CLI.
|
|
// Register restorations for every routing key inherited by the test process
|
|
// before exercising that boundary in-process.
|
|
for _, entry := range os.Environ() {
|
|
key := worktreeGitEnvKey(entry)
|
|
if gitenv.IsRoutingKeyForOS(key, runtime.GOOS) {
|
|
t.Setenv(key, os.Getenv(key))
|
|
}
|
|
}
|
|
t.Setenv("GIT_DIR", "/wrong")
|
|
t.Setenv("GIT_CONFIG_COUNT", "1")
|
|
t.Setenv("GIT_OPTIONAL_LOCKS", "1")
|
|
t.Setenv("GIT_NO_REPLACE_OBJECTS", "1")
|
|
t.Setenv("BD_WORKTREE_ENV_SENTINEL", "keep")
|
|
|
|
if err := clearWorktreeGitRoutingEnv(worktreeListCmd); err != nil {
|
|
t.Fatalf("clearWorktreeGitRoutingEnv() error = %v", err)
|
|
}
|
|
for _, key := range []string{"GIT_DIR", "GIT_CONFIG_COUNT"} {
|
|
if value, ok := os.LookupEnv(key); ok {
|
|
t.Fatalf("%s remains set to %q", key, value)
|
|
}
|
|
}
|
|
for key, want := range map[string]string{
|
|
"GIT_OPTIONAL_LOCKS": "1",
|
|
"GIT_NO_REPLACE_OBJECTS": "1",
|
|
"BD_WORKTREE_ENV_SENTINEL": "keep",
|
|
} {
|
|
if got := os.Getenv(key); got != want {
|
|
t.Fatalf("%s = %q, want %q", key, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestWorktreeCommandsIgnoreInheritedGitRoutingState(t *testing.T) {
|
|
bdBinary := buildBDForInitTests(t)
|
|
|
|
decoyRepo := newGitRepo(t)
|
|
commitTestFile(t, decoyRepo, "README.md", "decoy\n", "decoy commit")
|
|
decoyBeadsDir := filepath.Join(decoyRepo, ".beads")
|
|
writeTestConfigYAML(t, decoyBeadsDir, "issue-prefix: decoy\n")
|
|
if err := os.WriteFile(
|
|
filepath.Join(decoyBeadsDir, ".env"),
|
|
[]byte("BEADS_DIR="+filepath.ToSlash(decoyBeadsDir)+"\n"),
|
|
0o600,
|
|
); err != nil {
|
|
t.Fatalf("write decoy selector environment: %v", err)
|
|
}
|
|
// An unsanitized post-create status check would see this file and reject an
|
|
// otherwise clean target worktree.
|
|
if err := os.WriteFile(filepath.Join(decoyRepo, "decoy-dirty.txt"), []byte("dirty\n"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
poisonConfig := filepath.Join(t.TempDir(), "poison.gitconfig")
|
|
if err := os.WriteFile(poisonConfig, []byte("[core]\n\tworktree = "+filepath.ToSlash(decoyRepo)+"\n"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
poisonEnv := []string{
|
|
"GIT_DIR=" + filepath.Join(decoyRepo, ".git"),
|
|
"GIT_WORK_TREE=" + decoyRepo,
|
|
"GIT_COMMON_DIR=" + filepath.Join(decoyRepo, ".git"),
|
|
"GIT_INDEX_FILE=" + filepath.Join(decoyRepo, ".git", "index"),
|
|
"GIT_OBJECT_DIRECTORY=" + filepath.Join(decoyRepo, ".git", "objects"),
|
|
"GIT_ALTERNATE_OBJECT_DIRECTORIES=" + filepath.Join(decoyRepo, ".git", "objects"),
|
|
"GIT_EXEC_PATH=" + filepath.Join(decoyRepo, "missing-git-exec"),
|
|
"GIT_NAMESPACE=poison",
|
|
"GIT_CONFIG_GLOBAL=" + poisonConfig,
|
|
"GIT_CONFIG_SYSTEM=" + poisonConfig,
|
|
"GIT_CONFIG_NOSYSTEM=0",
|
|
"GIT_CONFIG_COUNT=1",
|
|
"GIT_CONFIG_KEY_0=core.worktree",
|
|
"GIT_CONFIG_VALUE_0=" + decoyRepo,
|
|
"GIT_OPTIONAL_LOCKS=1",
|
|
"BEADS_DIR=",
|
|
"BEADS_DB=",
|
|
"BD_DB=",
|
|
}
|
|
|
|
t.Run("create list and remove stay in the target repository", func(t *testing.T) {
|
|
targetRepo := newGitRepo(t)
|
|
commitTestFile(t, targetRepo, "README.md", "target\n", "target commit")
|
|
writeTestConfigYAML(t, filepath.Join(targetRepo, ".beads"), "issue-prefix: target\n")
|
|
decoyRegistryBefore := gitWorktreeRegistry(t, decoyRepo)
|
|
|
|
create := runWorktreeCommandProcess(t, bdBinary, targetRepo, "create", poisonEnv, "lane")
|
|
create.requireSuccess(t)
|
|
var created struct {
|
|
Path string `json:"path"`
|
|
Branch string `json:"branch"`
|
|
}
|
|
decodeWorktreeCommandJSON(t, create.stdout, &created)
|
|
lane := filepath.Join(targetRepo, "lane")
|
|
if !sameWorktreePath(created.Path, lane) || created.Branch != "lane" {
|
|
t.Fatalf("create result = %#v, want target lane %q", created, lane)
|
|
}
|
|
assertGitWorktreePaths(t, targetRepo, targetRepo, lane)
|
|
if got := gitWorktreeRegistry(t, decoyRepo); got != decoyRegistryBefore {
|
|
t.Fatalf("create mutated decoy worktree registry:\n--- before ---\n%s\n--- after ---\n%s", decoyRegistryBefore, got)
|
|
}
|
|
|
|
list := runWorktreeCommandProcess(t, bdBinary, targetRepo, "list", poisonEnv)
|
|
list.requireSuccess(t)
|
|
var listed []WorktreeInfo
|
|
decodeWorktreeCommandJSON(t, list.stdout, &listed)
|
|
if len(listed) != 2 ||
|
|
!sameWorktreePath(listed[0].Path, targetRepo) || listed[0].BeadsState != "shared" ||
|
|
!sameWorktreePath(listed[1].Path, lane) || listed[1].BeadsState != "none" {
|
|
t.Fatalf("list resolved outside target repository or beads state: %#v", listed)
|
|
}
|
|
|
|
info := runWorktreeCommandProcess(t, bdBinary, lane, "info", poisonEnv)
|
|
info.requireSuccess(t)
|
|
var details struct {
|
|
IsWorktree bool `json:"is_worktree"`
|
|
Path string `json:"path"`
|
|
MainRepo string `json:"main_repo"`
|
|
Branch string `json:"branch"`
|
|
BeadsRedirected bool `json:"beads_redirected"`
|
|
}
|
|
decodeWorktreeCommandJSON(t, info.stdout, &details)
|
|
if !details.IsWorktree ||
|
|
!sameWorktreePath(details.Path, lane) ||
|
|
!sameWorktreePath(details.MainRepo, targetRepo) ||
|
|
details.Branch != "lane" ||
|
|
details.BeadsRedirected {
|
|
t.Fatalf("info resolved outside the target worktree: %#v", details)
|
|
}
|
|
|
|
remove := runWorktreeCommandProcess(t, bdBinary, targetRepo, "remove", poisonEnv, lane, "--force")
|
|
remove.requireSuccess(t)
|
|
if _, err := os.Stat(lane); !os.IsNotExist(err) {
|
|
t.Fatalf("remove left target worktree behind: %v", err)
|
|
}
|
|
assertGitWorktreePaths(t, targetRepo, targetRepo)
|
|
if _, err := os.Stat(filepath.Join(decoyRepo, "decoy-dirty.txt")); err != nil {
|
|
t.Fatalf("remove mutated decoy repository: %v", err)
|
|
}
|
|
})
|
|
|
|
t.Run("create refuses a target without a beads workspace", func(t *testing.T) {
|
|
targetRepo := newGitRepo(t)
|
|
commitTestFile(t, targetRepo, "README.md", "target without beads\n", "target commit")
|
|
lane := filepath.Join(targetRepo, "lane")
|
|
registryBefore := gitWorktreeRegistry(t, targetRepo)
|
|
|
|
create := runWorktreeCommandProcess(t, bdBinary, targetRepo, "create", poisonEnv, "lane")
|
|
if create.exitCode == 0 {
|
|
t.Fatalf("create accepted the decoy beads workspace\nstdout:\n%s\nstderr:\n%s", create.stdout, create.stderr)
|
|
}
|
|
if _, err := os.Stat(lane); !os.IsNotExist(err) {
|
|
t.Fatalf("refused create mutated target path: %v", err)
|
|
}
|
|
if got := gitWorktreeRegistry(t, targetRepo); got != registryBefore {
|
|
t.Fatalf("refused create mutated target worktree registry:\n--- before ---\n%s\n--- after ---\n%s", registryBefore, got)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(targetRepo, ".gitignore")); !os.IsNotExist(err) {
|
|
t.Fatalf("refused create mutated target gitignore: %v", err)
|
|
}
|
|
})
|
|
|
|
t.Run("startup ignores malformed config selected by linked-worktree routing", func(t *testing.T) {
|
|
targetRepo := newGitRepo(t)
|
|
commitTestFile(t, targetRepo, "README.md", "target\n", "target commit")
|
|
|
|
decoyMain := newGitRepo(t)
|
|
commitTestFile(t, decoyMain, "README.md", "linked decoy\n", "decoy commit")
|
|
writeTestConfigYAML(t, filepath.Join(decoyMain, ".beads"), "json: [\n")
|
|
decoyLane := filepath.Join(t.TempDir(), "linked-decoy")
|
|
command := exec.Command("git", "worktree", "add", "-b", "linked-decoy", decoyLane)
|
|
command.Dir = decoyMain
|
|
if output, err := command.CombinedOutput(); err != nil {
|
|
t.Fatalf("create decoy linked worktree: %v\n%s", err, output)
|
|
}
|
|
decoyAdminDir := gitRevParsePath(t, decoyLane, "--absolute-git-dir")
|
|
decoyCommonDir := gitRevParsePath(t, decoyLane, "--git-common-dir")
|
|
|
|
linkedPoisonEnv := append(append([]string(nil), poisonEnv...),
|
|
"GIT_DIR="+decoyAdminDir,
|
|
"GIT_WORK_TREE="+decoyLane,
|
|
"GIT_COMMON_DIR="+decoyCommonDir,
|
|
"GIT_INDEX_FILE="+filepath.Join(decoyAdminDir, "index"),
|
|
"GIT_OBJECT_DIRECTORY="+filepath.Join(decoyCommonDir, "objects"),
|
|
"GIT_ALTERNATE_OBJECT_DIRECTORIES="+filepath.Join(decoyCommonDir, "objects"),
|
|
"BEADS_TEST_IGNORE_REPO_CONFIG=",
|
|
)
|
|
list := runWorktreeCommandProcess(t, bdBinary, targetRepo, "list", linkedPoisonEnv)
|
|
list.requireSuccess(t)
|
|
if strings.Contains(strings.ToLower(list.stderr), "failed to initialize config") {
|
|
t.Fatalf("malformed decoy config reached startup initialization:\n%s", list.stderr)
|
|
}
|
|
var listed []WorktreeInfo
|
|
decodeWorktreeCommandJSON(t, list.stdout, &listed)
|
|
if len(listed) != 1 || !sameWorktreePath(listed[0].Path, targetRepo) {
|
|
t.Fatalf("list resolved outside target repository: %#v", listed)
|
|
}
|
|
})
|
|
}
|
|
|
|
func runWorktreeCommandProcess(t *testing.T, bdBinary, dir, action string, extraEnv []string, args ...string) worktreeRemoveProcessResult {
|
|
t.Helper()
|
|
commandArgs := append([]string{"--json", "worktree", action}, args...)
|
|
command := exec.Command(bdBinary, commandArgs...)
|
|
command.Dir = dir
|
|
command.Env = overrideWorktreeRemoveEnv(os.Environ(), append([]string{
|
|
"BD_DISABLE_METRICS=1",
|
|
"BD_DISABLE_EVENT_FLUSH=1",
|
|
"BEADS_TEST_MODE=1",
|
|
}, extraEnv...))
|
|
var stdout, stderr bytes.Buffer
|
|
command.Stdout, command.Stderr = &stdout, &stderr
|
|
err := command.Run()
|
|
exitCode := 0
|
|
if err != nil {
|
|
var exitError *exec.ExitError
|
|
if !errors.As(err, &exitError) {
|
|
t.Fatalf("launch worktree command helper: %v", err)
|
|
}
|
|
exitCode = exitError.ExitCode()
|
|
}
|
|
return worktreeRemoveProcessResult{stdout: stdout.String(), stderr: stderr.String(), exitCode: exitCode}
|
|
}
|
|
|
|
func decodeWorktreeCommandJSON(t *testing.T, output string, target any) {
|
|
t.Helper()
|
|
if err := json.NewDecoder(strings.NewReader(output)).Decode(target); err != nil {
|
|
t.Fatalf("decode helper JSON: %v\noutput:\n%s", err, output)
|
|
}
|
|
}
|
|
|
|
func gitWorktreeRegistry(t *testing.T, repoDir string) string {
|
|
t.Helper()
|
|
command := exec.Command("git", "worktree", "list", "--porcelain")
|
|
command.Dir = repoDir
|
|
output, err := command.CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("list Git worktree registry for %q: %v\n%s", repoDir, err, output)
|
|
}
|
|
return strings.TrimSpace(string(output))
|
|
}
|
|
|
|
func assertGitWorktreePaths(t *testing.T, repoDir string, want ...string) {
|
|
t.Helper()
|
|
var got []string
|
|
for _, line := range strings.Split(gitWorktreeRegistry(t, repoDir), "\n") {
|
|
if path, ok := strings.CutPrefix(strings.TrimSpace(line), "worktree "); ok {
|
|
got = append(got, filepath.FromSlash(path))
|
|
}
|
|
}
|
|
if len(got) != len(want) {
|
|
t.Fatalf("Git worktree paths = %#v, want %#v", got, want)
|
|
}
|
|
matched := make([]bool, len(got))
|
|
for _, wantPath := range want {
|
|
found := false
|
|
for index, gotPath := range got {
|
|
if !matched[index] && sameWorktreePath(gotPath, wantPath) {
|
|
matched[index] = true
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatalf("Git worktree paths = %#v, missing %q", got, wantPath)
|
|
}
|
|
}
|
|
}
|
|
|
|
func gitRevParsePath(t *testing.T, repoDir, selector string) string {
|
|
t.Helper()
|
|
command := exec.Command("git", "rev-parse", selector)
|
|
command.Dir = repoDir
|
|
output, err := command.CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("git rev-parse %s in %q: %v\n%s", selector, repoDir, err, output)
|
|
}
|
|
path := filepath.FromSlash(strings.TrimSpace(string(output)))
|
|
if !filepath.IsAbs(path) {
|
|
path = filepath.Join(repoDir, path)
|
|
}
|
|
return filepath.Clean(path)
|
|
}
|
|
|
|
// TestResolveWorktreePathByName verifies that resolveWorktreePath can find
|
|
// worktrees by name (basename) when they're in subdirectories like .worktrees/
|
|
func TestResolveWorktreePathByName(t *testing.T) {
|
|
// Create a temp directory for the main repo
|
|
mainDir := newGitRepo(t)
|
|
|
|
// Create initial commit (required for worktrees)
|
|
if err := os.WriteFile(filepath.Join(mainDir, "README.md"), []byte("# Test\n"), 0644); err != nil {
|
|
t.Fatalf("Failed to create test file: %v", err)
|
|
}
|
|
cmd := exec.Command("git", "add", ".")
|
|
cmd.Dir = mainDir
|
|
_ = cmd.Run()
|
|
cmd = exec.Command("git", "commit", "-m", "Initial commit")
|
|
cmd.Dir = mainDir
|
|
if output, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("Failed to create initial commit: %v\n%s", err, output)
|
|
}
|
|
|
|
// Create .worktrees subdirectory
|
|
worktreesDir := filepath.Join(mainDir, ".worktrees")
|
|
if err := os.MkdirAll(worktreesDir, 0755); err != nil {
|
|
t.Fatalf("Failed to create .worktrees dir: %v", err)
|
|
}
|
|
|
|
// Create a worktree inside .worktrees/
|
|
worktreePath := filepath.Join(worktreesDir, "test-wt")
|
|
cmd = exec.Command("git", "worktree", "add", "-b", "test-wt", worktreePath)
|
|
cmd.Dir = mainDir
|
|
if output, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("Failed to create worktree: %v\n%s", err, output)
|
|
}
|
|
defer func() {
|
|
// Cleanup worktree
|
|
cmd := exec.Command("git", "worktree", "remove", worktreePath, "--force")
|
|
cmd.Dir = mainDir
|
|
_ = cmd.Run()
|
|
}()
|
|
|
|
ctx := context.Background()
|
|
|
|
t.Run("resolves by name when worktree is in subdirectory", func(t *testing.T) {
|
|
// This should find the worktree by consulting git's registry
|
|
resolved, err := resolveWorktreePath(ctx, mainDir, "test-wt")
|
|
if err != nil {
|
|
t.Errorf("resolveWorktreePath(repoRoot, \"test-wt\") failed: %v", err)
|
|
return
|
|
}
|
|
// Compare resolved paths to handle symlinks (e.g., /var -> /private/var on macOS)
|
|
wantResolved, _ := filepath.EvalSymlinks(worktreePath)
|
|
gotResolved, _ := filepath.EvalSymlinks(resolved)
|
|
if gotResolved != wantResolved {
|
|
t.Errorf("resolveWorktreePath returned %q, want %q", resolved, worktreePath)
|
|
}
|
|
})
|
|
|
|
t.Run("resolves by relative path", func(t *testing.T) {
|
|
// This should work via the existing relative-to-repo-root logic
|
|
resolved, err := resolveWorktreePath(ctx, mainDir, ".worktrees/test-wt")
|
|
if err != nil {
|
|
t.Errorf("resolveWorktreePath(repoRoot, \".worktrees/test-wt\") failed: %v", err)
|
|
return
|
|
}
|
|
if resolved != worktreePath {
|
|
t.Errorf("resolveWorktreePath returned %q, want %q", resolved, worktreePath)
|
|
}
|
|
})
|
|
|
|
t.Run("resolves by absolute path", func(t *testing.T) {
|
|
resolved, err := resolveWorktreePath(ctx, mainDir, worktreePath)
|
|
if err != nil {
|
|
t.Errorf("resolveWorktreePath(repoRoot, absolutePath) failed: %v", err)
|
|
return
|
|
}
|
|
if resolved != worktreePath {
|
|
t.Errorf("resolveWorktreePath returned %q, want %q", resolved, worktreePath)
|
|
}
|
|
})
|
|
|
|
t.Run("returns error for non-existent worktree", func(t *testing.T) {
|
|
_, err := resolveWorktreePath(ctx, mainDir, "non-existent")
|
|
if err == nil {
|
|
t.Error("resolveWorktreePath should return error for non-existent worktree")
|
|
}
|
|
})
|
|
}
|