aboutsummaryrefslogtreecommitdiffstats
path: root/entities
diff options
context:
space:
mode:
authorKalin Staykov <k.t.staykov@gmail.com>2022-11-26 15:49:59 +0200
committerMichael Muré <batolettre@gmail.com>2023-01-11 14:58:58 +0100
commitfc266b733cf0ea794209031e3500ab3f86db6cee (patch)
treebae1996ec19fb74446429200e52cdb942f60ac54 /entities
parent9c50a359704f4edd2f33df6d256e032feae3a576 (diff)
downloadgit-bug-fc266b733cf0ea794209031e3500ab3f86db6cee.tar.gz
add wipe sub-command that remove local bugs and identities
Diffstat (limited to 'entities')
-rw-r--r--entities/bug/bug_actions.go6
-rw-r--r--entities/identity/identity.go50
-rw-r--r--entities/identity/identity_actions.go71
-rw-r--r--entities/identity/identity_test.go2
-rw-r--r--entities/identity/identity_user.go4
5 files changed, 82 insertions, 51 deletions
diff --git a/entities/bug/bug_actions.go b/entities/bug/bug_actions.go
index 198e4ed0..651d24dc 100644
--- a/entities/bug/bug_actions.go
+++ b/entities/bug/bug_actions.go
@@ -37,3 +37,9 @@ func MergeAll(repo repository.ClockedRepo, resolvers entity.Resolvers, remote st
func Remove(repo repository.ClockedRepo, id entity.Id) error {
return dag.Remove(def, repo, id)
}
+
+// RemoveAll will remove all local bugs.
+// RemoveAll is idempotent.
+func RemoveAll(repo repository.ClockedRepo) error {
+ return dag.RemoveAll(def, repo)
+}
diff --git a/entities/identity/identity.go b/entities/identity/identity.go
index 22ce652c..7c91b7b3 100644
--- a/entities/identity/identity.go
+++ b/entities/identity/identity.go
@@ -164,56 +164,6 @@ func ListLocalIds(repo repository.Repo) ([]entity.Id, error) {
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 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
-}
-
// ReadAllLocal read and parse all local Identity
func ReadAllLocal(repo repository.ClockedRepo) <-chan entity.StreamedEntity[*Identity] {
return readAll(repo, identityRefPattern)
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
+}
diff --git a/entities/identity/identity_test.go b/entities/identity/identity_test.go
index 0ecc8058..85d5385b 100644
--- a/entities/identity/identity_test.go
+++ b/entities/identity/identity_test.go
@@ -275,7 +275,7 @@ func TestIdentityRemove(t *testing.T) {
_, err = Fetch(repo, "remoteB")
require.NoError(t, err)
- err = RemoveIdentity(repo, rene.Id())
+ err = Remove(repo, rene.Id())
require.NoError(t, err)
_, err = ReadLocal(repo, rene.Id())
diff --git a/entities/identity/identity_user.go b/entities/identity/identity_user.go
index 7eb374d4..f9e39bb2 100644
--- a/entities/identity/identity_user.go
+++ b/entities/identity/identity_user.go
@@ -15,6 +15,10 @@ func SetUserIdentity(repo repository.RepoConfig, identity *Identity) error {
return repo.LocalConfig().StoreString(identityConfigKey, identity.Id().String())
}
+func ClearUserIdentity(repo repository.RepoConfig) error {
+ return repo.LocalConfig().RemoveAll(identityConfigKey)
+}
+
// GetUserIdentity read the current user identity, set with a git config entry
func GetUserIdentity(repo repository.Repo) (*Identity, error) {
id, err := GetUserIdentityId(repo)