2020-07-03 19:31:48 +03:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2022-12-21 23:54:36 +03:00
|
|
|
"sync"
|
|
|
|
|
2020-12-05 05:08:54 +03:00
|
|
|
"github.com/go-git/go-billy/v5"
|
2020-07-03 19:31:48 +03:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2022-08-19 00:34:05 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entities/identity"
|
2020-07-03 19:31:48 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entity"
|
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c *RepoCache) Name() string {
|
|
|
|
return c.name
|
|
|
|
}
|
|
|
|
|
|
|
|
// LocalConfig give access to the repository scoped configuration
|
|
|
|
func (c *RepoCache) LocalConfig() repository.Config {
|
|
|
|
return c.repo.LocalConfig()
|
|
|
|
}
|
|
|
|
|
2020-09-27 01:54:14 +03:00
|
|
|
// GlobalConfig give access to the global scoped configuration
|
|
|
|
func (c *RepoCache) GlobalConfig() repository.Config {
|
|
|
|
return c.repo.GlobalConfig()
|
|
|
|
}
|
|
|
|
|
|
|
|
// AnyConfig give access to a merged local/global configuration
|
|
|
|
func (c *RepoCache) AnyConfig() repository.ConfigRead {
|
|
|
|
return c.repo.AnyConfig()
|
|
|
|
}
|
|
|
|
|
2020-12-05 05:08:54 +03:00
|
|
|
// Keyring give access to a user-wide storage for secrets
|
2020-07-28 13:56:46 +03:00
|
|
|
func (c *RepoCache) Keyring() repository.Keyring {
|
|
|
|
return c.repo.Keyring()
|
2020-07-03 19:31:48 +03:00
|
|
|
}
|
|
|
|
|
2022-07-31 15:38:32 +03:00
|
|
|
// GetUserName returns the name the user has used to configure git
|
2020-12-05 05:08:54 +03:00
|
|
|
func (c *RepoCache) GetUserName() (string, error) {
|
|
|
|
return c.repo.GetUserName()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUserEmail returns the email address that the user has used to configure git.
|
|
|
|
func (c *RepoCache) GetUserEmail() (string, error) {
|
|
|
|
return c.repo.GetUserEmail()
|
2020-07-03 19:31:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetCoreEditor returns the name of the editor that the user has used to configure git.
|
|
|
|
func (c *RepoCache) GetCoreEditor() (string, error) {
|
|
|
|
return c.repo.GetCoreEditor()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRemotes returns the configured remotes repositories.
|
|
|
|
func (c *RepoCache) GetRemotes() (map[string]string, error) {
|
|
|
|
return c.repo.GetRemotes()
|
|
|
|
}
|
|
|
|
|
2020-12-05 05:08:54 +03:00
|
|
|
// LocalStorage return a billy.Filesystem giving access to $RepoPath/.git/git-bug
|
|
|
|
func (c *RepoCache) LocalStorage() billy.Filesystem {
|
|
|
|
return c.repo.LocalStorage()
|
2020-07-03 19:31:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadData will attempt to read arbitrary data from the given hash
|
|
|
|
func (c *RepoCache) ReadData(hash repository.Hash) ([]byte, error) {
|
|
|
|
return c.repo.ReadData(hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StoreData will store arbitrary data and return the corresponding hash
|
|
|
|
func (c *RepoCache) StoreData(data []byte) (repository.Hash, error) {
|
|
|
|
return c.repo.StoreData(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch retrieve updates from a remote
|
|
|
|
// This does not change the local bugs or identities state
|
|
|
|
func (c *RepoCache) Fetch(remote string) (string, error) {
|
2022-12-21 23:54:36 +03:00
|
|
|
prefixes := make([]string, len(c.subcaches))
|
|
|
|
for i, subcache := range c.subcaches {
|
|
|
|
prefixes[i] = subcache.GetNamespace()
|
2020-07-03 19:31:48 +03:00
|
|
|
}
|
|
|
|
|
2022-12-21 23:54:36 +03:00
|
|
|
// fetch everything at once, to have a single auth step if required.
|
|
|
|
return c.repo.FetchRefs(remote, prefixes...)
|
2020-07-03 19:31:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// MergeAll will merge all the available remote bug and identities
|
|
|
|
func (c *RepoCache) MergeAll(remote string) <-chan entity.MergeResult {
|
|
|
|
out := make(chan entity.MergeResult)
|
|
|
|
|
2022-12-21 23:54:36 +03:00
|
|
|
dependency := [][]cacheMgmt{
|
|
|
|
{c.identities},
|
|
|
|
{c.bugs},
|
|
|
|
}
|
|
|
|
|
|
|
|
// run MergeAll according to entities dependencies and merge the results
|
2020-07-03 19:31:48 +03:00
|
|
|
go func() {
|
|
|
|
defer close(out)
|
|
|
|
|
2022-12-21 23:54:36 +03:00
|
|
|
for _, subcaches := range dependency {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for _, subcache := range subcaches {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(subcache cacheMgmt) {
|
|
|
|
for res := range subcache.MergeAll(remote) {
|
|
|
|
out <- res
|
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}(subcache)
|
2020-07-03 19:31:48 +03:00
|
|
|
}
|
2022-12-21 23:54:36 +03:00
|
|
|
wg.Wait()
|
2020-07-03 19:31:48 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push update a remote with the local changes
|
|
|
|
func (c *RepoCache) Push(remote string) (string, error) {
|
2022-12-21 23:54:36 +03:00
|
|
|
prefixes := make([]string, len(c.subcaches))
|
|
|
|
for i, subcache := range c.subcaches {
|
|
|
|
prefixes[i] = subcache.GetNamespace()
|
2020-07-03 19:31:48 +03:00
|
|
|
}
|
|
|
|
|
2022-12-21 23:54:36 +03:00
|
|
|
// push everything at once, to have a single auth step if required
|
|
|
|
return c.repo.PushRefs(remote, prefixes...)
|
2020-07-03 19:31:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pull will do a Fetch + MergeAll
|
|
|
|
// This function will return an error if a merge fail
|
|
|
|
func (c *RepoCache) Pull(remote string) error {
|
|
|
|
_, err := c.Fetch(remote)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for merge := range c.MergeAll(remote) {
|
|
|
|
if merge.Err != nil {
|
|
|
|
return merge.Err
|
|
|
|
}
|
|
|
|
if merge.Status == entity.MergeStatusInvalid {
|
|
|
|
return errors.Errorf("merge failure: %s", merge.Reason)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RepoCache) SetUserIdentity(i *IdentityCache) error {
|
2022-12-19 20:09:59 +03:00
|
|
|
c.muUserIdentity.RLock()
|
|
|
|
defer c.muUserIdentity.RUnlock()
|
2020-07-03 19:31:48 +03:00
|
|
|
|
|
|
|
// Make sure that everything is fine
|
2022-12-19 20:09:59 +03:00
|
|
|
if _, err := c.identities.Resolve(i.Id()); err != nil {
|
2020-07-03 19:31:48 +03:00
|
|
|
panic("SetUserIdentity while the identity is not from the cache, something is wrong")
|
|
|
|
}
|
|
|
|
|
2022-12-19 20:09:59 +03:00
|
|
|
err := identity.SetUserIdentity(c.repo, i.Identity)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-03 19:31:48 +03:00
|
|
|
c.userIdentityId = i.Id()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RepoCache) GetUserIdentity() (*IdentityCache, error) {
|
2022-12-19 20:09:59 +03:00
|
|
|
c.muUserIdentity.RLock()
|
2020-07-03 19:31:48 +03:00
|
|
|
if c.userIdentityId != "" {
|
2022-12-19 20:09:59 +03:00
|
|
|
defer c.muUserIdentity.RUnlock()
|
|
|
|
return c.identities.Resolve(c.userIdentityId)
|
2020-07-03 19:31:48 +03:00
|
|
|
}
|
2022-12-19 20:09:59 +03:00
|
|
|
c.muUserIdentity.RUnlock()
|
2020-07-03 19:31:48 +03:00
|
|
|
|
2022-12-19 20:09:59 +03:00
|
|
|
c.muUserIdentity.Lock()
|
|
|
|
defer c.muUserIdentity.Unlock()
|
2020-07-03 19:31:48 +03:00
|
|
|
|
2022-12-19 20:09:59 +03:00
|
|
|
i, err := identity.GetUserIdentityId(c.repo)
|
2020-07-03 19:31:48 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-19 20:09:59 +03:00
|
|
|
c.userIdentityId = i
|
2020-07-03 19:31:48 +03:00
|
|
|
|
2022-12-19 20:09:59 +03:00
|
|
|
return c.identities.Resolve(i)
|
2020-07-03 19:31:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RepoCache) GetUserIdentityExcerpt() (*IdentityExcerpt, error) {
|
2022-12-19 20:09:59 +03:00
|
|
|
c.muUserIdentity.RLock()
|
|
|
|
if c.userIdentityId != "" {
|
|
|
|
defer c.muUserIdentity.RUnlock()
|
|
|
|
return c.identities.ResolveExcerpt(c.userIdentityId)
|
2020-07-03 19:31:48 +03:00
|
|
|
}
|
2022-12-19 20:09:59 +03:00
|
|
|
c.muUserIdentity.RUnlock()
|
2020-07-03 19:31:48 +03:00
|
|
|
|
2022-12-19 20:09:59 +03:00
|
|
|
c.muUserIdentity.Lock()
|
|
|
|
defer c.muUserIdentity.Unlock()
|
2020-07-03 19:31:48 +03:00
|
|
|
|
2022-12-19 20:09:59 +03:00
|
|
|
i, err := identity.GetUserIdentityId(c.repo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-07-03 19:31:48 +03:00
|
|
|
}
|
2022-12-19 20:09:59 +03:00
|
|
|
|
|
|
|
c.userIdentityId = i
|
|
|
|
|
|
|
|
return c.identities.ResolveExcerpt(i)
|
2020-07-03 19:31:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RepoCache) IsUserIdentitySet() (bool, error) {
|
|
|
|
return identity.IsUserIdentitySet(c.repo)
|
|
|
|
}
|