From 36f300cb35b203310e923cf956310c7f20ed7406 Mon Sep 17 00:00:00 2001 From: vince Date: Thu, 16 Jul 2020 17:12:48 +0800 Subject: [PATCH 1/8] Add the 'rm' command This commit adds a command that removes a bug from the repository, given a prefix. --- cache/repo_cache_bug.go | 18 +++++++++++++++++ commands/rm.go | 45 +++++++++++++++++++++++++++++++++++++++++ commands/root.go | 1 + 3 files changed, 64 insertions(+) create mode 100644 commands/rm.go diff --git a/cache/repo_cache_bug.go b/cache/repo_cache_bug.go index 306923633a4e4f0772c651a15ced0ca4140cec16..0492e7f1237905885ef5d9972ae19f944df2c137 100644 --- a/cache/repo_cache_bug.go +++ b/cache/repo_cache_bug.go @@ -359,3 +359,21 @@ func (c *RepoCache) NewBugRaw(author *IdentityCache, unixTime int64, title strin return cached, op, nil } + +// RemoveBug removes a bug from the cache and repo +func (c *RepoCache) RemoveBug(prefix string) error { + b, err := c.ResolveBugPrefix(prefix) + if err != nil { + return err + } + + err = bug.RemoveLocalBug(c.repo, b.Id()) + if err != nil { + return err + } + + delete(c.bugs, b.Id()) + delete(c.bugExcerpts, b.Id()) + + return c.writeBugCache() +} diff --git a/commands/rm.go b/commands/rm.go new file mode 100644 index 0000000000000000000000000000000000000000..7b34a6299f8027092196210518fb0f697c971ff2 --- /dev/null +++ b/commands/rm.go @@ -0,0 +1,45 @@ +package commands + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +type rmOptions struct { +} + +func newRmCommand() *cobra.Command { + env := newEnv() + options := rmOptions{} + + cmd := &cobra.Command{ + Use: "rm ", + Short: "Remove an existing bug.", + PreRunE: loadBackendEnsureUser(env), + PostRunE: closeBackend(env), + RunE: func(cmd *cobra.Command, args []string) error { + return runRm(env, options, args) + }, + } + + flags := cmd.Flags() + flags.SortFlags = false + + return cmd +} + +func runRm(env *Env, opts rmOptions, args []string) error { + if len(args) == 0 { + return fmt.Errorf("you must provide a bug id prefix to remove") + } + + err := env.backend.RemoveBug(args[0]) + if err != nil { + return err + } + + env.out.Printf("bug %s removed\n", args[0]) + + return nil +} diff --git a/commands/root.go b/commands/root.go index a67fec1a3f69c4323dd3fedcee3d273ea03d6f3b..e7848363ba4f80edc69079ff9e51900f670e509e 100644 --- a/commands/root.go +++ b/commands/root.go @@ -71,6 +71,7 @@ _git_bug() { cmd.AddCommand(newLsLabelCommand()) cmd.AddCommand(newPullCommand()) cmd.AddCommand(newPushCommand()) + cmd.AddCommand(newRmCommand()) cmd.AddCommand(newSelectCommand()) cmd.AddCommand(newShowCommand()) cmd.AddCommand(newStatusCommand()) From 4e4ca106aea25da74f1df49d33f4eaa272a6e8f0 Mon Sep 17 00:00:00 2001 From: vince Date: Mon, 20 Jul 2020 09:55:14 +0800 Subject: [PATCH 2/8] Allow user to delete remote bugs --- bug/bug.go | 7 +++++++ cache/repo_cache_bug.go | 17 ++++++++++++++--- commands/rm.go | 22 +++++++--------------- 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/bug/bug.go b/bug/bug.go index 04bd599602375e0457edf4e96faf6e594002fddc..95c4325fb929e3c3d948036c00896aab6b169980 100644 --- a/bug/bug.go +++ b/bug/bug.go @@ -242,11 +242,18 @@ func readBug(repo repository.ClockedRepo, ref string) (*Bug, error) { return &bug, nil } +// RemoveLocalBug will remove a local bug from its hash func RemoveLocalBug(repo repository.ClockedRepo, id entity.Id) error { ref := bugsRefPattern + id.String() return repo.RemoveRef(ref) } +// RemoveRemoteBug will remove a remote bug locally from its hash +func RemoveRemoteBug(repo repository.ClockedRepo, remote string, id entity.Id) error { + ref := fmt.Sprintf(bugsRemoteRefPattern, remote) + id.String() + return repo.RemoveRef(ref) +} + type StreamedBug struct { Bug *Bug Err error diff --git a/cache/repo_cache_bug.go b/cache/repo_cache_bug.go index 0492e7f1237905885ef5d9972ae19f944df2c137..0c26cb389ec40bf012047edcc1d74cc3df441c1e 100644 --- a/cache/repo_cache_bug.go +++ b/cache/repo_cache_bug.go @@ -361,13 +361,24 @@ func (c *RepoCache) NewBugRaw(author *IdentityCache, unixTime int64, title strin } // RemoveBug removes a bug from the cache and repo -func (c *RepoCache) RemoveBug(prefix string) error { - b, err := c.ResolveBugPrefix(prefix) +// args[0] specifies the bug prefix to remove +// args[1] (if present) specifies the remote the bug was imported from +func (c *RepoCache) RemoveBug(args []string) error { + if len(args) == 0 { + return fmt.Errorf("you must provide a bug prefix to remove") + } + + b, err := c.ResolveBugPrefix(args[0]) + if err != nil { return err } - err = bug.RemoveLocalBug(c.repo, b.Id()) + if len(args) == 1 { + err = bug.RemoveLocalBug(c.repo, b.Id()) + } else { + err = bug.RemoveRemoteBug(c.repo, args[1], b.Id()) + } if err != nil { return err } diff --git a/commands/rm.go b/commands/rm.go index 7b34a6299f8027092196210518fb0f697c971ff2..718fb4a34adfdd764404f10ec8e227cd356ae27d 100644 --- a/commands/rm.go +++ b/commands/rm.go @@ -1,25 +1,20 @@ package commands import ( - "fmt" - "github.com/spf13/cobra" ) -type rmOptions struct { -} - func newRmCommand() *cobra.Command { env := newEnv() - options := rmOptions{} cmd := &cobra.Command{ - Use: "rm ", + Use: "rm []", Short: "Remove an existing bug.", + Long: "Remove an existing bug in the local repository. If the bug was imported from a bridge, specify the remote name to remove it from. Note removing bugs that were imported from bridges will not remove the bug remote, and will only remove the local copy of the bug.", PreRunE: loadBackendEnsureUser(env), PostRunE: closeBackend(env), RunE: func(cmd *cobra.Command, args []string) error { - return runRm(env, options, args) + return runRm(env, args) }, } @@ -29,17 +24,14 @@ func newRmCommand() *cobra.Command { return cmd } -func runRm(env *Env, opts rmOptions, args []string) error { - if len(args) == 0 { - return fmt.Errorf("you must provide a bug id prefix to remove") - } +func runRm(env *Env, args []string) (err error) { + err = env.backend.RemoveBug(args) - err := env.backend.RemoveBug(args[0]) if err != nil { - return err + return } env.out.Printf("bug %s removed\n", args[0]) - return nil + return } From 7dbcca84c7a0b6658463e3e54690b8860b662b6e Mon Sep 17 00:00:00 2001 From: vince Date: Mon, 20 Jul 2020 18:08:37 +0800 Subject: [PATCH 3/8] Add test for removing bug from cache --- cache/repo_cache_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cache/repo_cache_test.go b/cache/repo_cache_test.go index 0a333c8f31bda8ce827595e270ba180b05468ee3..276da4be0680d2a80de1ad0eb69fa1fc971d9298 100644 --- a/cache/repo_cache_test.go +++ b/cache/repo_cache_test.go @@ -101,6 +101,11 @@ func TestCache(t *testing.T) { require.NoError(t, err) _, err = cache.ResolveBugPrefix(bug1.Id().String()[:10]) require.NoError(t, err) + + // Possible to delete a bug + err = cache.RemoveBug([]string{bug1.Id().Human()}) + require.NoError(t, err) + require.Equal(t, len(cache.AllBugsIds()), 1) } func TestPushPull(t *testing.T) { From 9436cf4b8983699ef7a164f4036686dd83d345d4 Mon Sep 17 00:00:00 2001 From: vince Date: Tue, 21 Jul 2020 11:41:46 +0800 Subject: [PATCH 4/8] Move args parsing out of repo cache --- cache/repo_cache_bug.go | 12 ++++-------- cache/repo_cache_test.go | 2 +- commands/rm.go | 12 +++++++++++- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/cache/repo_cache_bug.go b/cache/repo_cache_bug.go index 0c26cb389ec40bf012047edcc1d74cc3df441c1e..c7e2ed34a74aa0eb995d885ef53523c9ba3c2549 100644 --- a/cache/repo_cache_bug.go +++ b/cache/repo_cache_bug.go @@ -363,21 +363,17 @@ func (c *RepoCache) NewBugRaw(author *IdentityCache, unixTime int64, title strin // RemoveBug removes a bug from the cache and repo // args[0] specifies the bug prefix to remove // args[1] (if present) specifies the remote the bug was imported from -func (c *RepoCache) RemoveBug(args []string) error { - if len(args) == 0 { - return fmt.Errorf("you must provide a bug prefix to remove") - } - - b, err := c.ResolveBugPrefix(args[0]) +func (c *RepoCache) RemoveBug(prefix string, remote string) error { + b, err := c.ResolveBugPrefix(prefix) if err != nil { return err } - if len(args) == 1 { + if remote == "" { err = bug.RemoveLocalBug(c.repo, b.Id()) } else { - err = bug.RemoveRemoteBug(c.repo, args[1], b.Id()) + err = bug.RemoveRemoteBug(c.repo, remote, b.Id()) } if err != nil { return err diff --git a/cache/repo_cache_test.go b/cache/repo_cache_test.go index 276da4be0680d2a80de1ad0eb69fa1fc971d9298..e5f8a0373dcaf51605278286533d24314fa9147b 100644 --- a/cache/repo_cache_test.go +++ b/cache/repo_cache_test.go @@ -103,7 +103,7 @@ func TestCache(t *testing.T) { require.NoError(t, err) // Possible to delete a bug - err = cache.RemoveBug([]string{bug1.Id().Human()}) + err = cache.RemoveBug(bug1.Id().Human(), "") require.NoError(t, err) require.Equal(t, len(cache.AllBugsIds()), 1) } diff --git a/commands/rm.go b/commands/rm.go index 718fb4a34adfdd764404f10ec8e227cd356ae27d..800f16e13899e86fe737286a38e310cfff58c357 100644 --- a/commands/rm.go +++ b/commands/rm.go @@ -1,6 +1,8 @@ package commands import ( + "errors" + "github.com/spf13/cobra" ) @@ -25,7 +27,15 @@ func newRmCommand() *cobra.Command { } func runRm(env *Env, args []string) (err error) { - err = env.backend.RemoveBug(args) + switch len(args) { + case 1: + err = env.backend.RemoveBug(args[0], "") + break + case 2: + err = env.backend.RemoveBug(args[0], args[1]) + default: + return errors.New("invalid number of arguments for rm command") + } if err != nil { return From 4e5f377d75824e31a058313cad2d2e478f348c28 Mon Sep 17 00:00:00 2001 From: vince Date: Fri, 24 Jul 2020 11:25:38 +0800 Subject: [PATCH 5/8] Remove need to specify remote This commit makes the removeBug command use the listRefs repo command to search for the bug, eliminating the need to input the remote the bug came from. --- bug/bug.go | 21 ++++++++++++--------- cache/repo_cache_bug.go | 13 ++----------- commands/rm.go | 14 +++++--------- 3 files changed, 19 insertions(+), 29 deletions(-) diff --git a/bug/bug.go b/bug/bug.go index 95c4325fb929e3c3d948036c00896aab6b169980..148e49bf83864d97c1aaae2f09de89e9397e8bf9 100644 --- a/bug/bug.go +++ b/bug/bug.go @@ -243,15 +243,18 @@ func readBug(repo repository.ClockedRepo, ref string) (*Bug, error) { } // RemoveLocalBug will remove a local bug from its hash -func RemoveLocalBug(repo repository.ClockedRepo, id entity.Id) error { - ref := bugsRefPattern + id.String() - return repo.RemoveRef(ref) -} - -// RemoveRemoteBug will remove a remote bug locally from its hash -func RemoveRemoteBug(repo repository.ClockedRepo, remote string, id entity.Id) error { - ref := fmt.Sprintf(bugsRemoteRefPattern, remote) + id.String() - return repo.RemoveRef(ref) +func RemoveBug(repo repository.ClockedRepo, id entity.Id) error { + refs, err := repo.ListRefs(id.String()) + if err != nil { + return err + } + for _, ref := range refs { + err = repo.RemoveRef(ref) + if err != nil { + return err + } + } + return nil } type StreamedBug struct { diff --git a/cache/repo_cache_bug.go b/cache/repo_cache_bug.go index c7e2ed34a74aa0eb995d885ef53523c9ba3c2549..34e2a14426b81e1c4c409378b09442ab89a4a10a 100644 --- a/cache/repo_cache_bug.go +++ b/cache/repo_cache_bug.go @@ -361,23 +361,14 @@ func (c *RepoCache) NewBugRaw(author *IdentityCache, unixTime int64, title strin } // RemoveBug removes a bug from the cache and repo -// args[0] specifies the bug prefix to remove -// args[1] (if present) specifies the remote the bug was imported from -func (c *RepoCache) RemoveBug(prefix string, remote string) error { +func (c *RepoCache) RemoveBug(prefix string) error { b, err := c.ResolveBugPrefix(prefix) if err != nil { return err } - if remote == "" { - err = bug.RemoveLocalBug(c.repo, b.Id()) - } else { - err = bug.RemoveRemoteBug(c.repo, remote, b.Id()) - } - if err != nil { - return err - } + err = bug.RemoveBug(c.repo, b.Id()) delete(c.bugs, b.Id()) delete(c.bugExcerpts, b.Id()) diff --git a/commands/rm.go b/commands/rm.go index 800f16e13899e86fe737286a38e310cfff58c357..90a79539bef554ce72e5fddf4b54baabbbb20cf0 100644 --- a/commands/rm.go +++ b/commands/rm.go @@ -10,7 +10,7 @@ func newRmCommand() *cobra.Command { env := newEnv() cmd := &cobra.Command{ - Use: "rm []", + Use: "rm ", Short: "Remove an existing bug.", Long: "Remove an existing bug in the local repository. If the bug was imported from a bridge, specify the remote name to remove it from. Note removing bugs that were imported from bridges will not remove the bug remote, and will only remove the local copy of the bug.", PreRunE: loadBackendEnsureUser(env), @@ -27,16 +27,12 @@ func newRmCommand() *cobra.Command { } func runRm(env *Env, args []string) (err error) { - switch len(args) { - case 1: - err = env.backend.RemoveBug(args[0], "") - break - case 2: - err = env.backend.RemoveBug(args[0], args[1]) - default: - return errors.New("invalid number of arguments for rm command") + if len(args) == 0 { + return errors.New("you must provide a bug prefix to remove") } + err = env.backend.RemoveBug(args[0]) + if err != nil { return } From 6e315ea131553d42768e678d5f86445dd3c54aa4 Mon Sep 17 00:00:00 2001 From: vince Date: Sun, 26 Jul 2020 15:55:25 +0800 Subject: [PATCH 6/8] Update docs --- cache/repo_cache_bug.go | 2 +- commands/rm.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cache/repo_cache_bug.go b/cache/repo_cache_bug.go index 34e2a14426b81e1c4c409378b09442ab89a4a10a..bcbfcea3b542216cbc1561a902d0f4339ec3c1ce 100644 --- a/cache/repo_cache_bug.go +++ b/cache/repo_cache_bug.go @@ -360,7 +360,7 @@ func (c *RepoCache) NewBugRaw(author *IdentityCache, unixTime int64, title strin return cached, op, nil } -// RemoveBug removes a bug from the cache and repo +// RemoveBug removes a bug from the cache and repo given a bug id prefix func (c *RepoCache) RemoveBug(prefix string) error { b, err := c.ResolveBugPrefix(prefix) diff --git a/commands/rm.go b/commands/rm.go index 90a79539bef554ce72e5fddf4b54baabbbb20cf0..09f6a6ccf7b5b4379b1b146fc137f4fd1b1df28b 100644 --- a/commands/rm.go +++ b/commands/rm.go @@ -12,7 +12,7 @@ func newRmCommand() *cobra.Command { cmd := &cobra.Command{ Use: "rm ", Short: "Remove an existing bug.", - Long: "Remove an existing bug in the local repository. If the bug was imported from a bridge, specify the remote name to remove it from. Note removing bugs that were imported from bridges will not remove the bug remote, and will only remove the local copy of the bug.", + Long: "Remove an existing bug in the local repository. Note removing bugs that were imported from bridges will not remove the bug on the remote, and will only remove the local copy of the bug.", PreRunE: loadBackendEnsureUser(env), PostRunE: closeBackend(env), RunE: func(cmd *cobra.Command, args []string) error { From 18ddc775905bdeabf2be4afb6fe62d2013b06dec Mon Sep 17 00:00:00 2001 From: vince Date: Sun, 26 Jul 2020 16:14:11 +0800 Subject: [PATCH 7/8] Fix bug tests --- bug/bug.go | 55 +++++++++++++++++++++++++++---- bug/bug_test.go | 71 +++++++++++++++++++++++++++++++++------- cache/repo_cache_test.go | 53 +++++++++++++++++++++++++++--- 3 files changed, 156 insertions(+), 23 deletions(-) diff --git a/bug/bug.go b/bug/bug.go index 148e49bf83864d97c1aaae2f09de89e9397e8bf9..a47cd9dbc5134087b4c5fa79b598886be33e7a86 100644 --- a/bug/bug.go +++ b/bug/bug.go @@ -109,8 +109,8 @@ func ReadLocalBug(repo repository.ClockedRepo, id entity.Id) (*Bug, error) { } // ReadRemoteBug will read a remote bug from its hash -func ReadRemoteBug(repo repository.ClockedRepo, remote string, id string) (*Bug, error) { - ref := fmt.Sprintf(bugsRemoteRefPattern, remote) + id +func ReadRemoteBug(repo repository.ClockedRepo, remote string, id entity.Id) (*Bug, error) { + ref := fmt.Sprintf(bugsRemoteRefPattern, remote) + id.String() return readBug(repo, ref) } @@ -242,18 +242,55 @@ func readBug(repo repository.ClockedRepo, ref string) (*Bug, error) { return &bug, nil } -// RemoveLocalBug will remove a local bug from its hash +// RemoveBug will remove a local bug from its entity.Id func RemoveBug(repo repository.ClockedRepo, id entity.Id) error { - refs, err := repo.ListRefs(id.String()) + matching := entity.Id("") + fullMatches := []string{} + + refs, err := repo.ListRefs(bugsRefPattern + id.String()) + if err != nil { + return err + } else if num := len(refs); num > 1 { + return NewErrMultipleMatchBug(refsToIds(refs)) + } else if num == 1 { + matching = refToId(refs[0]) + fullMatches = []string{refs[0]} + } + + remotes, err := repo.GetRemotes() if err != nil { return err } - for _, ref := range refs { + + for remote := range remotes { + remotePrefix := fmt.Sprintf(bugsRemoteRefPattern+id.String(), remote) + remoteRefs, err := repo.ListRefs(remotePrefix) + if err != nil { + return err + } else if num := len(remoteRefs); num > 1 { + return NewErrMultipleMatchBug(refsToIds(refs)) + } else if num == 1 { + id := refToId(remoteRefs[0]) + fullMatches = append(fullMatches, remoteRefs[0]) + if matching.String() == "" { + matching = id + } else if id != matching { + return NewErrMultipleMatchBug([]entity.Id{matching, id}) + } + } + } + + if matching == "" { + return ErrBugNotExist + } + + for _, ref := range fullMatches { err = repo.RemoveRef(ref) if err != nil { return err } } + return nil } @@ -315,13 +352,17 @@ func refsToIds(refs []string) []entity.Id { ids := make([]entity.Id, len(refs)) for i, ref := range refs { - split := strings.Split(ref, "/") - ids[i] = entity.Id(split[len(split)-1]) + ids[i] = refToId(ref) } return ids } +func refToId(ref string) entity.Id { + split := strings.Split(ref, "/") + return entity.Id(split[len(split)-1]) +} + // Validate check if the Bug data is valid func (bug *Bug) Validate() error { // non-empty diff --git a/bug/bug_test.go b/bug/bug_test.go index 43e760af47512614acd8292448338fee3ee31084..d6c80efc48a30e912bbab0250ea291abd2c1313e 100644 --- a/bug/bug_test.go +++ b/bug/bug_test.go @@ -1,10 +1,12 @@ package bug import ( + "fmt" "testing" "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/MichaelMure/git-bug/identity" "github.com/MichaelMure/git-bug/repository" @@ -64,7 +66,7 @@ func TestBugValidity(t *testing.T) { } } -func TestBugCommitLoadRemove(t *testing.T) { +func TestBugCommitLoad(t *testing.T) { bug1 := NewBug() rene := identity.NewIdentity("René Descartes", "rene@descartes.fr") @@ -100,16 +102,6 @@ func TestBugCommitLoadRemove(t *testing.T) { bug3, err := ReadLocalBug(repo, bug1.Id()) assert.NoError(t, err) equivalentBug(t, bug1, bug3) - - err = RemoveLocalBug(repo, bug1.Id()) - assert.NoError(t, err) - - streamedBugs := ReadAllLocalBugs(repo) - count := 0 - for range streamedBugs { - count++ - } - assert.Equal(t, 0, count) } func equivalentBug(t *testing.T, expected, actual *Bug) { @@ -123,3 +115,60 @@ func equivalentBug(t *testing.T, expected, actual *Bug) { assert.Equal(t, expected, actual) } + +func TestBugRemove(t *testing.T) { + repo := repository.CreateTestRepo(false) + remoteA := repository.CreateTestRepo(true) + remoteB := repository.CreateTestRepo(true) + defer repository.CleanupTestRepos(repo, remoteA, remoteB) + + err := repo.AddRemote("remoteA", "file://"+remoteA.GetPath()) + require.NoError(t, err) + + err = repo.AddRemote("remoteB", "file://"+remoteB.GetPath()) + require.NoError(t, err) + + // generate a bunch of bugs + rene := identity.NewIdentity("René Descartes", "rene@descartes.fr") + err = rene.Commit(repo) + require.NoError(t, err) + + for i := 0; i < 100; i++ { + b := NewBug() + createOp := NewCreateOp(rene, time.Now().Unix(), "title", fmt.Sprintf("message%v", i), nil) + b.Append(createOp) + err = b.Commit(repo) + require.NoError(t, err) + } + + // and one more for testing + b := NewBug() + createOp := NewCreateOp(rene, time.Now().Unix(), "title", "message", nil) + b.Append(createOp) + err = b.Commit(repo) + require.NoError(t, err) + + _, err = Push(repo, "remoteA") + require.NoError(t, err) + + _, err = Push(repo, "remoteB") + require.NoError(t, err) + + _, err = Fetch(repo, "remoteA") + require.NoError(t, err) + + _, err = Fetch(repo, "remoteB") + require.NoError(t, err) + + err = RemoveBug(repo, b.Id()) + require.NoError(t, err) + + _, err = ReadLocalBug(repo, b.Id()) + require.Error(t, ErrBugNotExist, err) + + _, err = ReadRemoteBug(repo, "remoteA", b.Id()) + require.Error(t, ErrBugNotExist, err) + + _, err = ReadRemoteBug(repo, "remoteB", b.Id()) + require.Error(t, ErrBugNotExist, err) +} diff --git a/cache/repo_cache_test.go b/cache/repo_cache_test.go index e5f8a0373dcaf51605278286533d24314fa9147b..e5603fca1ffa1ccb3298bfa2da134f547b8a3088 100644 --- a/cache/repo_cache_test.go +++ b/cache/repo_cache_test.go @@ -1,8 +1,11 @@ package cache import ( + "fmt" "testing" + "time" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/MichaelMure/git-bug/query" @@ -101,11 +104,6 @@ func TestCache(t *testing.T) { require.NoError(t, err) _, err = cache.ResolveBugPrefix(bug1.Id().String()[:10]) require.NoError(t, err) - - // Possible to delete a bug - err = cache.RemoveBug(bug1.Id().Human(), "") - require.NoError(t, err) - require.Equal(t, len(cache.AllBugsIds()), 1) } func TestPushPull(t *testing.T) { @@ -162,3 +160,48 @@ func TestPushPull(t *testing.T) { require.Len(t, cacheA.AllBugsIds(), 2) } + +func TestRemove(t *testing.T) { + repo := repository.CreateTestRepo(false) + remoteA := repository.CreateTestRepo(true) + remoteB := repository.CreateTestRepo(true) + defer repository.CleanupTestRepos(repo, remoteA, remoteB) + + err := repo.AddRemote("remoteA", "file://"+remoteA.GetPath()) + require.NoError(t, err) + + err = repo.AddRemote("remoteB", "file://"+remoteB.GetPath()) + require.NoError(t, err) + + repoCache, err := NewRepoCache(repo) + require.NoError(t, err) + + // generate a bunch of bugs + rene, err := repoCache.NewIdentity("René Descartes", "rene@descartes.fr") + require.NoError(t, err) + + for i := 0; i < 100; i++ { + _, _, err := repoCache.NewBugRaw(rene, time.Now().Unix(), "title", fmt.Sprintf("message%v", i), nil, nil) + require.NoError(t, err) + } + + // and one more for testing + b1, _, err := repoCache.NewBugRaw(rene, time.Now().Unix(), "title", "message", nil, nil) + require.NoError(t, err) + + _, err = repoCache.Push("remoteA") + require.NoError(t, err) + + _, err = repoCache.Push("remoteB") + require.NoError(t, err) + + _, err = repoCache.Fetch("remoteA") + require.NoError(t, err) + + _, err = repoCache.Fetch("remoteB") + require.NoError(t, err) + + err = repoCache.RemoveBug(b1.Id().String()) + require.NoError(t, err) + assert.Equal(t, 100, len(repoCache.bugs)) +} From a62ce78c4fc4e541cecaab0ab22def8752d3d9e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Mur=C3=A9?= Date: Tue, 28 Jul 2020 14:24:04 +0200 Subject: [PATCH 8/8] bug: code cleanup for the rm feature --- bug/bug.go | 28 +++++++++++++--------------- bug/bug_test.go | 4 ++++ cache/repo_cache_test.go | 5 +++++ 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/bug/bug.go b/bug/bug.go index a47cd9dbc5134087b4c5fa79b598886be33e7a86..2ee890310814ab8e62e47ef7fab77a437eb3a0c6 100644 --- a/bug/bug.go +++ b/bug/bug.go @@ -244,17 +244,18 @@ func readBug(repo repository.ClockedRepo, ref string) (*Bug, error) { // RemoveBug will remove a local bug from its entity.Id func RemoveBug(repo repository.ClockedRepo, id entity.Id) error { - matching := entity.Id("") - fullMatches := []string{} + var fullMatches []string refs, err := repo.ListRefs(bugsRefPattern + id.String()) if err != nil { return err - } else if num := len(refs); num > 1 { + } + if len(refs) > 1 { return NewErrMultipleMatchBug(refsToIds(refs)) - } else if num == 1 { - matching = refToId(refs[0]) - fullMatches = []string{refs[0]} + } + if len(refs) == 1 { + // we have the bug locally + fullMatches = append(fullMatches, refs[0]) } remotes, err := repo.GetRemotes() @@ -267,20 +268,17 @@ func RemoveBug(repo repository.ClockedRepo, id entity.Id) error { remoteRefs, err := repo.ListRefs(remotePrefix) if err != nil { return err - } else if num := len(remoteRefs); num > 1 { + } + if len(remoteRefs) > 1 { return NewErrMultipleMatchBug(refsToIds(refs)) - } else if num == 1 { - id := refToId(remoteRefs[0]) + } + if len(remoteRefs) == 1 { + // found the bug in a remote fullMatches = append(fullMatches, remoteRefs[0]) - if matching.String() == "" { - matching = id - } else if id != matching { - return NewErrMultipleMatchBug([]entity.Id{matching, id}) - } } } - if matching == "" { + if len(fullMatches) == 0 { return ErrBugNotExist } diff --git a/bug/bug_test.go b/bug/bug_test.go index d6c80efc48a30e912bbab0250ea291abd2c1313e..400e50f8711e2dbe182f7565932e9ce774045b78 100644 --- a/bug/bug_test.go +++ b/bug/bug_test.go @@ -171,4 +171,8 @@ func TestBugRemove(t *testing.T) { _, err = ReadRemoteBug(repo, "remoteB", b.Id()) require.Error(t, ErrBugNotExist, err) + + ids, err := ListLocalIds(repo) + require.NoError(t, err) + require.Len(t, ids, 100) } diff --git a/cache/repo_cache_test.go b/cache/repo_cache_test.go index e5603fca1ffa1ccb3298bfa2da134f547b8a3088..0deb155ed506a996a64688ac53bd2eaaa3649c64 100644 --- a/cache/repo_cache_test.go +++ b/cache/repo_cache_test.go @@ -8,6 +8,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/MichaelMure/git-bug/bug" "github.com/MichaelMure/git-bug/query" "github.com/MichaelMure/git-bug/repository" ) @@ -204,4 +205,8 @@ func TestRemove(t *testing.T) { err = repoCache.RemoveBug(b1.Id().String()) require.NoError(t, err) assert.Equal(t, 100, len(repoCache.bugs)) + assert.Equal(t, 100, len(repoCache.bugExcerpts)) + + _, err = repoCache.ResolveBug(b1.Id()) + assert.Error(t, bug.ErrBugNotExist, err) }