From 0ac8ccac81eeb78aeac5dc13c62b079d6a561709 Mon Sep 17 00:00:00 2001 From: Arsen Musayelyan Date: Wed, 30 Nov 2022 22:38:22 -0800 Subject: [PATCH] Handle the possibility of the DB not existing on startup --- internal/config/dirs.go | 7 +++++++ internal/repos/pull.go | 19 +++++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/internal/config/dirs.go b/internal/config/dirs.go index 2a917a7..d0c5867 100644 --- a/internal/config/dirs.go +++ b/internal/config/dirs.go @@ -17,6 +17,10 @@ var ( DBPath string ) +// DBPresent is true if the database +// was present when LURE was started +var DBPresent bool + func init() { cfgDir, err := os.UserConfigDir() if err != nil { @@ -66,4 +70,7 @@ func init() { } DBPath = filepath.Join(CacheDir, "db") + + _, err = os.ReadDir(DBPath) + DBPresent = err == nil } diff --git a/internal/repos/pull.go b/internal/repos/pull.go index 06df81f..cb3af86 100644 --- a/internal/repos/pull.go +++ b/internal/repos/pull.go @@ -68,15 +68,26 @@ func Pull(ctx context.Context, gdb *genji.DB, repos []types.Repo) error { } repoFS = w.Filesystem - if !errors.Is(err, git.NoErrAlreadyUpToDate) { + // Make sure the DB is created even if the repo is up to date + if !errors.Is(err, git.NoErrAlreadyUpToDate) || !config.DBPresent { new, err := r.Head() if err != nil { return err } - err = processRepoChanges(ctx, repo, r, old, new, gdb) - if err != nil { - return err + // If the DB was not present at startup, that means it's + // empty. In this case, we need to update the DB fully + // rather than just incrementally. + if config.DBPresent { + err = processRepoChanges(ctx, repo, r, old, new, gdb) + if err != nil { + return err + } + } else { + err = processRepoFull(ctx, repo, repoDir, gdb) + if err != nil { + return err + } } } } else {