diff options
author | Michael Muré <batolettre@gmail.com> | 2022-11-29 13:01:53 +0100 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2022-11-29 13:01:53 +0100 |
commit | 4a341b5e1714a6a36ec7f5839a6a1b73571d4851 (patch) | |
tree | 261e108d1c9bd78e15e19379f611cfecb900fef5 /cache/identity_subcache.go | |
parent | 0ac39a7ab5db077fcf0df827e32bf6e625e980da (diff) | |
download | git-bug-4a341b5e1714a6a36ec7f5839a6a1b73571d4851.tar.gz |
WIP
Diffstat (limited to 'cache/identity_subcache.go')
-rw-r--r-- | cache/identity_subcache.go | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/cache/identity_subcache.go b/cache/identity_subcache.go new file mode 100644 index 00000000..a3b5a0f8 --- /dev/null +++ b/cache/identity_subcache.go @@ -0,0 +1,79 @@ +package cache + +import ( + "fmt" + + "github.com/MichaelMure/git-bug/entities/identity" +) + +type RepoCacheIdentity struct { + SubCache[*IdentityExcerpt, *IdentityCache, identity.Interface] +} + +// ResolveIdentityImmutableMetadata retrieve an Identity that has the exact given metadata on +// one of its version. If multiple version have the same key, the first defined take precedence. +func (c *RepoCacheIdentity) ResolveIdentityImmutableMetadata(key string, value string) (*IdentityCache, error) { + return c.ResolveMatcher(func(excerpt *IdentityExcerpt) bool { + return excerpt.ImmutableMetadata[key] == value + }) +} + +func (c *RepoCacheIdentity) NewIdentityFromGitUser() (*IdentityCache, error) { + return c.NewIdentityFromGitUserRaw(nil) +} + +func (c *RepoCacheIdentity) NewIdentityFromGitUserRaw(metadata map[string]string) (*IdentityCache, error) { + i, err := identity.NewFromGitUser(c.repo) + if err != nil { + return nil, err + } + return c.finishIdentity(i, metadata) +} + +// NewIdentity create a new identity +// The new identity is written in the repository (commit) +func (c *RepoCacheIdentity) NewIdentity(name string, email string) (*IdentityCache, error) { + return c.NewIdentityRaw(name, email, "", "", nil, nil) +} + +// NewIdentityFull create a new identity +// The new identity is written in the repository (commit) +func (c *RepoCacheIdentity) NewIdentityFull(name string, email string, login string, avatarUrl string, keys []*identity.Key) (*IdentityCache, error) { + return c.NewIdentityRaw(name, email, login, avatarUrl, keys, nil) +} + +func (c *RepoCacheIdentity) NewIdentityRaw(name string, email string, login string, avatarUrl string, keys []*identity.Key, metadata map[string]string) (*IdentityCache, error) { + i, err := identity.NewIdentityFull(c.repo, name, email, login, avatarUrl, keys) + if err != nil { + return nil, err + } + return c.finishIdentity(i, metadata) +} + +func (c *RepoCacheIdentity) finishIdentity(i *identity.Identity, metadata map[string]string) (*IdentityCache, error) { + for key, value := range metadata { + i.SetMetadata(key, value) + } + + err := i.Commit(c.repo) + if err != nil { + return nil, err + } + + c.mu.Lock() + if _, has := c.cached[i.Id()]; has { + return nil, fmt.Errorf("identity %s already exist in the cache", i.Id()) + } + + cached := NewIdentityCache(c, i) + c.cached[i.Id()] = cached + c.mu.Unlock() + + // force the write of the excerpt + err = c.entityUpdated(i.Id()) + if err != nil { + return nil, err + } + + return cached, nil +} |