-
Notifications
You must be signed in to change notification settings - Fork 783
fix(sync): set GIT_DIR and GIT_WORK_TREE in GitCmd for worktree support #1312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
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>
There was a problem hiding this 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.GitCmdto explicitly setGIT_DIRandGIT_WORK_TREEfor worktree-safe git operations. - Added an integration-style test intended to validate
GitCmdbehavior 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. |
internal/beads/context.go
Outdated
| 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 |
Copilot
AI
Jan 25, 2026
There was a problem hiding this comment.
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.
| // 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() |
Copilot
AI
Jan 25, 2026
There was a problem hiding this comment.
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.
Summary
Test plan
Closes johnhughes3/stock-drop#2538
🤖 Generated with Claude Code