2018-07-25 22:26:25 +03:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
|
|
|
)
|
|
|
|
|
2018-08-21 19:46:53 +03:00
|
|
|
const lockfile = "lock"
|
2022-05-31 22:38:28 +03:00
|
|
|
const defaultRepoName = "__default"
|
2018-07-25 22:26:25 +03:00
|
|
|
|
2019-01-19 18:01:06 +03:00
|
|
|
// MultiRepoCache is the root cache, holding multiple RepoCache.
|
2018-09-02 16:45:14 +03:00
|
|
|
type MultiRepoCache struct {
|
2018-08-23 22:24:57 +03:00
|
|
|
repos map[string]*RepoCache
|
2018-07-25 22:26:25 +03:00
|
|
|
}
|
|
|
|
|
2020-06-21 23:12:04 +03:00
|
|
|
func NewMultiRepoCache() *MultiRepoCache {
|
|
|
|
return &MultiRepoCache{
|
2018-08-23 22:24:57 +03:00
|
|
|
repos: make(map[string]*RepoCache),
|
2018-07-25 22:26:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-21 19:46:53 +03:00
|
|
|
// RegisterRepository register a named repository. Use this for multi-repo setup
|
2020-06-21 23:12:04 +03:00
|
|
|
func (c *MultiRepoCache) RegisterRepository(ref string, repo repository.ClockedRepo) (*RepoCache, error) {
|
2018-08-23 22:24:57 +03:00
|
|
|
r, err := NewRepoCache(repo)
|
|
|
|
if err != nil {
|
2020-06-21 23:12:04 +03:00
|
|
|
return nil, err
|
2018-08-23 22:24:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
c.repos[ref] = r
|
2020-06-21 23:12:04 +03:00
|
|
|
return r, nil
|
2018-07-25 22:26:25 +03:00
|
|
|
}
|
|
|
|
|
2018-08-21 19:46:53 +03:00
|
|
|
// RegisterDefaultRepository register a unnamed repository. Use this for mono-repo setup
|
2020-06-21 23:12:04 +03:00
|
|
|
func (c *MultiRepoCache) RegisterDefaultRepository(repo repository.ClockedRepo) (*RepoCache, error) {
|
2018-08-23 22:24:57 +03:00
|
|
|
r, err := NewRepoCache(repo)
|
|
|
|
if err != nil {
|
2020-06-21 23:12:04 +03:00
|
|
|
return nil, err
|
2018-08-23 22:24:57 +03:00
|
|
|
}
|
|
|
|
|
2022-05-31 22:38:28 +03:00
|
|
|
c.repos[defaultRepoName] = r
|
2020-06-21 23:12:04 +03:00
|
|
|
return r, nil
|
2018-07-25 22:26:25 +03:00
|
|
|
}
|
|
|
|
|
2019-06-16 22:29:49 +03:00
|
|
|
// DefaultRepo retrieve the default repository
|
2018-09-02 16:45:14 +03:00
|
|
|
func (c *MultiRepoCache) DefaultRepo() (*RepoCache, error) {
|
2018-08-21 19:46:53 +03:00
|
|
|
if len(c.repos) != 1 {
|
|
|
|
return nil, fmt.Errorf("repository is not unique")
|
2018-08-02 15:56:50 +03:00
|
|
|
}
|
|
|
|
|
2018-08-21 19:46:53 +03:00
|
|
|
for _, r := range c.repos {
|
|
|
|
return r, nil
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
|
|
|
|
2018-08-21 19:46:53 +03:00
|
|
|
panic("unreachable")
|
2018-08-03 00:37:49 +03:00
|
|
|
}
|
|
|
|
|
2019-06-23 22:28:01 +03:00
|
|
|
// ResolveRepo retrieve a repository with a reference
|
2018-09-02 16:45:14 +03:00
|
|
|
func (c *MultiRepoCache) ResolveRepo(ref string) (*RepoCache, error) {
|
2018-08-21 19:46:53 +03:00
|
|
|
r, ok := c.repos[ref]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unknown repo")
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
2018-08-21 19:46:53 +03:00
|
|
|
return r, nil
|
2018-08-01 22:57:12 +03:00
|
|
|
}
|
|
|
|
|
2018-08-23 22:24:57 +03:00
|
|
|
// Close will do anything that is needed to close the cache properly
|
2018-09-02 16:45:14 +03:00
|
|
|
func (c *MultiRepoCache) Close() error {
|
2018-08-21 19:46:53 +03:00
|
|
|
for _, cachedRepo := range c.repos {
|
2018-08-31 14:18:03 +03:00
|
|
|
err := cachedRepo.Close()
|
2018-08-21 19:46:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-08-02 17:35:13 +03:00
|
|
|
return nil
|
2018-07-25 22:26:25 +03:00
|
|
|
}
|