aboutsummaryrefslogtreecommitdiffstats
path: root/storage/filesystem
diff options
context:
space:
mode:
Diffstat (limited to 'storage/filesystem')
-rw-r--r--storage/filesystem/object.go28
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