bug: code cleanup for the rm feature

Michael Muré created

Change summary

bug/bug.go               | 28 +++++++++++++---------------
bug/bug_test.go          |  4 ++++
cache/repo_cache_test.go |  5 +++++
3 files changed, 22 insertions(+), 15 deletions(-)

Detailed changes

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
 	}
 

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)
 }

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)
 }