Add functionality to remove bugs from a local repository. (#423)

Add functionality to remove bugs from a local repository.

This adds a function to remove git references in the repo and another one to remove bugs.
This commit is contained in:
Vincent Tiu 2020-07-09 20:40:44 +08:00 committed by GitHub
parent de062a78ec
commit f3304bdc1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 38 additions and 4 deletions

View File

@ -242,6 +242,11 @@ func readBug(repo repository.ClockedRepo, ref string) (*Bug, error) {
return &bug, nil
}
func RemoveLocalBug(repo repository.ClockedRepo, id entity.Id) error {
ref := bugsRefPattern + id.String()
return repo.RemoveRef(ref)
}
type StreamedBug struct {
Bug *Bug
Err error

View File

@ -4,9 +4,10 @@ import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/MichaelMure/git-bug/identity"
"github.com/MichaelMure/git-bug/repository"
"github.com/stretchr/testify/assert"
)
func TestBugId(t *testing.T) {
@ -63,7 +64,7 @@ func TestBugValidity(t *testing.T) {
}
}
func TestBugCommitLoad(t *testing.T) {
func TestBugCommitLoadRemove(t *testing.T) {
bug1 := NewBug()
rene := identity.NewIdentity("René Descartes", "rene@descartes.fr")
@ -99,6 +100,16 @@ func TestBugCommitLoad(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) {

View File

@ -282,6 +282,13 @@ func (repo *GitRepo) UpdateRef(ref string, hash Hash) error {
return err
}
// RemoveRef will remove a Git reference
func (repo *GitRepo) RemoveRef(ref string) error {
_, err := repo.runGitCommand("update-ref", "-d", ref)
return err
}
// ListRefs will return a list of Git ref matching the given refspec
func (repo *GitRepo) ListRefs(refspec string) ([]string, error) {
stdout, err := repo.runGitCommand("for-each-ref", "--format=%(refname)", refspec)

View File

@ -134,6 +134,11 @@ func (r *mockRepoForTest) UpdateRef(ref string, hash Hash) error {
return nil
}
func (r *mockRepoForTest) RemoveRef(ref string) error {
delete(r.refs, ref)
return nil
}
func (r *mockRepoForTest) RefExist(ref string) (bool, error) {
_, exist := r.refs[ref]
return exist, nil

View File

@ -83,6 +83,9 @@ type Repo interface {
// UpdateRef will create or update a Git reference
UpdateRef(ref string, hash Hash) error
// RemoveRef will remove a Git reference
RemoveRef(ref string) error
// ListRefs will return a list of Git ref matching the given refspec
ListRefs(refspec string) ([]string, error)

View File

@ -145,7 +145,7 @@ func RepoTest(t *testing.T, creator RepoCreator, cleaner RepoCleaner) {
ls, err := repo.ListRefs("refs/bugs")
require.NoError(t, err)
assert.Equal(t, []string{"refs/bugs/ref1"}, ls)
assert.ElementsMatch(t, []string{"refs/bugs/ref1"}, ls)
err = repo.CopyRef("refs/bugs/ref1", "refs/bugs/ref2")
require.NoError(t, err)
@ -156,7 +156,7 @@ func RepoTest(t *testing.T, creator RepoCreator, cleaner RepoCleaner) {
commits, err := repo.ListCommits("refs/bugs/ref2")
require.NoError(t, err)
assert.Equal(t, []Hash{commit1, commit2}, commits)
assert.ElementsMatch(t, []Hash{commit1, commit2}, commits)
// Graph
@ -166,6 +166,9 @@ func RepoTest(t *testing.T, creator RepoCreator, cleaner RepoCleaner) {
ancestorHash, err := repo.FindCommonAncestor(commit2, commit3)
require.NoError(t, err)
assert.Equal(t, commit1, ancestorHash)
err = repo.RemoveRef("refs/bugs/ref1")
require.NoError(t, err)
})
t.Run("Local config", func(t *testing.T) {