From fc266b733cf0ea794209031e3500ab3f86db6cee Mon Sep 17 00:00:00 2001 From: Kalin Staykov Date: Sat, 26 Nov 2022 15:49:59 +0200 Subject: add wipe sub-command that remove local bugs and identities --- entities/identity/identity_actions.go | 71 +++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) (limited to 'entities/identity/identity_actions.go') diff --git a/entities/identity/identity_actions.go b/entities/identity/identity_actions.go index 13776078..07560dc0 100644 --- a/entities/identity/identity_actions.go +++ b/entities/identity/identity_actions.go @@ -123,3 +123,74 @@ func MergeAll(repo repository.ClockedRepo, remote string) <-chan entity.MergeRes return out } + +// Remove will remove a local identity from its entity.Id. +// It is left as a responsibility to the caller to make sure that this identities is not +// linked from another entity, otherwise it would break it. +// Remove is idempotent. +func Remove(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 entity.NewErrMultipleMatch(Typename, 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 entity.NewErrMultipleMatch(Typename, entity.RefsToIds(refs)) + } + if len(remoteRefs) == 1 { + // found the identity in a remote + fullMatches = append(fullMatches, remoteRefs[0]) + } + } + + if len(fullMatches) == 0 { + return entity.NewErrNotFound(Typename) + } + + for _, ref := range fullMatches { + err = repo.RemoveRef(ref) + if err != nil { + return err + } + } + + return nil +} + +// RemoveAll will remove all local identities. +// It is left as a responsibility to the caller to make sure that those identities are not +// linked from another entity, otherwise it would break them. +// RemoveAll is idempotent. +func RemoveAll(repo repository.ClockedRepo) error { + localIds, err := ListLocalIds(repo) + if err != nil { + return err + } + for _, id := range localIds { + err = Remove(repo, id) + if err != nil { + return err + } + } + return nil +} -- cgit