From 647b05eee4b612f33a2f22576821ea1c012d7c84 Mon Sep 17 00:00:00 2001 From: vince Date: Thu, 19 Nov 2020 20:57:57 +0800 Subject: add remove identity feature also adds: - listlocalidentities - refactors refstoids into entity package --- identity/identity.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'identity/identity.go') diff --git a/identity/identity.go b/identity/identity.go index b7bf9e64..8182e263 100644 --- a/identity/identity.go +++ b/identity/identity.go @@ -27,6 +27,10 @@ var ErrNoIdentitySet = errors.New("No identity is set.\n" + "\"git bug user create\"") 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{} @@ -175,6 +179,66 @@ func read(repo repository.Repo, ref string) (*Identity, error) { return i, nil } +// ListLocalIds list all the available local identity ids +func ListLocalIds(repo repository.Repo) ([]entity.Id, error) { + refs, err := repo.ListRefs(identityRefPattern) + if err != nil { + return nil, err + } + + return entity.RefsToIds(refs), nil +} + +// RemoveIdentity will remove a local identity from its entity.Id +func RemoveIdentity(repo repository.ClockedRepo, id entity.Id) error { + var fullMatches []string + + refs, err := repo.ListRefs(identityRefPattern + id.String()) + if err != nil { + return err + } + if len(refs) > 1 { + return NewErrMultipleMatchIdentity(entity.RefsToIds(refs)) + } + if len(refs) == 1 { + // we have the identity locally + fullMatches = append(fullMatches, refs[0]) + } + + remotes, err := repo.GetRemotes() + if err != nil { + return err + } + + for remote := range remotes { + remotePrefix := fmt.Sprintf(identityRemoteRefPattern+id.String(), remote) + remoteRefs, err := repo.ListRefs(remotePrefix) + if err != nil { + return err + } + if len(remoteRefs) > 1 { + return NewErrMultipleMatchIdentity(entity.RefsToIds(refs)) + } + if len(remoteRefs) == 1 { + // found the identity in a remote + fullMatches = append(fullMatches, remoteRefs[0]) + } + } + + if len(fullMatches) == 0 { + return ErrIdentityNotExist + } + + for _, ref := range fullMatches { + err = repo.RemoveRef(ref) + if err != nil { + return err + } + } + + return nil +} + type StreamedIdentity struct { Identity *Identity Err error -- cgit