diff options
-rw-r--r-- | bug/bug.go | 5 | ||||
-rw-r--r-- | bug/bug_test.go | 15 | ||||
-rw-r--r-- | repository/git.go | 7 | ||||
-rw-r--r-- | repository/mock_repo.go | 5 | ||||
-rw-r--r-- | repository/repo.go | 3 | ||||
-rw-r--r-- | repository/repo_testing.go | 7 |
6 files changed, 38 insertions, 4 deletions
@@ -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 diff --git a/bug/bug_test.go b/bug/bug_test.go index 480d312e..43e760af 100644 --- a/bug/bug_test.go +++ b/bug/bug_test.go @@ -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) { diff --git a/repository/git.go b/repository/git.go index 22055dbb..3d756324 100644 --- a/repository/git.go +++ b/repository/git.go @@ -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) diff --git a/repository/mock_repo.go b/repository/mock_repo.go index df2af14b..576e984e 100644 --- a/repository/mock_repo.go +++ b/repository/mock_repo.go @@ -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 diff --git a/repository/repo.go b/repository/repo.go index 8c76af5e..30d95806 100644 --- a/repository/repo.go +++ b/repository/repo.go @@ -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) diff --git a/repository/repo_testing.go b/repository/repo_testing.go index 34312011..28eb9a21 100644 --- a/repository/repo_testing.go +++ b/repository/repo_testing.go @@ -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) { |