Skip to content

Conversation

@jj3ny
Copy link

@jj3ny jj3ny commented Jan 25, 2026

Summary

  • When running bd sync from a git worktree, the process inherits environment variables pointing to the worktree's .git directory instead of the main repo
  • This caused "pathspec outside repository" errors when trying to git add files in the main repo's .beads directory
  • Fix by explicitly setting GIT_DIR and GIT_WORK_TREE environment variables in GitCmd to ensure git operates on the correct repository (the one containing .beads/)

Test plan

  • Added TestGitCmd_WorktreeContext test that creates a worktree and verifies git operations work correctly
  • Existing worktree tests pass (TestFindGitRoot_Worktree, TestFindBeadsDir_Worktree, etc.)

Closes johnhughes3/stock-drop#2538

🤖 Generated with Claude Code

When running bd sync from a git worktree, the process inherits
environment variables pointing to the worktree's .git directory instead
of the main repo. This caused "pathspec outside repository" errors when
trying to git add files in the main repo's .beads directory.

Fix by explicitly setting GIT_DIR and GIT_WORK_TREE environment
variables in GitCmd to ensure git operates on the correct repository
(the one containing .beads/).

Closes #2538

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings January 25, 2026 02:58
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses bd sync failures when executed from a Git worktree by ensuring git commands consistently target the main repository that contains .beads/, rather than inheriting worktree-specific Git context.

Changes:

  • Updated RepoContext.GitCmd to explicitly set GIT_DIR and GIT_WORK_TREE for worktree-safe git operations.
  • Added an integration-style test intended to validate GitCmd behavior when invoked from within a worktree.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
internal/beads/context.go Forces git operations to run against the repo containing .beads/ by setting GIT_DIR/GIT_WORK_TREE in GitCmd.
internal/beads/context_test.go Adds TestGitCmd_WorktreeContext to validate git add/status behavior when the process CWD is a worktree.

Comment on lines 147 to +161
cmd.Env = append(os.Environ(),
"GIT_HOOKS_PATH=", // Disable hooks
"GIT_TEMPLATE_DIR=", // Disable templates
"GIT_HOOKS_PATH=", // Disable hooks
"GIT_TEMPLATE_DIR=", // Disable templates
"GIT_DIR="+gitDir, // Ensure git uses the correct .git directory
"GIT_WORK_TREE="+rc.RepoRoot, // Ensure git uses the correct work tree
Copy link

Copilot AI Jan 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GitCmd is trying to override inherited git context, but building cmd.Env via append(os.Environ(), ...) will include any existing GIT_DIR/GIT_WORK_TREE entries from the parent process, resulting in duplicate env keys. To make the behavior deterministic (and ensure the override actually takes effect), filter os.Environ() to remove any existing GIT_* entries you’re setting (at least GIT_DIR, GIT_WORK_TREE, and ideally GIT_HOOKS_PATH/GIT_TEMPLATE_DIR) before appending the intended values.

Copilot uses AI. Check for mistakes.
Comment on lines +574 to +580
// Change to worktree directory to simulate running from worktree context
if err := os.Chdir(worktreeDir); err != nil {
t.Fatalf("failed to chdir to worktree: %v", err)
}

// Reset git caches after chdir (simulates fresh process in worktree)
git.ResetCaches()
Copy link

Copilot AI Jan 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test doesn’t currently reproduce the GH#2538 failure mode described in the PR (inherited GIT_DIR/GIT_WORK_TREE from the worktree context). Since the test never sets those env vars, the git add/status calls will likely succeed even without the GitCmd env override. Consider setting GIT_DIR and GIT_WORK_TREE (via t.Setenv) to the worktree’s values before calling rc.GitCmd, so the test fails on the old behavior and passes with the fix.

Copilot generated this review using guidance from repository custom instructions.
@jj3ny jj3ny marked this pull request as draft January 25, 2026 07:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant