Fixes #7349. The same race #7314 closed in internal/beads and
internal/config, in the fixtures it did not cover: a fixture's plain
`git commit` spawns the detached `git maintenance run --auto` child, whose
worktree-prune races the fixture's next `git worktree add` and deletes
.git/worktrees/<name>/locked under it (seen on PR #7312 run 37585997441,
cmd/bd Dolt-server tier shard 4/16, TestBootstrap_WorktreeFallbackDir).
One noAutoMaintenance + gitCommand(args...) helper per test package, each
in an existing untagged shared test file (cmd/bd/helpers_test.go,
doctor/git_test_helpers_test.go, doctor/fix/common_test.go,
formula/parser_test.go, git/gitdir_test.go, storage/domain/git/git_test.go;
cmd/bd/test_helpers_test.go is //go:build cgo so unusable for the pure
lane), and every inline exec.Command("git", ...) in those six packages
swapped for it. The flags ride the command line, never env config: the
routing-key scrub drops env config. No new files, so no BUILD.bazel rows.
Proof: GIT_TRACE on `go test ./cmd/bd -run TestBootstrap_WorktreeFallbackDir`
counts 3 `maintenance run --auto` spawns at d8d8bf587b and 0 with this
change.
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
65 lines
2.9 KiB
Go
65 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/steveyegge/beads/internal/gitenv"
|
|
)
|
|
|
|
// TestInitGitBootstrapProbeWidensPastLegitimateCeiling pins the git-bootstrap decision in
|
|
// `bd init` — `initArtifactGitCommand(cwd, "rev-parse", "--git-dir").Run() != nil` gates the
|
|
// fresh `git init` — against the *widening* half of the GIT_CEILING_DIRECTORIES membership
|
|
// decision documented on gitenv.IsRoutingKeyForOS.
|
|
//
|
|
// Every other ceiling assertion in the tree checks only that the key is scrubbed. Scrubbing a
|
|
// ceiling that was set legitimately widens the upward search rather than narrowing it, so a
|
|
// working directory that is not itself a repository now resolves the containing one: the probe
|
|
// succeeds and bd therefore does *not* create a fresh repository in cwd. That is the
|
|
// user-visible consequence — an operator who fenced off an ancestor repository previously got a
|
|
// fresh `git init` in cwd and now gets .beads/ inside the ancestor's working tree. It is
|
|
// deliberate, because an inherited ceiling must not take working-directory authority away from
|
|
// the selected project directory, but it is the half a future change to routingKeys would break
|
|
// silently.
|
|
func TestInitGitBootstrapProbeWidensPastLegitimateCeiling(t *testing.T) {
|
|
ancestor := t.TempDir()
|
|
initRepo := gitCommand("init", "--quiet")
|
|
initRepo.Dir = ancestor
|
|
initRepo.Env = gitenv.ScrubRouting(os.Environ())
|
|
if out, err := initRepo.CombinedOutput(); err != nil {
|
|
t.Fatalf("initialize ancestor repository: %v\n%s", err, out)
|
|
}
|
|
|
|
// The selected project directory: inside the ancestor's working tree, not a repository
|
|
// itself. This is the shape `bd init` faces at the bootstrap probe.
|
|
project := filepath.Join(ancestor, "project")
|
|
if err := os.MkdirAll(project, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// git resolves symlinks in ceiling entries before comparing, and a temp root can be
|
|
// symlinked (/tmp -> /private/tmp), so fence with the real path.
|
|
ceiling, err := filepath.EvalSymlinks(ancestor)
|
|
if err != nil {
|
|
t.Fatalf("EvalSymlinks(%q): %v", ancestor, err)
|
|
}
|
|
|
|
// Control: identical environment except the ceiling is left in place, which is what the
|
|
// probe did before this change. It must fail — otherwise the ceiling never bit and the
|
|
// assertion below would pass for the wrong reason.
|
|
fenced := gitCommand("-C", project, "rev-parse", "--git-dir")
|
|
fenced.Env = append(gitenv.ScrubRouting(os.Environ()), "GIT_CEILING_DIRECTORIES="+ceiling)
|
|
if out, ferr := fenced.Output(); ferr == nil {
|
|
t.Fatalf("ceiling %q did not fence off %q (git found %q); this case cannot prove the widening",
|
|
ceiling, project, strings.TrimSpace(string(out)))
|
|
}
|
|
|
|
t.Setenv("GIT_CEILING_DIRECTORIES", ceiling)
|
|
|
|
if err := initArtifactGitCommand(project, "rev-parse", "--git-dir").Run(); err != nil {
|
|
t.Fatalf("bootstrap probe honored the inherited ceiling (%v); `bd init` would create a "+
|
|
"second repository inside %s instead of reusing it", err, ancestor)
|
|
}
|
|
}
|