aboutsummaryrefslogtreecommitdiffstats
path: root/entities/identity
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2022-12-23 01:48:14 +0100
committerGitHub <noreply@github.com>2022-12-23 01:48:14 +0100
commit0a5a0ec1ef4ad98bc2116a953e201f96474941ab (patch)
tree660a9b17b5247fe2f954bfa814cce3193c5afa23 /entities/identity
parent108518530e822e3bdf59c8bfc333ad0bbe2d5fc8 (diff)
parent95911100823b5c809225d664de74ad2d64e91972 (diff)
downloadgit-bug-0a5a0ec1ef4ad98bc2116a953e201f96474941ab.tar.gz
Merge pull request #938 from MichaelMure/cache-reorg
Generic cache layer
Diffstat (limited to 'entities/identity')
-rw-r--r--entities/identity/common.go9
-rw-r--r--entities/identity/identity.go38
-rw-r--r--entities/identity/identity_actions.go4
-rw-r--r--entities/identity/identity_actions_test.go5
-rw-r--r--entities/identity/identity_test.go7
-rw-r--r--entities/identity/identity_user.go2
-rw-r--r--entities/identity/resolver.go15
7 files changed, 27 insertions, 53 deletions
diff --git a/entities/identity/common.go b/entities/identity/common.go
index 5c6445e9..88e30e33 100644
--- a/entities/identity/common.go
+++ b/entities/identity/common.go
@@ -2,18 +2,9 @@ package identity
import (
"encoding/json"
- "errors"
"fmt"
-
- "github.com/MichaelMure/git-bug/entity"
)
-var ErrIdentityNotExist = errors.New("identity doesn't exist")
-
-func NewErrMultipleMatch(matching []entity.Id) *entity.ErrMultipleMatch {
- return entity.NewErrMultipleMatch("identity", matching)
-}
-
// Custom unmarshaling function to allow package user to delegate
// the decoding of an Identity and distinguish between an Identity
// and a Bare.
diff --git a/entities/identity/identity.go b/entities/identity/identity.go
index d497dbcc..b0cee43b 100644
--- a/entities/identity/identity.go
+++ b/entities/identity/identity.go
@@ -19,16 +19,15 @@ const identityRemoteRefPattern = "refs/remotes/%s/identities/"
const versionEntryName = "version"
const identityConfigKey = "git-bug.identity"
+const Typename = "identity"
+const Namespace = "identities"
+
var ErrNonFastForwardMerge = errors.New("non fast-forward identity merge")
var ErrNoIdentitySet = errors.New("No identity is set.\n" +
"To interact with bugs, an identity first needs to be created using " +
"\"git bug user new\" or adopted with \"git bug user adopt\"")
var ErrMultipleIdentitiesSet = errors.New("multiple user identities set")
-func NewErrMultipleMatchIdentity(matching []entity.Id) *entity.ErrMultipleMatch {
- return entity.NewErrMultipleMatch("identity", matching)
-}
-
var _ Interface = &Identity{}
var _ entity.Interface = &Identity{}
@@ -109,7 +108,7 @@ func read(repo repository.Repo, ref string) (*Identity, error) {
hashes, err := repo.ListCommits(ref)
if err != nil {
- return nil, ErrIdentityNotExist
+ return nil, entity.NewErrNotFound(Typename)
}
if len(hashes) == 0 {
return nil, fmt.Errorf("empty identity")
@@ -174,7 +173,7 @@ func RemoveIdentity(repo repository.ClockedRepo, id entity.Id) error {
return err
}
if len(refs) > 1 {
- return NewErrMultipleMatchIdentity(entity.RefsToIds(refs))
+ return entity.NewErrMultipleMatch(Typename, entity.RefsToIds(refs))
}
if len(refs) == 1 {
// we have the identity locally
@@ -193,7 +192,7 @@ func RemoveIdentity(repo repository.ClockedRepo, id entity.Id) error {
return err
}
if len(remoteRefs) > 1 {
- return NewErrMultipleMatchIdentity(entity.RefsToIds(refs))
+ return entity.NewErrMultipleMatch(Typename, entity.RefsToIds(refs))
}
if len(remoteRefs) == 1 {
// found the identity in a remote
@@ -202,7 +201,7 @@ func RemoveIdentity(repo repository.ClockedRepo, id entity.Id) error {
}
if len(fullMatches) == 0 {
- return ErrIdentityNotExist
+ return entity.NewErrNotFound(Typename)
}
for _, ref := range fullMatches {
@@ -215,44 +214,39 @@ func RemoveIdentity(repo repository.ClockedRepo, id entity.Id) error {
return nil
}
-type StreamedIdentity struct {
- Identity *Identity
- Err error
-}
-
// ReadAllLocal read and parse all local Identity
-func ReadAllLocal(repo repository.ClockedRepo) <-chan StreamedIdentity {
+func ReadAllLocal(repo repository.ClockedRepo) <-chan entity.StreamedEntity[*Identity] {
return readAll(repo, identityRefPattern)
}
// ReadAllRemote read and parse all remote Identity for a given remote
-func ReadAllRemote(repo repository.ClockedRepo, remote string) <-chan StreamedIdentity {
+func ReadAllRemote(repo repository.ClockedRepo, remote string) <-chan entity.StreamedEntity[*Identity] {
refPrefix := fmt.Sprintf(identityRemoteRefPattern, remote)
return readAll(repo, refPrefix)
}
// readAll read and parse all available bug with a given ref prefix
-func readAll(repo repository.ClockedRepo, refPrefix string) <-chan StreamedIdentity {
- out := make(chan StreamedIdentity)
+func readAll(repo repository.ClockedRepo, refPrefix string) <-chan entity.StreamedEntity[*Identity] {
+ out := make(chan entity.StreamedEntity[*Identity])
go func() {
defer close(out)
refs, err := repo.ListRefs(refPrefix)
if err != nil {
- out <- StreamedIdentity{Err: err}
+ out <- entity.StreamedEntity[*Identity]{Err: err}
return
}
for _, ref := range refs {
- b, err := read(repo, ref)
+ i, err := read(repo, ref)
if err != nil {
- out <- StreamedIdentity{Err: err}
+ out <- entity.StreamedEntity[*Identity]{Err: err}
return
}
- out <- StreamedIdentity{Identity: b}
+ out <- entity.StreamedEntity[*Identity]{Entity: i}
}
}()
@@ -308,7 +302,7 @@ func (i *Identity) Mutate(repo repository.RepoClock, f func(orig *Mutator)) erro
return nil
}
-// Write the identity into the Repository. In particular, this ensure that
+// Commit write the identity into the Repository. In particular, this ensures that
// the Id is properly set.
func (i *Identity) Commit(repo repository.ClockedRepo) error {
if !i.NeedCommit() {
diff --git a/entities/identity/identity_actions.go b/entities/identity/identity_actions.go
index b58bb2d9..13776078 100644
--- a/entities/identity/identity_actions.go
+++ b/entities/identity/identity_actions.go
@@ -13,12 +13,12 @@ import (
// Fetch retrieve updates from a remote
// This does not change the local identities state
func Fetch(repo repository.Repo, remote string) (string, error) {
- return repo.FetchRefs(remote, "identities")
+ return repo.FetchRefs(remote, Namespace)
}
// Push update a remote with the local changes
func Push(repo repository.Repo, remote string) (string, error) {
- return repo.PushRefs(remote, "identities")
+ return repo.PushRefs(remote, Namespace)
}
// Pull will do a Fetch + MergeAll
diff --git a/entities/identity/identity_actions_test.go b/entities/identity/identity_actions_test.go
index 351fb7a4..e9626cb9 100644
--- a/entities/identity/identity_actions_test.go
+++ b/entities/identity/identity_actions_test.go
@@ -5,6 +5,7 @@ import (
"github.com/stretchr/testify/require"
+ "github.com/MichaelMure/git-bug/entity"
"github.com/MichaelMure/git-bug/repository"
)
@@ -145,13 +146,13 @@ func TestIdentityPushPull(t *testing.T) {
}
}
-func allIdentities(t testing.TB, identities <-chan StreamedIdentity) []*Identity {
+func allIdentities(t testing.TB, identities <-chan entity.StreamedEntity[*Identity]) []*Identity {
var result []*Identity
for streamed := range identities {
if streamed.Err != nil {
t.Fatal(streamed.Err)
}
- result = append(result, streamed.Identity)
+ result = append(result, streamed.Entity)
}
return result
}
diff --git a/entities/identity/identity_test.go b/entities/identity/identity_test.go
index f0c3bbe9..0ecc8058 100644
--- a/entities/identity/identity_test.go
+++ b/entities/identity/identity_test.go
@@ -6,6 +6,7 @@ import (
"github.com/stretchr/testify/require"
+ "github.com/MichaelMure/git-bug/entity"
"github.com/MichaelMure/git-bug/repository"
"github.com/MichaelMure/git-bug/util/lamport"
)
@@ -278,13 +279,13 @@ func TestIdentityRemove(t *testing.T) {
require.NoError(t, err)
_, err = ReadLocal(repo, rene.Id())
- require.Error(t, ErrIdentityNotExist, err)
+ require.ErrorAs(t, entity.ErrNotFound{}, err)
_, err = ReadRemote(repo, "remoteA", string(rene.Id()))
- require.Error(t, ErrIdentityNotExist, err)
+ require.ErrorAs(t, entity.ErrNotFound{}, err)
_, err = ReadRemote(repo, "remoteB", string(rene.Id()))
- require.Error(t, ErrIdentityNotExist, err)
+ require.ErrorAs(t, entity.ErrNotFound{}, err)
ids, err := ListLocalIds(repo)
require.NoError(t, err)
diff --git a/entities/identity/identity_user.go b/entities/identity/identity_user.go
index cd67459e..e671e662 100644
--- a/entities/identity/identity_user.go
+++ b/entities/identity/identity_user.go
@@ -23,7 +23,7 @@ func GetUserIdentity(repo repository.Repo) (*Identity, error) {
}
i, err := ReadLocal(repo, id)
- if err == ErrIdentityNotExist {
+ if entity.IsErrNotFound(err) {
innerErr := repo.LocalConfig().RemoveAll(identityConfigKey)
if innerErr != nil {
_, _ = fmt.Fprintln(os.Stderr, errors.Wrap(innerErr, "can't clear user identity").Error())
diff --git a/entities/identity/resolver.go b/entities/identity/resolver.go
index 5468a8f8..a4b676f3 100644
--- a/entities/identity/resolver.go
+++ b/entities/identity/resolver.go
@@ -16,19 +16,6 @@ func NewSimpleResolver(repo repository.Repo) *SimpleResolver {
return &SimpleResolver{repo: repo}
}
-func (r *SimpleResolver) Resolve(id entity.Id) (entity.Interface, error) {
+func (r *SimpleResolver) Resolve(id entity.Id) (entity.Resolved, error) {
return ReadLocal(r.repo, id)
}
-
-var _ entity.Resolver = &StubResolver{}
-
-// StubResolver is a Resolver that doesn't load anything, only returning IdentityStub instances
-type StubResolver struct{}
-
-func NewStubResolver() *StubResolver {
- return &StubResolver{}
-}
-
-func (s *StubResolver) Resolve(id entity.Id) (entity.Interface, error) {
- return &IdentityStub{id: id}, nil
-}