Skip to content
Snippets Groups Projects
Select Git revision
  • b168d71fc15c98f4b5f7c32a9c5e477bb2e49164
  • master default protected
  • gh-pages
  • trunk
  • dependabot/go_modules/go_modules-985326579b
  • graphql-subscription
  • feat/1467/git-repo-path
  • board
  • I738207f8cb254b66f3ef18aa525fce39c71060e2
  • Ia1ad61e54e305bb073efc6853738d3eed81d576c
  • feat/lipgloss-formatting
  • bootstrap-rework-2
  • boostrap-rework
  • github-config-resilience
  • feat-849-terminal-output-in-core
  • gitea-bridge
  • feat-licences-checks
  • complete-comment
  • graphql-generics
  • fix/810-finish-reading-loop
  • gobilly
  • v0.10.1
  • v0.10.0
  • v0.9.0
  • v0.8.1
  • v0.8.0
  • v0.7.2
  • 0.7.1
  • 0.7.0
  • 0.6.0
  • 0.5.0
  • 0.4.0
  • 0.3.0
  • 0.2.0
  • 0.1.0
35 results

multi_repo_cache.go

Blame
  • multi_repo_cache.go 1.47 KiB
    package cache
    
    import (
    	"fmt"
    
    	"github.com/MichaelMure/git-bug/repository"
    )
    
    const lockfile = "lock"
    
    type MultiRepoCache struct {
    	repos map[string]*RepoCache
    }
    
    func NewMultiRepoCache() MultiRepoCache {
    	return MultiRepoCache{
    		repos: make(map[string]*RepoCache),
    	}
    }
    
    // RegisterRepository register a named repository. Use this for multi-repo setup
    func (c *MultiRepoCache) 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 *MultiRepoCache) 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 *MultiRepoCache) 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 *MultiRepoCache) 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 *MultiRepoCache) Close() error {
    	for _, cachedRepo := range c.repos {
    		err := cachedRepo.Close()
    		if err != nil {
    			return err
    		}
    	}
    	return nil
    }