From 50908dd8d8885a146bd72f3754bcc9df1ad7d0f7 Mon Sep 17 00:00:00 2001 From: Matt Van Horn Date: Sun, 8 Mar 2026 21:46:05 -0700 Subject: [PATCH] fix: resolve repo root for initialization when started from subdirectory When `entire enable` is run from a subdirectory, change to the repo root before proceeding. This ensures agent hooks (e.g., .gemini/settings.json, .claude/settings.json) are created at the correct location and can be found by the agent regardless of CWD. Prints a note when operating from a subdirectory so the user knows the repo root is being used. Fixes #559 Co-Authored-By: Claude Opus 4.6 --- cmd/entire/cli/setup.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/cmd/entire/cli/setup.go b/cmd/entire/cli/setup.go index 4ba84d9ec..24fc131aa 100644 --- a/cmd/entire/cli/setup.go +++ b/cmd/entire/cli/setup.go @@ -56,11 +56,24 @@ modifying your active branch.`, // Check if we're in a git repository first - this is a prerequisite error, // not a usage error, so we silence Cobra's output and use SilentError // to prevent duplicate error output in main.go - if _, err := paths.WorktreeRoot(ctx); err != nil { + repoRoot, err := paths.WorktreeRoot(ctx) + if err != nil { fmt.Fprintln(cmd.ErrOrStderr(), "Not a git repository. Please run 'entire enable' from within a git repository.") return NewSilentError(errors.New("not a git repository")) } + // Change to repo root so all paths resolve correctly. + // This ensures agent hooks (e.g., .gemini/settings.json, .claude/settings.json) + // are created at the repo root even when running from a subdirectory. + cwd, _ := os.Getwd() //nolint:forbidigo // Need CWD to detect subdirectory + if cwd != repoRoot { + if err := os.Chdir(repoRoot); err != nil { //nolint:forbidigo // Intentional chdir to repo root for correct path resolution + return fmt.Errorf("failed to change to repository root %s: %w", repoRoot, err) + } + paths.ClearWorktreeRootCache() + fmt.Fprintf(cmd.ErrOrStderr(), "Note: Running from repository root (%s)\n\n", repoRoot) + } + if err := validateSetupFlags(opts.UseLocalSettings, opts.UseProjectSettings); err != nil { return err }