refactor: deduplicate FindJSONLInDir function (bd-8a5)
Extract shared JSONL file discovery logic to internal/utils/path.go. Both autoimport and beads packages now use this shared implementation. Changes: - Add utils.FindJSONLInDir with common logic - Update autoimport.go to use utils.FindJSONLInDir - Update beads.go to delegate to utils.FindJSONLInDir - Update server_export_import_auto.go to use utils.FindJSONLInDir - Move FindJSONLInDir test to utils/path_test.go - Fix pre-existing duplicate countIssuesInJSONLFile in init.go 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -5,6 +5,56 @@ import (
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// FindJSONLInDir finds the JSONL file in the given .beads directory.
|
||||
// It prefers issues.jsonl over other .jsonl files to prevent accidentally
|
||||
// reading/writing to deletions.jsonl or merge artifacts (bd-tqo fix).
|
||||
// Always returns a path (defaults to issues.jsonl if nothing suitable found).
|
||||
//
|
||||
// Search order:
|
||||
// 1. issues.jsonl (canonical name)
|
||||
// 2. beads.jsonl (legacy support)
|
||||
// 3. Any other .jsonl file except deletions/merge artifacts
|
||||
// 4. Default to issues.jsonl
|
||||
func FindJSONLInDir(dbDir string) string {
|
||||
pattern := filepath.Join(dbDir, "*.jsonl")
|
||||
matches, err := filepath.Glob(pattern)
|
||||
if err != nil || len(matches) == 0 {
|
||||
// Default to issues.jsonl if glob fails or no matches
|
||||
return filepath.Join(dbDir, "issues.jsonl")
|
||||
}
|
||||
|
||||
// Prefer issues.jsonl over other .jsonl files (bd-tqo fix)
|
||||
// This prevents accidentally using deletions.jsonl or merge artifacts
|
||||
for _, match := range matches {
|
||||
if filepath.Base(match) == "issues.jsonl" {
|
||||
return match
|
||||
}
|
||||
}
|
||||
|
||||
// Fall back to beads.jsonl for legacy support
|
||||
for _, match := range matches {
|
||||
if filepath.Base(match) == "beads.jsonl" {
|
||||
return match
|
||||
}
|
||||
}
|
||||
|
||||
// Last resort: use first match (but skip deletions.jsonl and merge artifacts)
|
||||
for _, match := range matches {
|
||||
base := filepath.Base(match)
|
||||
// Skip deletions manifest and merge artifacts
|
||||
if base == "deletions.jsonl" ||
|
||||
base == "beads.base.jsonl" ||
|
||||
base == "beads.left.jsonl" ||
|
||||
base == "beads.right.jsonl" {
|
||||
continue
|
||||
}
|
||||
return match
|
||||
}
|
||||
|
||||
// If only deletions/merge files exist, default to issues.jsonl
|
||||
return filepath.Join(dbDir, "issues.jsonl")
|
||||
}
|
||||
|
||||
// CanonicalizePath converts a path to its canonical form by:
|
||||
// 1. Converting to absolute path
|
||||
// 2. Resolving symlinks
|
||||
|
||||
@@ -72,6 +72,82 @@ func TestCanonicalizePath(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestFindJSONLInDir tests that FindJSONLInDir correctly prefers issues.jsonl
|
||||
// and avoids deletions.jsonl and merge artifacts (bd-tqo fix)
|
||||
func TestFindJSONLInDir(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
files []string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "only issues.jsonl",
|
||||
files: []string{"issues.jsonl"},
|
||||
expected: "issues.jsonl",
|
||||
},
|
||||
{
|
||||
name: "issues.jsonl and deletions.jsonl - prefers issues",
|
||||
files: []string{"deletions.jsonl", "issues.jsonl"},
|
||||
expected: "issues.jsonl",
|
||||
},
|
||||
{
|
||||
name: "issues.jsonl with merge artifacts - prefers issues",
|
||||
files: []string{"beads.base.jsonl", "beads.left.jsonl", "beads.right.jsonl", "issues.jsonl"},
|
||||
expected: "issues.jsonl",
|
||||
},
|
||||
{
|
||||
name: "beads.jsonl as legacy fallback",
|
||||
files: []string{"beads.jsonl"},
|
||||
expected: "beads.jsonl",
|
||||
},
|
||||
{
|
||||
name: "issues.jsonl preferred over beads.jsonl",
|
||||
files: []string{"beads.jsonl", "issues.jsonl"},
|
||||
expected: "issues.jsonl",
|
||||
},
|
||||
{
|
||||
name: "only deletions.jsonl - returns default issues.jsonl",
|
||||
files: []string{"deletions.jsonl"},
|
||||
expected: "issues.jsonl",
|
||||
},
|
||||
{
|
||||
name: "only merge artifacts - returns default issues.jsonl",
|
||||
files: []string{"beads.base.jsonl", "beads.left.jsonl", "beads.right.jsonl"},
|
||||
expected: "issues.jsonl",
|
||||
},
|
||||
{
|
||||
name: "no files - returns default issues.jsonl",
|
||||
files: []string{},
|
||||
expected: "issues.jsonl",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tmpDir, err := os.MkdirTemp("", "bd-findjsonl-test-*")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
// Create test files
|
||||
for _, file := range tt.files {
|
||||
path := filepath.Join(tmpDir, file)
|
||||
if err := os.WriteFile(path, []byte("{}"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
result := FindJSONLInDir(tmpDir)
|
||||
got := filepath.Base(result)
|
||||
|
||||
if got != tt.expected {
|
||||
t.Errorf("FindJSONLInDir() = %q, want %q", got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCanonicalizePathSymlink(t *testing.T) {
|
||||
// Create a temporary directory
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
Reference in New Issue
Block a user