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:
251
cmd/bd/hooks.go
251
cmd/bd/hooks.go
@@ -2,12 +2,34 @@ package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"embed"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
//go:embed templates/hooks/*
|
||||
var hooksFS embed.FS
|
||||
|
||||
func getEmbeddedHooks() (map[string]string, error) {
|
||||
hooks := make(map[string]string)
|
||||
hookNames := []string{"pre-commit", "post-merge", "pre-push", "post-checkout"}
|
||||
|
||||
for _, name := range hookNames {
|
||||
content, err := hooksFS.ReadFile("templates/hooks/" + name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read embedded hook %s: %w", name, err)
|
||||
}
|
||||
hooks[name] = string(content)
|
||||
}
|
||||
|
||||
return hooks, nil
|
||||
}
|
||||
|
||||
const hookVersionPrefix = "# bd-hooks-version: "
|
||||
|
||||
// HookStatus represents the status of a single git hook
|
||||
@@ -91,12 +113,12 @@ func FormatHookWarnings(statuses []HookStatus) string {
|
||||
|
||||
if missingCount > 0 {
|
||||
warnings = append(warnings, fmt.Sprintf("⚠️ Git hooks not installed (%d missing)", missingCount))
|
||||
warnings = append(warnings, " Run: examples/git-hooks/install.sh")
|
||||
warnings = append(warnings, " Run: bd hooks install")
|
||||
}
|
||||
|
||||
if outdatedCount > 0 {
|
||||
warnings = append(warnings, fmt.Sprintf("⚠️ Git hooks are outdated (%d hooks)", outdatedCount))
|
||||
warnings = append(warnings, " Run: examples/git-hooks/install.sh")
|
||||
warnings = append(warnings, " Run: bd hooks install")
|
||||
}
|
||||
|
||||
if len(warnings) > 0 {
|
||||
@@ -105,3 +127,228 @@ func FormatHookWarnings(statuses []HookStatus) string {
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// Cobra commands
|
||||
|
||||
var hooksCmd = &cobra.Command{
|
||||
Use: "hooks",
|
||||
Short: "Manage git hooks for bd auto-sync",
|
||||
Long: `Install, uninstall, or list git hooks that provide automatic bd sync.
|
||||
|
||||
The hooks ensure that:
|
||||
- pre-commit: Flushes pending changes to JSONL before commit
|
||||
- post-merge: Imports updated JSONL after pull/merge
|
||||
- pre-push: Prevents pushing stale JSONL
|
||||
- post-checkout: Imports JSONL after branch checkout`,
|
||||
}
|
||||
|
||||
var hooksInstallCmd = &cobra.Command{
|
||||
Use: "install",
|
||||
Short: "Install bd git hooks",
|
||||
Long: `Install git hooks for automatic bd sync.
|
||||
|
||||
Hooks are installed to .git/hooks/ in the current repository.
|
||||
Existing hooks are backed up with a .backup suffix.
|
||||
|
||||
Installed hooks:
|
||||
- pre-commit: Flush changes to JSONL before commit
|
||||
- post-merge: Import JSONL after pull/merge
|
||||
- pre-push: Prevent pushing stale JSONL
|
||||
- post-checkout: Import JSONL after branch checkout`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
force, _ := cmd.Flags().GetBool("force")
|
||||
|
||||
embeddedHooks, err := getEmbeddedHooks()
|
||||
if err != nil {
|
||||
if jsonOutput {
|
||||
output := map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
}
|
||||
jsonBytes, _ := json.MarshalIndent(output, "", " ")
|
||||
fmt.Println(string(jsonBytes))
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "Error loading hooks: %v\n", err)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if err := installHooks(embeddedHooks, force); err != nil {
|
||||
if jsonOutput {
|
||||
output := map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
}
|
||||
jsonBytes, _ := json.MarshalIndent(output, "", " ")
|
||||
fmt.Println(string(jsonBytes))
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "Error installing hooks: %v\n", err)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if jsonOutput {
|
||||
output := map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "Git hooks installed successfully",
|
||||
}
|
||||
jsonBytes, _ := json.MarshalIndent(output, "", " ")
|
||||
fmt.Println(string(jsonBytes))
|
||||
} else {
|
||||
fmt.Println("✓ Git hooks installed successfully")
|
||||
fmt.Println()
|
||||
fmt.Println("Installed hooks:")
|
||||
for hookName := range embeddedHooks {
|
||||
fmt.Printf(" - %s\n", hookName)
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
var hooksUninstallCmd = &cobra.Command{
|
||||
Use: "uninstall",
|
||||
Short: "Uninstall bd git hooks",
|
||||
Long: `Remove bd git hooks from .git/hooks/ directory.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if err := uninstallHooks(); err != nil {
|
||||
if jsonOutput {
|
||||
output := map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
}
|
||||
jsonBytes, _ := json.MarshalIndent(output, "", " ")
|
||||
fmt.Println(string(jsonBytes))
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "Error uninstalling hooks: %v\n", err)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if jsonOutput {
|
||||
output := map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "Git hooks uninstalled successfully",
|
||||
}
|
||||
jsonBytes, _ := json.MarshalIndent(output, "", " ")
|
||||
fmt.Println(string(jsonBytes))
|
||||
} else {
|
||||
fmt.Println("✓ Git hooks uninstalled successfully")
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
var hooksListCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List installed git hooks status",
|
||||
Long: `Show the status of bd git hooks (installed, outdated, missing).`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
statuses, err := CheckGitHooks()
|
||||
if err != nil {
|
||||
if jsonOutput {
|
||||
output := map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
}
|
||||
jsonBytes, _ := json.MarshalIndent(output, "", " ")
|
||||
fmt.Println(string(jsonBytes))
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "Error checking hooks: %v\n", err)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if jsonOutput {
|
||||
output := map[string]interface{}{
|
||||
"hooks": statuses,
|
||||
}
|
||||
jsonBytes, _ := json.MarshalIndent(output, "", " ")
|
||||
fmt.Println(string(jsonBytes))
|
||||
} else {
|
||||
fmt.Println("Git hooks status:")
|
||||
for _, status := range statuses {
|
||||
if !status.Installed {
|
||||
fmt.Printf(" ✗ %s: not installed\n", status.Name)
|
||||
} else if status.Outdated {
|
||||
fmt.Printf(" ⚠ %s: installed (version %s, current: %s) - outdated\n",
|
||||
status.Name, status.Version, Version)
|
||||
} else {
|
||||
fmt.Printf(" ✓ %s: installed (version %s)\n", status.Name, status.Version)
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func installHooks(embeddedHooks map[string]string, force bool) error {
|
||||
// Check if .git directory exists
|
||||
gitDir := ".git"
|
||||
if _, err := os.Stat(gitDir); os.IsNotExist(err) {
|
||||
return fmt.Errorf("not a git repository (no .git directory found)")
|
||||
}
|
||||
|
||||
hooksDir := filepath.Join(gitDir, "hooks")
|
||||
|
||||
// Create hooks directory if it doesn't exist
|
||||
if err := os.MkdirAll(hooksDir, 0755); err != nil {
|
||||
return fmt.Errorf("failed to create hooks directory: %w", err)
|
||||
}
|
||||
|
||||
// Install each hook
|
||||
for hookName, hookContent := range embeddedHooks {
|
||||
hookPath := filepath.Join(hooksDir, hookName)
|
||||
|
||||
// Check if hook already exists
|
||||
if _, err := os.Stat(hookPath); err == nil {
|
||||
// Hook exists - back it up unless force is set
|
||||
if !force {
|
||||
backupPath := hookPath + ".backup"
|
||||
if err := os.Rename(hookPath, backupPath); err != nil {
|
||||
return fmt.Errorf("failed to backup %s: %w", hookName, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Write hook file
|
||||
if err := os.WriteFile(hookPath, []byte(hookContent), 0755); err != nil {
|
||||
return fmt.Errorf("failed to write %s: %w", hookName, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func uninstallHooks() error {
|
||||
hooksDir := filepath.Join(".git", "hooks")
|
||||
hookNames := []string{"pre-commit", "post-merge", "pre-push", "post-checkout"}
|
||||
|
||||
for _, hookName := range hookNames {
|
||||
hookPath := filepath.Join(hooksDir, hookName)
|
||||
|
||||
// Check if hook exists
|
||||
if _, err := os.Stat(hookPath); os.IsNotExist(err) {
|
||||
continue
|
||||
}
|
||||
|
||||
// Remove hook
|
||||
if err := os.Remove(hookPath); err != nil {
|
||||
return fmt.Errorf("failed to remove %s: %w", hookName, err)
|
||||
}
|
||||
|
||||
// Restore backup if exists
|
||||
backupPath := hookPath + ".backup"
|
||||
if _, err := os.Stat(backupPath); err == nil {
|
||||
if err := os.Rename(backupPath, hookPath); err != nil {
|
||||
// Non-fatal - just warn
|
||||
fmt.Fprintf(os.Stderr, "Warning: failed to restore backup for %s: %v\n", hookName, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
hooksInstallCmd.Flags().Bool("force", false, "Overwrite existing hooks without backup")
|
||||
|
||||
hooksCmd.AddCommand(hooksInstallCmd)
|
||||
hooksCmd.AddCommand(hooksUninstallCmd)
|
||||
hooksCmd.AddCommand(hooksListCmd)
|
||||
|
||||
rootCmd.AddCommand(hooksCmd)
|
||||
}
|
||||
|
||||
246
cmd/bd/hooks_test.go
Normal file
246
cmd/bd/hooks_test.go
Normal file
@@ -0,0 +1,246 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetEmbeddedHooks(t *testing.T) {
|
||||
hooks, err := getEmbeddedHooks()
|
||||
if err != nil {
|
||||
t.Fatalf("getEmbeddedHooks() failed: %v", err)
|
||||
}
|
||||
|
||||
expectedHooks := []string{"pre-commit", "post-merge", "pre-push", "post-checkout"}
|
||||
for _, hookName := range expectedHooks {
|
||||
content, ok := hooks[hookName]
|
||||
if !ok {
|
||||
t.Errorf("Missing hook: %s", hookName)
|
||||
continue
|
||||
}
|
||||
if len(content) == 0 {
|
||||
t.Errorf("Hook %s has empty content", hookName)
|
||||
}
|
||||
// Verify it's a shell script
|
||||
if content[:2] != "#!" {
|
||||
t.Errorf("Hook %s doesn't start with shebang: %s", hookName, content[:50])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestInstallHooks(t *testing.T) {
|
||||
// Create temp directory with fake .git
|
||||
tmpDir := t.TempDir()
|
||||
gitDir := filepath.Join(tmpDir, ".git", "hooks")
|
||||
if err := os.MkdirAll(gitDir, 0755); err != nil {
|
||||
t.Fatalf("Failed to create test git dir: %v", err)
|
||||
}
|
||||
|
||||
// Change to temp directory
|
||||
oldWd, _ := os.Getwd()
|
||||
defer os.Chdir(oldWd)
|
||||
os.Chdir(tmpDir)
|
||||
|
||||
// Get embedded hooks
|
||||
hooks, err := getEmbeddedHooks()
|
||||
if err != nil {
|
||||
t.Fatalf("getEmbeddedHooks() failed: %v", err)
|
||||
}
|
||||
|
||||
// Install hooks
|
||||
if err := installHooks(hooks, false); err != nil {
|
||||
t.Fatalf("installHooks() failed: %v", err)
|
||||
}
|
||||
|
||||
// Verify hooks were installed
|
||||
for hookName := range hooks {
|
||||
hookPath := filepath.Join(gitDir, hookName)
|
||||
if _, err := os.Stat(hookPath); os.IsNotExist(err) {
|
||||
t.Errorf("Hook %s was not installed", hookName)
|
||||
}
|
||||
// Check it's executable
|
||||
info, err := os.Stat(hookPath)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to stat %s: %v", hookName, err)
|
||||
continue
|
||||
}
|
||||
if info.Mode()&0111 == 0 {
|
||||
t.Errorf("Hook %s is not executable", hookName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestInstallHooksBackup(t *testing.T) {
|
||||
// Create temp directory with fake .git
|
||||
tmpDir := t.TempDir()
|
||||
gitDir := filepath.Join(tmpDir, ".git", "hooks")
|
||||
if err := os.MkdirAll(gitDir, 0755); err != nil {
|
||||
t.Fatalf("Failed to create test git dir: %v", err)
|
||||
}
|
||||
|
||||
// Change to temp directory
|
||||
oldWd, _ := os.Getwd()
|
||||
defer os.Chdir(oldWd)
|
||||
os.Chdir(tmpDir)
|
||||
|
||||
// Create an existing hook
|
||||
existingHook := filepath.Join(gitDir, "pre-commit")
|
||||
existingContent := "#!/bin/sh\necho old hook\n"
|
||||
if err := os.WriteFile(existingHook, []byte(existingContent), 0755); err != nil {
|
||||
t.Fatalf("Failed to create existing hook: %v", err)
|
||||
}
|
||||
|
||||
// Get embedded hooks
|
||||
hooks, err := getEmbeddedHooks()
|
||||
if err != nil {
|
||||
t.Fatalf("getEmbeddedHooks() failed: %v", err)
|
||||
}
|
||||
|
||||
// Install hooks (should backup existing)
|
||||
if err := installHooks(hooks, false); err != nil {
|
||||
t.Fatalf("installHooks() failed: %v", err)
|
||||
}
|
||||
|
||||
// Verify backup was created
|
||||
backupPath := existingHook + ".backup"
|
||||
if _, err := os.Stat(backupPath); os.IsNotExist(err) {
|
||||
t.Errorf("Backup was not created")
|
||||
}
|
||||
|
||||
// Verify backup has original content
|
||||
backupContent, err := os.ReadFile(backupPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read backup: %v", err)
|
||||
}
|
||||
if string(backupContent) != existingContent {
|
||||
t.Errorf("Backup content mismatch: got %q, want %q", string(backupContent), existingContent)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInstallHooksForce(t *testing.T) {
|
||||
// Create temp directory with fake .git
|
||||
tmpDir := t.TempDir()
|
||||
gitDir := filepath.Join(tmpDir, ".git", "hooks")
|
||||
if err := os.MkdirAll(gitDir, 0755); err != nil {
|
||||
t.Fatalf("Failed to create test git dir: %v", err)
|
||||
}
|
||||
|
||||
// Change to temp directory
|
||||
oldWd, _ := os.Getwd()
|
||||
defer os.Chdir(oldWd)
|
||||
os.Chdir(tmpDir)
|
||||
|
||||
// Create an existing hook
|
||||
existingHook := filepath.Join(gitDir, "pre-commit")
|
||||
if err := os.WriteFile(existingHook, []byte("old"), 0755); err != nil {
|
||||
t.Fatalf("Failed to create existing hook: %v", err)
|
||||
}
|
||||
|
||||
// Get embedded hooks
|
||||
hooks, err := getEmbeddedHooks()
|
||||
if err != nil {
|
||||
t.Fatalf("getEmbeddedHooks() failed: %v", err)
|
||||
}
|
||||
|
||||
// Install hooks with force (should not create backup)
|
||||
if err := installHooks(hooks, true); err != nil {
|
||||
t.Fatalf("installHooks() failed: %v", err)
|
||||
}
|
||||
|
||||
// Verify no backup was created
|
||||
backupPath := existingHook + ".backup"
|
||||
if _, err := os.Stat(backupPath); !os.IsNotExist(err) {
|
||||
t.Errorf("Backup should not have been created with --force")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUninstallHooks(t *testing.T) {
|
||||
// Create temp directory with fake .git
|
||||
tmpDir := t.TempDir()
|
||||
gitDir := filepath.Join(tmpDir, ".git", "hooks")
|
||||
if err := os.MkdirAll(gitDir, 0755); err != nil {
|
||||
t.Fatalf("Failed to create test git dir: %v", err)
|
||||
}
|
||||
|
||||
// Change to temp directory
|
||||
oldWd, _ := os.Getwd()
|
||||
defer os.Chdir(oldWd)
|
||||
os.Chdir(tmpDir)
|
||||
|
||||
// Get embedded hooks and install them
|
||||
hooks, err := getEmbeddedHooks()
|
||||
if err != nil {
|
||||
t.Fatalf("getEmbeddedHooks() failed: %v", err)
|
||||
}
|
||||
if err := installHooks(hooks, false); err != nil {
|
||||
t.Fatalf("installHooks() failed: %v", err)
|
||||
}
|
||||
|
||||
// Uninstall hooks
|
||||
if err := uninstallHooks(); err != nil {
|
||||
t.Fatalf("uninstallHooks() failed: %v", err)
|
||||
}
|
||||
|
||||
// Verify hooks were removed
|
||||
hookNames := []string{"pre-commit", "post-merge", "pre-push", "post-checkout"}
|
||||
for _, hookName := range hookNames {
|
||||
hookPath := filepath.Join(gitDir, hookName)
|
||||
if _, err := os.Stat(hookPath); !os.IsNotExist(err) {
|
||||
t.Errorf("Hook %s was not removed", hookName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHooksCheckGitHooks(t *testing.T) {
|
||||
// Create temp directory with fake .git
|
||||
tmpDir := t.TempDir()
|
||||
gitDir := filepath.Join(tmpDir, ".git", "hooks")
|
||||
if err := os.MkdirAll(gitDir, 0755); err != nil {
|
||||
t.Fatalf("Failed to create test git dir: %v", err)
|
||||
}
|
||||
|
||||
// Change to temp directory
|
||||
oldWd, _ := os.Getwd()
|
||||
defer os.Chdir(oldWd)
|
||||
os.Chdir(tmpDir)
|
||||
|
||||
// Initially no hooks installed
|
||||
statuses, err := CheckGitHooks()
|
||||
if err != nil {
|
||||
t.Fatalf("CheckGitHooks() failed: %v", err)
|
||||
}
|
||||
|
||||
for _, status := range statuses {
|
||||
if status.Installed {
|
||||
t.Errorf("Hook %s should not be installed initially", status.Name)
|
||||
}
|
||||
}
|
||||
|
||||
// Install hooks
|
||||
hooks, err := getEmbeddedHooks()
|
||||
if err != nil {
|
||||
t.Fatalf("getEmbeddedHooks() failed: %v", err)
|
||||
}
|
||||
if err := installHooks(hooks, false); err != nil {
|
||||
t.Fatalf("installHooks() failed: %v", err)
|
||||
}
|
||||
|
||||
// Check again
|
||||
statuses, err = CheckGitHooks()
|
||||
if err != nil {
|
||||
t.Fatalf("CheckGitHooks() failed: %v", err)
|
||||
}
|
||||
|
||||
for _, status := range statuses {
|
||||
if !status.Installed {
|
||||
t.Errorf("Hook %s should be installed", status.Name)
|
||||
}
|
||||
if status.Version != Version {
|
||||
t.Errorf("Hook %s version mismatch: got %s, want %s", status.Name, status.Version, Version)
|
||||
}
|
||||
if status.Outdated {
|
||||
t.Errorf("Hook %s should not be outdated", status.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
39
cmd/bd/templates/hooks/post-checkout
Executable file
39
cmd/bd/templates/hooks/post-checkout
Executable 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
|
||||
42
cmd/bd/templates/hooks/post-merge
Executable file
42
cmd/bd/templates/hooks/post-merge
Executable 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
|
||||
44
cmd/bd/templates/hooks/pre-commit
Executable file
44
cmd/bd/templates/hooks/pre-commit
Executable 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
62
cmd/bd/templates/hooks/pre-push
Executable 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
|
||||
Reference in New Issue
Block a user