Add bd hooks install command with embedded git hooks (bd-908z)

- Embed git hooks in binary using go:embed and templates directory
- Add bd hooks install/uninstall/list subcommands
- Install hooks with automatic backup of existing hooks
- Update AGENTS.md to reference 'bd hooks install' instead of install.sh
- Add comprehensive tests for hooks commands
- Update hook version to 0.22.1

Closes bd-908z
This commit is contained in:
Steve Yegge
2025-11-08 01:28:19 -08:00
parent a140436db8
commit 187fdd258a
11 changed files with 692 additions and 10 deletions

View File

@@ -0,0 +1,39 @@
#!/usr/bin/env bash
#
# Beads post-checkout hook
# Automatically imports JSONL to SQLite database after checking out branches
#
# Install: cp examples/git-hooks/post-checkout .git/hooks/post-checkout && chmod +x .git/hooks/post-checkout
# Arguments provided by git:
# $1 = ref of previous HEAD
# $2 = ref of new HEAD
# $3 = flag (1 if branch checkout, 0 if file checkout)
# Only run on branch checkouts
if [[ "$3" != "1" ]]; then
exit 0
fi
set -e
# Check if bd is installed
if ! command -v bd &> /dev/null; then
exit 0
fi
# Check if issues.jsonl exists
if [[ ! -f .beads/issues.jsonl ]]; then
exit 0
fi
# Import issues from JSONL
echo "🔗 Importing beads issues from JSONL..."
if bd import -i .beads/issues.jsonl 2>/dev/null; then
echo "✓ Beads issues imported successfully"
else
echo "Warning: bd import failed"
fi
exit 0

View File

@@ -0,0 +1,42 @@
#!/bin/sh
# bd-hooks-version: 0.22.1
#
# bd (beads) post-merge hook
#
# This hook syncs the bd database after a git pull or merge:
# 1. Checks if any .beads/*.jsonl file was updated
# 2. Runs 'bd sync --import-only' to import changes
#
# Installation:
# cp examples/git-hooks/post-merge .git/hooks/post-merge
# chmod +x .git/hooks/post-merge
#
# Or use the install script:
# examples/git-hooks/install.sh
# Check if bd is available
if ! command -v bd >/dev/null 2>&1; then
echo "Warning: bd command not found, skipping post-merge sync" >&2
exit 0
fi
# Check if we're in a bd workspace
if [ ! -d .beads ]; then
# Not a bd workspace, nothing to do
exit 0
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 to import the updated JSONL
# This is more robust than direct import as it handles all edge cases
if ! bd sync --import-only >/dev/null 2>&1; then
echo "Warning: Failed to sync bd changes after merge" >&2
echo "Run 'bd sync --import-only' manually" >&2
# Don't fail the merge, just warn
fi
exit 0

View File

@@ -0,0 +1,44 @@
#!/bin/sh
# bd-hooks-version: 0.22.1
#
# bd (beads) pre-commit hook
#
# This hook ensures that any pending bd issue changes are flushed to
# .beads/beads.jsonl before the commit is created, preventing the
# race condition where daemon auto-flush fires after the commit.
#
# Installation:
# cp examples/git-hooks/pre-commit .git/hooks/pre-commit
# chmod +x .git/hooks/pre-commit
#
# Or use the install script:
# examples/git-hooks/install.sh
# 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
if [ ! -d .beads ]; then
# Not a bd workspace, nothing to do
exit 0
fi
# Flush pending changes to JSONL
# Use --flush-only to skip git operations (we're already in a git hook)
# Suppress output unless there's an error
if ! bd sync --flush-only >/dev/null 2>&1; then
echo "Error: Failed to flush bd changes to JSONL" >&2
echo "Run 'bd sync --flush-only' manually to diagnose" >&2
exit 1
fi
# Stage both possible JSONL files (backward compatibility)
# git add is harmless if file doesn't exist
for f in .beads/beads.jsonl .beads/issues.jsonl; do
[ -f "$f" ] && git add "$f" 2>/dev/null || true
done
exit 0

62
cmd/bd/templates/hooks/pre-push Executable file
View File

@@ -0,0 +1,62 @@
#!/bin/sh
# bd-hooks-version: 0.22.1
#
# bd (beads) pre-push hook
#
# 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
#
# 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
# Check if we're in a bd workspace
if [ ! -d .beads ]; then
# Not a bd workspace, nothing to do
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 (supports both old and new names)
FILES=""
for f in .beads/beads.jsonl .beads/issues.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: Beads JSONL has uncommitted changes" >&2
echo "" >&2
echo "You made changes to bd issues between your last commit and this push." >&2
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
exit 0