Fix CI regressions and stabilize tests

This commit is contained in:
Codex Agent
2025-11-17 10:06:35 -07:00
parent 42233073bc
commit 7b63b5a30b
16 changed files with 575 additions and 583 deletions

View File

@@ -64,19 +64,19 @@ func RunPerformanceDiagnostics(path string) {
fmt.Printf("\nOperation Performance:\n")
// Measure GetReadyWork
readyDuration := measureOperation("bd ready", func() error {
readyDuration := measureOperation(func() error {
return runReadyWork(dbPath)
})
fmt.Printf(" bd ready %dms\n", readyDuration.Milliseconds())
// Measure SearchIssues (list open)
listDuration := measureOperation("bd list --status=open", func() error {
listDuration := measureOperation(func() error {
return runListOpen(dbPath)
})
fmt.Printf(" bd list --status=open %dms\n", listDuration.Milliseconds())
// Measure GetIssue (show random issue)
showDuration := measureOperation("bd show <issue>", func() error {
showDuration := measureOperation(func() error {
return runShowRandom(dbPath)
})
if showDuration > 0 {
@@ -84,7 +84,7 @@ func RunPerformanceDiagnostics(path string) {
}
// Measure SearchIssues with filters
searchDuration := measureOperation("bd list (complex filters)", func() error {
searchDuration := measureOperation(func() error {
return runComplexSearch(dbPath)
})
fmt.Printf(" bd list (complex filters) %dms\n", searchDuration.Milliseconds())
@@ -205,7 +205,7 @@ func stopCPUProfile() {
}
}
func measureOperation(name string, op func() error) time.Duration {
func measureOperation(op func() error) time.Duration {
start := time.Now()
if err := op(); err != nil {
return 0

View File

@@ -18,7 +18,7 @@ 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 {
@@ -26,7 +26,7 @@ func getEmbeddedHooks() (map[string]string, error) {
}
hooks[name] = string(content)
}
return hooks, nil
}
@@ -41,7 +41,7 @@ type HookStatus struct {
}
// CheckGitHooks checks the status of bd git hooks in .git/hooks/
func CheckGitHooks() ([]HookStatus, error) {
func CheckGitHooks() []HookStatus {
hooks := []string{"pre-commit", "post-merge", "pre-push", "post-checkout"}
statuses := make([]HookStatus, 0, len(hooks))
@@ -59,7 +59,7 @@ func CheckGitHooks() ([]HookStatus, error) {
} else {
status.Installed = true
status.Version = version
// Check if outdated (compare to current bd version)
if version != "" && version != Version {
status.Outdated = true
@@ -69,7 +69,7 @@ func CheckGitHooks() ([]HookStatus, error) {
statuses = append(statuses, status)
}
return statuses, nil
return statuses
}
// getHookVersion extracts the version from a hook file
@@ -99,10 +99,10 @@ func getHookVersion(path string) (string, error) {
// FormatHookWarnings returns a formatted warning message if hooks are outdated
func FormatHookWarnings(statuses []HookStatus) string {
var warnings []string
missingCount := 0
outdatedCount := 0
for _, status := range statuses {
if !status.Installed {
missingCount++
@@ -110,21 +110,21 @@ func FormatHookWarnings(statuses []HookStatus) string {
outdatedCount++
}
}
if missingCount > 0 {
warnings = append(warnings, fmt.Sprintf("⚠️ Git hooks not installed (%d missing)", missingCount))
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: bd hooks install")
}
if len(warnings) > 0 {
return strings.Join(warnings, "\n")
}
return ""
}
@@ -157,7 +157,7 @@ Installed hooks:
- 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 {
@@ -171,7 +171,7 @@ Installed hooks:
}
os.Exit(1)
}
if err := installHooks(embeddedHooks, force); err != nil {
if jsonOutput {
output := map[string]interface{}{
@@ -184,7 +184,7 @@ Installed hooks:
}
os.Exit(1)
}
if jsonOutput {
output := map[string]interface{}{
"success": true,
@@ -220,7 +220,7 @@ var hooksUninstallCmd = &cobra.Command{
}
os.Exit(1)
}
if jsonOutput {
output := map[string]interface{}{
"success": true,
@@ -239,20 +239,8 @@ var hooksListCmd = &cobra.Command{
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)
}
statuses := CheckGitHooks()
if jsonOutput {
output := map[string]interface{}{
"hooks": statuses,
@@ -265,7 +253,7 @@ var hooksListCmd = &cobra.Command{
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",
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)
@@ -281,18 +269,18 @@ func installHooks(embeddedHooks map[string]string, force bool) error {
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
@@ -303,33 +291,33 @@ func installHooks(embeddedHooks map[string]string, force bool) error {
}
}
}
// 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 {
@@ -339,16 +327,16 @@ func uninstallHooks() error {
}
}
}
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)
}

View File

@@ -3,6 +3,7 @@ package main
import (
"os"
"path/filepath"
"runtime"
"testing"
)
@@ -59,7 +60,11 @@ func TestInstallHooks(t *testing.T) {
if _, err := os.Stat(hookPath); os.IsNotExist(err) {
t.Errorf("Hook %s was not installed", hookName)
}
// Check it's executable
// Windows does not support POSIX executable bits, so skip the check there.
if runtime.GOOS == "windows" {
continue
}
info, err := os.Stat(hookPath)
if err != nil {
t.Errorf("Failed to stat %s: %v", hookName, err)
@@ -206,10 +211,7 @@ func TestHooksCheckGitHooks(t *testing.T) {
os.Chdir(tmpDir)
// Initially no hooks installed
statuses, err := CheckGitHooks()
if err != nil {
t.Fatalf("CheckGitHooks() failed: %v", err)
}
statuses := CheckGitHooks()
for _, status := range statuses {
if status.Installed {
@@ -227,10 +229,7 @@ func TestHooksCheckGitHooks(t *testing.T) {
}
// Check again
statuses, err = CheckGitHooks()
if err != nil {
t.Fatalf("CheckGitHooks() failed: %v", err)
}
statuses = CheckGitHooks()
for _, status := range statuses {
if !status.Installed {

View File

@@ -100,12 +100,12 @@ Examples:
// Save current daemon state
wasDaemon := daemonClient != nil
var tempErr error
if wasDaemon {
// Temporarily switch to direct mode to read config
tempErr = ensureDirectMode("info: reading config")
}
if store != nil {
ctx := context.Background()
configMap, err := store.GetAllConfig(ctx)
@@ -113,7 +113,7 @@ Examples:
info["config"] = configMap
}
}
// Note: We don't restore daemon mode since info is a read-only command
// and the process will exit immediately after this
_ = tempErr // silence unused warning
@@ -121,23 +121,23 @@ Examples:
// Add schema information if requested
if schemaFlag && store != nil {
ctx := context.Background()
// Get schema version
schemaVersion, err := store.GetMetadata(ctx, "bd_version")
if err != nil {
schemaVersion = "unknown"
}
// Get tables
tables := []string{"issues", "dependencies", "labels", "config", "metadata"}
// Get config
configMap := make(map[string]string)
prefix, _ := store.GetConfig(ctx, "issue_prefix")
if prefix != "" {
configMap["issue_prefix"] = prefix
}
// Get sample issue IDs
filter := types.IssueFilter{}
issues, err := store.SearchIssues(ctx, "", filter)
@@ -157,13 +157,13 @@ Examples:
detectedPrefix = extractPrefix(issues[0].ID)
}
}
info["schema"] = map[string]interface{}{
"tables": tables,
"schema_version": schemaVersion,
"config": configMap,
"tables": tables,
"schema_version": schemaVersion,
"config": configMap,
"sample_issue_ids": sampleIDs,
"detected_prefix": detectedPrefix,
"detected_prefix": detectedPrefix,
}
}
@@ -229,11 +229,9 @@ Examples:
}
// Check git hooks status
hookStatuses, err := CheckGitHooks()
if err == nil {
if warning := FormatHookWarnings(hookStatuses); warning != "" {
fmt.Printf("\n%s\n", warning)
}
hookStatuses := CheckGitHooks()
if warning := FormatHookWarnings(hookStatuses); warning != "" {
fmt.Printf("\n%s\n", warning)
}
fmt.Println()

View File

@@ -68,7 +68,7 @@ With --no-db: creates .beads/ directory and issues.jsonl file instead of SQLite
}
}
}
// auto-detect prefix from directory name
if prefix == "" {
// Auto-detect from directory name
@@ -88,108 +88,108 @@ With --no-db: creates .beads/ directory and issues.jsonl file instead of SQLite
// Use global dbPath if set via --db flag or BEADS_DB env var, otherwise default to .beads/beads.db
initDBPath := dbPath
if initDBPath == "" {
initDBPath = filepath.Join(".beads", beads.CanonicalDatabaseName)
initDBPath = filepath.Join(".beads", beads.CanonicalDatabaseName)
}
// Migrate old database files if they exist
if err := migrateOldDatabases(initDBPath, quiet); err != nil {
fmt.Fprintf(os.Stderr, "Error during database migration: %v\n", err)
os.Exit(1)
}
// Determine if we should create .beads/ directory in CWD
// Only create it if the database will be stored there
cwd, err := os.Getwd()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: failed to get current directory: %v\n", err)
os.Exit(1)
}
// Prevent nested .beads directories
// Check if current working directory is inside a .beads directory
if strings.Contains(filepath.Clean(cwd), string(filepath.Separator)+".beads"+string(filepath.Separator)) ||
strings.HasSuffix(filepath.Clean(cwd), string(filepath.Separator)+".beads") {
fmt.Fprintf(os.Stderr, "Error: cannot initialize bd inside a .beads directory\n")
fmt.Fprintf(os.Stderr, "Current directory: %s\n", cwd)
fmt.Fprintf(os.Stderr, "Please run 'bd init' from outside the .beads directory.\n")
os.Exit(1)
}
localBeadsDir := filepath.Join(cwd, ".beads")
initDBDir := filepath.Dir(initDBPath)
// Convert both to absolute paths for comparison
localBeadsDirAbs, err := filepath.Abs(localBeadsDir)
if err != nil {
localBeadsDirAbs = filepath.Clean(localBeadsDir)
}
initDBDirAbs, err := filepath.Abs(initDBDir)
if err != nil {
initDBDirAbs = filepath.Clean(initDBDir)
}
useLocalBeads := filepath.Clean(initDBDirAbs) == filepath.Clean(localBeadsDirAbs)
if useLocalBeads {
// Create .beads directory
if err := os.MkdirAll(localBeadsDir, 0750); err != nil {
fmt.Fprintf(os.Stderr, "Error: failed to create .beads directory: %v\n", err)
if err := migrateOldDatabases(initDBPath, quiet); err != nil {
fmt.Fprintf(os.Stderr, "Error during database migration: %v\n", err)
os.Exit(1)
}
// Handle --no-db mode: create issues.jsonl file instead of database
if noDb {
// Create empty issues.jsonl file
jsonlPath := filepath.Join(localBeadsDir, "issues.jsonl")
if _, err := os.Stat(jsonlPath); os.IsNotExist(err) {
// nolint:gosec // G306: JSONL file needs to be readable by other tools
if err := os.WriteFile(jsonlPath, []byte{}, 0644); err != nil {
fmt.Fprintf(os.Stderr, "Error: failed to create issues.jsonl: %v\n", err)
os.Exit(1)
// Determine if we should create .beads/ directory in CWD
// Only create it if the database will be stored there
cwd, err := os.Getwd()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: failed to get current directory: %v\n", err)
os.Exit(1)
}
// Prevent nested .beads directories
// Check if current working directory is inside a .beads directory
if strings.Contains(filepath.Clean(cwd), string(filepath.Separator)+".beads"+string(filepath.Separator)) ||
strings.HasSuffix(filepath.Clean(cwd), string(filepath.Separator)+".beads") {
fmt.Fprintf(os.Stderr, "Error: cannot initialize bd inside a .beads directory\n")
fmt.Fprintf(os.Stderr, "Current directory: %s\n", cwd)
fmt.Fprintf(os.Stderr, "Please run 'bd init' from outside the .beads directory.\n")
os.Exit(1)
}
localBeadsDir := filepath.Join(cwd, ".beads")
initDBDir := filepath.Dir(initDBPath)
// Convert both to absolute paths for comparison
localBeadsDirAbs, err := filepath.Abs(localBeadsDir)
if err != nil {
localBeadsDirAbs = filepath.Clean(localBeadsDir)
}
initDBDirAbs, err := filepath.Abs(initDBDir)
if err != nil {
initDBDirAbs = filepath.Clean(initDBDir)
}
useLocalBeads := filepath.Clean(initDBDirAbs) == filepath.Clean(localBeadsDirAbs)
if useLocalBeads {
// Create .beads directory
if err := os.MkdirAll(localBeadsDir, 0750); err != nil {
fmt.Fprintf(os.Stderr, "Error: failed to create .beads directory: %v\n", err)
os.Exit(1)
}
// Handle --no-db mode: create issues.jsonl file instead of database
if noDb {
// Create empty issues.jsonl file
jsonlPath := filepath.Join(localBeadsDir, "issues.jsonl")
if _, err := os.Stat(jsonlPath); os.IsNotExist(err) {
// nolint:gosec // G306: JSONL file needs to be readable by other tools
if err := os.WriteFile(jsonlPath, []byte{}, 0644); err != nil {
fmt.Fprintf(os.Stderr, "Error: failed to create issues.jsonl: %v\n", err)
os.Exit(1)
}
}
// Create metadata.json for --no-db mode
cfg := configfile.DefaultConfig()
if err := cfg.Save(localBeadsDir); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to create metadata.json: %v\n", err)
// Non-fatal - continue anyway
}
// Create config.yaml with no-db: true
if err := createConfigYaml(localBeadsDir, true); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to create config.yaml: %v\n", err)
// Non-fatal - continue anyway
}
if !quiet {
green := color.New(color.FgGreen).SprintFunc()
cyan := color.New(color.FgCyan).SprintFunc()
fmt.Printf("\n%s bd initialized successfully in --no-db mode!\n\n", green("✓"))
fmt.Printf(" Mode: %s\n", cyan("no-db (JSONL-only)"))
fmt.Printf(" Issues file: %s\n", cyan(jsonlPath))
fmt.Printf(" Issue prefix: %s\n", cyan(prefix))
fmt.Printf(" Issues will be named: %s\n\n", cyan(prefix+"-1, "+prefix+"-2, ..."))
fmt.Printf("Run %s to get started.\n\n", cyan("bd --no-db quickstart"))
}
return
}
// Create metadata.json for --no-db mode
cfg := configfile.DefaultConfig()
if err := cfg.Save(localBeadsDir); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to create metadata.json: %v\n", err)
// Create/update .gitignore in .beads directory (idempotent - always update to latest)
gitignorePath := filepath.Join(localBeadsDir, ".gitignore")
if err := os.WriteFile(gitignorePath, []byte(doctor.GitignoreTemplate), 0600); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to create/update .gitignore: %v\n", err)
// Non-fatal - continue anyway
}
// Create config.yaml with no-db: true
if err := createConfigYaml(localBeadsDir, true); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to create config.yaml: %v\n", err)
// Non-fatal - continue anyway
}
if !quiet {
green := color.New(color.FgGreen).SprintFunc()
cyan := color.New(color.FgCyan).SprintFunc()
fmt.Printf("\n%s bd initialized successfully in --no-db mode!\n\n", green("✓"))
fmt.Printf(" Mode: %s\n", cyan("no-db (JSONL-only)"))
fmt.Printf(" Issues file: %s\n", cyan(jsonlPath))
fmt.Printf(" Issue prefix: %s\n", cyan(prefix))
fmt.Printf(" Issues will be named: %s\n\n", cyan(prefix+"-1, "+prefix+"-2, ..."))
fmt.Printf("Run %s to get started.\n\n", cyan("bd --no-db quickstart"))
}
return
}
// Create/update .gitignore in .beads directory (idempotent - always update to latest)
gitignorePath := filepath.Join(localBeadsDir, ".gitignore")
if err := os.WriteFile(gitignorePath, []byte(doctor.GitignoreTemplate), 0600); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to create/update .gitignore: %v\n", err)
// Non-fatal - continue anyway
}
}
// Ensure parent directory exists for the database
if err := os.MkdirAll(initDBDir, 0750); err != nil {
fmt.Fprintf(os.Stderr, "Error: failed to create database directory %s: %v\n", initDBDir, err)
os.Exit(1)
fmt.Fprintf(os.Stderr, "Error: failed to create database directory %s: %v\n", initDBDir, err)
os.Exit(1)
}
store, err := sqlite.New(initDBPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: failed to create database: %v\n", err)
@@ -199,192 +199,192 @@ With --no-db: creates .beads/ directory and issues.jsonl file instead of SQLite
// Set the issue prefix in config
ctx := context.Background()
if err := store.SetConfig(ctx, "issue_prefix", prefix); err != nil {
fmt.Fprintf(os.Stderr, "Error: failed to set issue prefix: %v\n", err)
_ = store.Close()
os.Exit(1)
}
// Set sync.branch if specified
if branch != "" {
if err := syncbranch.Set(ctx, store, branch); err != nil {
fmt.Fprintf(os.Stderr, "Error: failed to set sync branch: %v\n", err)
fmt.Fprintf(os.Stderr, "Error: failed to set issue prefix: %v\n", err)
_ = store.Close()
os.Exit(1)
}
if !quiet {
fmt.Printf(" Sync branch: %s\n", branch)
// Set sync.branch if specified
if branch != "" {
if err := syncbranch.Set(ctx, store, branch); err != nil {
fmt.Fprintf(os.Stderr, "Error: failed to set sync branch: %v\n", err)
_ = store.Close()
os.Exit(1)
}
if !quiet {
fmt.Printf(" Sync branch: %s\n", branch)
}
}
}
// Store the bd version in metadata (for version mismatch detection)
if err := store.SetMetadata(ctx, "bd_version", Version); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to store version metadata: %v\n", err)
// Non-fatal - continue anyway
}
// Compute and store repository fingerprint
repoID, err := beads.ComputeRepoID()
if err != nil {
if !quiet {
fmt.Fprintf(os.Stderr, "Warning: could not compute repository ID: %v\n", err)
}
} else {
if err := store.SetMetadata(ctx, "repo_id", repoID); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to set repo_id: %v\n", err)
} else if !quiet {
fmt.Printf(" Repository ID: %s\n", repoID[:8])
}
}
// Store clone-specific ID
cloneID, err := beads.GetCloneID()
if err != nil {
if !quiet {
fmt.Fprintf(os.Stderr, "Warning: could not compute clone ID: %v\n", err)
}
} else {
if err := store.SetMetadata(ctx, "clone_id", cloneID); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to set clone_id: %v\n", err)
} else if !quiet {
fmt.Printf(" Clone ID: %s\n", cloneID)
}
}
// Create metadata.json for database metadata
if useLocalBeads {
cfg := configfile.DefaultConfig()
if err := cfg.Save(localBeadsDir); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to create metadata.json: %v\n", err)
fmt.Fprintf(os.Stderr, "Warning: failed to store version metadata: %v\n", err)
// Non-fatal - continue anyway
}
// Create config.yaml template
if err := createConfigYaml(localBeadsDir, false); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to create config.yaml: %v\n", err)
// Non-fatal - continue anyway
}
}
// Check if git has existing issues to import (fresh clone scenario)
issueCount, jsonlPath := checkGitForIssues()
if issueCount > 0 {
if !quiet {
fmt.Fprintf(os.Stderr, "\n✓ Database initialized. Found %d issues in git, importing...\n", issueCount)
}
if err := importFromGit(ctx, initDBPath, store, jsonlPath); err != nil {
// Compute and store repository fingerprint
repoID, err := beads.ComputeRepoID()
if err != nil {
if !quiet {
fmt.Fprintf(os.Stderr, "Warning: auto-import failed: %v\n", err)
fmt.Fprintf(os.Stderr, "Try manually: git show HEAD:%s | bd import -i /dev/stdin\n", jsonlPath)
fmt.Fprintf(os.Stderr, "Warning: could not compute repository ID: %v\n", err)
}
} else {
if err := store.SetMetadata(ctx, "repo_id", repoID); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to set repo_id: %v\n", err)
} else if !quiet {
fmt.Printf(" Repository ID: %s\n", repoID[:8])
}
// Non-fatal - continue with empty database
} else if !quiet {
fmt.Fprintf(os.Stderr, "✓ Successfully imported %d issues from git.\n\n", issueCount)
}
}
// Run contributor wizard if --contributor flag is set
if contributor {
if err := runContributorWizard(ctx, store); err != nil {
fmt.Fprintf(os.Stderr, "Error running contributor wizard: %v\n", err)
_ = store.Close()
os.Exit(1)
// Store clone-specific ID
cloneID, err := beads.GetCloneID()
if err != nil {
if !quiet {
fmt.Fprintf(os.Stderr, "Warning: could not compute clone ID: %v\n", err)
}
} else {
if err := store.SetMetadata(ctx, "clone_id", cloneID); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to set clone_id: %v\n", err)
} else if !quiet {
fmt.Printf(" Clone ID: %s\n", cloneID)
}
}
}
// Run team wizard if --team flag is set
if team {
if err := runTeamWizard(ctx, store); err != nil {
fmt.Fprintf(os.Stderr, "Error running team wizard: %v\n", err)
_ = store.Close()
os.Exit(1)
// Create metadata.json for database metadata
if useLocalBeads {
cfg := configfile.DefaultConfig()
if err := cfg.Save(localBeadsDir); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to create metadata.json: %v\n", err)
// Non-fatal - continue anyway
}
// Create config.yaml template
if err := createConfigYaml(localBeadsDir, false); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to create config.yaml: %v\n", err)
// Non-fatal - continue anyway
}
}
}
if err := store.Close(); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to close database: %v\n", err)
}
// Check if git has existing issues to import (fresh clone scenario)
issueCount, jsonlPath := checkGitForIssues()
if issueCount > 0 {
if !quiet {
fmt.Fprintf(os.Stderr, "\n✓ Database initialized. Found %d issues in git, importing...\n", issueCount)
}
// Check if we're in a git repo and hooks aren't installed
// Do this BEFORE quiet mode return so hooks get installed for agents
if isGitRepo() && !hooksInstalled() {
if quiet {
// Auto-install hooks silently in quiet mode (best default for agents)
_ = installGitHooks() // Ignore errors in quiet mode
} else {
// Defer to interactive prompt below
}
}
if err := importFromGit(ctx, initDBPath, store, jsonlPath); err != nil {
if !quiet {
fmt.Fprintf(os.Stderr, "Warning: auto-import failed: %v\n", err)
fmt.Fprintf(os.Stderr, "Try manually: git show HEAD:%s | bd import -i /dev/stdin\n", jsonlPath)
}
// Non-fatal - continue with empty database
} else if !quiet {
fmt.Fprintf(os.Stderr, "✓ Successfully imported %d issues from git.\n\n", issueCount)
}
}
// Check if we're in a git repo and merge driver isn't configured
// Do this BEFORE quiet mode return so merge driver gets configured for agents
if !skipMergeDriver && isGitRepo() && !mergeDriverInstalled() {
if quiet {
// Auto-install merge driver silently in quiet mode (best default for agents)
_ = installMergeDriver() // Ignore errors in quiet mode
} else {
// Defer to interactive prompt below
}
}
// Run contributor wizard if --contributor flag is set
if contributor {
if err := runContributorWizard(ctx, store); err != nil {
fmt.Fprintf(os.Stderr, "Error running contributor wizard: %v\n", err)
_ = store.Close()
os.Exit(1)
}
}
// Skip output if quiet mode
if quiet {
return
}
// Run team wizard if --team flag is set
if team {
if err := runTeamWizard(ctx, store); err != nil {
fmt.Fprintf(os.Stderr, "Error running team wizard: %v\n", err)
_ = store.Close()
os.Exit(1)
}
}
if err := store.Close(); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to close database: %v\n", err)
}
// Check if we're in a git repo and hooks aren't installed
// Do this BEFORE quiet mode return so hooks get installed for agents
if isGitRepo() && !hooksInstalled() {
if quiet {
// Auto-install hooks silently in quiet mode (best default for agents)
_ = installGitHooks() // Ignore errors in quiet mode
} else {
// Defer to interactive prompt below
}
}
// Check if we're in a git repo and merge driver isn't configured
// Do this BEFORE quiet mode return so merge driver gets configured for agents
if !skipMergeDriver && isGitRepo() && !mergeDriverInstalled() {
if quiet {
// Auto-install merge driver silently in quiet mode (best default for agents)
_ = installMergeDriver() // Ignore errors in quiet mode
} else {
// Defer to interactive prompt below
}
}
// Skip output if quiet mode
if quiet {
return
}
green := color.New(color.FgGreen).SprintFunc()
cyan := color.New(color.FgCyan).SprintFunc()
yellow := color.New(color.FgYellow).SprintFunc()
yellow := color.New(color.FgYellow).SprintFunc()
fmt.Printf("\n%s bd initialized successfully!\n\n", green("✓"))
fmt.Printf(" Database: %s\n", cyan(initDBPath))
fmt.Printf(" Issue prefix: %s\n", cyan(prefix))
fmt.Printf(" Issues will be named: %s\n\n", cyan(prefix+"-1, "+prefix+"-2, ..."))
// Interactive git hooks prompt for humans
if isGitRepo() && !hooksInstalled() {
fmt.Printf("%s Git hooks not installed\n", yellow("⚠"))
fmt.Printf(" Install git hooks to prevent race conditions between commits and auto-flush.\n")
fmt.Printf(" Run: %s\n\n", cyan("./examples/git-hooks/install.sh"))
// Prompt to install
fmt.Printf("Install git hooks now? [Y/n] ")
var response string
_, _ = fmt.Scanln(&response) // ignore EOF on empty input
response = strings.ToLower(strings.TrimSpace(response))
if response == "" || response == "y" || response == "yes" {
if err := installGitHooks(); err != nil {
fmt.Fprintf(os.Stderr, "Error installing hooks: %v\n", err)
fmt.Printf("You can install manually with: %s\n\n", cyan("./examples/git-hooks/install.sh"))
} else {
fmt.Printf("%s Git hooks installed successfully!\n\n", green("✓"))
// Interactive git hooks prompt for humans
if isGitRepo() && !hooksInstalled() {
fmt.Printf("%s Git hooks not installed\n", yellow("⚠"))
fmt.Printf(" Install git hooks to prevent race conditions between commits and auto-flush.\n")
fmt.Printf(" Run: %s\n\n", cyan("./examples/git-hooks/install.sh"))
// Prompt to install
fmt.Printf("Install git hooks now? [Y/n] ")
var response string
_, _ = fmt.Scanln(&response) // ignore EOF on empty input
response = strings.ToLower(strings.TrimSpace(response))
if response == "" || response == "y" || response == "yes" {
if err := installGitHooks(); err != nil {
fmt.Fprintf(os.Stderr, "Error installing hooks: %v\n", err)
fmt.Printf("You can install manually with: %s\n\n", cyan("./examples/git-hooks/install.sh"))
} else {
fmt.Printf("%s Git hooks installed successfully!\n\n", green("✓"))
}
}
}
}
// Interactive git merge driver prompt for humans
if !skipMergeDriver && isGitRepo() && !mergeDriverInstalled() {
fmt.Printf("%s Git merge driver not configured\n", yellow("⚠"))
fmt.Printf(" bd merge provides intelligent JSONL merging to prevent conflicts.\n")
fmt.Printf(" This will configure git to use 'bd merge' for .beads/beads.jsonl\n\n")
// Prompt to install
fmt.Printf("Configure git merge driver now? [Y/n] ")
var response string
_, _ = fmt.Scanln(&response) // ignore EOF on empty input
response = strings.ToLower(strings.TrimSpace(response))
if response == "" || response == "y" || response == "yes" {
if err := installMergeDriver(); err != nil {
fmt.Fprintf(os.Stderr, "Error configuring merge driver: %v\n", err)
} else {
fmt.Printf("%s Git merge driver configured successfully!\n\n", green("✓"))
// Interactive git merge driver prompt for humans
if !skipMergeDriver && isGitRepo() && !mergeDriverInstalled() {
fmt.Printf("%s Git merge driver not configured\n", yellow("⚠"))
fmt.Printf(" bd merge provides intelligent JSONL merging to prevent conflicts.\n")
fmt.Printf(" This will configure git to use 'bd merge' for .beads/beads.jsonl\n\n")
// Prompt to install
fmt.Printf("Configure git merge driver now? [Y/n] ")
var response string
_, _ = fmt.Scanln(&response) // ignore EOF on empty input
response = strings.ToLower(strings.TrimSpace(response))
if response == "" || response == "y" || response == "yes" {
if err := installMergeDriver(); err != nil {
fmt.Fprintf(os.Stderr, "Error configuring merge driver: %v\n", err)
} else {
fmt.Printf("%s Git merge driver configured successfully!\n\n", green("✓"))
}
}
}
}
fmt.Printf("Run %s to get started.\n\n", cyan("bd quickstart"))
fmt.Printf("Run %s to get started.\n\n", cyan("bd quickstart"))
},
}
@@ -402,50 +402,50 @@ func init() {
func hooksInstalled() bool {
preCommit := filepath.Join(".git", "hooks", "pre-commit")
postMerge := filepath.Join(".git", "hooks", "post-merge")
// Check if both hooks exist
_, err1 := os.Stat(preCommit)
_, err2 := os.Stat(postMerge)
if err1 != nil || err2 != nil {
return false
}
// Verify they're bd hooks by checking for signature comment
// #nosec G304 - controlled path from git directory
preCommitContent, err := os.ReadFile(preCommit)
if err != nil || !strings.Contains(string(preCommitContent), "bd (beads) pre-commit hook") {
return false
}
// #nosec G304 - controlled path from git directory
postMergeContent, err := os.ReadFile(postMerge)
if err != nil || !strings.Contains(string(postMergeContent), "bd (beads) post-merge hook") {
return false
}
return true
}
// hookInfo contains information about an existing hook
type hookInfo struct {
name string
path string
exists bool
isBdHook bool
isPreCommit bool
content string
name string
path string
exists bool
isBdHook bool
isPreCommit bool
content string
}
// detectExistingHooks scans for existing git hooks
func detectExistingHooks() ([]hookInfo, error) {
func detectExistingHooks() []hookInfo {
hooksDir := filepath.Join(".git", "hooks")
hooks := []hookInfo{
{name: "pre-commit", path: filepath.Join(hooksDir, "pre-commit")},
{name: "post-merge", path: filepath.Join(hooksDir, "post-merge")},
{name: "pre-push", path: filepath.Join(hooksDir, "pre-push")},
}
for i := range hooks {
content, err := os.ReadFile(hooks[i].path)
if err == nil {
@@ -459,14 +459,14 @@ func detectExistingHooks() ([]hookInfo, error) {
}
}
}
return hooks, nil
return hooks
}
// promptHookAction asks user what to do with existing hooks
func promptHookAction(existingHooks []hookInfo) string {
yellow := color.New(color.FgYellow).SprintFunc()
fmt.Printf("\n%s Found existing git hooks:\n", yellow("⚠"))
for _, hook := range existingHooks {
if hook.exists && !hook.isBdHook {
@@ -477,35 +477,32 @@ func promptHookAction(existingHooks []hookInfo) string {
fmt.Printf(" - %s (%s)\n", hook.name, hookType)
}
}
fmt.Printf("\nHow should bd proceed?\n")
fmt.Printf(" [1] Chain with existing hooks (recommended)\n")
fmt.Printf(" [2] Overwrite existing hooks\n")
fmt.Printf(" [3] Skip git hooks installation\n")
fmt.Printf("Choice [1-3]: ")
var response string
_, _ = fmt.Scanln(&response)
response = strings.TrimSpace(response)
return response
}
// installGitHooks installs git hooks inline (no external dependencies)
func installGitHooks() error {
hooksDir := filepath.Join(".git", "hooks")
// Ensure hooks directory exists
if err := os.MkdirAll(hooksDir, 0750); err != nil {
return fmt.Errorf("failed to create hooks directory: %w", err)
}
// Detect existing hooks
existingHooks, err := detectExistingHooks()
if err != nil {
return fmt.Errorf("failed to detect existing hooks: %w", err)
}
existingHooks := detectExistingHooks()
// Check if any non-bd hooks exist
hasExistingHooks := false
for _, hook := range existingHooks {
@@ -514,7 +511,7 @@ func installGitHooks() error {
break
}
}
// Determine installation mode
chainHooks := false
if hasExistingHooks {
@@ -543,11 +540,11 @@ func installGitHooks() error {
return fmt.Errorf("invalid choice: %s", choice)
}
}
// pre-commit hook
preCommitPath := filepath.Join(hooksDir, "pre-commit")
var preCommitContent string
if chainHooks {
// Find existing pre-commit hook
var existingPreCommit string
@@ -562,7 +559,7 @@ func installGitHooks() error {
break
}
}
preCommitContent = `#!/bin/sh
#
# bd (beads) pre-commit hook (chained)
@@ -641,11 +638,11 @@ fi
exit 0
`
}
// post-merge hook
postMergePath := filepath.Join(hooksDir, "post-merge")
var postMergeContent string
if chainHooks {
// Find existing post-merge hook
var existingPostMerge string
@@ -660,7 +657,7 @@ exit 0
break
}
}
postMergeContent = `#!/bin/sh
#
# bd (beads) post-merge hook (chained)
@@ -737,24 +734,24 @@ fi
exit 0
`
}
// Write pre-commit hook (executable scripts need 0700)
// #nosec G306 - git hooks must be executable
if err := os.WriteFile(preCommitPath, []byte(preCommitContent), 0700); err != nil {
return fmt.Errorf("failed to write pre-commit hook: %w", err)
}
// Write post-merge hook (executable scripts need 0700)
// #nosec G306 - git hooks must be executable
if err := os.WriteFile(postMergePath, []byte(postMergeContent), 0700); err != nil {
return fmt.Errorf("failed to write post-merge hook: %w", err)
}
if chainHooks {
green := color.New(color.FgGreen).SprintFunc()
fmt.Printf("%s Chained bd hooks with existing hooks\n", green("✓"))
}
return nil
}
@@ -766,17 +763,17 @@ func mergeDriverInstalled() bool {
if err != nil || len(output) == 0 {
return false
}
// Check if .gitattributes has the merge driver configured
gitattributesPath := ".gitattributes"
content, err := os.ReadFile(gitattributesPath)
if err != nil {
return false
}
// Look for beads JSONL merge attribute
return strings.Contains(string(content), ".beads/beads.jsonl") &&
strings.Contains(string(content), "merge=beads")
return strings.Contains(string(content), ".beads/beads.jsonl") &&
strings.Contains(string(content), "merge=beads")
}
// installMergeDriver configures git to use bd merge for JSONL files
@@ -786,44 +783,44 @@ func installMergeDriver() error {
if output, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("failed to configure git merge driver: %w\n%s", err, output)
}
cmd = exec.Command("git", "config", "merge.beads.name", "bd JSONL merge driver")
if output, err := cmd.CombinedOutput(); err != nil {
// Non-fatal, the name is just descriptive
fmt.Fprintf(os.Stderr, "Warning: failed to set merge driver name: %v\n%s", err, output)
}
// Create or update .gitattributes
gitattributesPath := ".gitattributes"
// Read existing .gitattributes if it exists
var existingContent string
content, err := os.ReadFile(gitattributesPath)
if err == nil {
existingContent = string(content)
}
// Check if beads merge driver is already configured
hasBeadsMerge := strings.Contains(existingContent, ".beads/beads.jsonl") &&
strings.Contains(existingContent, "merge=beads")
strings.Contains(existingContent, "merge=beads")
if !hasBeadsMerge {
// Append beads merge driver configuration
beadsMergeAttr := "\n# Use bd merge for beads JSONL files\n.beads/beads.jsonl merge=beads\n"
newContent := existingContent
if !strings.HasSuffix(newContent, "\n") && len(newContent) > 0 {
newContent += "\n"
}
newContent += beadsMergeAttr
// Write updated .gitattributes (0644 is standard for .gitattributes)
// #nosec G306 - .gitattributes needs to be readable
if err := os.WriteFile(gitattributesPath, []byte(newContent), 0644); err != nil {
return fmt.Errorf("failed to update .gitattributes: %w", err)
}
}
return nil
}
@@ -831,24 +828,24 @@ func installMergeDriver() error {
func migrateOldDatabases(targetPath string, quiet bool) error {
targetDir := filepath.Dir(targetPath)
targetName := filepath.Base(targetPath)
// If target already exists, no migration needed
if _, err := os.Stat(targetPath); err == nil {
return nil
}
// Create .beads directory if it doesn't exist
if err := os.MkdirAll(targetDir, 0750); err != nil {
return fmt.Errorf("failed to create .beads directory: %w", err)
}
// Look for existing .db files in the .beads directory
pattern := filepath.Join(targetDir, "*.db")
matches, err := filepath.Glob(pattern)
if err != nil {
return fmt.Errorf("failed to search for existing databases: %w", err)
}
// Filter out the target file name and any backup files
var oldDBs []string
for _, match := range matches {
@@ -857,50 +854,50 @@ func migrateOldDatabases(targetPath string, quiet bool) error {
oldDBs = append(oldDBs, match)
}
}
if len(oldDBs) == 0 {
// No old databases to migrate
return nil
}
if len(oldDBs) > 1 {
// Multiple databases found - ambiguous, require manual intervention
return fmt.Errorf("multiple database files found in %s: %v\nPlease manually rename the correct database to %s and remove others",
targetDir, oldDBs, targetName)
}
// Migrate the single old database
oldDB := oldDBs[0]
if !quiet {
fmt.Fprintf(os.Stderr, "→ Migrating database: %s → %s\n", filepath.Base(oldDB), targetName)
}
// Rename the old database to the new canonical name
if err := os.Rename(oldDB, targetPath); err != nil {
return fmt.Errorf("failed to migrate database %s to %s: %w", oldDB, targetPath, err)
}
if !quiet {
fmt.Fprintf(os.Stderr, "✓ Database migration complete\n\n")
}
return nil
}
// createConfigYaml creates the config.yaml template in the specified directory
func createConfigYaml(beadsDir string, noDbMode bool) error {
configYamlPath := filepath.Join(beadsDir, "config.yaml")
// Skip if already exists
if _, err := os.Stat(configYamlPath); err == nil {
return nil
}
noDbLine := "# no-db: false"
if noDbMode {
noDbLine = "no-db: true # JSONL-only mode, no SQLite database"
}
configYamlTemplate := fmt.Sprintf(`# Beads Configuration File
# This file configures default behavior for all bd commands in this repository
# All settings can also be set via environment variables (BD_* prefix)
@@ -958,11 +955,11 @@ func createConfigYaml(beadsDir string, noDbMode bool) error {
# - github.repo
# - sync.branch - Git branch for beads commits (use BEADS_SYNC_BRANCH env var or bd config set)
`, noDbLine)
if err := os.WriteFile(configYamlPath, []byte(configYamlTemplate), 0600); err != nil {
return fmt.Errorf("failed to write config.yaml: %w", err)
}
return nil
}

View File

@@ -26,11 +26,8 @@ func runContributorWizard(ctx context.Context, store storage.Storage) error {
// Step 1: Detect fork relationship
fmt.Printf("%s Detecting git repository setup...\n", cyan("▶"))
isFork, upstreamURL, err := detectForkSetup()
if err != nil {
return fmt.Errorf("failed to detect git setup: %w", err)
}
isFork, upstreamURL := detectForkSetup()
if isFork {
fmt.Printf("%s Detected fork workflow (upstream: %s)\n", green("✓"), upstreamURL)
@@ -39,24 +36,24 @@ func runContributorWizard(ctx context.Context, store storage.Storage) error {
fmt.Println("\n For fork workflows, add an 'upstream' remote:")
fmt.Println(" git remote add upstream <original-repo-url>")
fmt.Println()
// Ask if they want to continue anyway
fmt.Print("Continue with contributor setup? [y/N]: ")
reader := bufio.NewReader(os.Stdin)
response, _ := reader.ReadString('\n')
response = strings.TrimSpace(strings.ToLower(response))
if response != "y" && response != "yes" {
fmt.Println("Setup cancelled.")
fmt.Println("Setup canceled.")
return nil
}
}
// Step 2: Check push access to origin
fmt.Printf("\n%s Checking repository access...\n", cyan("▶"))
hasPushAccess, originURL := checkPushAccess()
if hasPushAccess {
fmt.Printf("%s You have push access to origin (%s)\n", green("✓"), originURL)
fmt.Printf(" %s You can commit directly to this repository.\n", yellow("⚠"))
@@ -65,9 +62,9 @@ func runContributorWizard(ctx context.Context, store storage.Storage) error {
reader := bufio.NewReader(os.Stdin)
response, _ := reader.ReadString('\n')
response = strings.TrimSpace(strings.ToLower(response))
if response == "n" || response == "no" {
fmt.Println("\nSetup cancelled. Your issues will be stored in the current repository.")
fmt.Println("\nSetup canceled. Your issues will be stored in the current repository.")
return nil
}
} else {
@@ -77,26 +74,26 @@ func runContributorWizard(ctx context.Context, store storage.Storage) error {
// Step 3: Configure planning repository
fmt.Printf("\n%s Setting up planning repository...\n", cyan("▶"))
homeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("failed to get home directory: %w", err)
}
defaultPlanningRepo := filepath.Join(homeDir, ".beads-planning")
fmt.Printf("\nWhere should contributor planning issues be stored?\n")
fmt.Printf("Default: %s\n", cyan(defaultPlanningRepo))
fmt.Print("Planning repo path [press Enter for default]: ")
reader := bufio.NewReader(os.Stdin)
planningPath, _ := reader.ReadString('\n')
planningPath = strings.TrimSpace(planningPath)
if planningPath == "" {
planningPath = defaultPlanningRepo
}
// Expand ~ if present
if strings.HasPrefix(planningPath, "~/") {
planningPath = filepath.Join(homeDir, planningPath[2:])
@@ -105,30 +102,30 @@ func runContributorWizard(ctx context.Context, store storage.Storage) error {
// Create planning repository if it doesn't exist
if _, err := os.Stat(planningPath); os.IsNotExist(err) {
fmt.Printf("\nCreating planning repository at %s\n", cyan(planningPath))
if err := os.MkdirAll(planningPath, 0750); err != nil {
return fmt.Errorf("failed to create planning repo directory: %w", err)
}
// Initialize git repo in planning directory
cmd := exec.Command("git", "init")
cmd.Dir = planningPath
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to initialize git in planning repo: %w", err)
}
// Initialize beads in planning repo
beadsDir := filepath.Join(planningPath, ".beads")
if err := os.MkdirAll(beadsDir, 0750); err != nil {
return fmt.Errorf("failed to create .beads in planning repo: %w", err)
}
// Create issues.jsonl
jsonlPath := filepath.Join(beadsDir, "beads.jsonl")
if err := os.WriteFile(jsonlPath, []byte{}, 0644); err != nil {
return fmt.Errorf("failed to create issues.jsonl: %w", err)
}
// Create README in planning repo
readmePath := filepath.Join(planningPath, "README.md")
readmeContent := fmt.Sprintf(`# Beads Planning Repository
@@ -150,16 +147,16 @@ Created by: bd init --contributor
if err := os.WriteFile(readmePath, []byte(readmeContent), 0644); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to create README: %v\n", err)
}
// Initial commit in planning repo
cmd = exec.Command("git", "add", ".")
cmd.Dir = planningPath
_ = cmd.Run()
cmd = exec.Command("git", "commit", "-m", "Initial commit: beads planning repository")
cmd.Dir = planningPath
_ = cmd.Run()
fmt.Printf("%s Planning repository created\n", green("✓"))
} else {
fmt.Printf("%s Using existing planning repository\n", green("✓"))
@@ -167,22 +164,22 @@ Created by: bd init --contributor
// Step 4: Configure contributor routing
fmt.Printf("\n%s Configuring contributor auto-routing...\n", cyan("▶"))
// Set contributor.planning_repo config
if err := store.SetConfig(ctx, "contributor.planning_repo", planningPath); err != nil {
return fmt.Errorf("failed to set planning repo config: %w", err)
}
// Set contributor.auto_route to true
if err := store.SetConfig(ctx, "contributor.auto_route", "true"); err != nil {
return fmt.Errorf("failed to enable auto-routing: %w", err)
}
fmt.Printf("%s Auto-routing enabled\n", green("✓"))
// Step 5: Summary
fmt.Printf("\n%s %s\n\n", green("✓"), bold("Contributor setup complete!"))
fmt.Println("Configuration:")
fmt.Printf(" Current repo issues: %s\n", cyan(".beads/beads.jsonl"))
fmt.Printf(" Planning repo issues: %s\n", cyan(filepath.Join(planningPath, ".beads/beads.jsonl")))
@@ -199,16 +196,16 @@ Created by: bd init --contributor
}
// detectForkSetup checks if we're in a fork by looking for upstream remote
func detectForkSetup() (isFork bool, upstreamURL string, err error) {
func detectForkSetup() (isFork bool, upstreamURL string) {
cmd := exec.Command("git", "remote", "get-url", "upstream")
output, err := cmd.Output()
if err != nil {
// No upstream remote found
return false, "", nil
return false, ""
}
upstreamURL = strings.TrimSpace(string(output))
return true, upstreamURL, nil
return true, upstreamURL
}
// checkPushAccess determines if we have push access to origin
@@ -219,19 +216,19 @@ func checkPushAccess() (hasPush bool, originURL string) {
if err != nil {
return false, ""
}
originURL = strings.TrimSpace(string(output))
// SSH URLs indicate likely push access (git@github.com:...)
if strings.HasPrefix(originURL, "git@") {
return true, originURL
}
// HTTPS URLs typically indicate read-only clone
if strings.HasPrefix(originURL, "https://") {
return false, originURL
}
// Other protocols (file://, etc.) assume push access
return true, originURL
}

View File

@@ -15,43 +15,43 @@ func TestDetectExistingHooks(t *testing.T) {
t.Fatal(err)
}
defer os.Chdir(oldDir)
if err := os.Chdir(tmpDir); err != nil {
t.Fatal(err)
}
// Initialize a git repository
gitDir := filepath.Join(tmpDir, ".git")
hooksDir := filepath.Join(gitDir, "hooks")
if err := os.MkdirAll(hooksDir, 0750); err != nil {
t.Fatal(err)
}
tests := []struct {
name string
setupHook string
hookContent string
wantExists bool
wantIsBdHook bool
name string
setupHook string
hookContent string
wantExists bool
wantIsBdHook bool
wantIsPreCommit bool
}{
{
name: "no hook",
setupHook: "",
wantExists: false,
name: "no hook",
setupHook: "",
wantExists: false,
},
{
name: "bd hook",
setupHook: "pre-commit",
hookContent: "#!/bin/sh\n# bd (beads) pre-commit hook\necho test",
wantExists: true,
name: "bd hook",
setupHook: "pre-commit",
hookContent: "#!/bin/sh\n# bd (beads) pre-commit hook\necho test",
wantExists: true,
wantIsBdHook: true,
},
{
name: "pre-commit framework hook",
setupHook: "pre-commit",
hookContent: "#!/bin/sh\n# pre-commit framework\npre-commit run",
wantExists: true,
name: "pre-commit framework hook",
setupHook: "pre-commit",
hookContent: "#!/bin/sh\n# pre-commit framework\npre-commit run",
wantExists: true,
wantIsPreCommit: true,
},
{
@@ -61,13 +61,13 @@ func TestDetectExistingHooks(t *testing.T) {
wantExists: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Clean up hooks directory
os.RemoveAll(hooksDir)
os.MkdirAll(hooksDir, 0750)
// Setup hook if needed
if tt.setupHook != "" {
hookPath := filepath.Join(hooksDir, tt.setupHook)
@@ -75,13 +75,10 @@ func TestDetectExistingHooks(t *testing.T) {
t.Fatal(err)
}
}
// Detect hooks
hooks, err := detectExistingHooks()
if err != nil {
t.Fatalf("detectExistingHooks() error = %v", err)
}
hooks := detectExistingHooks()
// Find the hook we're testing
var found *hookInfo
for i := range hooks {
@@ -90,11 +87,11 @@ func TestDetectExistingHooks(t *testing.T) {
break
}
}
if found == nil {
t.Fatal("pre-commit hook not found in results")
}
if found.exists != tt.wantExists {
t.Errorf("exists = %v, want %v", found.exists, tt.wantExists)
}
@@ -116,26 +113,26 @@ func TestInstallGitHooks_NoExistingHooks(t *testing.T) {
t.Fatal(err)
}
defer os.Chdir(oldDir)
if err := os.Chdir(tmpDir); err != nil {
t.Fatal(err)
}
// Initialize a git repository
gitDir := filepath.Join(tmpDir, ".git")
hooksDir := filepath.Join(gitDir, "hooks")
if err := os.MkdirAll(hooksDir, 0750); err != nil {
t.Fatal(err)
}
// Note: Can't fully test interactive prompt in automated tests
// This test verifies the logic works when no existing hooks present
// For full testing, we'd need to mock user input
// Check hooks were created
preCommitPath := filepath.Join(hooksDir, "pre-commit")
postMergePath := filepath.Join(hooksDir, "post-merge")
if _, err := os.Stat(preCommitPath); err == nil {
content, _ := os.ReadFile(preCommitPath)
if !strings.Contains(string(content), "bd (beads)") {
@@ -145,7 +142,7 @@ func TestInstallGitHooks_NoExistingHooks(t *testing.T) {
t.Error("pre-commit hook shouldn't be chained when no existing hooks")
}
}
if _, err := os.Stat(postMergePath); err == nil {
content, _ := os.ReadFile(postMergePath)
if !strings.Contains(string(content), "bd (beads)") {
@@ -162,31 +159,28 @@ func TestInstallGitHooks_ExistingHookBackup(t *testing.T) {
t.Fatal(err)
}
defer os.Chdir(oldDir)
if err := os.Chdir(tmpDir); err != nil {
t.Fatal(err)
}
// Initialize a git repository
gitDir := filepath.Join(tmpDir, ".git")
hooksDir := filepath.Join(gitDir, "hooks")
if err := os.MkdirAll(hooksDir, 0750); err != nil {
t.Fatal(err)
}
// Create an existing pre-commit hook
preCommitPath := filepath.Join(hooksDir, "pre-commit")
existingContent := "#!/bin/sh\necho existing hook"
if err := os.WriteFile(preCommitPath, []byte(existingContent), 0700); err != nil {
t.Fatal(err)
}
// Detect that hook exists
hooks, err := detectExistingHooks()
if err != nil {
t.Fatal(err)
}
hooks := detectExistingHooks()
hasExisting := false
for _, hook := range hooks {
if hook.exists && !hook.isBdHook && hook.name == "pre-commit" {
@@ -194,7 +188,7 @@ func TestInstallGitHooks_ExistingHookBackup(t *testing.T) {
break
}
}
if !hasExisting {
t.Error("should detect existing non-bd hook")
}

View File

@@ -138,15 +138,15 @@ type migrateIssuesParams struct {
}
type migrationPlan struct {
TotalSelected int `json:"total_selected"`
AddedByDependency int `json:"added_by_dependency"`
IncomingEdges int `json:"incoming_edges"`
OutgoingEdges int `json:"outgoing_edges"`
Orphans int `json:"orphans"`
OrphanSamples []string `json:"orphan_samples,omitempty"`
IssueIDs []string `json:"issue_ids"`
From string `json:"from"`
To string `json:"to"`
TotalSelected int `json:"total_selected"`
AddedByDependency int `json:"added_by_dependency"`
IncomingEdges int `json:"incoming_edges"`
OutgoingEdges int `json:"outgoing_edges"`
Orphans int `json:"orphans"`
OrphanSamples []string `json:"orphan_samples,omitempty"`
IssueIDs []string `json:"issue_ids"`
From string `json:"from"`
To string `json:"to"`
}
func executeMigrateIssues(ctx context.Context, p migrateIssuesParams) error {
@@ -186,7 +186,7 @@ func executeMigrateIssues(ctx context.Context, p migrateIssuesParams) error {
}
// Step 4: Check for orphaned dependencies
orphans, err := checkOrphanedDependencies(ctx, db, migrationSet)
orphans, err := checkOrphanedDependencies(ctx, db)
if err != nil {
return fmt.Errorf("failed to check dependencies: %w", err)
}
@@ -207,7 +207,7 @@ func executeMigrateIssues(ctx context.Context, p migrateIssuesParams) error {
if !p.dryRun {
if !p.yes && !jsonOutput {
if !confirmMigration(plan) {
fmt.Println("Migration cancelled")
fmt.Println("Migration canceled")
return nil
}
}
@@ -523,7 +523,7 @@ func countCrossRepoEdges(ctx context.Context, db *sql.DB, migrationSet []string)
}, nil
}
func checkOrphanedDependencies(ctx context.Context, db *sql.DB, migrationSet []string) ([]string, error) {
func checkOrphanedDependencies(ctx context.Context, db *sql.DB) ([]string, error) {
// Check for dependencies referencing non-existent issues
query := `
SELECT DISTINCT d.depends_on_id
@@ -580,7 +580,8 @@ func displayMigrationPlan(plan migrationPlan, dryRun bool) error {
"plan": plan,
"dry_run": dryRun,
}
outputJSON(output); return nil
outputJSON(output)
return nil
}
// Human-readable output