fix: prevent infinite loop in bd hooks run when .old is a bd shim (#843)

When bd hooks install --chain renamed an existing bd shim to .old,
subsequent bd hooks run would execute the .old shim, which would
call bd hooks run again, creating infinite recursion.

Two fixes:
1. installHooks(): Skip renaming to .old if existing hook is a bd shim
2. runChainedHook(): Skip executing .old if it is a bd shim

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
beads/crew/wolf
2026-01-01 19:46:20 -08:00
committed by Steve Yegge
parent d6045ab297
commit 050d1a4413
2 changed files with 102 additions and 3 deletions

View File

@@ -345,10 +345,17 @@ func installHooks(embeddedHooks map[string]string, force bool, shared bool, chai
if _, err := os.Stat(hookPath); err == nil {
if chain {
// Chain mode - rename to .old so bd hooks run can call it
oldPath := hookPath + ".old"
if err := os.Rename(hookPath, oldPath); err != nil {
return fmt.Errorf("failed to rename %s to .old for chaining: %w", hookName, err)
// But skip if existing hook is already a bd shim - renaming it would
// cause infinite recursion. See: https://github.com/steveyegge/beads/issues/843
versionInfo, verr := getHookVersion(hookPath)
if verr != nil || !versionInfo.IsShim {
// Not a bd shim - safe to rename for chaining
oldPath := hookPath + ".old"
if err := os.Rename(hookPath, oldPath); err != nil {
return fmt.Errorf("failed to rename %s to .old for chaining: %w", hookName, err)
}
}
// If it IS a bd shim, just overwrite it (no rename needed)
} else if !force {
// Default mode - back it up
backupPath := hookPath + ".backup"
@@ -444,6 +451,15 @@ func runChainedHook(hookName string, args []string) int {
return 0 // Not executable
}
// Check if .old is itself a bd shim - skip to prevent infinite recursion
// This can happen if user runs `bd hooks install --chain` multiple times,
// renaming an existing bd shim to .old. See: https://github.com/steveyegge/beads/issues/843
versionInfo, err := getHookVersion(oldHookPath)
if err == nil && versionInfo.IsShim {
// Skip execution - .old is a bd shim which would call us again
return 0
}
// Run the chained hook
// #nosec G204 -- hookName is from controlled list, path is from git directory
cmd := exec.Command(oldHookPath, args...)