Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit 18e481c

Browse files
committed
filesystem: ObjectStorage, keep packfiles open if KeepDescriptors is enabled
Signed-off-by: Filip Navara <[email protected]>
1 parent aa6f288 commit 18e481c

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

storage/filesystem/object.go

+34-13
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ type ObjectStorage struct {
2424
// loaded loose objects
2525
objectCache cache.Object
2626

27-
dir *dotgit.DotGit
28-
index map[plumbing.Hash]idxfile.Index
27+
dir *dotgit.DotGit
28+
index map[plumbing.Hash]idxfile.Index
29+
packfiles map[plumbing.Hash]*packfile.Packfile
2930
}
3031

3132
// NewObjectStorage creates a new ObjectStorage with the given .git directory and cache.
@@ -215,14 +216,7 @@ func (s *ObjectStorage) encodedObjectSizeFromPackfile(h plumbing.Hash) (
215216
return 0, err
216217
}
217218

218-
var p *packfile.Packfile
219-
if s.objectCache != nil {
220-
p = packfile.NewPackfileWithCache(idx, s.dir.Fs(), f, s.objectCache)
221-
} else {
222-
p = packfile.NewPackfile(idx, s.dir.Fs(), f)
223-
}
224-
225-
return p.GetSizeByOffset(offset)
219+
return s.getPackfile(f, idx, pack).GetSizeByOffset(offset)
226220
}
227221

228222
// EncodedObjectSize returns the plaintext size of the given object,
@@ -372,16 +366,37 @@ func (s *ObjectStorage) getFromPackfile(h plumbing.Hash, canBeDelta bool) (
372366

373367
idx := s.index[pack]
374368
if canBeDelta {
375-
return s.decodeDeltaObjectAt(f, idx, offset, hash)
369+
return s.decodeDeltaObjectAt(f, idx, offset, hash, pack)
376370
}
377371

378-
return s.decodeObjectAt(f, idx, offset)
372+
return s.decodeObjectAt(f, idx, offset, hash, pack)
373+
}
374+
375+
func (s *ObjectStorage) getPackfile(f billy.File, idx idxfile.Index, pack plumbing.Hash) *packfile.Packfile {
376+
var p *packfile.Packfile
377+
var ok bool
378+
379+
if p, ok = s.packfiles[pack]; !ok {
380+
if s.objectCache != nil {
381+
p = packfile.NewPackfileWithCache(idx, s.dir.Fs(), f, s.objectCache)
382+
} else {
383+
p = packfile.NewPackfile(idx, s.dir.Fs(), f)
384+
}
385+
386+
if s.options.KeepDescriptors {
387+
s.packfiles[pack] = p
388+
}
389+
}
390+
391+
return p
379392
}
380393

381394
func (s *ObjectStorage) decodeObjectAt(
382395
f billy.File,
383396
idx idxfile.Index,
384397
offset int64,
398+
hash plumbing.Hash,
399+
pack plumbing.Hash,
385400
) (plumbing.EncodedObject, error) {
386401
hash, err := idx.FindHash(offset)
387402
if err == nil {
@@ -410,6 +425,7 @@ func (s *ObjectStorage) decodeDeltaObjectAt(
410425
idx idxfile.Index,
411426
offset int64,
412427
hash plumbing.Hash,
428+
pack plumbing.Hash,
413429
) (plumbing.EncodedObject, error) {
414430
if _, err := f.Seek(0, io.SeekStart); err != nil {
415431
return nil, err
@@ -434,7 +450,7 @@ func (s *ObjectStorage) decodeDeltaObjectAt(
434450
return nil, err
435451
}
436452
default:
437-
return s.decodeObjectAt(f, idx, offset)
453+
return s.decodeObjectAt(f, idx, offset, hash, pack)
438454
}
439455

440456
obj := &plumbing.MemoryObject{}
@@ -515,6 +531,11 @@ func (s *ObjectStorage) buildPackfileIters(
515531

516532
// Close closes all opened files.
517533
func (s *ObjectStorage) Close() error {
534+
for _, p := range s.packfiles {
535+
p.Close()
536+
}
537+
s.packfiles = make(map[plumbing.Hash]*packfile.Packfile)
538+
518539
return s.dir.Close()
519540
}
520541

0 commit comments

Comments
 (0)