From b42fae382af0a43b0d51fb312abd4ff55df43877 Mon Sep 17 00:00:00 2001 From: Steve Moyer Date: Wed, 25 May 2022 07:55:28 -0400 Subject: feat: make local storage configurable --- repository/gogit.go | 40 +++++++++++++++++++++++++++------------- repository/gogit_test.go | 6 +++--- repository/gogit_testing.go | 6 ++++-- 3 files changed, 34 insertions(+), 18 deletions(-) (limited to 'repository') diff --git a/repository/gogit.go b/repository/gogit.go index 54677902..af1b9fa4 100644 --- a/repository/gogit.go +++ b/repository/gogit.go @@ -26,6 +26,7 @@ import ( ) const clockPath = "clocks" +const indexPath = "indexes" var _ ClockedRepo = &GoGitRepo{} var _ TestedRepo = &GoGitRepo{} @@ -49,8 +50,11 @@ type GoGitRepo struct { localStorage billy.Filesystem } -// OpenGoGitRepo open an already existing repo at the given path -func OpenGoGitRepo(path string, clockLoaders []ClockLoader) (*GoGitRepo, error) { +// OpenGoGitRepo opens an already existing repo at the given path and with +// the specified application name. Given a repository path of "~/myrepo" +// and an application name of "git-bug", local storage for the application +// will be configured at "~/myrepo/.git/git-bug". +func OpenGoGitRepo(path, application string, clockLoaders []ClockLoader) (*GoGitRepo, error) { path, err := detectGitPath(path) if err != nil { return nil, err @@ -72,7 +76,7 @@ func OpenGoGitRepo(path string, clockLoaders []ClockLoader) (*GoGitRepo, error) clocks: make(map[string]lamport.Clock), indexes: make(map[string]bleve.Index), keyring: k, - localStorage: osfs.New(filepath.Join(path, "git-bug")), + localStorage: osfs.New(filepath.Join(path, application)), } for _, loader := range clockLoaders { @@ -94,8 +98,11 @@ func OpenGoGitRepo(path string, clockLoaders []ClockLoader) (*GoGitRepo, error) return repo, nil } -// InitGoGitRepo create a new empty git repo at the given path -func InitGoGitRepo(path string) (*GoGitRepo, error) { +// InitGoGitRepo creates a new empty git repo at the given path and with +// the specified application name. Given a repository path of "~/myrepo" +// and an application name of "git-bug", local storage for the application +// will be configured at "~/myrepo/.git/git-bug". +func InitGoGitRepo(path, application string) (*GoGitRepo, error) { r, err := gogit.PlainInit(path, false) if err != nil { return nil, err @@ -112,12 +119,15 @@ func InitGoGitRepo(path string) (*GoGitRepo, error) { clocks: make(map[string]lamport.Clock), indexes: make(map[string]bleve.Index), keyring: k, - localStorage: osfs.New(filepath.Join(path, ".git", "git-bug")), + localStorage: osfs.New(filepath.Join(path, ".git", application)), }, nil } -// InitBareGoGitRepo create a new --bare empty git repo at the given path -func InitBareGoGitRepo(path string) (*GoGitRepo, error) { +// InitBareGoGitRepo creates a new --bare empty git repo at the given path +// and with the specified application name. Given a repository path of +// "~/myrepo" and an application name of "git-bug", local storage for the +// application will be configured at "~/myrepo/.git/git-bug". +func InitBareGoGitRepo(path, application string) (*GoGitRepo, error) { r, err := gogit.PlainInit(path, true) if err != nil { return nil, err @@ -134,7 +144,7 @@ func InitBareGoGitRepo(path string) (*GoGitRepo, error) { clocks: make(map[string]lamport.Clock), indexes: make(map[string]bleve.Index), keyring: k, - localStorage: osfs.New(filepath.Join(path, "git-bug")), + localStorage: osfs.New(filepath.Join(path, application)), }, nil } @@ -295,7 +305,8 @@ func (repo *GoGitRepo) GetRemotes() (map[string]string, error) { return result, nil } -// LocalStorage return a billy.Filesystem giving access to $RepoPath/.git/git-bug +// LocalStorage returns a billy.Filesystem giving access to +// $RepoPath/.git/$ApplicationName. func (repo *GoGitRepo) LocalStorage() billy.Filesystem { return repo.localStorage } @@ -309,7 +320,8 @@ func (repo *GoGitRepo) GetBleveIndex(name string) (bleve.Index, error) { return index, nil } - path := filepath.Join(repo.path, "git-bug", "indexes", name) + // path := filepath.Join(repo.path, "git-bug", "indexes", name) + path := filepath.Join(repo.localStorage.Root(), indexPath, name) index, err := bleve.Open(path) if err == nil { @@ -340,7 +352,8 @@ func (repo *GoGitRepo) ClearBleveIndex(name string) error { repo.indexesMutex.Lock() defer repo.indexesMutex.Unlock() - path := filepath.Join(repo.path, "git-bug", "indexes", name) + // path := filepath.Join(repo.path, "git-bug", "indexes", name) + path := filepath.Join(repo.localStorage.Root(), indexPath, name) err := os.RemoveAll(path) if err != nil { @@ -781,7 +794,8 @@ func (repo *GoGitRepo) AllClocks() (map[string]lamport.Clock, error) { result := make(map[string]lamport.Clock) - files, err := ioutil.ReadDir(filepath.Join(repo.path, "git-bug", clockPath)) + // files, err := ioutil.ReadDir(filepath.Join(repo.path, "git-bug", clockPath)) + files, err := ioutil.ReadDir(filepath.Join(repo.localStorage.Root(), clockPath)) if os.IsNotExist(err) { return nil, nil } diff --git a/repository/gogit_test.go b/repository/gogit_test.go index a2bb49b9..941b563a 100644 --- a/repository/gogit_test.go +++ b/repository/gogit_test.go @@ -17,7 +17,7 @@ func TestNewGoGitRepo(t *testing.T) { require.NoError(t, err) defer os.RemoveAll(plainRoot) - _, err = InitGoGitRepo(plainRoot) + _, err = InitGoGitRepo(plainRoot, testApplicationName) require.NoError(t, err) plainGitDir := filepath.Join(plainRoot, ".git") @@ -26,7 +26,7 @@ func TestNewGoGitRepo(t *testing.T) { require.NoError(t, err) defer os.RemoveAll(bareRoot) - _, err = InitBareGoGitRepo(bareRoot) + _, err = InitBareGoGitRepo(bareRoot, testApplicationName) require.NoError(t, err) bareGitDir := bareRoot @@ -52,7 +52,7 @@ func TestNewGoGitRepo(t *testing.T) { } for i, tc := range tests { - r, err := OpenGoGitRepo(tc.inPath, nil) + r, err := OpenGoGitRepo(tc.inPath, testApplicationName, nil) if tc.err { require.Error(t, err, i) diff --git a/repository/gogit_testing.go b/repository/gogit_testing.go index cad776b3..f80f62c0 100644 --- a/repository/gogit_testing.go +++ b/repository/gogit_testing.go @@ -7,6 +7,8 @@ import ( "github.com/99designs/keyring" ) +const testApplicationName = "git-bug" + // This is intended for testing only func CreateGoGitTestRepo(bare bool) TestedRepo { @@ -15,7 +17,7 @@ func CreateGoGitTestRepo(bare bool) TestedRepo { log.Fatal(err) } - var creator func(string) (*GoGitRepo, error) + var creator func(string, string) (*GoGitRepo, error) if bare { creator = InitBareGoGitRepo @@ -23,7 +25,7 @@ func CreateGoGitTestRepo(bare bool) TestedRepo { creator = InitGoGitRepo } - repo, err := creator(dir) + repo, err := creator(dir, testApplicationName) if err != nil { log.Fatal(err) } -- cgit From e29f58bf853d0cd4825cb590ba973a9d9ab7ea36 Mon Sep 17 00:00:00 2001 From: Steve Moyer Date: Wed, 25 May 2022 07:59:56 -0400 Subject: chore: clean-up commented code --- repository/gogit.go | 3 --- 1 file changed, 3 deletions(-) (limited to 'repository') diff --git a/repository/gogit.go b/repository/gogit.go index af1b9fa4..e876629a 100644 --- a/repository/gogit.go +++ b/repository/gogit.go @@ -320,7 +320,6 @@ func (repo *GoGitRepo) GetBleveIndex(name string) (bleve.Index, error) { return index, nil } - // path := filepath.Join(repo.path, "git-bug", "indexes", name) path := filepath.Join(repo.localStorage.Root(), indexPath, name) index, err := bleve.Open(path) @@ -352,7 +351,6 @@ func (repo *GoGitRepo) ClearBleveIndex(name string) error { repo.indexesMutex.Lock() defer repo.indexesMutex.Unlock() - // path := filepath.Join(repo.path, "git-bug", "indexes", name) path := filepath.Join(repo.localStorage.Root(), indexPath, name) err := os.RemoveAll(path) @@ -794,7 +792,6 @@ func (repo *GoGitRepo) AllClocks() (map[string]lamport.Clock, error) { result := make(map[string]lamport.Clock) - // files, err := ioutil.ReadDir(filepath.Join(repo.path, "git-bug", clockPath)) files, err := ioutil.ReadDir(filepath.Join(repo.localStorage.Root(), clockPath)) if os.IsNotExist(err) { return nil, nil -- cgit From e120fdb97e3af76198eadc8a44e6feb732b4dd83 Mon Sep 17 00:00:00 2001 From: Steve Moyer Date: Thu, 26 May 2022 13:39:13 -0400 Subject: refactor: use namespace instead of application of applicationName --- repository/gogit.go | 38 +++++++++++++++++++------------------- repository/gogit_testing.go | 4 ++-- 2 files changed, 21 insertions(+), 21 deletions(-) (limited to 'repository') diff --git a/repository/gogit.go b/repository/gogit.go index e876629a..46a2cb0a 100644 --- a/repository/gogit.go +++ b/repository/gogit.go @@ -50,11 +50,11 @@ type GoGitRepo struct { localStorage billy.Filesystem } -// OpenGoGitRepo opens an already existing repo at the given path and with -// the specified application name. Given a repository path of "~/myrepo" -// and an application name of "git-bug", local storage for the application -// will be configured at "~/myrepo/.git/git-bug". -func OpenGoGitRepo(path, application string, clockLoaders []ClockLoader) (*GoGitRepo, error) { +// OpenGoGitRepo opens an already existing repo at the given path and +// with the specified LocalStorage namespace. Given a repository path +// of "~/myrepo" and a namespace of "git-bug", local storage for the +// GoGitRepo will be configured at "~/myrepo/.git/git-bug". +func OpenGoGitRepo(path, namespace string, clockLoaders []ClockLoader) (*GoGitRepo, error) { path, err := detectGitPath(path) if err != nil { return nil, err @@ -76,7 +76,7 @@ func OpenGoGitRepo(path, application string, clockLoaders []ClockLoader) (*GoGit clocks: make(map[string]lamport.Clock), indexes: make(map[string]bleve.Index), keyring: k, - localStorage: osfs.New(filepath.Join(path, application)), + localStorage: osfs.New(filepath.Join(path, namespace)), } for _, loader := range clockLoaders { @@ -98,11 +98,11 @@ func OpenGoGitRepo(path, application string, clockLoaders []ClockLoader) (*GoGit return repo, nil } -// InitGoGitRepo creates a new empty git repo at the given path and with -// the specified application name. Given a repository path of "~/myrepo" -// and an application name of "git-bug", local storage for the application -// will be configured at "~/myrepo/.git/git-bug". -func InitGoGitRepo(path, application string) (*GoGitRepo, error) { +// InitGoGitRepo creates a new empty git repo at the given path and +// with the specified LocalStorage namespace. Given a repository path +// of "~/myrepo" and a namespace of "git-bug", local storage for the +// GoGitRepo will be configured at "~/myrepo/.git/git-bug". +func InitGoGitRepo(path, namespace string) (*GoGitRepo, error) { r, err := gogit.PlainInit(path, false) if err != nil { return nil, err @@ -119,15 +119,15 @@ func InitGoGitRepo(path, application string) (*GoGitRepo, error) { clocks: make(map[string]lamport.Clock), indexes: make(map[string]bleve.Index), keyring: k, - localStorage: osfs.New(filepath.Join(path, ".git", application)), + localStorage: osfs.New(filepath.Join(path, ".git", namespace)), }, nil } -// InitBareGoGitRepo creates a new --bare empty git repo at the given path -// and with the specified application name. Given a repository path of -// "~/myrepo" and an application name of "git-bug", local storage for the -// application will be configured at "~/myrepo/.git/git-bug". -func InitBareGoGitRepo(path, application string) (*GoGitRepo, error) { +// InitBareGoGitRepo creates a new --bare empty git repo at the given +// path and with the specified LocalStorage namespace. Given a repository +// path of "~/myrepo" and a namespace of "git-bug", local storage for the +// GoGitRepo will be configured at "~/myrepo/.git/git-bug". +func InitBareGoGitRepo(path, namespace string) (*GoGitRepo, error) { r, err := gogit.PlainInit(path, true) if err != nil { return nil, err @@ -144,7 +144,7 @@ func InitBareGoGitRepo(path, application string) (*GoGitRepo, error) { clocks: make(map[string]lamport.Clock), indexes: make(map[string]bleve.Index), keyring: k, - localStorage: osfs.New(filepath.Join(path, application)), + localStorage: osfs.New(filepath.Join(path, namespace)), }, nil } @@ -306,7 +306,7 @@ func (repo *GoGitRepo) GetRemotes() (map[string]string, error) { } // LocalStorage returns a billy.Filesystem giving access to -// $RepoPath/.git/$ApplicationName. +// $RepoPath/.git/$Namespace. func (repo *GoGitRepo) LocalStorage() billy.Filesystem { return repo.localStorage } diff --git a/repository/gogit_testing.go b/repository/gogit_testing.go index f80f62c0..7647c711 100644 --- a/repository/gogit_testing.go +++ b/repository/gogit_testing.go @@ -7,7 +7,7 @@ import ( "github.com/99designs/keyring" ) -const testApplicationName = "git-bug" +const namespace = "git-bug" // This is intended for testing only @@ -25,7 +25,7 @@ func CreateGoGitTestRepo(bare bool) TestedRepo { creator = InitGoGitRepo } - repo, err := creator(dir, testApplicationName) + repo, err := creator(dir, namespace) if err != nil { log.Fatal(err) } -- cgit From 8821b67d1bd0809d3fd3e87baf391e07aa54722a Mon Sep 17 00:00:00 2001 From: Steve Moyer Date: Thu, 26 May 2022 13:40:52 -0400 Subject: test: add verification that localStorage.Root() resolves to the correct absolute filepath --- repository/gogit_test.go | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) (limited to 'repository') diff --git a/repository/gogit_test.go b/repository/gogit_test.go index 941b563a..d7b919b9 100644 --- a/repository/gogit_test.go +++ b/repository/gogit_test.go @@ -17,7 +17,7 @@ func TestNewGoGitRepo(t *testing.T) { require.NoError(t, err) defer os.RemoveAll(plainRoot) - _, err = InitGoGitRepo(plainRoot, testApplicationName) + _, err = InitGoGitRepo(plainRoot, namespace) require.NoError(t, err) plainGitDir := filepath.Join(plainRoot, ".git") @@ -26,7 +26,7 @@ func TestNewGoGitRepo(t *testing.T) { require.NoError(t, err) defer os.RemoveAll(bareRoot) - _, err = InitBareGoGitRepo(bareRoot, testApplicationName) + _, err = InitBareGoGitRepo(bareRoot, namespace) require.NoError(t, err) bareGitDir := bareRoot @@ -52,7 +52,7 @@ func TestNewGoGitRepo(t *testing.T) { } for i, tc := range tests { - r, err := OpenGoGitRepo(tc.inPath, testApplicationName, nil) + r, err := OpenGoGitRepo(tc.inPath, namespace, nil) if tc.err { require.Error(t, err, i) @@ -66,3 +66,36 @@ func TestNewGoGitRepo(t *testing.T) { func TestGoGitRepo(t *testing.T) { RepoTest(t, CreateGoGitTestRepo, CleanupTestRepos) } + +func TestGoGitRepo_Indexes(t *testing.T) { + t.Parallel() + + plainRoot, err := ioutil.TempDir("", "") + require.NoError(t, err) + // defer os.RemoveAll(plainRoot) + + repo, err := InitGoGitRepo(plainRoot, namespace) + require.NoError(t, err) + + // Can create indices + indexA, err := repo.GetBleveIndex("a") + require.NoError(t, err) + require.NotZero(t, indexA) + require.FileExists(t, filepath.Join(plainRoot, ".git", namespace, "indexes", "a", "index_meta.json")) + require.FileExists(t, filepath.Join(plainRoot, ".git", namespace, "indexes", "a", "store")) + + indexB, err := repo.GetBleveIndex("b") + require.NoError(t, err) + require.NotZero(t, indexB) + require.DirExists(t, filepath.Join(plainRoot, ".git", namespace, "indexes", "b")) + + // Can get an existing index + indexA, err = repo.GetBleveIndex("a") + require.NoError(t, err) + require.NotZero(t, indexA) + + // Can delete an index + err = repo.ClearBleveIndex("a") + require.NoError(t, err) + require.NoDirExists(t, filepath.Join(plainRoot, ".git", namespace, "indexes", "a")) +} -- cgit From 86dd450aaf592aa065a17d49d70d9d0352fa5ca3 Mon Sep 17 00:00:00 2001 From: Steve Moyer Date: Sat, 28 May 2022 08:07:34 -0400 Subject: test: clean up temp dir and repo correctly --- repository/gogit_test.go | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'repository') diff --git a/repository/gogit_test.go b/repository/gogit_test.go index d7b919b9..49eae309 100644 --- a/repository/gogit_test.go +++ b/repository/gogit_test.go @@ -15,19 +15,25 @@ func TestNewGoGitRepo(t *testing.T) { // Plain plainRoot, err := ioutil.TempDir("", "") require.NoError(t, err) - defer os.RemoveAll(plainRoot) + t.Cleanup(func() { + require.NoError(t, os.RemoveAll(plainRoot)) + }) - _, err = InitGoGitRepo(plainRoot, namespace) + plainRepo, err := InitGoGitRepo(plainRoot, namespace) require.NoError(t, err) + require.NoError(t, plainRepo.Close()) plainGitDir := filepath.Join(plainRoot, ".git") // Bare bareRoot, err := ioutil.TempDir("", "") require.NoError(t, err) - defer os.RemoveAll(bareRoot) + t.Cleanup(func() { + require.NoError(t, os.RemoveAll(bareRoot)) + }) - _, err = InitBareGoGitRepo(bareRoot, namespace) + bareRepo, err := InitBareGoGitRepo(bareRoot, namespace) require.NoError(t, err) + require.NoError(t, bareRepo.Close()) bareGitDir := bareRoot tests := []struct { @@ -59,6 +65,7 @@ func TestNewGoGitRepo(t *testing.T) { } else { require.NoError(t, err, i) assert.Equal(t, filepath.ToSlash(tc.outPath), filepath.ToSlash(r.path), i) + require.NoError(t, r.Close()) } } } @@ -72,10 +79,15 @@ func TestGoGitRepo_Indexes(t *testing.T) { plainRoot, err := ioutil.TempDir("", "") require.NoError(t, err) - // defer os.RemoveAll(plainRoot) + t.Cleanup(func() { + require.NoError(t, os.RemoveAll(plainRoot)) + }) repo, err := InitGoGitRepo(plainRoot, namespace) require.NoError(t, err) + t.Cleanup(func() { + require.NoError(t, repo.Close()) + }) // Can create indices indexA, err := repo.GetBleveIndex("a") -- cgit From 50de0306dff5aa83981471f1245363aa40a268fa Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Tue, 31 May 2022 12:24:58 +0200 Subject: gogit: close index before deleting it on disk --- repository/gogit.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'repository') diff --git a/repository/gogit.go b/repository/gogit.go index 46a2cb0a..71cddfb2 100644 --- a/repository/gogit.go +++ b/repository/gogit.go @@ -351,21 +351,20 @@ func (repo *GoGitRepo) ClearBleveIndex(name string) error { repo.indexesMutex.Lock() defer repo.indexesMutex.Unlock() - path := filepath.Join(repo.localStorage.Root(), indexPath, name) - - err := os.RemoveAll(path) - if err != nil { - return err - } - if index, ok := repo.indexes[name]; ok { - err = index.Close() + err := index.Close() if err != nil { return err } delete(repo.indexes, name) } + path := filepath.Join(repo.localStorage.Root(), indexPath, name) + err := os.RemoveAll(path) + if err != nil { + return err + } + return nil } @@ -580,7 +579,7 @@ func (repo *GoGitRepo) StoreCommit(treeHash Hash, parents ...Hash) (Hash, error) return repo.StoreSignedCommit(treeHash, nil, parents...) } -// StoreCommit will store a Git commit with the given Git tree. If signKey is not nil, the commit +// StoreSignedCommit will store a Git commit with the given Git tree. If signKey is not nil, the commit // will be signed accordingly. func (repo *GoGitRepo) StoreSignedCommit(treeHash Hash, signKey *openpgp.Entity, parents ...Hash) (Hash, error) { cfg, err := repo.r.Config() -- cgit From da9f95e495476679866863007f44e611627ee2b3 Mon Sep 17 00:00:00 2001 From: Steve Moyer Date: Tue, 31 May 2022 07:22:55 -0400 Subject: fix: remove only t.Parallel() --- repository/gogit_test.go | 2 -- 1 file changed, 2 deletions(-) (limited to 'repository') diff --git a/repository/gogit_test.go b/repository/gogit_test.go index 49eae309..7fee91fb 100644 --- a/repository/gogit_test.go +++ b/repository/gogit_test.go @@ -75,8 +75,6 @@ func TestGoGitRepo(t *testing.T) { } func TestGoGitRepo_Indexes(t *testing.T) { - t.Parallel() - plainRoot, err := ioutil.TempDir("", "") require.NoError(t, err) t.Cleanup(func() { -- cgit From ccc342e81482e5fe6144fdb6c547331eab2cb98a Mon Sep 17 00:00:00 2001 From: Steve Moyer Date: Tue, 31 May 2022 07:47:56 -0400 Subject: refactor: simplify creation of temp dir - after 1.15 --- repository/gogit_test.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'repository') diff --git a/repository/gogit_test.go b/repository/gogit_test.go index 7fee91fb..c376de22 100644 --- a/repository/gogit_test.go +++ b/repository/gogit_test.go @@ -75,11 +75,7 @@ func TestGoGitRepo(t *testing.T) { } func TestGoGitRepo_Indexes(t *testing.T) { - plainRoot, err := ioutil.TempDir("", "") - require.NoError(t, err) - t.Cleanup(func() { - require.NoError(t, os.RemoveAll(plainRoot)) - }) + plainRoot := t.TempDir() repo, err := InitGoGitRepo(plainRoot, namespace) require.NoError(t, err) -- cgit