Fix git hooks not working in worktrees (#1126)

Git hooks are shared across all worktrees and live in the common git
directory (e.g., /repo/.git/hooks), not the worktree-specific directory
(e.g., /repo/.git/worktrees/feature/hooks).

The core issue was in GetGitHooksDir() which used GetGitDir() instead
of GetGitCommonDir(). This caused hooks to be installed to/read from
the wrong location when running in a worktree.

Additionally, several places in the codebase manually constructed
hooks paths using gitDir + "hooks" instead of calling GetGitHooksDir().
These have been updated to use the proper worktree-aware path.

Affected areas:
- GetGitHooksDir() now uses GetGitCommonDir()
- CheckGitHooks() uses GetGitHooksDir()
- installHooks/uninstallHooks use GetGitHooksDir()
- runChainedHook() uses GetGitHooksDir()
- Doctor checks use git-common-dir for hooks paths
- Reset command uses GetGitCommonDir() for hooks and beads-worktrees

Symptoms that this fixes:
- Chained hooks (pre-commit.old) not running in worktrees
- bd hooks install not finding/installing hooks correctly in worktrees
- bd hooks list showing incorrect status in worktrees
- bd doctor reporting incorrect hooks status in worktrees

Co-authored-by: Zain Rizvi <4468967+ZainRizvi@users.noreply.github.com>
This commit is contained in:
Zain Rizvi
2026-01-16 14:01:43 -06:00
committed by GitHub
parent 493b7008a2
commit e723417168
7 changed files with 58 additions and 70 deletions

View File

@@ -44,8 +44,8 @@ func runReset(cmd *cobra.Command, args []string) {
force, _ := cmd.Flags().GetBool("force")
// Check if we're in a git repo
gitDir, err := git.GetGitDir()
// Get common git directory (for hooks and beads-worktrees, which are shared across worktrees)
gitCommonDir, err := git.GetGitCommonDir()
if err != nil {
if jsonOutput {
outputJSON(map[string]interface{}{
@@ -73,7 +73,7 @@ func runReset(cmd *cobra.Command, args []string) {
}
// Collect what would be deleted
items := collectResetItems(gitDir, beadsDir)
items := collectResetItems(gitCommonDir, beadsDir)
if !force {
// Dry-run mode: show what would be deleted
@@ -82,7 +82,7 @@ func runReset(cmd *cobra.Command, args []string) {
}
// Actually perform the reset
performReset(items, gitDir, beadsDir)
performReset(items, gitCommonDir, beadsDir)
}
type resetItem struct {
@@ -91,7 +91,7 @@ type resetItem struct {
Description string `json:"description"`
}
func collectResetItems(gitDir, beadsDir string) []resetItem {
func collectResetItems(gitCommonDir, beadsDir string) []resetItem {
var items []resetItem
// Check for running daemon
@@ -106,9 +106,9 @@ func collectResetItems(gitDir, beadsDir string) []resetItem {
}
}
// Check for git hooks
// Check for git hooks (hooks are in common git dir, shared across worktrees)
hookNames := []string{"pre-commit", "post-merge", "pre-push", "post-checkout"}
hooksDir := filepath.Join(gitDir, "hooks")
hooksDir := filepath.Join(gitCommonDir, "hooks")
for _, hookName := range hookNames {
hookPath := filepath.Join(hooksDir, hookName)
if _, err := os.Stat(hookPath); err == nil {
@@ -141,8 +141,8 @@ func collectResetItems(gitDir, beadsDir string) []resetItem {
})
}
// Check for sync branch worktrees
worktreesDir := filepath.Join(gitDir, "beads-worktrees")
// Check for sync branch worktrees (in common git dir, shared across worktrees)
worktreesDir := filepath.Join(gitCommonDir, "beads-worktrees")
if info, err := os.Stat(worktreesDir); err == nil && info.IsDir() {
items = append(items, resetItem{
Type: "worktrees",