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"
|
2018-07-25 22:26:25 +03:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-09-02 16:45:14 +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
|
2018-09-21 19:18:51 +03:00
|
|
|
func (c *MultiRepoCache) RegisterRepository(ref string, repo repository.ClockedRepo) error {
|
2018-08-23 22:24:57 +03:00
|
|
|
r, err := NewRepoCache(repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.repos[ref] = r
|
2018-08-21 19:46:53 +03:00
|
|
|
return 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
|
2018-09-21 19:18:51 +03:00
|
|
|
func (c *MultiRepoCache) RegisterDefaultRepository(repo repository.ClockedRepo) error {
|
2018-08-23 22:24:57 +03:00
|
|
|
r, err := NewRepoCache(repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.repos[""] = r
|
2018-08-21 19:46:53 +03:00
|
|
|
return nil
|
2018-07-25 22:26:25 +03:00
|
|
|
}
|
|
|
|
|
2018-08-21 19:46:53 +03:00
|
|
|
// ResolveRepo retrieve a repository by name
|
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
|
|
|
}
|
|
|
|
|
2018-08-21 19:46:53 +03:00
|
|
|
// DefaultRepo retrieve the default repository
|
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
|
|
|
}
|