Skip to content
Snippets Groups Projects
Unverified Commit b168d71f authored by Michael Muré's avatar Michael Muré
Browse files

cache: attempt to future-proof the cache file

parent f8b0b4f5
No related branches found
No related tags found
No related merge requests found
...@@ -7,7 +7,6 @@ import ( ...@@ -7,7 +7,6 @@ import (
) )
const lockfile = "lock" const lockfile = "lock"
const excerptsFile = "excerpts"
type MultiRepoCache struct { type MultiRepoCache struct {
repos map[string]*RepoCache repos map[string]*RepoCache
......
...@@ -19,6 +19,9 @@ import ( ...@@ -19,6 +19,9 @@ import (
"github.com/MichaelMure/git-bug/util/process" "github.com/MichaelMure/git-bug/util/process"
) )
const cacheFile = "cache"
const formatVersion = 1
type RepoCache struct { type RepoCache struct {
// the underlying repo // the underlying repo
repo repository.Repo repo repository.Repo
...@@ -39,14 +42,14 @@ func NewRepoCache(r repository.Repo) (*RepoCache, error) { ...@@ -39,14 +42,14 @@ func NewRepoCache(r repository.Repo) (*RepoCache, error) {
return &RepoCache{}, err return &RepoCache{}, err
} }
err = c.loadExcerpts() err = c.load()
if err == nil { if err == nil {
return c, nil return c, nil
} }
c.buildAllExcerpt() c.buildCache()
return c, c.writeExcerpts() return c, c.write()
} }
// Repository return the underlying repository. // Repository return the underlying repository.
...@@ -92,45 +95,56 @@ func (c *RepoCache) bugUpdated(id string) error { ...@@ -92,45 +95,56 @@ func (c *RepoCache) bugUpdated(id string) error {
c.excerpts[id] = NewBugExcerpt(b.bug, b.Snapshot()) c.excerpts[id] = NewBugExcerpt(b.bug, b.Snapshot())
return c.writeExcerpts() return c.write()
} }
// loadExcerpts will try to read from the disk the bug excerpt file // load will try to read from the disk the bug cache file
func (c *RepoCache) loadExcerpts() error { func (c *RepoCache) load() error {
excerptsPath := repoExcerptsFilePath(c.repo) f, err := os.Open(cacheFilePath(c.repo))
f, err := os.Open(excerptsPath)
if err != nil { if err != nil {
return err return err
} }
decoder := gob.NewDecoder(f) decoder := gob.NewDecoder(f)
var excerpts map[string]*BugExcerpt aux := struct {
Version uint
Excerpts map[string]*BugExcerpt
}{}
err = decoder.Decode(&excerpts) err = decoder.Decode(&aux)
if err != nil { if err != nil {
return err return err
} }
c.excerpts = excerpts if aux.Version != 1 {
return fmt.Errorf("unknown cache format version %v", aux.Version)
}
c.excerpts = aux.Excerpts
return nil return nil
} }
// writeExcerpts will serialize on disk the BugExcerpt array // write will serialize on disk the bug cache file
func (c *RepoCache) writeExcerpts() error { func (c *RepoCache) write() error {
var data bytes.Buffer var data bytes.Buffer
aux := struct {
Version uint
Excerpts map[string]*BugExcerpt
}{
Version: formatVersion,
Excerpts: c.excerpts,
}
encoder := gob.NewEncoder(&data) encoder := gob.NewEncoder(&data)
err := encoder.Encode(c.excerpts) err := encoder.Encode(aux)
if err != nil { if err != nil {
return err return err
} }
excerptsPath := repoExcerptsFilePath(c.repo) f, err := os.Create(cacheFilePath(c.repo))
f, err := os.Create(excerptsPath)
if err != nil { if err != nil {
return err return err
} }
...@@ -143,11 +157,11 @@ func (c *RepoCache) writeExcerpts() error { ...@@ -143,11 +157,11 @@ func (c *RepoCache) writeExcerpts() error {
return f.Close() return f.Close()
} }
func repoExcerptsFilePath(repo repository.Repo) string { func cacheFilePath(repo repository.Repo) string {
return path.Join(repo.GetPath(), ".git", "git-bug", excerptsFile) return path.Join(repo.GetPath(), ".git", "git-bug", cacheFile)
} }
func (c *RepoCache) buildAllExcerpt() { func (c *RepoCache) buildCache() {
fmt.Printf("Building bug cache... ") fmt.Printf("Building bug cache... ")
c.excerpts = make(map[string]*BugExcerpt) c.excerpts = make(map[string]*BugExcerpt)
...@@ -324,7 +338,7 @@ func (c *RepoCache) MergeAll(remote string) <-chan bug.MergeResult { ...@@ -324,7 +338,7 @@ func (c *RepoCache) MergeAll(remote string) <-chan bug.MergeResult {
out <- result out <- result
} }
err := c.writeExcerpts() err := c.write()
// No easy way out here .. // No easy way out here ..
if err != nil { if err != nil {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment