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:
@@ -1,130 +1,18 @@
|
||||
#!/bin/sh
|
||||
# bd-hooks-version: 0.30.7
|
||||
# bd-shim v1
|
||||
#
|
||||
# bd (beads) pre-push hook
|
||||
# bd (beads) pre-push hook - thin shim
|
||||
#
|
||||
# This hook prevents pushing stale JSONL by:
|
||||
# 1. Flushing any pending in-memory changes to JSONL (if bd available)
|
||||
# 2. Checking for uncommitted changes (staged, unstaged, untracked, deleted)
|
||||
# 3. Failing the push with clear instructions if changes found
|
||||
#
|
||||
# When sync-branch is configured in config.yaml, .beads changes are committed
|
||||
# to a separate branch via worktree, so the uncommitted check is skipped.
|
||||
#
|
||||
# The pre-commit hook already exports changes, but this catches:
|
||||
# - Changes made between commit and push
|
||||
# - Pending debounced flushes (5s daemon delay)
|
||||
#
|
||||
# Installation:
|
||||
# cp examples/git-hooks/pre-push .git/hooks/pre-push
|
||||
# chmod +x .git/hooks/pre-push
|
||||
#
|
||||
# Or use the install script:
|
||||
# examples/git-hooks/install.sh
|
||||
# This shim delegates to 'bd hooks run pre-push' 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 we're in a bd workspace
|
||||
if [ ! -d .beads ]; then
|
||||
# Not a bd workspace, nothing to do
|
||||
# Check if bd is available
|
||||
if ! command -v bd >/dev/null 2>&1; then
|
||||
echo "Warning: bd command not found in PATH, skipping pre-push hook" >&2
|
||||
echo " Install bd: brew install steveyegge/tap/bd" >&2
|
||||
echo " Or add bd to your PATH" >&2
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Skip if bd sync is already in progress (GH#532: prevents circular error)
|
||||
# bd sync sets this env var when pushing from worktree
|
||||
if [ -n "$BD_SYNC_IN_PROGRESS" ]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Check if sync-branch is configured in config.yaml or env var
|
||||
# If so, .beads changes go to a separate branch via worktree, not the current branch
|
||||
SYNC_BRANCH="${BEADS_SYNC_BRANCH:-}"
|
||||
if [ -z "$SYNC_BRANCH" ] && [ -f .beads/config.yaml ]; then
|
||||
# Extract sync-branch value from YAML (handles quoted and unquoted values)
|
||||
# Use head -1 to only take first match if file is malformed
|
||||
SYNC_BRANCH=$(grep -E '^sync-branch:' .beads/config.yaml 2>/dev/null | head -1 | sed 's/^sync-branch:[[:space:]]*//' | sed 's/^["'"'"']//' | sed 's/["'"'"']$//')
|
||||
fi
|
||||
if [ -n "$SYNC_BRANCH" ]; then
|
||||
# sync-branch is configured, skip .beads uncommitted check
|
||||
# Changes are synced to the separate branch, not this one
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Optionally flush pending bd changes so they surface in JSONL
|
||||
# This prevents the race where a debounced flush lands after the check
|
||||
if command -v bd >/dev/null 2>&1; then
|
||||
bd sync --flush-only >/dev/null 2>&1 || true
|
||||
fi
|
||||
|
||||
# Collect all tracked or existing JSONL files (beads.jsonl, issues.jsonl for backward compat, deletions.jsonl for deletion propagation)
|
||||
FILES=""
|
||||
for f in .beads/beads.jsonl .beads/issues.jsonl .beads/deletions.jsonl; do
|
||||
# Include file if it exists in working tree OR is tracked by git (even if deleted)
|
||||
if git ls-files --error-unmatch "$f" >/dev/null 2>&1 || [ -f "$f" ]; then
|
||||
FILES="$FILES $f"
|
||||
fi
|
||||
done
|
||||
|
||||
# Check for any uncommitted changes using porcelain status
|
||||
# This catches: staged, unstaged, untracked, deleted, renamed, and conflicts
|
||||
if [ -n "$FILES" ]; then
|
||||
# shellcheck disable=SC2086
|
||||
if [ -n "$(git status --porcelain -- $FILES 2>/dev/null)" ]; then
|
||||
echo "❌ Error: Uncommitted changes detected" >&2
|
||||
echo "" >&2
|
||||
echo "Before pushing, ensure all changes are committed. This includes:" >&2
|
||||
echo " • bd JSONL updates (run 'bd sync')" >&2
|
||||
echo " • any other modified files (run 'git status' to review)" >&2
|
||||
echo "" >&2
|
||||
|
||||
# Check if bd is available and offer auto-sync
|
||||
if command -v bd >/dev/null 2>&1; then
|
||||
# Check if we're in an interactive terminal
|
||||
if [ -t 0 ]; then
|
||||
echo "Would you like to run 'bd sync' now to commit and push these changes? [y/N]" >&2
|
||||
read -r response
|
||||
case "$response" in
|
||||
[yY][eE][sS]|[yY])
|
||||
echo "" >&2
|
||||
echo "Running: bd sync" >&2
|
||||
if bd sync; then
|
||||
echo "" >&2
|
||||
echo "✓ Sync complete. Continuing with push..." >&2
|
||||
exit 0
|
||||
else
|
||||
echo "" >&2
|
||||
echo "❌ Sync failed. Push aborted." >&2
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
*)
|
||||
echo "" >&2
|
||||
echo "Push aborted. Run 'bd sync' manually when ready:" >&2
|
||||
echo "" >&2
|
||||
echo " bd sync" >&2
|
||||
echo " git push" >&2
|
||||
echo "" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
else
|
||||
# Non-interactive: just show the message
|
||||
echo "Run 'bd sync' to commit these changes:" >&2
|
||||
echo "" >&2
|
||||
echo " bd sync" >&2
|
||||
echo "" >&2
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
# bd not available, fall back to manual git commands
|
||||
echo "Please commit the updated JSONL before pushing:" >&2
|
||||
echo "" >&2
|
||||
# shellcheck disable=SC2086
|
||||
echo " git add $FILES" >&2
|
||||
echo ' git commit -m "Update bd JSONL"' >&2
|
||||
echo " git push" >&2
|
||||
echo "" >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
exit 0
|
||||
exec bd hooks run pre-push "$@"
|
||||
|
||||
Reference in New Issue
Block a user