feat: make local storage configurable

This commit is contained in:
Steve Moyer 2022-05-25 07:55:28 -04:00
parent 650c11dcb6
commit b42fae382a
No known key found for this signature in database
GPG Key ID: D8D464C9D22FB8E0
6 changed files with 43 additions and 22 deletions

View File

@ -14,6 +14,8 @@ import (
"github.com/MichaelMure/git-bug/util/interrupt" "github.com/MichaelMure/git-bug/util/interrupt"
) )
const gitBugApplicationName = "git-bug"
// Env is the environment of a command // Env is the environment of a command
type Env struct { type Env struct {
repo repository.ClockedRepo repo repository.ClockedRepo
@ -54,7 +56,7 @@ func loadRepo(env *Env) func(*cobra.Command, []string) error {
return fmt.Errorf("unable to get the current working directory: %q", err) return fmt.Errorf("unable to get the current working directory: %q", err)
} }
env.repo, err = repository.OpenGoGitRepo(cwd, []repository.ClockLoader{bug.ClockLoader}) env.repo, err = repository.OpenGoGitRepo(cwd, gitBugApplicationName, []repository.ClockLoader{bug.ClockLoader})
if err == repository.ErrNotARepo { if err == repository.ErrNotARepo {
return fmt.Errorf("%s must be run from within a git repo", rootCommandName) return fmt.Errorf("%s must be run from within a git repo", rootCommandName)
} }

View File

@ -336,14 +336,15 @@ func Read(repo repository.ClockedRepo, id entity.Id) (*ProjectConfig, error) {
} }
func Example_entity() { func Example_entity() {
const gitBugApplicationName = "git-bug"
// Note: this example ignore errors for readability // Note: this example ignore errors for readability
// Note: variable names get a little confusing as we are simulating both side in the same function // Note: variable names get a little confusing as we are simulating both side in the same function
// Let's start by defining two git repository and connecting them as remote // Let's start by defining two git repository and connecting them as remote
repoRenePath, _ := os.MkdirTemp("", "") repoRenePath, _ := os.MkdirTemp("", "")
repoIsaacPath, _ := os.MkdirTemp("", "") repoIsaacPath, _ := os.MkdirTemp("", "")
repoRene, _ := repository.InitGoGitRepo(repoRenePath) repoRene, _ := repository.InitGoGitRepo(repoRenePath, gitBugApplicationName)
repoIsaac, _ := repository.InitGoGitRepo(repoIsaacPath) repoIsaac, _ := repository.InitGoGitRepo(repoIsaacPath, gitBugApplicationName)
_ = repoRene.AddRemote("origin", repoIsaacPath) _ = repoRene.AddRemote("origin", repoIsaacPath)
_ = repoIsaac.AddRemote("origin", repoRenePath) _ = repoIsaac.AddRemote("origin", repoRenePath)

View File

@ -11,6 +11,8 @@ import (
// This program will randomly generate a collection of bugs in the repository // This program will randomly generate a collection of bugs in the repository
// of the current path // of the current path
func main() { func main() {
const gitBugApplicationName = "git-bug"
dir, err := os.Getwd() dir, err := os.Getwd()
if err != nil { if err != nil {
panic(err) panic(err)
@ -20,7 +22,7 @@ func main() {
bug.ClockLoader, bug.ClockLoader,
} }
repo, err := repository.OpenGoGitRepo(dir, loaders) repo, err := repository.OpenGoGitRepo(dir, gitBugApplicationName, loaders)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -26,6 +26,7 @@ import (
) )
const clockPath = "clocks" const clockPath = "clocks"
const indexPath = "indexes"
var _ ClockedRepo = &GoGitRepo{} var _ ClockedRepo = &GoGitRepo{}
var _ TestedRepo = &GoGitRepo{} var _ TestedRepo = &GoGitRepo{}
@ -49,8 +50,11 @@ type GoGitRepo struct {
localStorage billy.Filesystem localStorage billy.Filesystem
} }
// OpenGoGitRepo open an already existing repo at the given path // OpenGoGitRepo opens an already existing repo at the given path and with
func OpenGoGitRepo(path string, clockLoaders []ClockLoader) (*GoGitRepo, error) { // 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) path, err := detectGitPath(path)
if err != nil { if err != nil {
return nil, err return nil, err
@ -72,7 +76,7 @@ func OpenGoGitRepo(path string, clockLoaders []ClockLoader) (*GoGitRepo, error)
clocks: make(map[string]lamport.Clock), clocks: make(map[string]lamport.Clock),
indexes: make(map[string]bleve.Index), indexes: make(map[string]bleve.Index),
keyring: k, keyring: k,
localStorage: osfs.New(filepath.Join(path, "git-bug")), localStorage: osfs.New(filepath.Join(path, application)),
} }
for _, loader := range clockLoaders { for _, loader := range clockLoaders {
@ -94,8 +98,11 @@ func OpenGoGitRepo(path string, clockLoaders []ClockLoader) (*GoGitRepo, error)
return repo, nil return repo, nil
} }
// InitGoGitRepo create a new empty git repo at the given path // InitGoGitRepo creates a new empty git repo at the given path and with
func InitGoGitRepo(path string) (*GoGitRepo, error) { // 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) r, err := gogit.PlainInit(path, false)
if err != nil { if err != nil {
return nil, err return nil, err
@ -112,12 +119,15 @@ func InitGoGitRepo(path string) (*GoGitRepo, error) {
clocks: make(map[string]lamport.Clock), clocks: make(map[string]lamport.Clock),
indexes: make(map[string]bleve.Index), indexes: make(map[string]bleve.Index),
keyring: k, keyring: k,
localStorage: osfs.New(filepath.Join(path, ".git", "git-bug")), localStorage: osfs.New(filepath.Join(path, ".git", application)),
}, nil }, nil
} }
// InitBareGoGitRepo create a new --bare empty git repo at the given path // InitBareGoGitRepo creates a new --bare empty git repo at the given path
func InitBareGoGitRepo(path string) (*GoGitRepo, error) { // 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) r, err := gogit.PlainInit(path, true)
if err != nil { if err != nil {
return nil, err return nil, err
@ -134,7 +144,7 @@ func InitBareGoGitRepo(path string) (*GoGitRepo, error) {
clocks: make(map[string]lamport.Clock), clocks: make(map[string]lamport.Clock),
indexes: make(map[string]bleve.Index), indexes: make(map[string]bleve.Index),
keyring: k, keyring: k,
localStorage: osfs.New(filepath.Join(path, "git-bug")), localStorage: osfs.New(filepath.Join(path, application)),
}, nil }, nil
} }
@ -295,7 +305,8 @@ func (repo *GoGitRepo) GetRemotes() (map[string]string, error) {
return result, nil 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 { func (repo *GoGitRepo) LocalStorage() billy.Filesystem {
return repo.localStorage return repo.localStorage
} }
@ -309,7 +320,8 @@ func (repo *GoGitRepo) GetBleveIndex(name string) (bleve.Index, error) {
return index, nil 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) index, err := bleve.Open(path)
if err == nil { if err == nil {
@ -340,7 +352,8 @@ func (repo *GoGitRepo) ClearBleveIndex(name string) error {
repo.indexesMutex.Lock() repo.indexesMutex.Lock()
defer repo.indexesMutex.Unlock() 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) err := os.RemoveAll(path)
if err != nil { if err != nil {
@ -781,7 +794,8 @@ func (repo *GoGitRepo) AllClocks() (map[string]lamport.Clock, error) {
result := make(map[string]lamport.Clock) 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) { if os.IsNotExist(err) {
return nil, nil return nil, nil
} }

View File

@ -17,7 +17,7 @@ func TestNewGoGitRepo(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
defer os.RemoveAll(plainRoot) defer os.RemoveAll(plainRoot)
_, err = InitGoGitRepo(plainRoot) _, err = InitGoGitRepo(plainRoot, testApplicationName)
require.NoError(t, err) require.NoError(t, err)
plainGitDir := filepath.Join(plainRoot, ".git") plainGitDir := filepath.Join(plainRoot, ".git")
@ -26,7 +26,7 @@ func TestNewGoGitRepo(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
defer os.RemoveAll(bareRoot) defer os.RemoveAll(bareRoot)
_, err = InitBareGoGitRepo(bareRoot) _, err = InitBareGoGitRepo(bareRoot, testApplicationName)
require.NoError(t, err) require.NoError(t, err)
bareGitDir := bareRoot bareGitDir := bareRoot
@ -52,7 +52,7 @@ func TestNewGoGitRepo(t *testing.T) {
} }
for i, tc := range tests { for i, tc := range tests {
r, err := OpenGoGitRepo(tc.inPath, nil) r, err := OpenGoGitRepo(tc.inPath, testApplicationName, nil)
if tc.err { if tc.err {
require.Error(t, err, i) require.Error(t, err, i)

View File

@ -7,6 +7,8 @@ import (
"github.com/99designs/keyring" "github.com/99designs/keyring"
) )
const testApplicationName = "git-bug"
// This is intended for testing only // This is intended for testing only
func CreateGoGitTestRepo(bare bool) TestedRepo { func CreateGoGitTestRepo(bare bool) TestedRepo {
@ -15,7 +17,7 @@ func CreateGoGitTestRepo(bare bool) TestedRepo {
log.Fatal(err) log.Fatal(err)
} }
var creator func(string) (*GoGitRepo, error) var creator func(string, string) (*GoGitRepo, error)
if bare { if bare {
creator = InitBareGoGitRepo creator = InitBareGoGitRepo
@ -23,7 +25,7 @@ func CreateGoGitTestRepo(bare bool) TestedRepo {
creator = InitGoGitRepo creator = InitGoGitRepo
} }
repo, err := creator(dir) repo, err := creator(dir, testApplicationName)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }