Skip to content

Commit 8bb4ffa

Browse files
Store gob cache files under specific directory for easy upgrade in future.
1 parent 4f3dbc7 commit 8bb4ffa

3 files changed

Lines changed: 61 additions & 45 deletions

File tree

cache.go

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,30 @@ func warnFail(cb cache.Backend, err error) cache.Cache {
1919
}
2020

2121
func getCache() cache.Cache {
22-
var cb cache.Backend = cacheBackends.NoopBackend{}
23-
24-
if cache.IsCachingEnabled() {
25-
gitRootPath, err := git.GetRoot()
26-
if err != nil {
27-
return warnFail(cb, err)
28-
}
29-
30-
p, err := cacheBackends.GobCachePathXDG(gitRootPath)
31-
if err != nil {
32-
return warnFail(cb, err)
33-
}
34-
35-
err = os.MkdirAll(filepath.Dir(p), 0o700)
36-
if err != nil {
37-
return warnFail(cb, err)
38-
}
39-
40-
logger().Debug("cache initialized", "path", p)
41-
cb = &cacheBackends.GobBackend{Path: p}
22+
var fallback cache.Backend = cacheBackends.NoopBackend{}
23+
24+
if !cache.IsCachingEnabled() {
25+
return cache.NewCache(fallback)
4226
}
4327

44-
return cache.NewCache(cb)
28+
cacheStorageDir, err := cache.CacheStorageDir(
29+
cacheBackends.GobBackendName,
30+
)
31+
if err != nil {
32+
return warnFail(fallback, err)
33+
}
34+
35+
gitRootPath, err := git.GetRoot()
36+
if err != nil {
37+
return warnFail(fallback, err)
38+
}
39+
40+
p := cacheBackends.GobCachePath(cacheStorageDir, gitRootPath)
41+
err = os.MkdirAll(filepath.Dir(p), 0o700)
42+
if err != nil {
43+
return warnFail(fallback, err)
44+
}
45+
46+
logger().Debug("cache initialized", "path", p)
47+
return cache.NewCache(&cacheBackends.GobBackend{Path: p})
4548
}

internal/cache/backends/gob.go

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"io"
1313
"io/fs"
1414
"os"
15-
"os/user"
1615
"path/filepath"
1716

1817
"github.com/sinclairtarget/git-who/internal/cache"
@@ -40,8 +39,10 @@ type GobBackend struct {
4039
isDirty bool
4140
}
4241

42+
const GobBackendName string = "gob"
43+
4344
func (b *GobBackend) Name() string {
44-
return "gob"
45+
return GobBackendName
4546
}
4647

4748
func (b *GobBackend) compressedPath() string {
@@ -284,33 +285,13 @@ func (b *GobBackend) Clear() error {
284285
return nil
285286
}
286287

287-
// Returns the absolute path at which we should store the Gob data.
288-
//
289-
// Tries to store it under the XDG_CACHE_HOME dir.
290-
func GobCachePathXDG(gitRootPath string) (string, error) {
288+
func GobCachePath(prefix string, gitRootPath string) string {
291289
// Filename includes hash of path to repo so we don't collide with other
292290
// git-who caches for other repos.
293291
h := fnv.New32()
294292
h.Write([]byte(gitRootPath))
295293

296294
base := filepath.Base(gitRootPath)
297295
filename := fmt.Sprintf("%s-%x.gobs", base, h.Sum32())
298-
299-
usr, err := user.Current()
300-
if err != nil {
301-
return "", err
302-
}
303-
304-
cacheHome := filepath.Join(usr.HomeDir, ".cache")
305-
if len(os.Getenv("XDG_CACHE_HOME")) > 0 {
306-
cacheHome = os.Getenv("XDG_CACHE_HOME")
307-
}
308-
309-
p := filepath.Join(cacheHome, "git-who", filename)
310-
absP, err := filepath.Abs(p)
311-
if err != nil {
312-
return "", err
313-
}
314-
315-
return absP, nil
296+
return filepath.Join(prefix, filename)
316297
}

internal/cache/cache.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"iter"
77
"os"
8+
"os/user"
9+
"path/filepath"
810
"slices"
911
"time"
1012

@@ -159,3 +161,33 @@ func (c *Cache) Clear() error {
159161
logger().Debug("cache clear")
160162
return nil
161163
}
164+
165+
// Returns the absolute path at which we should store data for a given cache
166+
// backend.
167+
//
168+
// Tries to store it under the XDG_CACHE_HOME dir.
169+
func CacheStorageDir(name string) (_ string, err error) {
170+
defer func() {
171+
if err != nil {
172+
err = fmt.Errorf("failed to determine cache storage path: %w", err)
173+
}
174+
}()
175+
176+
usr, err := user.Current()
177+
if err != nil {
178+
return "", err
179+
}
180+
181+
cacheHome := filepath.Join(usr.HomeDir, ".cache")
182+
if len(os.Getenv("XDG_CACHE_HOME")) > 0 {
183+
cacheHome = os.Getenv("XDG_CACHE_HOME")
184+
}
185+
186+
p := filepath.Join(cacheHome, "git-who", name)
187+
absP, err := filepath.Abs(p)
188+
if err != nil {
189+
return "", err
190+
}
191+
192+
return absP, nil
193+
}

0 commit comments

Comments
 (0)