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:
@@ -108,20 +108,20 @@ var hookManagerPatterns = []hookManagerPattern{
|
||||
// DetectActiveHookManager reads the git hooks to determine which manager installed them.
|
||||
// This is more reliable than just checking for config files when multiple managers exist.
|
||||
func DetectActiveHookManager(path string) string {
|
||||
// Get git dir
|
||||
cmd := exec.Command("git", "rev-parse", "--git-dir")
|
||||
// Get common git dir (hooks are shared across worktrees)
|
||||
cmd := exec.Command("git", "rev-parse", "--git-common-dir")
|
||||
cmd.Dir = path
|
||||
output, err := cmd.Output()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
gitDir := strings.TrimSpace(string(output))
|
||||
if !filepath.IsAbs(gitDir) {
|
||||
gitDir = filepath.Join(path, gitDir)
|
||||
gitCommonDir := strings.TrimSpace(string(output))
|
||||
if !filepath.IsAbs(gitCommonDir) {
|
||||
gitCommonDir = filepath.Join(path, gitCommonDir)
|
||||
}
|
||||
|
||||
// Check for custom hooks path (core.hooksPath)
|
||||
hooksDir := filepath.Join(gitDir, "hooks")
|
||||
hooksDir := filepath.Join(gitCommonDir, "hooks")
|
||||
hooksPathCmd := exec.Command("git", "config", "--get", "core.hooksPath")
|
||||
hooksPathCmd.Dir = path
|
||||
if hooksPathOutput, err := hooksPathCmd.Output(); err == nil {
|
||||
|
||||
Reference in New Issue
Block a user