Skip to content

Commit

Permalink
add cleanup sub-command that remove local bugs and identities
Browse files Browse the repository at this point in the history
  • Loading branch information
zinderic committed Nov 26, 2022
1 parent 70bd737 commit a985d33
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 15 deletions.
43 changes: 43 additions & 0 deletions commands/cleanup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package commands

import (
"fmt"
"github.com/MichaelMure/git-bug/entities/bug"
"github.com/MichaelMure/git-bug/entities/identity"

"github.com/spf13/cobra"

"github.com/MichaelMure/git-bug/commands/execenv"
)

func newCleanUpCommand() *cobra.Command {
env := execenv.NewEnv()

cmd := &cobra.Command{
Use: "cleanup",
Short: "Cleanup git-bug from git repository",
Long: "Cleanup git-bug from git repository",
PreRunE: execenv.LoadRepo(env),
RunE: execenv.CloseBackend(env, func(cmd *cobra.Command, args []string) error {
return runCleanup(env)
}),
}

return cmd
}

func runCleanup(env *execenv.Env) error {

fmt.Println("cleaning bug items..")
err := bug.RemoveAll(env.Repo)
if err != nil {
return err
}
fmt.Println("cleaning identities..")
err = identity.RemoveAll(env.Repo)
if err != nil {
return err
}

return nil
}
5 changes: 3 additions & 2 deletions commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (

"github.com/spf13/cobra"

"github.com/MichaelMure/git-bug/commands/bridge"
bridgecmd "github.com/MichaelMure/git-bug/commands/bridge"
usercmd "github.com/MichaelMure/git-bug/commands/user"

"github.com/MichaelMure/git-bug/commands/bug"
bugcmd "github.com/MichaelMure/git-bug/commands/bug"
"github.com/MichaelMure/git-bug/commands/execenv"
)

Expand Down Expand Up @@ -82,6 +82,7 @@ the same git remote you are already using to collaborate with other people.

cmd.AddCommand(newCommandsCommand())
cmd.AddCommand(newVersionCommand())
cmd.AddCommand(newCleanUpCommand())

return cmd
}
Expand Down
4 changes: 4 additions & 0 deletions entities/bug/bug_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,7 @@ 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)
}

func RemoveAll(repo repository.ClockedRepo) error {
return dag.RemoveAll(def, repo)
}
15 changes: 15 additions & 0 deletions entities/identity/identity_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,18 @@ func MergeAll(repo repository.ClockedRepo, remote string) <-chan entity.MergeRes

return out
}

func RemoveAll(repo repository.ClockedRepo) error {
localIds, err := ListLocalIds(repo)
if err != nil {
return err
}
for _, id := range localIds {
fmt.Println("removing", id.Human())
err = RemoveIdentity(repo, id)
if err != nil {
return err
}
}
return nil
}
45 changes: 32 additions & 13 deletions entity/dag/entity_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dag

import (
"fmt"

"github.com/pkg/errors"

"github.com/MichaelMure/git-bug/entities/identity"
Expand Down Expand Up @@ -53,18 +52,18 @@ func Pull(def Definition, repo repository.ClockedRepo, resolvers entity.Resolver
// MergeAll will merge all the available remote Entity:
//
// Multiple scenario exist:
// 1. if the remote Entity doesn't exist locally, it's created
// --> emit entity.MergeStatusNew
// 2. if the remote and local Entity have the same state, nothing is changed
// --> emit entity.MergeStatusNothing
// 3. if the local Entity has new commits but the remote don't, nothing is changed
// --> emit entity.MergeStatusNothing
// 4. if the remote has new commit, the local bug is updated to match the same history
// (fast-forward update)
// --> emit entity.MergeStatusUpdated
// 5. if both local and remote Entity have new commits (that is, we have a concurrent edition),
// a merge commit with an empty operationPack is created to join both branch and form a DAG.
// --> emit entity.MergeStatusUpdated
// 1. if the remote Entity doesn't exist locally, it's created
// --> emit entity.MergeStatusNew
// 2. if the remote and local Entity have the same state, nothing is changed
// --> emit entity.MergeStatusNothing
// 3. if the local Entity has new commits but the remote don't, nothing is changed
// --> emit entity.MergeStatusNothing
// 4. if the remote has new commit, the local bug is updated to match the same history
// (fast-forward update)
// --> emit entity.MergeStatusUpdated
// 5. if both local and remote Entity have new commits (that is, we have a concurrent edition),
// a merge commit with an empty operationPack is created to join both branch and form a DAG.
// --> emit entity.MergeStatusUpdated
//
// Note: an author is necessary for the case where a merge commit is created, as this commit will
// have an author and may be signed if a signing key is available.
Expand Down Expand Up @@ -258,3 +257,23 @@ func Remove(def Definition, repo repository.ClockedRepo, id entity.Id) error {

return nil
}

// RemoveAll delete all Entity items for all identities
func RemoveAll(def Definition, repo repository.ClockedRepo) error {
localIds, err := ListLocalIds(def, repo)
if err != nil {
return err
}
for _, id := range localIds {
fmt.Println("removing bug " + id.String())
err = Remove(def, repo, id)
if err != nil {
return err
}
}
err = repo.ClearBleveIndex(def.Typename)
if err != nil {
return err
}
return nil
}
33 changes: 33 additions & 0 deletions entity/dag/entity_actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,36 @@ func TestRemove(t *testing.T) {
err = Remove(def, repoA, e.Id())
require.NoError(t, err)
}

func TestRemoveAll(t *testing.T) {
type args struct {
def Definition
repo repository.ClockedRepo
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "foo",
args: args{
def: Definition{
Typename: "foo",
Namespace: "foo",
OperationUnmarshaler: nil,
FormatVersion: 0,
},
repo: repository.NewMockRepo(),
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := RemoveAll(tt.args.def, tt.args.repo); (err != nil) != tt.wantErr {
t.Errorf("RemoveAll() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

0 comments on commit a985d33

Please sign in to comment.