aboutsummaryrefslogtreecommitdiffstats
path: root/cache
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2019-02-17 16:12:06 +0100
committerMichael Muré <batolettre@gmail.com>2019-03-01 22:40:25 +0100
commit864eae0d6bd0732260c0c56583bb77f9b25b60f6 (patch)
treeb1a0384f2fdb0af0c8aaf0f1b0fbc1c445c418f3 /cache
parentda558b05ef79f4c80df10c6969a9ae5f4f764f96 (diff)
downloadgit-bug-864eae0d6bd0732260c0c56583bb77f9b25b60f6.tar.gz
identity: work on higher level now, cache, first two identity commands
Diffstat (limited to 'cache')
-rw-r--r--cache/bug_cache.go38
-rw-r--r--cache/identity_cache.go26
-rw-r--r--cache/repo_cache.go68
3 files changed, 96 insertions, 36 deletions
diff --git a/cache/bug_cache.go b/cache/bug_cache.go
index 2a570667..ce46837a 100644
--- a/cache/bug_cache.go
+++ b/cache/bug_cache.go
@@ -5,8 +5,6 @@ import (
"strings"
"time"
- "github.com/MichaelMure/git-bug/identity"
-
"github.com/MichaelMure/git-bug/bug"
"github.com/MichaelMure/git-bug/util/git"
)
@@ -93,7 +91,7 @@ func (c *BugCache) AddComment(message string) error {
}
func (c *BugCache) AddCommentWithFiles(message string, files []git.Hash) error {
- author, err := identity.GetUserIdentity(c.repoCache.repo)
+ author, err := c.repoCache.GetUserIdentity()
if err != nil {
return err
}
@@ -101,8 +99,8 @@ func (c *BugCache) AddCommentWithFiles(message string, files []git.Hash) error {
return c.AddCommentRaw(author, time.Now().Unix(), message, files, nil)
}
-func (c *BugCache) AddCommentRaw(author identity.Interface, unixTime int64, message string, files []git.Hash, metadata map[string]string) error {
- op, err := bug.AddCommentWithFiles(c.bug, author, unixTime, message, files)
+func (c *BugCache) AddCommentRaw(author *IdentityCache, unixTime int64, message string, files []git.Hash, metadata map[string]string) error {
+ op, err := bug.AddCommentWithFiles(c.bug, author.Identity, unixTime, message, files)
if err != nil {
return err
}
@@ -115,7 +113,7 @@ func (c *BugCache) AddCommentRaw(author identity.Interface, unixTime int64, mess
}
func (c *BugCache) ChangeLabels(added []string, removed []string) ([]bug.LabelChangeResult, error) {
- author, err := identity.GetUserIdentity(c.repoCache.repo)
+ author, err := c.repoCache.GetUserIdentity()
if err != nil {
return nil, err
}
@@ -123,8 +121,8 @@ func (c *BugCache) ChangeLabels(added []string, removed []string) ([]bug.LabelCh
return c.ChangeLabelsRaw(author, time.Now().Unix(), added, removed, nil)
}
-func (c *BugCache) ChangeLabelsRaw(author identity.Interface, unixTime int64, added []string, removed []string, metadata map[string]string) ([]bug.LabelChangeResult, error) {
- changes, op, err := bug.ChangeLabels(c.bug, author, unixTime, added, removed)
+func (c *BugCache) ChangeLabelsRaw(author *IdentityCache, unixTime int64, added []string, removed []string, metadata map[string]string) ([]bug.LabelChangeResult, error) {
+ changes, op, err := bug.ChangeLabels(c.bug, author.Identity, unixTime, added, removed)
if err != nil {
return changes, err
}
@@ -142,7 +140,7 @@ func (c *BugCache) ChangeLabelsRaw(author identity.Interface, unixTime int64, ad
}
func (c *BugCache) Open() error {
- author, err := identity.GetUserIdentity(c.repoCache.repo)
+ author, err := c.repoCache.GetUserIdentity()
if err != nil {
return err
}
@@ -150,8 +148,8 @@ func (c *BugCache) Open() error {
return c.OpenRaw(author, time.Now().Unix(), nil)
}
-func (c *BugCache) OpenRaw(author identity.Interface, unixTime int64, metadata map[string]string) error {
- op, err := bug.Open(c.bug, author, unixTime)
+func (c *BugCache) OpenRaw(author *IdentityCache, unixTime int64, metadata map[string]string) error {
+ op, err := bug.Open(c.bug, author.Identity, unixTime)
if err != nil {
return err
}
@@ -164,7 +162,7 @@ func (c *BugCache) OpenRaw(author identity.Interface, unixTime int64, metadata m
}
func (c *BugCache) Close() error {
- author, err := identity.GetUserIdentity(c.repoCache.repo)
+ author, err := c.repoCache.GetUserIdentity()
if err != nil {
return err
}
@@ -172,8 +170,8 @@ func (c *BugCache) Close() error {
return c.CloseRaw(author, time.Now().Unix(), nil)
}
-func (c *BugCache) CloseRaw(author identity.Interface, unixTime int64, metadata map[string]string) error {
- op, err := bug.Close(c.bug, author, unixTime)
+func (c *BugCache) CloseRaw(author *IdentityCache, unixTime int64, metadata map[string]string) error {
+ op, err := bug.Close(c.bug, author.Identity, unixTime)
if err != nil {
return err
}
@@ -186,7 +184,7 @@ func (c *BugCache) CloseRaw(author identity.Interface, unixTime int64, metadata
}
func (c *BugCache) SetTitle(title string) error {
- author, err := identity.GetUserIdentity(c.repoCache.repo)
+ author, err := c.repoCache.GetUserIdentity()
if err != nil {
return err
}
@@ -194,8 +192,8 @@ func (c *BugCache) SetTitle(title string) error {
return c.SetTitleRaw(author, time.Now().Unix(), title, nil)
}
-func (c *BugCache) SetTitleRaw(author identity.Interface, unixTime int64, title string, metadata map[string]string) error {
- op, err := bug.SetTitle(c.bug, author, unixTime, title)
+func (c *BugCache) SetTitleRaw(author *IdentityCache, unixTime int64, title string, metadata map[string]string) error {
+ op, err := bug.SetTitle(c.bug, author.Identity, unixTime, title)
if err != nil {
return err
}
@@ -208,7 +206,7 @@ func (c *BugCache) SetTitleRaw(author identity.Interface, unixTime int64, title
}
func (c *BugCache) EditComment(target git.Hash, message string) error {
- author, err := identity.GetUserIdentity(c.repoCache.repo)
+ author, err := c.repoCache.GetUserIdentity()
if err != nil {
return err
}
@@ -216,8 +214,8 @@ func (c *BugCache) EditComment(target git.Hash, message string) error {
return c.EditCommentRaw(author, time.Now().Unix(), target, message, nil)
}
-func (c *BugCache) EditCommentRaw(author identity.Interface, unixTime int64, target git.Hash, message string, metadata map[string]string) error {
- op, err := bug.EditComment(c.bug, author, unixTime, target, message)
+func (c *BugCache) EditCommentRaw(author *IdentityCache, unixTime int64, target git.Hash, message string, metadata map[string]string) error {
+ op, err := bug.EditComment(c.bug, author.Identity, unixTime, target, message)
if err != nil {
return err
}
diff --git a/cache/identity_cache.go b/cache/identity_cache.go
new file mode 100644
index 00000000..93b2dc4b
--- /dev/null
+++ b/cache/identity_cache.go
@@ -0,0 +1,26 @@
+package cache
+
+import (
+ "github.com/MichaelMure/git-bug/identity"
+)
+
+// IdentityCache is a wrapper around an Identity. It provide multiple functions:
+type IdentityCache struct {
+ *identity.Identity
+ repoCache *RepoCache
+}
+
+func NewIdentityCache(repoCache *RepoCache, id *identity.Identity) *IdentityCache {
+ return &IdentityCache{
+ Identity: id,
+ repoCache: repoCache,
+ }
+}
+
+func (i *IdentityCache) Commit() error {
+ return i.Identity.Commit(i.repoCache.repo)
+}
+
+func (i *IdentityCache) CommitAsNeeded() error {
+ return i.Identity.CommitAsNeeded(i.repoCache.repo)
+}
diff --git a/cache/repo_cache.go b/cache/repo_cache.go
index 74f04e11..f64a1b76 100644
--- a/cache/repo_cache.go
+++ b/cache/repo_cache.go
@@ -45,13 +45,16 @@ type RepoCache struct {
// bug loaded in memory
bugs map[string]*BugCache
// identities loaded in memory
- identities map[string]*identity.Identity
+ identities map[string]*IdentityCache
+ // the user identity's id, if known
+ userIdentityId string
}
func NewRepoCache(r repository.ClockedRepo) (*RepoCache, error) {
c := &RepoCache{
- repo: r,
- bugs: make(map[string]*BugCache),
+ repo: r,
+ bugs: make(map[string]*BugCache),
+ identities: make(map[string]*IdentityCache),
}
err := c.lock()
@@ -394,7 +397,7 @@ func (c *RepoCache) NewBug(title string, message string) (*BugCache, error) {
// NewBugWithFiles create a new bug with attached files for the message
// The new bug is written in the repository (commit)
func (c *RepoCache) NewBugWithFiles(title string, message string, files []git.Hash) (*BugCache, error) {
- author, err := identity.GetUserIdentity(c.repo)
+ author, err := c.GetUserIdentity()
if err != nil {
return nil, err
}
@@ -405,8 +408,8 @@ func (c *RepoCache) NewBugWithFiles(title string, message string, files []git.Ha
// NewBugWithFilesMeta create a new bug with attached files for the message, as
// well as metadata for the Create operation.
// The new bug is written in the repository (commit)
-func (c *RepoCache) NewBugRaw(author identity.Interface, unixTime int64, title string, message string, files []git.Hash, metadata map[string]string) (*BugCache, error) {
- b, op, err := bug.CreateWithFiles(author, unixTime, title, message, files)
+func (c *RepoCache) NewBugRaw(author *IdentityCache, unixTime int64, title string, message string, files []git.Hash, metadata map[string]string) (*BugCache, error) {
+ b, op, err := bug.CreateWithFiles(author.Identity, unixTime, title, message, files)
if err != nil {
return nil, err
}
@@ -549,7 +552,7 @@ func repoIsAvailable(repo repository.Repo) error {
}
// ResolveIdentity retrieve an identity matching the exact given id
-func (c *RepoCache) ResolveIdentity(id string) (*identity.Identity, error) {
+func (c *RepoCache) ResolveIdentity(id string) (*IdentityCache, error) {
cached, ok := c.identities[id]
if ok {
return cached, nil
@@ -560,14 +563,15 @@ func (c *RepoCache) ResolveIdentity(id string) (*identity.Identity, error) {
return nil, err
}
- c.identities[id] = i
+ cached = NewIdentityCache(c, i)
+ c.identities[id] = cached
- return i, nil
+ return cached, nil
}
// ResolveIdentityPrefix retrieve an Identity matching an id prefix.
// It fails if multiple identities match.
-func (c *RepoCache) ResolveIdentityPrefix(prefix string) (*identity.Identity, error) {
+func (c *RepoCache) ResolveIdentityPrefix(prefix string) (*IdentityCache, error) {
// preallocate but empty
matching := make([]string, 0, 5)
@@ -590,7 +594,7 @@ func (c *RepoCache) ResolveIdentityPrefix(prefix string) (*identity.Identity, er
// ResolveIdentityImmutableMetadata retrieve an Identity that has the exact given metadata on
// one of it's version. If multiple version have the same key, the first defined take precedence.
-func (c *RepoCache) ResolveIdentityImmutableMetadata(key string, value string) (*identity.Identity, error) {
+func (c *RepoCache) ResolveIdentityImmutableMetadata(key string, value string) (*IdentityCache, error) {
// preallocate but empty
matching := make([]string, 0, 5)
@@ -611,19 +615,50 @@ func (c *RepoCache) ResolveIdentityImmutableMetadata(key string, value string) (
return c.ResolveIdentity(matching[0])
}
+func (c *RepoCache) SetUserIdentity(i *IdentityCache) error {
+ err := identity.SetUserIdentity(c.repo, i.Identity)
+ if err != nil {
+ return err
+ }
+
+ c.userIdentityId = i.Id()
+
+ return nil
+}
+
+func (c *RepoCache) GetUserIdentity() (*IdentityCache, error) {
+ if c.userIdentityId != "" {
+ i, ok := c.identities[c.userIdentityId]
+ if ok {
+ return i, nil
+ }
+ }
+
+ i, err := identity.GetUserIdentity(c.repo)
+ if err != nil {
+ return nil, err
+ }
+
+ cached := NewIdentityCache(c, i)
+ c.identities[i.Id()] = cached
+ c.userIdentityId = i.Id()
+
+ return cached, nil
+}
+
// NewIdentity create a new identity
// The new identity is written in the repository (commit)
-func (c *RepoCache) NewIdentity(name string, email string) (*identity.Identity, error) {
+func (c *RepoCache) NewIdentity(name string, email string) (*IdentityCache, error) {
return c.NewIdentityRaw(name, email, "", "", nil)
}
// NewIdentityFull create a new identity
// The new identity is written in the repository (commit)
-func (c *RepoCache) NewIdentityFull(name string, email string, login string, avatarUrl string) (*identity.Identity, error) {
+func (c *RepoCache) NewIdentityFull(name string, email string, login string, avatarUrl string) (*IdentityCache, error) {
return c.NewIdentityRaw(name, email, login, avatarUrl, nil)
}
-func (c *RepoCache) NewIdentityRaw(name string, email string, login string, avatarUrl string, metadata map[string]string) (*identity.Identity, error) {
+func (c *RepoCache) NewIdentityRaw(name string, email string, login string, avatarUrl string, metadata map[string]string) (*IdentityCache, error) {
i := identity.NewIdentityFull(name, email, login, avatarUrl)
for key, value := range metadata {
@@ -639,7 +674,8 @@ func (c *RepoCache) NewIdentityRaw(name string, email string, login string, avat
return nil, fmt.Errorf("identity %s already exist in the cache", i.Id())
}
- c.identities[i.Id()] = i
+ cached := NewIdentityCache(c, i)
+ c.identities[i.Id()] = cached
- return i, nil
+ return cached, nil
}