From bca9ae82745ffd619fd321f4200016c184849f94 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Tue, 1 Dec 2020 21:19:23 +0100 Subject: repo: more work towards RepoStorage --- repository/git.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'repository/git.go') diff --git a/repository/git.go b/repository/git.go index 504cdd89..8c319285 100644 --- a/repository/git.go +++ b/repository/git.go @@ -4,10 +4,15 @@ package repository import ( "bytes" "fmt" + "os" "path" + "path/filepath" "strings" "sync" + "github.com/go-git/go-billy/v5" + "github.com/go-git/go-billy/v5/osfs" + "github.com/MichaelMure/git-bug/util/lamport" ) @@ -174,6 +179,11 @@ func (repo *GitRepo) GetRemotes() (map[string]string, error) { return remotes, nil } +// LocalStorage return a billy.Filesystem giving access to $RepoPath/.git/git-bug +func (repo *GitRepo) LocalStorage() billy.Filesystem { + return osfs.New(repo.path) +} + // FetchRefs fetch git refs from a remote func (repo *GitRepo) FetchRefs(remote, refSpec string) (string, error) { stdout, err := repo.runGitCommand("fetch", remote, refSpec) @@ -408,3 +418,16 @@ func (repo *GitRepo) AddRemote(name string, url string) error { return err } + +// GetLocalRemote return the URL to use to add this repo as a local remote +func (repo *GitRepo) GetLocalRemote() string { + return repo.path +} + +// EraseFromDisk delete this repository entirely from the disk +func (repo *GitRepo) EraseFromDisk() error { + path := filepath.Clean(strings.TrimSuffix(repo.path, string(filepath.Separator)+".git")) + + // fmt.Println("Cleaning repo:", path) + return os.RemoveAll(path) +} -- cgit From 4ef2c1104079032336da9f2a7879f2432c2609ce Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sat, 5 Dec 2020 03:08:54 +0100 Subject: repo: finish RepoStorage move --- repository/git.go | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) (limited to 'repository/git.go') diff --git a/repository/git.go b/repository/git.go index 8c319285..e67e12f5 100644 --- a/repository/git.go +++ b/repository/git.go @@ -5,7 +5,6 @@ import ( "bytes" "fmt" "os" - "path" "path/filepath" "strings" "sync" @@ -379,9 +378,7 @@ func (repo *GitRepo) GetOrCreateClock(name string) (lamport.Clock, error) { repo.clocksMutex.Lock() defer repo.clocksMutex.Unlock() - p := path.Join(repo.path, clockPath, name+"-clock") - - c, err = lamport.NewPersistedClock(p) + c, err = lamport.NewPersistedClock(repo.LocalStorage(), name+"-clock") if err != nil { return nil, err } @@ -398,9 +395,7 @@ func (repo *GitRepo) getClock(name string) (lamport.Clock, error) { return c, nil } - p := path.Join(repo.path, clockPath, name+"-clock") - - c, err := lamport.LoadPersistedClock(p) + c, err := lamport.LoadPersistedClock(repo.LocalStorage(), name+"-clock") if err == nil { repo.clocks[name] = c return c, nil -- cgit From 71e1303234ef227851998f08c98c7c69670c9966 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Tue, 8 Dec 2020 13:15:21 +0100 Subject: repo: simpler clock mutex locking --- repository/git.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'repository/git.go') diff --git a/repository/git.go b/repository/git.go index e67e12f5..2348f5d5 100644 --- a/repository/git.go +++ b/repository/git.go @@ -367,6 +367,9 @@ func (repo *GitRepo) GetTreeHash(commit Hash) (Hash, error) { // GetOrCreateClock return a Lamport clock stored in the Repo. // If the clock doesn't exist, it's created. func (repo *GitRepo) GetOrCreateClock(name string) (lamport.Clock, error) { + repo.clocksMutex.Lock() + defer repo.clocksMutex.Unlock() + c, err := repo.getClock(name) if err == nil { return c, nil @@ -375,9 +378,6 @@ func (repo *GitRepo) GetOrCreateClock(name string) (lamport.Clock, error) { return nil, err } - repo.clocksMutex.Lock() - defer repo.clocksMutex.Unlock() - c, err = lamport.NewPersistedClock(repo.LocalStorage(), name+"-clock") if err != nil { return nil, err @@ -388,9 +388,6 @@ func (repo *GitRepo) GetOrCreateClock(name string) (lamport.Clock, error) { } func (repo *GitRepo) getClock(name string) (lamport.Clock, error) { - repo.clocksMutex.Lock() - defer repo.clocksMutex.Unlock() - if c, ok := repo.clocks[name]; ok { return c, nil } -- cgit From c884d557bf5e0ebdbe6e3b20535af24c2e97d29f Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Tue, 8 Dec 2020 14:42:13 +0100 Subject: repo: move bleve there --- repository/git.go | 86 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 78 insertions(+), 8 deletions(-) (limited to 'repository/git.go') diff --git a/repository/git.go b/repository/git.go index 2348f5d5..993e6cc6 100644 --- a/repository/git.go +++ b/repository/git.go @@ -9,6 +9,7 @@ import ( "strings" "sync" + "github.com/blevesearch/bleve" "github.com/go-git/go-billy/v5" "github.com/go-git/go-billy/v5/osfs" @@ -30,12 +31,15 @@ type GitRepo struct { clocksMutex sync.Mutex clocks map[string]lamport.Clock + indexesMutex sync.Mutex + indexes map[string]bleve.Index + keyring Keyring } -// NewGitRepo determines if the given working directory is inside of a git repository, +// OpenGitRepo determines if the given working directory is inside of a git repository, // and returns the corresponding GitRepo instance if it is. -func NewGitRepo(path string, clockLoaders []ClockLoader) (*GitRepo, error) { +func OpenGitRepo(path string, clockLoaders []ClockLoader) (*GitRepo, error) { k, err := defaultKeyring() if err != nil { return nil, err @@ -45,6 +49,7 @@ func NewGitRepo(path string, clockLoaders []ClockLoader) (*GitRepo, error) { gitCli: gitCli{path: path}, path: path, clocks: make(map[string]lamport.Clock), + indexes: make(map[string]bleve.Index), keyring: k, } @@ -84,9 +89,10 @@ func NewGitRepo(path string, clockLoaders []ClockLoader) (*GitRepo, error) { // InitGitRepo create a new empty git repo at the given path func InitGitRepo(path string) (*GitRepo, error) { repo := &GitRepo{ - gitCli: gitCli{path: path}, - path: path + "/.git", - clocks: make(map[string]lamport.Clock), + gitCli: gitCli{path: path}, + path: path + "/.git", + clocks: make(map[string]lamport.Clock), + indexes: make(map[string]bleve.Index), } _, err := repo.runGitCommand("init", path) @@ -100,9 +106,10 @@ func InitGitRepo(path string) (*GitRepo, error) { // InitBareGitRepo create a new --bare empty git repo at the given path func InitBareGitRepo(path string) (*GitRepo, error) { repo := &GitRepo{ - gitCli: gitCli{path: path}, - path: path, - clocks: make(map[string]lamport.Clock), + gitCli: gitCli{path: path}, + path: path, + clocks: make(map[string]lamport.Clock), + indexes: make(map[string]bleve.Index), } _, err := repo.runGitCommand("init", "--bare", path) @@ -113,6 +120,17 @@ func InitBareGitRepo(path string) (*GitRepo, error) { return repo, nil } +func (repo *GitRepo) Close() error { + var firstErr error + for _, index := range repo.indexes { + err := index.Close() + if err != nil && firstErr == nil { + firstErr = err + } + } + return firstErr +} + // LocalConfig give access to the repository scoped configuration func (repo *GitRepo) LocalConfig() Config { return newGitConfig(repo.gitCli, false) @@ -183,6 +201,58 @@ func (repo *GitRepo) LocalStorage() billy.Filesystem { return osfs.New(repo.path) } +// GetBleveIndex return a bleve.Index that can be used to index documents +func (repo *GitRepo) GetBleveIndex(name string) (bleve.Index, error) { + repo.indexesMutex.Lock() + defer repo.indexesMutex.Unlock() + + if index, ok := repo.indexes[name]; ok { + return index, nil + } + + path := filepath.Join(repo.path, "indexes", name) + + index, err := bleve.Open(path) + if err == nil { + repo.indexes[name] = index + return index, nil + } + + err = os.MkdirAll(path, os.ModeDir) + if err != nil { + return nil, err + } + + mapping := bleve.NewIndexMapping() + mapping.DefaultAnalyzer = "en" + + index, err = bleve.New(path, mapping) + if err != nil { + return nil, err + } + + repo.indexes[name] = index + + return index, nil +} + +// ClearBleveIndex will wipe the given index +func (repo *GitRepo) ClearBleveIndex(name string) error { + repo.indexesMutex.Lock() + defer repo.indexesMutex.Unlock() + + path := filepath.Join(repo.path, "indexes", name) + + err := os.RemoveAll(path) + if err != nil { + return err + } + + delete(repo.indexes, name) + + return nil +} + // FetchRefs fetch git refs from a remote func (repo *GitRepo) FetchRefs(remote, refSpec string) (string, error) { stdout, err := repo.runGitCommand("fetch", remote, refSpec) -- cgit From 8128bb79b0db9023a98c356e4e173d846057c577 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Tue, 8 Dec 2020 15:09:58 +0100 Subject: repo: close before deleting --- repository/git.go | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'repository/git.go') diff --git a/repository/git.go b/repository/git.go index 993e6cc6..bc9d8772 100644 --- a/repository/git.go +++ b/repository/git.go @@ -488,6 +488,11 @@ func (repo *GitRepo) GetLocalRemote() string { // EraseFromDisk delete this repository entirely from the disk func (repo *GitRepo) EraseFromDisk() error { + err := repo.Close() + if err != nil { + return err + } + path := filepath.Clean(strings.TrimSuffix(repo.path, string(filepath.Separator)+".git")) // fmt.Println("Cleaning repo:", path) -- cgit