aboutsummaryrefslogblamecommitdiffstats
path: root/cache/cache.go
blob: 751467b2e10479cb8a4df24124c9c6137518db31 (plain) (tree)
1
2
3
4
5
6
7
8
9



             
 


                                                   
                       
                               
 
                       
                                   

 

                           
                                                   


         

                                                                                





                                    
                  

 

                                                                                        





                                    
                  

 
                                            
                                                       

                                                                  
         
 

                                   

         
                            

 
                                              
                                                                 


                                                      
         
                     

 
                                                                    

                                            
                                         



                                  
                  
 
package cache

import (
	"fmt"

	"github.com/MichaelMure/git-bug/repository"
)

const lockfile = "lock"
const excerptsFile = "excerpts"

type RootCache struct {
	repos map[string]*RepoCache
}

func NewCache() RootCache {
	return RootCache{
		repos: make(map[string]*RepoCache),
	}
}

// RegisterRepository register a named repository. Use this for multi-repo setup
func (c *RootCache) RegisterRepository(ref string, repo repository.Repo) error {
	r, err := NewRepoCache(repo)
	if err != nil {
		return err
	}

	c.repos[ref] = r
	return nil
}

// RegisterDefaultRepository register a unnamed repository. Use this for mono-repo setup
func (c *RootCache) RegisterDefaultRepository(repo repository.Repo) error {
	r, err := NewRepoCache(repo)
	if err != nil {
		return err
	}

	c.repos[""] = r
	return nil
}

// ResolveRepo retrieve a repository by name
func (c *RootCache) DefaultRepo() (*RepoCache, error) {
	if len(c.repos) != 1 {
		return nil, fmt.Errorf("repository is not unique")
	}

	for _, r := range c.repos {
		return r, nil
	}

	panic("unreachable")
}

// DefaultRepo retrieve the default repository
func (c *RootCache) ResolveRepo(ref string) (*RepoCache, error) {
	r, ok := c.repos[ref]
	if !ok {
		return nil, fmt.Errorf("unknown repo")
	}
	return r, nil
}

// Close will do anything that is needed to close the cache properly
func (c *RootCache) Close() error {
	for _, cachedRepo := range c.repos {
		err := cachedRepo.Close()
		if err != nil {
			return err
		}
	}
	return nil
}