fix(init,import): preserve metadata.json and improve prefix detection

- bd init now preserves existing metadata.json settings instead of
  overwriting with defaults (bd-zai)
- bd init detects existing JSONL filename (beads.jsonl vs issues.jsonl)
  when creating new metadata.json
- bd import now correctly reports prefix source ("issues" vs "directory")
- bd import avoids using ".beads" as prefix when run from inside .beads/

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Steve Yegge
2025-11-26 22:57:17 -08:00
parent dfc68ed8f6
commit 6294ef0cc6
2 changed files with 35 additions and 5 deletions

View File

@@ -200,20 +200,29 @@ NOTE: Import requires direct database access and does not work with daemon mode.
}
// Check if database needs initialization (prefix not set)
// Detect prefix from the imported issues
// Detect prefix from the imported issues (bd-8an fix)
initCtx := rootCtx
configuredPrefix, err2 := store.GetConfig(initCtx, "issue_prefix")
if err2 != nil || strings.TrimSpace(configuredPrefix) == "" {
// Database exists but not initialized - detect prefix from issues
detectedPrefix := detectPrefixFromIssues(allIssues)
prefixSource := "issues"
if detectedPrefix == "" {
// No issues to import or couldn't detect prefix, use directory name
// But avoid using ".beads" as prefix - go up one level
cwd, err := os.Getwd()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: failed to get current directory: %v\n", err)
os.Exit(1)
}
detectedPrefix = filepath.Base(cwd)
dirName := filepath.Base(cwd)
if dirName == ".beads" || dirName == "beads" {
// Running from inside .beads/ - use parent directory
detectedPrefix = filepath.Base(filepath.Dir(cwd))
} else {
detectedPrefix = dirName
}
prefixSource = "directory"
}
detectedPrefix = strings.TrimRight(detectedPrefix, "-")
@@ -222,7 +231,7 @@ NOTE: Import requires direct database access and does not work with daemon mode.
os.Exit(1)
}
fmt.Fprintf(os.Stderr, "✓ Initialized database with prefix '%s' (detected from issues)\n", detectedPrefix)
fmt.Fprintf(os.Stderr, "✓ Initialized database with prefix '%s' (detected from %s)\n", detectedPrefix, prefixSource)
}
// Phase 2: Use shared import logic

View File

@@ -294,9 +294,30 @@ With --stealth: configures global git settings for invisible beads usage:
}
}
// Create metadata.json for database metadata
// Create or preserve metadata.json for database metadata (bd-zai fix)
if useLocalBeads {
cfg := configfile.DefaultConfig()
// First, check if metadata.json already exists
existingCfg, err := configfile.Load(localBeadsDir)
if err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to load existing metadata.json: %v\n", err)
}
var cfg *configfile.Config
if existingCfg != nil {
// Preserve existing config
cfg = existingCfg
} else {
// Create new config, detecting JSONL filename from existing files
cfg = configfile.DefaultConfig()
// Check if beads.jsonl exists but issues.jsonl doesn't (legacy)
issuesPath := filepath.Join(localBeadsDir, "issues.jsonl")
beadsPath := filepath.Join(localBeadsDir, "beads.jsonl")
if _, err := os.Stat(beadsPath); err == nil {
if _, err := os.Stat(issuesPath); os.IsNotExist(err) {
cfg.JSONLExport = "beads.jsonl" // Legacy filename
}
}
}
if err := cfg.Save(localBeadsDir); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to create metadata.json: %v\n", err)
// Non-fatal - continue anyway