diff options
author | Kalin Staykov <k.t.staykov@gmail.com> | 2022-11-26 15:49:59 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2023-01-11 14:58:58 +0100 |
commit | fc266b733cf0ea794209031e3500ab3f86db6cee (patch) | |
tree | bae1996ec19fb74446429200e52cdb942f60ac54 /repository | |
parent | 9c50a359704f4edd2f33df6d256e032feae3a576 (diff) | |
download | git-bug-fc266b733cf0ea794209031e3500ab3f86db6cee.tar.gz |
add wipe sub-command that remove local bugs and identities
Diffstat (limited to 'repository')
-rw-r--r-- | repository/gogit.go | 11 | ||||
-rw-r--r-- | repository/index_bleve.go | 7 | ||||
-rw-r--r-- | repository/localstorage_billy.go | 16 | ||||
-rw-r--r-- | repository/mock_repo.go | 12 | ||||
-rw-r--r-- | repository/repo.go | 10 | ||||
-rw-r--r-- | repository/repo_testing.go | 34 |
6 files changed, 77 insertions, 13 deletions
diff --git a/repository/gogit.go b/repository/gogit.go index 01c47d41..93806026 100644 --- a/repository/gogit.go +++ b/repository/gogit.go @@ -13,7 +13,6 @@ import ( "time" "github.com/ProtonMail/go-crypto/openpgp" - "github.com/go-git/go-billy/v5" "github.com/go-git/go-billy/v5/osfs" gogit "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/config" @@ -48,7 +47,7 @@ type GoGitRepo struct { indexes map[string]Index keyring Keyring - localStorage billy.Filesystem + localStorage LocalStorage } // OpenGoGitRepo opens an already existing repo at the given path and @@ -77,7 +76,7 @@ func OpenGoGitRepo(path, namespace string, clockLoaders []ClockLoader) (*GoGitRe clocks: make(map[string]lamport.Clock), indexes: make(map[string]Index), keyring: k, - localStorage: osfs.New(filepath.Join(path, namespace)), + localStorage: billyLocalStorage{Filesystem: osfs.New(filepath.Join(path, namespace))}, } loaderToRun := make([]ClockLoader, 0, len(clockLoaders)) @@ -131,7 +130,7 @@ func InitGoGitRepo(path, namespace string) (*GoGitRepo, error) { clocks: make(map[string]lamport.Clock), indexes: make(map[string]Index), keyring: k, - localStorage: osfs.New(filepath.Join(path, ".git", namespace)), + localStorage: billyLocalStorage{Filesystem: osfs.New(filepath.Join(path, ".git", namespace))}, }, nil } @@ -156,7 +155,7 @@ func InitBareGoGitRepo(path, namespace string) (*GoGitRepo, error) { clocks: make(map[string]lamport.Clock), indexes: make(map[string]Index), keyring: k, - localStorage: osfs.New(filepath.Join(path, namespace)), + localStorage: billyLocalStorage{Filesystem: osfs.New(filepath.Join(path, namespace))}, }, nil } @@ -320,7 +319,7 @@ func (repo *GoGitRepo) GetRemotes() (map[string]string, error) { // LocalStorage returns a billy.Filesystem giving access to // $RepoPath/.git/$Namespace. -func (repo *GoGitRepo) LocalStorage() billy.Filesystem { +func (repo *GoGitRepo) LocalStorage() LocalStorage { return repo.localStorage } diff --git a/repository/index_bleve.go b/repository/index_bleve.go index aae41d5f..40170919 100644 --- a/repository/index_bleve.go +++ b/repository/index_bleve.go @@ -129,6 +129,13 @@ func (b *bleveIndex) DocCount() (uint64, error) { return b.index.DocCount() } +func (b *bleveIndex) Remove(id string) error { + b.mu.Lock() + defer b.mu.Unlock() + + return b.index.Delete(id) +} + func (b *bleveIndex) Clear() error { b.mu.Lock() defer b.mu.Unlock() diff --git a/repository/localstorage_billy.go b/repository/localstorage_billy.go new file mode 100644 index 00000000..ab3909c9 --- /dev/null +++ b/repository/localstorage_billy.go @@ -0,0 +1,16 @@ +package repository + +import ( + "github.com/go-git/go-billy/v5" + "github.com/go-git/go-billy/v5/util" +) + +var _ LocalStorage = &billyLocalStorage{} + +type billyLocalStorage struct { + billy.Filesystem +} + +func (b billyLocalStorage) RemoveAll(path string) error { + return util.RemoveAll(b.Filesystem, path) +} diff --git a/repository/mock_repo.go b/repository/mock_repo.go index c2cef8ef..6ea5c71e 100644 --- a/repository/mock_repo.go +++ b/repository/mock_repo.go @@ -9,7 +9,6 @@ import ( "github.com/99designs/keyring" "github.com/ProtonMail/go-crypto/openpgp" - "github.com/go-git/go-billy/v5" "github.com/go-git/go-billy/v5/memfs" "github.com/MichaelMure/git-bug/util/lamport" @@ -123,14 +122,14 @@ func (r *mockRepoCommon) GetRemotes() (map[string]string, error) { var _ RepoStorage = &mockRepoStorage{} type mockRepoStorage struct { - localFs billy.Filesystem + localFs LocalStorage } func NewMockRepoStorage() *mockRepoStorage { - return &mockRepoStorage{localFs: memfs.New()} + return &mockRepoStorage{localFs: billyLocalStorage{Filesystem: memfs.New()}} } -func (m *mockRepoStorage) LocalStorage() billy.Filesystem { +func (m *mockRepoStorage) LocalStorage() LocalStorage { return m.localFs } @@ -204,6 +203,11 @@ func (m *mockIndex) DocCount() (uint64, error) { return uint64(len(*m)), nil } +func (m *mockIndex) Remove(id string) error { + delete(*m, id) + return nil +} + func (m *mockIndex) Clear() error { for k, _ := range *m { delete(*m, k) diff --git a/repository/repo.go b/repository/repo.go index 66baec65..c39051d5 100644 --- a/repository/repo.go +++ b/repository/repo.go @@ -76,10 +76,15 @@ type RepoCommon interface { GetRemotes() (map[string]string, error) } +type LocalStorage interface { + billy.Filesystem + RemoveAll(path string) error +} + // RepoStorage give access to the filesystem type RepoStorage interface { // LocalStorage return a billy.Filesystem giving access to $RepoPath/.git/git-bug - LocalStorage() billy.Filesystem + LocalStorage() LocalStorage } // RepoIndex gives access to full-text search indexes @@ -103,6 +108,9 @@ type Index interface { // DocCount returns the number of document in the index. DocCount() (uint64, error) + // Remove delete one document in the index. + Remove(id string) error + // Clear empty the index. Clear() error diff --git a/repository/repo_testing.go b/repository/repo_testing.go index 821eb762..09332e62 100644 --- a/repository/repo_testing.go +++ b/repository/repo_testing.go @@ -2,6 +2,7 @@ package repository import ( "math/rand" + "os" "testing" "github.com/ProtonMail/go-crypto/openpgp" @@ -10,8 +11,6 @@ import ( "github.com/MichaelMure/git-bug/util/lamport" ) -// TODO: add tests for RepoStorage - type RepoCreator func(t testing.TB, bare bool) TestedRepo // Test suite for a Repo implementation @@ -32,6 +31,10 @@ func RepoTest(t *testing.T, creator RepoCreator) { RepoConfigTest(t, repo) }) + t.Run("Storage", func(t *testing.T) { + RepoStorageTest(t, repo) + }) + t.Run("Index", func(t *testing.T) { RepoIndexTest(t, repo) }) @@ -48,6 +51,33 @@ func RepoConfigTest(t *testing.T, repo RepoConfig) { testConfig(t, repo.LocalConfig()) } +func RepoStorageTest(t *testing.T, repo RepoStorage) { + storage := repo.LocalStorage() + + err := storage.MkdirAll("foo/bar", 0755) + require.NoError(t, err) + + f, err := storage.Create("foo/bar/foofoo") + require.NoError(t, err) + + _, err = f.Write([]byte("hello")) + require.NoError(t, err) + + // remove all + err = storage.RemoveAll(".") + require.NoError(t, err) + + fi, err := storage.ReadDir(".") + // a real FS would remove the root directory with RemoveAll and subsequent call would fail + // a memory FS would still have a virtual root and subsequent call would succeed + // not ideal, but will do for now + if err == nil { + require.Empty(t, fi) + } else { + require.True(t, os.IsNotExist(err)) + } +} + func randomHash() Hash { var letterRunes = "abcdef0123456789" b := make([]byte, idLengthSHA256) |