refactor(massive simplification of vcs now that we're using ndjson natively):

This commit is contained in:
Lewis Wynne 2026-02-11 00:28:56 +00:00
parent 84c55311d1
commit cb441b112c
4 changed files with 61 additions and 203 deletions

View file

@ -9,22 +9,10 @@ import (
"path/filepath"
"strconv"
"strings"
gap "github.com/muesli/go-app-paths"
)
const storeDirName = "stores"
func vcsRepoRoot() (string, error) {
scope := gap.NewVendorScope(gap.User, "pda", "vcs")
dir, err := scope.DataPath("")
if err != nil {
return "", err
}
if err := os.MkdirAll(dir, 0o750); err != nil {
return "", err
}
return dir, nil
return (&Store{}).path()
}
func ensureVCSInitialized() (string, error) {
@ -47,10 +35,8 @@ func writeGitignore(repoDir string) error {
content := strings.Join([]string{
"# generated by pda",
"*",
"!/",
"!.gitignore",
"!" + storeDirName + "/",
"!" + storeDirName + "/*",
"!*.ndjson",
"",
}, "\n")
if err := os.WriteFile(path, []byte(content), 0o640); err != nil {
@ -66,71 +52,6 @@ func writeGitignore(repoDir string) error {
return nil
}
// snapshotDB copies a store's .ndjson file into the VCS directory.
func snapshotDB(store *Store, repoDir, db string) error {
targetDir := filepath.Join(repoDir, storeDirName)
if err := os.MkdirAll(targetDir, 0o750); err != nil {
return err
}
srcPath, err := store.storePath(db)
if err != nil {
return err
}
data, err := os.ReadFile(srcPath)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
target := filepath.Join(targetDir, db+".ndjson")
return os.WriteFile(target, data, 0o640)
}
// exportAllStores copies every store's .ndjson file to repoDir/stores
// and removes stale snapshot files for deleted stores.
func exportAllStores(store *Store, repoDir string) error {
stores, err := store.AllStores()
if err != nil {
return err
}
targetDir := filepath.Join(repoDir, storeDirName)
if err := os.MkdirAll(targetDir, 0o750); err != nil {
return err
}
current := make(map[string]struct{})
for _, db := range stores {
current[db] = struct{}{}
if err := snapshotDB(store, repoDir, db); err != nil {
return fmt.Errorf("snapshot %q: %w", db, err)
}
}
entries, err := os.ReadDir(targetDir)
if err != nil {
return err
}
for _, e := range entries {
if e.IsDir() || filepath.Ext(e.Name()) != ".ndjson" {
continue
}
dbName := strings.TrimSuffix(e.Name(), ".ndjson")
if _, ok := current[dbName]; ok {
continue
}
if err := os.Remove(filepath.Join(targetDir, e.Name())); err != nil && !os.IsNotExist(err) {
return err
}
}
return nil
}
func runGit(dir string, args ...string) error {
cmd := exec.Command("git", args...)
cmd.Dir = dir
@ -288,66 +209,6 @@ func currentBranch(dir string) (string, error) {
return branch, nil
}
// restoreAllSnapshots copies .ndjson files from VCS snapshot dir into store paths,
// and removes local stores that are not in the snapshot.
func restoreAllSnapshots(store *Store, repoDir string) error {
targetDir := filepath.Join(repoDir, storeDirName)
entries, err := os.ReadDir(targetDir)
if err != nil {
if os.IsNotExist(err) {
fmt.Printf("no existing stores found, not restoring")
return nil
}
return err
}
snapshotDBs := make(map[string]struct{})
for _, e := range entries {
if e.IsDir() {
continue
}
if filepath.Ext(e.Name()) != ".ndjson" {
continue
}
dbName := strings.TrimSuffix(e.Name(), ".ndjson")
snapshotDBs[dbName] = struct{}{}
srcPath := filepath.Join(targetDir, e.Name())
data, err := os.ReadFile(srcPath)
if err != nil {
return fmt.Errorf("restore %q: %w", dbName, err)
}
dstPath, err := store.storePath(dbName)
if err != nil {
return fmt.Errorf("restore %q: %w", dbName, err)
}
if err := os.WriteFile(dstPath, data, 0o640); err != nil {
return fmt.Errorf("restore %q: %w", dbName, err)
}
}
localDBs, err := store.AllStores()
if err != nil {
return err
}
for _, db := range localDBs {
if _, ok := snapshotDBs[db]; ok {
continue
}
p, err := store.storePath(db)
if err != nil {
return err
}
if err := os.Remove(p); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("remove store '%s': %w", db, err)
}
}
return nil
}
func wipeAllStores(store *Store) error {
dbs, err := store.AllStores()
if err != nil {