fix(hooks): pre-commit no longer blocks when .beads removed (GH#483)

Two fixes to make pre-commit hook more resilient:

1. Check if beads is actually initialized (has db, config, or jsonl)
   - Empty .beads directory no longer triggers sync attempt
   - Handles case where .beads was removed from git but dir lingers

2. Make sync failure a warning instead of blocking error
   - Beads issues shouldn't prevent code commits
   - User can still run 'bd sync --flush-only' manually

Also synced examples/git-hooks/pre-commit with template (worktree handling).

Closes #483

🤖 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-16 01:07:48 -08:00
parent b7cbce4e21
commit 3c58e0aa25
4 changed files with 342 additions and 178 deletions

2
.beads/.gitignore vendored
View File

@@ -30,3 +30,5 @@ beads.right.meta.json
!issues.jsonl !issues.jsonl
!metadata.json !metadata.json
!config.json !config.json
deletions.jsonl
deletions.jsonl.migrated

File diff suppressed because one or more lines are too long

View File

@@ -23,12 +23,20 @@ if ! command -v bd >/dev/null 2>&1; then
exit 0 exit 0
fi fi
# Check if we're in a bd workspace # Check if we're in a bd workspace with an actual database
# Just having a .beads directory isn't enough - it must be properly initialized
if [ ! -d .beads ]; then if [ ! -d .beads ]; then
# Not a bd workspace, nothing to do # Not a bd workspace, nothing to do
exit 0 exit 0
fi fi
# Verify beads is actually initialized (has database or config)
# This handles the case where .beads was removed from git but directory lingers
if [ ! -f .beads/beads.db ] && [ ! -f .beads/config.yaml ] && [ ! -f .beads/issues.jsonl ]; then
# Directory exists but beads not initialized, nothing to do
exit 0
fi
# Check if sync-branch is configured in config.yaml or env var # 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 # If so, .beads changes go to a separate branch via worktree, not the current branch
SYNC_BRANCH="${BEADS_SYNC_BRANCH:-}" SYNC_BRANCH="${BEADS_SYNC_BRANCH:-}"
@@ -46,10 +54,11 @@ fi
# Flush pending changes to JSONL # Flush pending changes to JSONL
# Use --flush-only to skip git operations (we're already in a git hook) # Use --flush-only to skip git operations (we're already in a git hook)
# Suppress output unless there's an error # Suppress output unless there's an error
# Note: We don't block commits on flush failure - beads issues shouldn't prevent code commits
if ! bd sync --flush-only >/dev/null 2>&1; then if ! bd sync --flush-only >/dev/null 2>&1; then
echo "Error: Failed to flush bd changes to JSONL" >&2 echo "Warning: Failed to flush bd changes to JSONL" >&2
echo "Run 'bd sync --flush-only' manually to diagnose" >&2 echo "Run 'bd sync --flush-only' manually to diagnose" >&2
exit 1 # Continue with commit - don't block code changes due to beads issues
fi fi
# Stage all tracked JSONL files (issues.jsonl is canonical, beads.jsonl for backward compat, deletions.jsonl for deletion propagation) # Stage all tracked JSONL files (issues.jsonl is canonical, beads.jsonl for backward compat, deletions.jsonl for deletion propagation)

View File

@@ -1,5 +1,5 @@
#!/bin/sh #!/bin/sh
# bd-hooks-version: 0.22.2 # bd-hooks-version: 0.29.0
# #
# bd (beads) pre-commit hook # bd (beads) pre-commit hook
# #
@@ -23,12 +23,20 @@ if ! command -v bd >/dev/null 2>&1; then
exit 0 exit 0
fi fi
# Check if we're in a bd workspace # Check if we're in a bd workspace with an actual database
# Just having a .beads directory isn't enough - it must be properly initialized
if [ ! -d .beads ]; then if [ ! -d .beads ]; then
# Not a bd workspace, nothing to do # Not a bd workspace, nothing to do
exit 0 exit 0
fi fi
# Verify beads is actually initialized (has database or config)
# This handles the case where .beads was removed from git but directory lingers
if [ ! -f .beads/beads.db ] && [ ! -f .beads/config.yaml ] && [ ! -f .beads/issues.jsonl ]; then
# Directory exists but beads not initialized, nothing to do
exit 0
fi
# Check if sync-branch is configured in config.yaml or env var # 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 # If so, .beads changes go to a separate branch via worktree, not the current branch
SYNC_BRANCH="${BEADS_SYNC_BRANCH:-}" SYNC_BRANCH="${BEADS_SYNC_BRANCH:-}"
@@ -46,16 +54,27 @@ fi
# Flush pending changes to JSONL # Flush pending changes to JSONL
# Use --flush-only to skip git operations (we're already in a git hook) # Use --flush-only to skip git operations (we're already in a git hook)
# Suppress output unless there's an error # Suppress output unless there's an error
# Note: We don't block commits on flush failure - beads issues shouldn't prevent code commits
if ! bd sync --flush-only >/dev/null 2>&1; then if ! bd sync --flush-only >/dev/null 2>&1; then
echo "Error: Failed to flush bd changes to JSONL" >&2 echo "Warning: Failed to flush bd changes to JSONL" >&2
echo "Run 'bd sync --flush-only' manually to diagnose" >&2 echo "Run 'bd sync --flush-only' manually to diagnose" >&2
exit 1 # Continue with commit - don't block code changes due to beads issues
fi fi
# Stage all tracked JSONL files (beads.jsonl, issues.jsonl for backward compat, deletions.jsonl for deletion propagation) # Stage all tracked JSONL files (issues.jsonl is canonical, beads.jsonl for backward compat, deletions.jsonl for deletion propagation)
# git add is harmless if file doesn't exist # For worktrees, .beads is in the main repo's working tree, not the worktree,
for f in .beads/beads.jsonl .beads/issues.jsonl .beads/deletions.jsonl; do # so we can't use git add. Skip staging for worktrees.
[ -f "$f" ] && git add "$f" 2>/dev/null || true if [ "$(git rev-parse --git-dir)" = "$(git rev-parse --git-common-dir)" ]; then
done # Regular repo: files are in the working tree, safe to add
# git add is harmless if file doesn't exist
for f in .beads/beads.jsonl .beads/issues.jsonl .beads/deletions.jsonl; do
[ -f "$f" ] && git add "$f" 2>/dev/null || true
done
else
# Worktree: .beads is in the main repo's working tree, not this worktree
# Git rejects adding files outside the worktree, so we skip it.
# The main repo will see the changes on the next pull/sync.
: # do nothing
fi
exit 0 exit 0