## Summary When metadata.json gets deleted (git clean, merge conflict, rebase), the version tracking code auto-recreates it using DefaultConfig() which hardcoded jsonl_export to 'issues.jsonl'. But many repos (including beads itself) use 'beads.jsonl', causing a mismatch between config and actual JSONL file. ## Changes 1. **bd doctor --fix auto-detection** (cmd/bd/doctor/fix/database_config.go) - New DatabaseConfig() fix function that auto-detects actual JSONL file - Prefers beads.jsonl over issues.jsonl (canonical name) - Skips backup files and merge artifacts - Wired into doctor.go applyFixes() 2. **Version tracking auto-detection** (cmd/bd/version_tracking.go) - trackBdVersion() now scans for existing JSONL files before defaulting - Prevents mismatches when metadata.json gets recreated - Added findActualJSONLFile() helper function 3. **Canonical default name** (internal/configfile/configfile.go) - DefaultConfig() changed from issues.jsonl to beads.jsonl - Aligns with canonical naming convention 4. **FindJSONLPath preference** (internal/beads/beads.go) - Now prefers beads.jsonl over issues.jsonl when scanning - Default changed from issues.jsonl to beads.jsonl 5. **Test coverage** - Added comprehensive tests for DatabaseConfig fix - Updated configfile tests for new default - Verified backup file skipping logic ## Testing - All existing tests pass - New tests verify auto-fix behavior - Integration tested with simulated mismatches Closes: bd-afd
97 lines
2.3 KiB
Go
97 lines
2.3 KiB
Go
package configfile
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
const ConfigFileName = "metadata.json"
|
|
|
|
type Config struct {
|
|
Database string `json:"database"`
|
|
JSONLExport string `json:"jsonl_export,omitempty"`
|
|
LastBdVersion string `json:"last_bd_version,omitempty"`
|
|
}
|
|
|
|
func DefaultConfig() *Config {
|
|
return &Config{
|
|
Database: "beads.db",
|
|
JSONLExport: "beads.jsonl", // Default to canonical name (was issues.jsonl)
|
|
}
|
|
}
|
|
|
|
func ConfigPath(beadsDir string) string {
|
|
return filepath.Join(beadsDir, ConfigFileName)
|
|
}
|
|
|
|
func Load(beadsDir string) (*Config, error) {
|
|
configPath := ConfigPath(beadsDir)
|
|
|
|
data, err := os.ReadFile(configPath) // #nosec G304 - controlled path from config
|
|
if os.IsNotExist(err) {
|
|
// Try legacy config.json location (migration path)
|
|
legacyPath := filepath.Join(beadsDir, "config.json")
|
|
data, err = os.ReadFile(legacyPath) // #nosec G304 - controlled path from config
|
|
if os.IsNotExist(err) {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, fmt.Errorf("reading legacy config: %w", err)
|
|
}
|
|
|
|
// Migrate: parse legacy config, save as metadata.json, remove old file
|
|
var cfg Config
|
|
if err := json.Unmarshal(data, &cfg); err != nil {
|
|
return nil, fmt.Errorf("parsing legacy config: %w", err)
|
|
}
|
|
|
|
// Save to new location
|
|
if err := cfg.Save(beadsDir); err != nil {
|
|
return nil, fmt.Errorf("migrating config to metadata.json: %w", err)
|
|
}
|
|
|
|
// Remove legacy file (best effort)
|
|
_ = os.Remove(legacyPath)
|
|
|
|
return &cfg, nil
|
|
}
|
|
if err != nil {
|
|
return nil, fmt.Errorf("reading config: %w", err)
|
|
}
|
|
|
|
var cfg Config
|
|
if err := json.Unmarshal(data, &cfg); err != nil {
|
|
return nil, fmt.Errorf("parsing config: %w", err)
|
|
}
|
|
|
|
return &cfg, nil
|
|
}
|
|
|
|
func (c *Config) Save(beadsDir string) error {
|
|
configPath := ConfigPath(beadsDir)
|
|
|
|
data, err := json.MarshalIndent(c, "", " ")
|
|
if err != nil {
|
|
return fmt.Errorf("marshaling config: %w", err)
|
|
}
|
|
|
|
if err := os.WriteFile(configPath, data, 0600); err != nil {
|
|
return fmt.Errorf("writing config: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *Config) DatabasePath(beadsDir string) string {
|
|
return filepath.Join(beadsDir, c.Database)
|
|
}
|
|
|
|
func (c *Config) JSONLPath(beadsDir string) string {
|
|
if c.JSONLExport == "" {
|
|
return filepath.Join(beadsDir, "issues.jsonl")
|
|
}
|
|
return filepath.Join(beadsDir, c.JSONLExport)
|
|
}
|