feat(hooks): implement thin shim pattern (GH#615)

Replace full shell script hooks with thin shims that delegate to
'bd hooks run <hook-name>'. This eliminates hook version drift -
upgrading bd automatically updates hook behavior.

Changes:
- Add 'bd hooks run' subcommand with hook logic in Go
- Convert templates to minimal shims (~17 lines each)
- Shims use '# bd-shim v1' marker (format version, not bd version)
- Update version checking to recognize shim format
- Shims are never marked as "outdated"

Benefits:
- No more manual 'bd hooks install --force' after upgrades
- Hook logic always matches installed bd version
- Follows pattern used by husky, pre-commit, lefthook

Migration: Run 'bd hooks install --force' one final time.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Steve Yegge
2025-12-20 02:38:12 -08:00
parent b4bc682d93
commit 3050fd2ef8
6 changed files with 358 additions and 314 deletions

View File

@@ -1,89 +1,16 @@
#!/bin/sh
# bd-hooks-version: 0.30.7
# bd-shim v1
#
# bd (beads) post-checkout hook
# bd (beads) post-checkout hook - thin shim
#
# This hook syncs the bd database after a branch checkout:
# 1. Checks if any .beads/*.jsonl file was updated
# 2. Runs 'bd sync --import-only --no-git-history' to import changes
#
# Arguments provided by git:
# $1 = ref of previous HEAD
# $2 = ref of new HEAD
# $3 = flag (1 if branch checkout, 0 if file checkout)
#
# Install: cp examples/git-hooks/post-checkout .git/hooks/post-checkout && chmod +x .git/hooks/post-checkout
# Only run on branch checkouts
if [ "$3" != "1" ]; then
exit 0
fi
# Skip during rebase - git checks out commits during rebase and we must not
# modify working tree files (like deletions.jsonl) or the rebase will fail
if [ -d .git/rebase-merge ] || [ -d .git/rebase-apply ]; then
exit 0
fi
# This shim delegates to 'bd hooks run post-checkout' which contains
# the actual hook logic. This pattern ensures hook behavior is always
# in sync with the installed bd version - no manual updates needed.
# Check if bd is available
if ! command -v bd >/dev/null 2>&1; then
# Silently skip - post-checkout is called frequently
exit 0
fi
# Check if we're in a bd workspace
if [ ! -d .beads ]; then
exit 0
fi
# Detect git worktree: compare --git-dir and --git-common-dir
GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
GIT_COMMON_DIR=$(git rev-parse --git-common-dir 2>/dev/null)
if [ -n "$GIT_DIR" ] && [ -n "$GIT_COMMON_DIR" ]; then
GIT_DIR_ABS=$(cd "$GIT_DIR" 2>/dev/null && pwd || echo "$GIT_DIR")
GIT_COMMON_DIR_ABS=$(cd "$GIT_COMMON_DIR" 2>/dev/null && pwd || echo "$GIT_COMMON_DIR")
if [ "$GIT_DIR_ABS" != "$GIT_COMMON_DIR_ABS" ]; then
# We're in a git worktree
cat >&2 <<'EOF'
╔══════════════════════════════════════════════════════════════════════════╗
║ Welcome to beads in git worktree! ║
╠══════════════════════════════════════════════════════════════════════════╣
║ Note: Daemon mode is not recommended with git worktrees. ║
║ Worktrees share the same database, and the daemon may commit changes ║
║ to the wrong branch. ║
║ ║
║ RECOMMENDED: Disable daemon for this session: ║
║ export BEADS_NO_DAEMON=1 ║
║ ║
║ For more information: ║
║ - Run: bd doctor ║
║ - Read: docs/GIT_INTEGRATION.md (lines 10-53) ║
║ ║
║ Issue tracking: bd-dc9 ║
╚══════════════════════════════════════════════════════════════════════════╝
EOF
fi
fi
# Check if any JSONL file exists in .beads/
if ! ls .beads/*.jsonl >/dev/null 2>&1; then
exit 0
fi
# Run bd sync --import-only --no-git-history to import the updated JSONL
# --no-git-history prevents writes to deletions.jsonl (critical during rebase)
if ! output=$(bd sync --import-only --no-git-history 2>&1); then
echo "Warning: Failed to sync bd changes after checkout" >&2
echo "$output" >&2
echo "" >&2
echo "Run 'bd doctor --fix' to diagnose and repair" >&2
# Don't fail the checkout, just warn
fi
# Run quick health check (silent on success, hints if issues found)
# This catches version mismatches, outdated hooks, etc.
bd doctor --check-health 2>/dev/null || true
exit 0
exec bd hooks run post-checkout "$@"