Fix bd-my64: Pre-push hook blocks instead of exports

The original pre-push hook tried to export DB → JSONL during the push,
then run 'git add', but this doesn't work because:

1. The commit is already created when pre-push runs
2. git add in pre-push stages files for a FUTURE commit
3. The current push sends the old commit with stale JSONL
4. Result: dirty git status after push

Fix:
- Pre-push now CHECKS for uncommitted JSONL changes
- If found, it FAILS the push with clear instructions
- User must commit JSONL before pushing
- This prevents stale JSONL from reaching remote

The pre-commit hook already properly flushes changes, so this
catch-all prevents changes made BETWEEN commit and push.

Amp-Thread-ID: https://ampcode.com/threads/T-39a89553-c301-4d4f-b39f-6df9c403d22b
Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
Steve Yegge
2025-11-06 18:57:34 -08:00
parent 2cb97e4d22
commit 3ba245e6c0
5 changed files with 76 additions and 40 deletions

View File

@@ -1,21 +1,29 @@
# bd Git Hooks
This directory contains git hooks that integrate bd (beads) with your git workflow, solving the race condition between daemon auto-flush and git commits.
This directory contains git hooks that integrate bd (beads) with your git workflow, preventing stale JSONL from being pushed to remote.
## The Problem
When using bd in daemon mode, operations trigger a 5-second debounced auto-flush to JSONL. This creates a race condition:
Two race conditions can occur:
1. User closes issue via MCP → daemon schedules flush (5 sec delay)
2. User commits code changes → JSONL appears clean
3. Daemon flush fires → JSONL modified after commit
4. Result: dirty working tree showing JSONL changes
1. **Between operations and commits**: Daemon auto-flush (5s debounce) may fire after commit
- User closes issue via MCP → daemon schedules flush (5 sec delay)
- User commits code changes → JSONL appears clean
- Daemon flush fires → JSONL modified after commit
- Result: dirty working tree showing JSONL changes
2. **Between commits and pushes**: Changes made after commit but before push (bd-my64)
- User commits → pre-commit hook flushes JSONL
- User adds comments or updates issues
- User pushes → outdated JSONL is pushed
- Result: remote has stale JSONL
## The Solution
These git hooks ensure bd changes are always synchronized with your commits:
These git hooks ensure bd changes are always synchronized with your commits and pushes:
- **pre-commit** - Flushes pending bd changes to JSONL before commit
- **pre-commit** - Flushes pending bd changes to JSONL before commit and stages it
- **pre-push** - Blocks push if JSONL has uncommitted changes (bd-my64)
- **post-merge** - Imports updated JSONL after git pull/merge
## Installation
@@ -37,8 +45,9 @@ This will:
```bash
cp examples/git-hooks/pre-commit .git/hooks/pre-commit
cp examples/git-hooks/pre-push .git/hooks/pre-push
cp examples/git-hooks/post-merge .git/hooks/post-merge
chmod +x .git/hooks/pre-commit .git/hooks/post-merge
chmod +x .git/hooks/pre-commit .git/hooks/pre-push .git/hooks/post-merge
```
## How It Works
@@ -58,16 +67,31 @@ This:
The hook is silent on success, fast (no git operations), and safe (fails commit if flush fails).
### pre-push
Before each push, the hook checks:
```bash
git diff --quiet .beads/beads.jsonl
```
This prevents pushing stale JSONL by:
1. Checking if JSONL has uncommitted changes (working tree or staging)
2. Failing the push with clear error message if changes exist
3. Instructing user to commit JSONL before pushing again
This solves bd-my64: changes made between commit and push are caught before reaching remote.
### post-merge
After a git pull or merge, the hook runs:
```bash
bd import -i .beads/issues.jsonl
bd import -i .beads/beads.jsonl
```
This ensures your local database reflects the merged state. The hook:
- Only runs if `.beads/issues.jsonl` exists
- Only runs if `.beads/beads.jsonl` exists (also checks `issues.jsonl` for backward compat)
- Imports any new issues or updates from the merge
- Warns on failure but doesn't block the merge
@@ -92,7 +116,7 @@ This ensures your local database reflects the merged state. The hook:
Remove the hooks:
```bash
rm .git/hooks/pre-commit .git/hooks/post-merge
rm .git/hooks/pre-commit .git/hooks/pre-push .git/hooks/post-merge
```
Your backed-up hooks (if any) are in `.git/hooks/*.backup-*`.

View File

@@ -59,7 +59,7 @@ echo "✓ Git hooks installed successfully"
echo ""
echo "Hooks installed:"
echo " pre-commit - Flushes pending bd changes to JSONL before commit"
echo " pre-push - Blocks push if JSONL has uncommitted changes (bd-my64)"
echo " post-merge - Imports updated JSONL after git pull/merge"
echo " pre-push - Exports database to JSONL before push (prevents stale JSONL)"
echo ""
echo "To uninstall, remove the hooks from .git/hooks/"

View File

@@ -4,7 +4,7 @@
# bd (beads) pre-commit hook
#
# This hook ensures that any pending bd issue changes are flushed to
# .beads/issues.jsonl before the commit is created, preventing the
# .beads/beads.jsonl before the commit is created, preventing the
# race condition where daemon auto-flush fires after the commit.
#
# Installation:
@@ -35,9 +35,17 @@ if ! bd sync --flush-only >/dev/null 2>&1; then
exit 1
fi
# Find the JSONL file (could be issues.jsonl or beads.jsonl)
JSONL_FILE=""
if [ -f .beads/beads.jsonl ]; then
JSONL_FILE=".beads/beads.jsonl"
elif [ -f .beads/issues.jsonl ]; then
JSONL_FILE=".beads/issues.jsonl"
fi
# If the JSONL file was modified, stage it
if [ -f .beads/issues.jsonl ]; then
git add .beads/issues.jsonl 2>/dev/null || true
if [ -n "$JSONL_FILE" ] && [ -f "$JSONL_FILE" ]; then
git add "$JSONL_FILE" 2>/dev/null || true
fi
exit 0

View File

@@ -3,9 +3,11 @@
#
# bd (beads) pre-push hook
#
# This hook ensures that the database is exported to JSONL before pushing,
# preventing the problem where database changes are committed without
# corresponding JSONL updates.
# This hook checks if the database has uncommitted changes and prevents
# pushing stale JSONL by failing with a clear error message.
#
# The pre-commit hook should have already exported changes, but if new
# changes were made between commit and push, this hook catches that.
#
# Installation:
# cp examples/git-hooks/pre-push .git/hooks/pre-push
@@ -16,7 +18,7 @@
# Check if bd is available
if ! command -v bd >/dev/null 2>&1; then
echo "Warning: bd command not found, skipping pre-push export check" >&2
# If bd is not available, we can't check - allow push
exit 0
fi
@@ -26,26 +28,28 @@ if [ ! -d .beads ]; then
exit 0
fi
# Check if database is newer than JSONL
DB_FILE=".beads/beads.db"
JSONL_FILE=".beads/beads.jsonl"
# Find the JSONL file (could be issues.jsonl or beads.jsonl)
JSONL_FILE=""
if [ -f .beads/beads.jsonl ]; then
JSONL_FILE=".beads/beads.jsonl"
elif [ -f .beads/issues.jsonl ]; then
JSONL_FILE=".beads/issues.jsonl"
fi
if [ -f "$DB_FILE" ] && [ -f "$JSONL_FILE" ]; then
# Get modification times
if [ "$DB_FILE" -nt "$JSONL_FILE" ]; then
echo "⚠️ Database is newer than JSONL - exporting before push..." >&2
# Force export to ensure JSONL is up to date
if ! BEADS_NO_DAEMON=1 bd export --output "$JSONL_FILE" >/dev/null 2>&1; then
echo "Error: Failed to export database to JSONL" >&2
echo "Run 'bd export' manually to diagnose" >&2
exit 1
fi
# Stage the updated JSONL
git add "$JSONL_FILE" 2>/dev/null || true
echo "✓ Exported database to JSONL" >&2
# Check if JSONL file has uncommitted changes
if [ -n "$JSONL_FILE" ] && [ -f "$JSONL_FILE" ]; then
# Check git status for the JSONL file
if ! git diff --quiet "$JSONL_FILE" 2>/dev/null || ! git diff --cached --quiet "$JSONL_FILE" 2>/dev/null; then
echo "❌ Error: $JSONL_FILE 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 file before pushing:" >&2
echo "" >&2
echo " git add $JSONL_FILE" >&2
echo " git commit -m \"Update beads JSONL\"" >&2
echo " git push" >&2
echo "" >&2
exit 1
fi
fi