beads/cmd/bd/init_git_hooks.go
Pablo LION 7dafce0743
feat: auto-update stale hooks during bd init (GH#1466) (#2008)
* feat: auto-update stale hooks during bd init (GH#1466)

bd init now detects outdated git hooks and reinstalls them, not just
missing ones. Previously, hooksInstalled() only checked existence and
signature — hooks from older CLI versions were never updated.

Adds hooksNeedUpdate() which uses getHookVersion() to compare hook
version markers against the current CLI version. Shim hooks are skipped
(they delegate to bd hooks run and are never stale).

Adds bd-hooks-version markers to all inline hook templates so newly
installed hooks are properly versioned and not flagged as outdated on
subsequent bd init runs.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: address review findings for stale hooks detection

- Fix misleading test: "non-executable hook files" now uses current
  version to isolate the permissions variable, documenting that
  hooksNeedUpdate checks version not permissions
- Distinguish file-not-found from permission/IO errors in
  hooksNeedUpdate — warn on stderr for non-NotExist errors instead
  of silently continuing
- Add user feedback distinguishing "update" from fresh "install" —
  prints "Updating hooks to version X.Y.Z..." when overwriting
  existing outdated hooks
- Document calling context constraint in hooksNeedUpdate doc comment

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: check scanner.Err in getHookVersion and handle read errors in hooksNeedUpdate

getHookVersion now checks scanner.Err() after reading hook files,
surfacing I/O errors instead of silently misclassifying hooks as
non-bd (which caused hooksNeedUpdate to skip them with no warning).

hooksNeedUpdate now tracks read errors and returns true when hooks
exist but couldn't be read, attempting re-install rather than silently
skipping the update.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-24 20:10:02 -08:00

491 lines
15 KiB
Go

package main
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/steveyegge/beads/internal/git"
"github.com/steveyegge/beads/internal/ui"
)
// preCommitFrameworkPattern matches pre-commit or prek framework hooks.
// Uses same patterns as hookManagerPatterns in doctor/fix/hooks.go for consistency.
// Includes all detection patterns: pre-commit run, prek run/hook-impl, config file refs, and pre-commit env vars.
var preCommitFrameworkPattern = regexp.MustCompile(`(?i)(pre-commit\s+run|prek\s+run|prek\s+hook-impl|\.pre-commit-config|INSTALL_PYTHON|PRE_COMMIT)`)
// hooksInstalled checks if bd git hooks are installed
func hooksInstalled() bool {
hooksDir, err := git.GetGitHooksDir()
if err != nil {
return false
}
preCommit := filepath.Join(hooksDir, "pre-commit")
postMerge := filepath.Join(hooksDir, "post-merge")
// Check if both hooks exist
_, err1 := os.Stat(preCommit)
_, err2 := os.Stat(postMerge)
if err1 != nil || err2 != nil {
return false
}
// Verify they're bd hooks by checking for signature comment
// #nosec G304 - controlled path from git directory
preCommitContent, err := os.ReadFile(preCommit)
if err != nil || !strings.Contains(string(preCommitContent), "bd (beads) pre-commit hook") {
return false
}
// #nosec G304 - controlled path from git directory
postMergeContent, err := os.ReadFile(postMerge)
if err != nil || !strings.Contains(string(postMergeContent), "bd (beads) post-merge hook") {
return false
}
// Verify hooks are executable
preCommitInfo, err := os.Stat(preCommit)
if err != nil {
return false
}
if preCommitInfo.Mode().Perm()&0111 == 0 {
return false // Not executable
}
postMergeInfo, err := os.Stat(postMerge)
if err != nil {
return false
}
if postMergeInfo.Mode().Perm()&0111 == 0 {
return false // Not executable
}
return true
}
// hooksNeedUpdate checks if installed bd hooks are outdated and need updating.
// Returns true if any bd hook has a version different from the current CLI version.
// Shim hooks are never outdated (they delegate to bd hooks run).
// Inline hooks without version markers are always considered outdated.
//
// On error (e.g. no git hooks dir), returns false. This is safe when called
// in the init.go condition (!hooksInstalled() || hooksNeedUpdate()) because
// hooksInstalled() fails the same way, making the OR expression evaluate to true.
func hooksNeedUpdate() bool {
hooksDir, err := git.GetGitHooksDir()
if err != nil {
return false
}
readErrors := false
hookNames := []string{"pre-commit", "post-merge"}
for _, name := range hookNames {
path := filepath.Join(hooksDir, name)
info, err := getHookVersion(path)
if err != nil {
if !os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "Warning: could not read hook %s: %v\n", name, err)
readErrors = true
}
continue
}
if !info.IsBdHook || info.IsShim {
continue
}
// Outdated if version is missing (inline hooks) or differs from current
if info.Version != Version {
return true
}
}
// If we couldn't read hooks that hooksInstalled() confirmed exist,
// attempt re-install rather than silently skipping the update.
return readErrors
}
// hookInfo contains information about an existing hook
type hookInfo struct {
name string
path string
exists bool
isBdHook bool
isPreCommitFramework bool // true for pre-commit or prek
content string
}
// detectExistingHooks scans for existing git hooks
func detectExistingHooks() []hookInfo {
hooksDir, err := git.GetGitHooksDir()
if err != nil {
return nil
}
hooks := []hookInfo{
{name: "pre-commit", path: filepath.Join(hooksDir, "pre-commit")},
{name: "post-merge", path: filepath.Join(hooksDir, "post-merge")},
{name: "pre-push", path: filepath.Join(hooksDir, "pre-push")},
}
for i := range hooks {
content, err := os.ReadFile(hooks[i].path)
if err == nil {
hooks[i].exists = true
hooks[i].content = string(content)
hooks[i].isBdHook = strings.Contains(hooks[i].content, "bd (beads)")
// Only detect pre-commit/prek framework if not a bd hook
// Use regex for consistency with DetectActiveHookManager patterns
if !hooks[i].isBdHook {
hooks[i].isPreCommitFramework = preCommitFrameworkPattern.MatchString(hooks[i].content)
}
}
}
return hooks
}
// installGitHooks installs git hooks inline (no external dependencies)
func installGitHooks() error {
hooksDir, err := git.GetGitHooksDir()
if err != nil {
return err
}
// Ensure hooks directory exists
if err := os.MkdirAll(hooksDir, 0750); err != nil {
return fmt.Errorf("failed to create hooks directory: %w", err)
}
// Detect existing hooks
existingHooks := detectExistingHooks()
// Check if any non-bd hooks exist
hasExistingHooks := false
for _, hook := range existingHooks {
if hook.exists && !hook.isBdHook {
hasExistingHooks = true
break
}
}
// Default to chaining with existing hooks (no prompting)
chainHooks := hasExistingHooks
if chainHooks {
// Chain mode - rename existing hooks to .old so they can be called
for _, hook := range existingHooks {
if hook.exists && !hook.isBdHook {
oldPath := hook.path + ".old"
if err := os.Rename(hook.path, oldPath); err != nil {
fmt.Fprintf(os.Stderr, "%s Failed to chain with existing %s hook: %v\n", ui.RenderWarn("⚠"), hook.name, err)
fmt.Fprintf(os.Stderr, "You can resolve this with: %s\n", ui.RenderAccent("bd doctor --fix"))
continue
}
fmt.Printf(" Chained with existing %s hook\n", hook.name)
}
}
}
// pre-commit hook
preCommitPath := filepath.Join(hooksDir, "pre-commit")
preCommitContent := buildPreCommitHook(chainHooks, existingHooks)
// post-merge hook
postMergePath := filepath.Join(hooksDir, "post-merge")
postMergeContent := buildPostMergeHook(chainHooks, existingHooks)
// Normalize line endings to LF — on Windows/NTFS, Go string literals
// are fine but concatenated content from other sources may have CRLF.
// Git hooks with CRLF fail: /usr/bin/env: 'sh\r': No such file or directory
preCommitContent = strings.ReplaceAll(preCommitContent, "\r\n", "\n")
postMergeContent = strings.ReplaceAll(postMergeContent, "\r\n", "\n")
// Write pre-commit hook (executable scripts need 0700)
// #nosec G306 - git hooks must be executable
if err := os.WriteFile(preCommitPath, []byte(preCommitContent), 0700); err != nil {
return fmt.Errorf("failed to write pre-commit hook: %w", err)
}
// Write post-merge hook (executable scripts need 0700)
// #nosec G306 - git hooks must be executable
if err := os.WriteFile(postMergePath, []byte(postMergeContent), 0700); err != nil {
return fmt.Errorf("failed to write post-merge hook: %w", err)
}
if chainHooks {
fmt.Printf("%s Chained bd hooks with existing hooks\n", ui.RenderPass("✓"))
}
return nil
}
// buildPreCommitHook generates the pre-commit hook content
func buildPreCommitHook(chainHooks bool, existingHooks []hookInfo) string {
if chainHooks {
// Find existing pre-commit hook (already renamed to .old by caller)
var existingPreCommit string
for _, hook := range existingHooks {
if hook.name == "pre-commit" && hook.exists && !hook.isBdHook {
existingPreCommit = hook.path + ".old"
break
}
}
return `#!/bin/sh
# bd-hooks-version: ` + Version + `
#
# bd (beads) pre-commit hook (chained)
#
# This hook chains bd functionality with your existing pre-commit hook.
# Run existing hook first
if [ -x "` + existingPreCommit + `" ]; then
"` + existingPreCommit + `" "$@"
EXIT_CODE=$?
if [ $EXIT_CODE -ne 0 ]; then
exit $EXIT_CODE
fi
fi
` + preCommitHookBody()
}
return `#!/bin/sh
# bd-hooks-version: ` + Version + `
#
# bd (beads) pre-commit hook
#
# This hook ensures that any pending bd issue changes are synced
# before the commit is created.
` + preCommitHookBody()
}
// preCommitHookBody returns the common pre-commit hook logic.
// Delegates to 'bd hooks run pre-commit' which handles Dolt export
// and sync-branch routing without lock deadlocks.
func preCommitHookBody() string {
return `# Check if bd is available
if ! command -v bd >/dev/null 2>&1; then
echo "Warning: bd command not found, skipping pre-commit flush" >&2
exit 0
fi
# Delegate to bd hooks run pre-commit.
# The Go code handles Dolt export in-process (no lock deadlocks)
# and sync-branch routing.
exec bd hooks run pre-commit "$@"
`
}
// buildPostMergeHook generates the post-merge hook content.
// With the Dolt backend, post-merge only needs to run chained hooks.
func buildPostMergeHook(chainHooks bool, existingHooks []hookInfo) string {
if chainHooks {
// Find existing post-merge hook (already renamed to .old by caller)
var existingPostMerge string
for _, hook := range existingHooks {
if hook.name == "post-merge" && hook.exists && !hook.isBdHook {
existingPostMerge = hook.path + ".old"
break
}
}
return `#!/bin/sh
# bd-hooks-version: ` + Version + `
#
# bd (beads) post-merge hook (chained)
#
# This hook chains bd functionality with your existing post-merge hook.
# Dolt backend handles sync internally.
# Run existing hook first
if [ -x "` + existingPostMerge + `" ]; then
"` + existingPostMerge + `" "$@"
EXIT_CODE=$?
if [ $EXIT_CODE -ne 0 ]; then
exit $EXIT_CODE
fi
fi
exit 0
`
}
return `#!/bin/sh
# bd-hooks-version: ` + Version + `
#
# bd (beads) post-merge hook
#
# Dolt backend handles sync internally, so this hook is a no-op.
# It exists to support chaining with user hooks.
exit 0
`
}
// installJJHooks installs simplified git hooks for colocated jujutsu+git repos.
// jj's model is simpler: the working copy IS always a commit, so no staging needed.
// Changes flow into the current change automatically.
func installJJHooks() error {
hooksDir, err := git.GetGitHooksDir()
if err != nil {
return err
}
// Ensure hooks directory exists
if err := os.MkdirAll(hooksDir, 0750); err != nil {
return fmt.Errorf("failed to create hooks directory: %w", err)
}
// Detect existing hooks
existingHooks := detectExistingHooks()
// Check if any non-bd hooks exist
hasExistingHooks := false
for _, hook := range existingHooks {
if hook.exists && !hook.isBdHook {
hasExistingHooks = true
break
}
}
// Default to chaining with existing hooks (no prompting)
chainHooks := hasExistingHooks
if chainHooks {
// Chain mode - rename existing hooks to .old so they can be called
for _, hook := range existingHooks {
if hook.exists && !hook.isBdHook {
oldPath := hook.path + ".old"
if err := os.Rename(hook.path, oldPath); err != nil {
fmt.Fprintf(os.Stderr, "%s Failed to chain with existing %s hook: %v\n", ui.RenderWarn("⚠"), hook.name, err)
fmt.Fprintf(os.Stderr, "You can resolve this with: %s\n", ui.RenderAccent("bd doctor --fix"))
continue
}
fmt.Printf(" Chained with existing %s hook\n", hook.name)
}
}
}
// pre-commit hook (simplified for jj - no staging)
preCommitPath := filepath.Join(hooksDir, "pre-commit")
preCommitContent := buildJJPreCommitHook(chainHooks, existingHooks)
// post-merge hook (same as git)
postMergePath := filepath.Join(hooksDir, "post-merge")
postMergeContent := buildPostMergeHook(chainHooks, existingHooks)
// Write pre-commit hook
// #nosec G306 - git hooks must be executable
if err := os.WriteFile(preCommitPath, []byte(preCommitContent), 0700); err != nil {
return fmt.Errorf("failed to write pre-commit hook: %w", err)
}
// Write post-merge hook
// #nosec G306 - git hooks must be executable
if err := os.WriteFile(postMergePath, []byte(postMergeContent), 0700); err != nil {
return fmt.Errorf("failed to write post-merge hook: %w", err)
}
if chainHooks {
fmt.Printf("%s Chained bd hooks with existing hooks (jj mode)\n", ui.RenderPass("✓"))
}
return nil
}
// buildJJPreCommitHook generates the pre-commit hook content for jujutsu repos.
// jj's model is simpler: no staging needed, changes flow into the working copy automatically.
func buildJJPreCommitHook(chainHooks bool, existingHooks []hookInfo) string {
if chainHooks {
// Find existing pre-commit hook (already renamed to .old by caller)
var existingPreCommit string
for _, hook := range existingHooks {
if hook.name == "pre-commit" && hook.exists && !hook.isBdHook {
existingPreCommit = hook.path + ".old"
break
}
}
return `#!/bin/sh
# bd-hooks-version: ` + Version + `
#
# bd (beads) pre-commit hook (chained, jujutsu mode)
#
# This hook chains bd functionality with your existing pre-commit hook.
# Simplified for jujutsu: no staging needed, jj auto-commits working copy.
# Run existing hook first
if [ -x "` + existingPreCommit + `" ]; then
"` + existingPreCommit + `" "$@"
EXIT_CODE=$?
if [ $EXIT_CODE -ne 0 ]; then
exit $EXIT_CODE
fi
fi
` + jjPreCommitHookBody()
}
return `#!/bin/sh
# bd-hooks-version: ` + Version + `
#
# bd (beads) pre-commit hook (jujutsu mode)
#
# This hook ensures that any pending bd issue changes are flushed
# before the commit.
#
# Simplified for jujutsu: no staging needed, jj auto-commits working copy changes.
` + jjPreCommitHookBody()
}
// jjPreCommitHookBody returns the pre-commit hook logic for jujutsu repos.
// Key difference from git: no git add needed, jj handles working copy automatically.
// Still needs worktree handling since colocated jj+git repos can use git worktrees.
func jjPreCommitHookBody() string {
return `# Check if bd is available
if ! command -v bd >/dev/null 2>&1; then
echo "Warning: bd command not found, skipping pre-commit flush" >&2
exit 0
fi
# Check if we're in a bd workspace
# For worktrees, .beads is in the main repository root, not the worktree
BEADS_DIR=""
if git rev-parse --git-dir >/dev/null 2>&1; then
# Check if we're in a worktree
if [ "$(git rev-parse --git-dir)" != "$(git rev-parse --git-common-dir)" ]; then
# Worktree: .beads is in main repo root
MAIN_REPO_ROOT="$(git rev-parse --git-common-dir)"
MAIN_REPO_ROOT="$(dirname "$MAIN_REPO_ROOT")"
if [ -d "$MAIN_REPO_ROOT/.beads" ]; then
BEADS_DIR="$MAIN_REPO_ROOT/.beads"
fi
else
# Regular repo: check current directory
if [ -d .beads ]; then
BEADS_DIR=".beads"
fi
fi
fi
if [ -z "$BEADS_DIR" ]; then
exit 0
fi
# Dolt handles persistence directly — no flush needed.
exit 0
`
}
// printJJAliasInstructions prints setup instructions for pure jujutsu repos.
// Since jj doesn't have native hooks yet, users need to set up aliases.
func printJJAliasInstructions() {
fmt.Printf("\n%s Jujutsu repository detected (not colocated with git)\n\n", ui.RenderWarn("⚠"))
fmt.Printf("Jujutsu doesn't support hooks yet. To auto-export beads on push,\n")
fmt.Printf("add this alias to your jj config (~/.config/jj/config.toml):\n\n")
fmt.Printf(" %s\n", ui.RenderAccent("[aliases]"))
fmt.Printf(" %s\n", ui.RenderAccent(`push = ["util", "exec", "--", "sh", "-c", "bd sync --flush-only && jj git push \"$@\"", ""]`))
fmt.Printf("\nThen use %s instead of %s\n\n", ui.RenderAccent("jj push"), ui.RenderAccent("jj git push"))
fmt.Printf("For more details, see: https://github.com/steveyegge/beads/blob/main/docs/JUJUTSU.md\n\n")
}