/{website,internal,docs,cmd}: integration tests and more split backend fixes
This commit is contained in:
@@ -216,8 +216,9 @@ func findLocalBeadsDir() string {
|
||||
// findDatabaseInBeadsDir searches for a database file within a .beads directory.
|
||||
// It implements the standard search order:
|
||||
// 1. Check metadata.json first (single source of truth)
|
||||
// - For SQLite backend: returns path to .db file
|
||||
// - For Dolt backend: returns path to dolt/ directory
|
||||
// - For SQLite backend: returns path to .db file
|
||||
// - For Dolt backend: returns path to dolt/ directory
|
||||
//
|
||||
// 2. Fall back to canonical beads.db
|
||||
// 3. Search for *.db files, filtering out backups and vc.db
|
||||
//
|
||||
@@ -231,8 +232,8 @@ func findDatabaseInBeadsDir(beadsDir string, warnOnIssues bool) string {
|
||||
if cfg, err := configfile.Load(beadsDir); err == nil && cfg != nil {
|
||||
backend := cfg.GetBackend()
|
||||
if backend == configfile.BackendDolt {
|
||||
// For Dolt, check if the dolt directory exists
|
||||
doltPath := filepath.Join(beadsDir, "dolt")
|
||||
// For Dolt, check if the configured database directory exists
|
||||
doltPath := cfg.DatabasePath(beadsDir)
|
||||
if info, err := os.Stat(doltPath); err == nil && info.IsDir() {
|
||||
return doltPath
|
||||
}
|
||||
@@ -575,9 +576,9 @@ func FindJSONLPath(dbPath string) string {
|
||||
|
||||
// DatabaseInfo contains information about a discovered beads database
|
||||
type DatabaseInfo struct {
|
||||
Path string // Full path to the .db file
|
||||
BeadsDir string // Parent .beads directory
|
||||
IssueCount int // Number of issues (-1 if unknown)
|
||||
Path string // Full path to the .db file
|
||||
BeadsDir string // Parent .beads directory
|
||||
IssueCount int // Number of issues (-1 if unknown)
|
||||
}
|
||||
|
||||
// findGitRoot returns the root directory of the current git repository,
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const ConfigFileName = "metadata.json"
|
||||
@@ -37,7 +38,7 @@ func ConfigPath(beadsDir string) string {
|
||||
|
||||
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)
|
||||
@@ -49,52 +50,79 @@ func Load(beadsDir string) (*Config, error) {
|
||||
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)
|
||||
backend := c.GetBackend()
|
||||
|
||||
// Treat Database as the on-disk storage location:
|
||||
// - SQLite: filename (default: beads.db)
|
||||
// - Dolt: directory name (default: dolt)
|
||||
//
|
||||
// Backward-compat: early dolt configs wrote "beads.db" even when Backend=dolt.
|
||||
// In that case, treat it as "dolt".
|
||||
if backend == BackendDolt {
|
||||
db := strings.TrimSpace(c.Database)
|
||||
if db == "" || db == "beads.db" {
|
||||
db = "dolt"
|
||||
}
|
||||
if filepath.IsAbs(db) {
|
||||
return db
|
||||
}
|
||||
return filepath.Join(beadsDir, db)
|
||||
}
|
||||
|
||||
// SQLite (default)
|
||||
db := strings.TrimSpace(c.Database)
|
||||
if db == "" {
|
||||
db = "beads.db"
|
||||
}
|
||||
if filepath.IsAbs(db) {
|
||||
return db
|
||||
}
|
||||
return filepath.Join(beadsDir, db)
|
||||
}
|
||||
|
||||
func (c *Config) JSONLPath(beadsDir string) string {
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
|
||||
func TestDefaultConfig(t *testing.T) {
|
||||
cfg := DefaultConfig()
|
||||
|
||||
|
||||
if cfg.Database != "beads.db" {
|
||||
t.Errorf("Database = %q, want beads.db", cfg.Database)
|
||||
}
|
||||
@@ -25,26 +25,26 @@ func TestLoadSaveRoundtrip(t *testing.T) {
|
||||
if err := os.MkdirAll(beadsDir, 0750); err != nil {
|
||||
t.Fatalf("failed to create .beads directory: %v", err)
|
||||
}
|
||||
|
||||
|
||||
cfg := DefaultConfig()
|
||||
|
||||
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatalf("Save() failed: %v", err)
|
||||
}
|
||||
|
||||
|
||||
loaded, err := Load(beadsDir)
|
||||
if err != nil {
|
||||
t.Fatalf("Load() failed: %v", err)
|
||||
}
|
||||
|
||||
|
||||
if loaded == nil {
|
||||
t.Fatal("Load() returned nil config")
|
||||
}
|
||||
|
||||
|
||||
if loaded.Database != cfg.Database {
|
||||
t.Errorf("Database = %q, want %q", loaded.Database, cfg.Database)
|
||||
}
|
||||
|
||||
|
||||
if loaded.JSONLExport != cfg.JSONLExport {
|
||||
t.Errorf("JSONLExport = %q, want %q", loaded.JSONLExport, cfg.JSONLExport)
|
||||
}
|
||||
@@ -52,12 +52,12 @@ func TestLoadSaveRoundtrip(t *testing.T) {
|
||||
|
||||
func TestLoadNonexistent(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
|
||||
cfg, err := Load(tmpDir)
|
||||
if err != nil {
|
||||
t.Fatalf("Load() returned error for nonexistent config: %v", err)
|
||||
}
|
||||
|
||||
|
||||
if cfg != nil {
|
||||
t.Errorf("Load() = %v, want nil for nonexistent config", cfg)
|
||||
}
|
||||
@@ -66,22 +66,44 @@ func TestLoadNonexistent(t *testing.T) {
|
||||
func TestDatabasePath(t *testing.T) {
|
||||
beadsDir := "/home/user/project/.beads"
|
||||
cfg := &Config{Database: "beads.db"}
|
||||
|
||||
|
||||
got := cfg.DatabasePath(beadsDir)
|
||||
want := filepath.Join(beadsDir, "beads.db")
|
||||
|
||||
|
||||
if got != want {
|
||||
t.Errorf("DatabasePath() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDatabasePath_Dolt(t *testing.T) {
|
||||
beadsDir := "/home/user/project/.beads"
|
||||
|
||||
t.Run("explicit dolt dir", func(t *testing.T) {
|
||||
cfg := &Config{Database: "dolt", Backend: BackendDolt}
|
||||
got := cfg.DatabasePath(beadsDir)
|
||||
want := filepath.Join(beadsDir, "dolt")
|
||||
if got != want {
|
||||
t.Errorf("DatabasePath() = %q, want %q", got, want)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("backward compat: dolt backend with beads.db field", func(t *testing.T) {
|
||||
cfg := &Config{Database: "beads.db", Backend: BackendDolt}
|
||||
got := cfg.DatabasePath(beadsDir)
|
||||
want := filepath.Join(beadsDir, "dolt")
|
||||
if got != want {
|
||||
t.Errorf("DatabasePath() = %q, want %q", got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestJSONLPath(t *testing.T) {
|
||||
beadsDir := "/home/user/project/.beads"
|
||||
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
cfg *Config
|
||||
want string
|
||||
name string
|
||||
cfg *Config
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "default",
|
||||
@@ -99,7 +121,7 @@ func TestJSONLPath(t *testing.T) {
|
||||
want: filepath.Join(beadsDir, "issues.jsonl"),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := tt.cfg.JSONLPath(beadsDir)
|
||||
@@ -122,9 +144,9 @@ func TestConfigPath(t *testing.T) {
|
||||
|
||||
func TestGetDeletionsRetentionDays(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
cfg *Config
|
||||
want int
|
||||
name string
|
||||
cfg *Config
|
||||
want int
|
||||
}{
|
||||
{
|
||||
name: "zero uses default",
|
||||
|
||||
+24
-2
@@ -122,11 +122,33 @@ func GetGitCommonDir() (string, error) {
|
||||
// and live in the common git directory (e.g., /repo/.git/hooks), not in
|
||||
// the worktree-specific directory (e.g., /repo/.git/worktrees/feature/hooks).
|
||||
func GetGitHooksDir() (string, error) {
|
||||
commonDir, err := GetGitCommonDir()
|
||||
ctx, err := getGitContext()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Join(commonDir, "hooks"), nil
|
||||
|
||||
// Respect core.hooksPath if configured.
|
||||
// This is used by beads' Dolt backend (hooks installed to .beads/hooks/).
|
||||
cmd := exec.Command("git", "config", "--get", "core.hooksPath")
|
||||
cmd.Dir = ctx.repoRoot
|
||||
if out, err := cmd.Output(); err == nil {
|
||||
hooksPath := strings.TrimSpace(string(out))
|
||||
if hooksPath != "" {
|
||||
if filepath.IsAbs(hooksPath) {
|
||||
return hooksPath, nil
|
||||
}
|
||||
// Git treats relative core.hooksPath as relative to the repo root in common usage.
|
||||
// (e.g., ".beads/hooks", ".githooks").
|
||||
p := filepath.Join(ctx.repoRoot, hooksPath)
|
||||
if abs, err := filepath.Abs(p); err == nil {
|
||||
return abs, nil
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Default: hooks are stored in the common git directory.
|
||||
return filepath.Join(ctx.commonDir, "hooks"), nil
|
||||
}
|
||||
|
||||
// GetGitRefsDir returns the path to the Git refs directory.
|
||||
|
||||
@@ -4,7 +4,6 @@ package factory
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
@@ -84,9 +83,7 @@ func NewFromConfigWithOptions(ctx context.Context, beadsDir string, opts Options
|
||||
case configfile.BackendSQLite:
|
||||
return NewWithOptions(ctx, backend, cfg.DatabasePath(beadsDir), opts)
|
||||
case configfile.BackendDolt:
|
||||
// For Dolt, use a subdirectory to store the Dolt database
|
||||
doltPath := filepath.Join(beadsDir, "dolt")
|
||||
return NewWithOptions(ctx, backend, doltPath, opts)
|
||||
return NewWithOptions(ctx, backend, cfg.DatabasePath(beadsDir), opts)
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown storage backend in config: %s", backend)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user