diff options
author | JP Sugarbroad <jpsugar@google.com> | 2017-04-06 11:54:38 -0700 |
---|---|---|
committer | JP Sugarbroad <jpsugar@google.com> | 2017-04-06 15:09:30 -0700 |
commit | 6ed3f8cbb0119887d000db77335c67b59cada59b (patch) | |
tree | e3f799741ddfc096d6cd43aede792218e4c572f0 | |
parent | 8ede7794a674fd890147f81201e25ecb517046f0 (diff) | |
download | go-git-6ed3f8cbb0119887d000db77335c67b59cada59b.tar.gz |
Lazily load object index.
fixes #327
-rw-r--r-- | storage/filesystem/object.go | 28 |
1 files changed, 22 insertions, 6 deletions
diff --git a/storage/filesystem/object.go b/storage/filesystem/object.go index 1518256..782b99b 100644 --- a/storage/filesystem/object.go +++ b/storage/filesystem/object.go @@ -22,14 +22,18 @@ type ObjectStorage struct { func newObjectStorage(dir *dotgit.DotGit) (ObjectStorage, error) { s := ObjectStorage{ - dir: dir, - index: make(map[plumbing.Hash]idx, 0), + dir: dir, } - return s, s.loadIdxFiles() + return s, nil } -func (s *ObjectStorage) loadIdxFiles() error { +func (s *ObjectStorage) requireIndex() error { + if s.index != nil { + return nil + } + + s.index = make(map[plumbing.Hash]idx, 0) packs, err := s.dir.ObjectPacks() if err != nil { return err @@ -49,6 +53,7 @@ func (s *ObjectStorage) loadIdxFile(h plumbing.Hash) error { if err != nil { return err } + defer idxfile.Close() s.index[h] = make(idx) return s.index[h].Decode(idxfile) @@ -59,6 +64,10 @@ func (s *ObjectStorage) NewEncodedObject() plumbing.EncodedObject { } func (s *ObjectStorage) PackfileWriter() (io.WriteCloser, error) { + if err := s.requireIndex(); err != nil { + return nil, err + } + w, err := s.dir.NewObjectPack() if err != nil { return nil, err @@ -163,6 +172,10 @@ func (s *ObjectStorage) getFromUnpacked(h plumbing.Hash) (obj plumbing.EncodedOb // Get returns the object with the given hash, by searching for it in // the packfile. func (s *ObjectStorage) getFromPackfile(h plumbing.Hash) (plumbing.EncodedObject, error) { + if err := s.requireIndex(); err != nil { + return nil, err + } + pack, offset := s.findObjectInPackfile(h) if offset == -1 { return nil, plumbing.ErrObjectNotFound @@ -219,8 +232,11 @@ func (s *ObjectStorage) IterEncodedObjects(t plumbing.ObjectType) (storer.Encode return storer.NewMultiEncodedObjectIter(iters), nil } -func (s *ObjectStorage) buildPackfileIters( - t plumbing.ObjectType, seen map[plumbing.Hash]bool) ([]storer.EncodedObjectIter, error) { +func (s *ObjectStorage) buildPackfileIters(t plumbing.ObjectType, seen map[plumbing.Hash]bool) ([]storer.EncodedObjectIter, error) { + if err := s.requireIndex(); err != nil { + return nil, err + } + packs, err := s.dir.ObjectPacks() if err != nil { return nil, err |