aboutsummaryrefslogtreecommitdiffstats
path: root/cache
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2020-02-09 01:31:00 +0100
committerGitHub <noreply@github.com>2020-02-09 01:31:00 +0100
commit9e1a987b4d94dc5c2115423ede5954d4faf1d342 (patch)
treee30bb28d55444a7d1e66c8fa8b8ba7d413805473 /cache
parent97bc5ccd229b7b438262a84e3c42783b4d4a82af (diff)
parent9b1aaa032d36e1ac05504916e359f767d1622d9d (diff)
downloadgit-bug-9e1a987b4d94dc5c2115423ede5954d4faf1d342.tar.gz
Merge pull request #294 from MichaelMure/cred-metadata
Cred metadata
Diffstat (limited to 'cache')
-rw-r--r--cache/bug_excerpt.go18
-rw-r--r--cache/filter.go3
-rw-r--r--cache/identity_cache.go4
-rw-r--r--cache/identity_excerpt.go17
-rw-r--r--cache/query.go4
-rw-r--r--cache/repo_cache.go91
6 files changed, 54 insertions, 83 deletions
diff --git a/cache/bug_excerpt.go b/cache/bug_excerpt.go
index 36c7dcfe..10e522f9 100644
--- a/cache/bug_excerpt.go
+++ b/cache/bug_excerpt.go
@@ -2,7 +2,6 @@ package cache
import (
"encoding/gob"
- "fmt"
"github.com/MichaelMure/git-bug/bug"
"github.com/MichaelMure/git-bug/entity"
@@ -43,21 +42,11 @@ type BugExcerpt struct {
// identity.Bare data are directly embedded in the bug excerpt
type LegacyAuthorExcerpt struct {
- Name string
- Login string
+ Name string
}
func (l LegacyAuthorExcerpt) DisplayName() string {
- switch {
- case l.Name == "" && l.Login != "":
- return l.Login
- case l.Name != "" && l.Login == "":
- return l.Name
- case l.Name != "" && l.Login != "":
- return fmt.Sprintf("%s (%s)", l.Name, l.Login)
- }
-
- panic("invalid person data")
+ return l.Name
}
func NewBugExcerpt(b bug.Interface, snap *bug.Snapshot) *BugExcerpt {
@@ -95,8 +84,7 @@ func NewBugExcerpt(b bug.Interface, snap *bug.Snapshot) *BugExcerpt {
e.AuthorId = snap.Author.Id()
case *identity.Bare:
e.LegacyAuthor = LegacyAuthorExcerpt{
- Login: snap.Author.Login(),
- Name: snap.Author.Name(),
+ Name: snap.Author.Name(),
}
default:
panic("unhandled identity type")
diff --git a/cache/filter.go b/cache/filter.go
index 27e92cf3..9b1de1d5 100644
--- a/cache/filter.go
+++ b/cache/filter.go
@@ -37,8 +37,7 @@ func AuthorFilter(query string) Filter {
}
// Legacy identity support
- return strings.Contains(strings.ToLower(excerpt.LegacyAuthor.Name), query) ||
- strings.Contains(strings.ToLower(excerpt.LegacyAuthor.Login), query)
+ return strings.Contains(strings.ToLower(excerpt.LegacyAuthor.Name), query)
}
}
diff --git a/cache/identity_cache.go b/cache/identity_cache.go
index 2ae55f2d..eb5ee183 100644
--- a/cache/identity_cache.go
+++ b/cache/identity_cache.go
@@ -21,8 +21,8 @@ func (i *IdentityCache) notifyUpdated() error {
return i.repoCache.identityUpdated(i.Identity.Id())
}
-func (i *IdentityCache) AddVersion(version *identity.Version) error {
- i.Identity.AddVersion(version)
+func (i *IdentityCache) Mutate(f func(identity.Mutator) identity.Mutator) error {
+ i.Identity.Mutate(f)
return i.notifyUpdated()
}
diff --git a/cache/identity_excerpt.go b/cache/identity_excerpt.go
index 18514e9a..06788aa5 100644
--- a/cache/identity_excerpt.go
+++ b/cache/identity_excerpt.go
@@ -2,7 +2,6 @@ package cache
import (
"encoding/gob"
- "fmt"
"strings"
"github.com/MichaelMure/git-bug/entity"
@@ -21,7 +20,6 @@ type IdentityExcerpt struct {
Id entity.Id
Name string
- Login string
ImmutableMetadata map[string]string
}
@@ -29,7 +27,6 @@ func NewIdentityExcerpt(i *identity.Identity) *IdentityExcerpt {
return &IdentityExcerpt{
Id: i.Id(),
Name: i.Name(),
- Login: i.Login(),
ImmutableMetadata: i.ImmutableMetadata(),
}
}
@@ -37,23 +34,13 @@ func NewIdentityExcerpt(i *identity.Identity) *IdentityExcerpt {
// DisplayName return a non-empty string to display, representing the
// identity, based on the non-empty values.
func (i *IdentityExcerpt) DisplayName() string {
- switch {
- case i.Name == "" && i.Login != "":
- return i.Login
- case i.Name != "" && i.Login == "":
- return i.Name
- case i.Name != "" && i.Login != "":
- return fmt.Sprintf("%s (%s)", i.Name, i.Login)
- }
-
- panic("invalid person data")
+ return i.Name
}
// Match matches a query with the identity name, login and ID prefixes
func (i *IdentityExcerpt) Match(query string) bool {
return i.Id.HasPrefix(query) ||
- strings.Contains(strings.ToLower(i.Name), query) ||
- strings.Contains(strings.ToLower(i.Login), query)
+ strings.Contains(strings.ToLower(i.Name), query)
}
/*
diff --git a/cache/query.go b/cache/query.go
index 633ef1c2..967c18d6 100644
--- a/cache/query.go
+++ b/cache/query.go
@@ -91,7 +91,7 @@ func ParseQuery(query string) (*Query, error) {
sortingDone = true
default:
- return nil, fmt.Errorf("unknow qualifier name %s", qualifierName)
+ return nil, fmt.Errorf("unknown qualifier name %s", qualifierName)
}
}
@@ -165,7 +165,7 @@ func (q *Query) parseSorting(query string) error {
q.OrderDirection = OrderAscending
default:
- return fmt.Errorf("unknow sorting %s", query)
+ return fmt.Errorf("unknown sorting %s", query)
}
return nil
diff --git a/cache/repo_cache.go b/cache/repo_cache.go
index 90a489c8..18be9b5a 100644
--- a/cache/repo_cache.go
+++ b/cache/repo_cache.go
@@ -409,36 +409,27 @@ func (c *RepoCache) ResolveBugExcerpt(id entity.Id) (*BugExcerpt, error) {
// ResolveBugPrefix retrieve a bug matching an id prefix. It fails if multiple
// bugs match.
func (c *RepoCache) ResolveBugPrefix(prefix string) (*BugCache, error) {
- // preallocate but empty
- matching := make([]entity.Id, 0, 5)
-
- for id := range c.bugExcerpts {
- if id.HasPrefix(prefix) {
- matching = append(matching, id)
- }
- }
-
- if len(matching) > 1 {
- return nil, bug.NewErrMultipleMatchBug(matching)
- }
-
- if len(matching) == 0 {
- return nil, bug.ErrBugNotExist
- }
-
- return c.ResolveBug(matching[0])
+ return c.ResolveBugMatcher(func(excerpt *BugExcerpt) bool {
+ return excerpt.Id.HasPrefix(prefix)
+ })
}
// ResolveBugCreateMetadata retrieve a bug that has the exact given metadata on
// its Create operation, that is, the first operation. It fails if multiple bugs
// match.
func (c *RepoCache) ResolveBugCreateMetadata(key string, value string) (*BugCache, error) {
+ return c.ResolveBugMatcher(func(excerpt *BugExcerpt) bool {
+ return excerpt.CreateMetadata[key] == value
+ })
+}
+
+func (c *RepoCache) ResolveBugMatcher(f func(*BugExcerpt) bool) (*BugCache, error) {
// preallocate but empty
matching := make([]entity.Id, 0, 5)
- for id, excerpt := range c.bugExcerpts {
- if excerpt.CreateMetadata[key] == value {
- matching = append(matching, id)
+ for _, excerpt := range c.bugExcerpts {
+ if f(excerpt) {
+ matching = append(matching, excerpt.Id)
}
}
@@ -785,35 +776,26 @@ func (c *RepoCache) ResolveIdentityExcerpt(id entity.Id) (*IdentityExcerpt, erro
// ResolveIdentityPrefix retrieve an Identity matching an id prefix.
// It fails if multiple identities match.
func (c *RepoCache) ResolveIdentityPrefix(prefix string) (*IdentityCache, error) {
- // preallocate but empty
- matching := make([]entity.Id, 0, 5)
-
- for id := range c.identitiesExcerpts {
- if id.HasPrefix(prefix) {
- matching = append(matching, id)
- }
- }
-
- if len(matching) > 1 {
- return nil, identity.NewErrMultipleMatch(matching)
- }
-
- if len(matching) == 0 {
- return nil, identity.ErrIdentityNotExist
- }
-
- return c.ResolveIdentity(matching[0])
+ return c.ResolveIdentityMatcher(func(excerpt *IdentityExcerpt) bool {
+ return excerpt.Id.HasPrefix(prefix)
+ })
}
// 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) (*IdentityCache, error) {
+ return c.ResolveIdentityMatcher(func(excerpt *IdentityExcerpt) bool {
+ return excerpt.ImmutableMetadata[key] == value
+ })
+}
+
+func (c *RepoCache) ResolveIdentityMatcher(f func(*IdentityExcerpt) bool) (*IdentityCache, error) {
// preallocate but empty
matching := make([]entity.Id, 0, 5)
- for id, i := range c.identitiesExcerpts {
- if i.ImmutableMetadata[key] == value {
- matching = append(matching, id)
+ for _, excerpt := range c.identitiesExcerpts {
+ if f(excerpt) {
+ matching = append(matching, excerpt.Id)
}
}
@@ -881,21 +863,36 @@ func (c *RepoCache) IsUserIdentitySet() (bool, error) {
return identity.IsUserIdentitySet(c.repo)
}
+func (c *RepoCache) NewIdentityFromGitUser() (*IdentityCache, error) {
+ return c.NewIdentityFromGitUserRaw(nil)
+}
+
+func (c *RepoCache) 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 *RepoCache) NewIdentity(name string, email string) (*IdentityCache, error) {
- return c.NewIdentityRaw(name, email, "", "", nil)
+ 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) (*IdentityCache, error) {
- return c.NewIdentityRaw(name, email, login, avatarUrl, nil)
+func (c *RepoCache) NewIdentityFull(name string, email string, avatarUrl string) (*IdentityCache, error) {
+ return c.NewIdentityRaw(name, email, avatarUrl, nil)
}
-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)
+func (c *RepoCache) NewIdentityRaw(name string, email string, avatarUrl string, metadata map[string]string) (*IdentityCache, error) {
+ i := identity.NewIdentityFull(name, email, avatarUrl)
+ return c.finishIdentity(i, metadata)
+}
+func (c *RepoCache) finishIdentity(i *identity.Identity, metadata map[string]string) (*IdentityCache, error) {
for key, value := range metadata {
i.SetMetadata(key, value)
}