1package cache
2
3import (
4 "fmt"
5
6 "github.com/MichaelMure/git-bug/repository"
7)
8
9const lockfile = "lock"
10const excerptsFile = "excerpts"
11
12type RootCache struct {
13 repos map[string]*RepoCache
14}
15
16func NewCache() RootCache {
17 return RootCache{
18 repos: make(map[string]*RepoCache),
19 }
20}
21
22// RegisterRepository register a named repository. Use this for multi-repo setup
23func (c *RootCache) RegisterRepository(ref string, repo repository.Repo) error {
24 r, err := NewRepoCache(repo)
25 if err != nil {
26 return err
27 }
28
29 c.repos[ref] = r
30 return nil
31}
32
33// RegisterDefaultRepository register a unnamed repository. Use this for mono-repo setup
34func (c *RootCache) RegisterDefaultRepository(repo repository.Repo) error {
35 r, err := NewRepoCache(repo)
36 if err != nil {
37 return err
38 }
39
40 c.repos[""] = r
41 return nil
42}
43
44// ResolveRepo retrieve a repository by name
45func (c *RootCache) DefaultRepo() (*RepoCache, error) {
46 if len(c.repos) != 1 {
47 return nil, fmt.Errorf("repository is not unique")
48 }
49
50 for _, r := range c.repos {
51 return r, nil
52 }
53
54 panic("unreachable")
55}
56
57// DefaultRepo retrieve the default repository
58func (c *RootCache) ResolveRepo(ref string) (*RepoCache, error) {
59 r, ok := c.repos[ref]
60 if !ok {
61 return nil, fmt.Errorf("unknown repo")
62 }
63 return r, nil
64}
65
66// Close will do anything that is needed to close the cache properly
67func (c *RootCache) Close() error {
68 for _, cachedRepo := range c.repos {
69 err := cachedRepo.Close()
70 if err != nil {
71 return err
72 }
73 }
74 return nil
75}