diff options
author | zeripath <art27@cantab.net> | 2021-05-12 21:42:07 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-12 22:42:07 +0200 |
commit | 720c192831a890d0a36b4c6720b60411fa4a0159 (patch) | |
tree | 9f3020c074fe9b113e22d6f2c601e8a36bf0ac49 /plumbing/format/packfile/fsobject.go | |
parent | e6e23391e4d044cc85e09b4420a2533715e7312d (diff) | |
download | go-git-720c192831a890d0a36b4c6720b60411fa4a0159.tar.gz |
plumbing: format/packfile, prevent large objects from being read into memory completely (#303)v5.4.0
This PR adds code to prevent large objects from being read into memory from packfiles or the filesystem.
Objects greater than 1Mb are now no longer directly stored in the cache
or read completely into memory.
Signed-off-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'plumbing/format/packfile/fsobject.go')
-rw-r--r-- | plumbing/format/packfile/fsobject.go | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/plumbing/format/packfile/fsobject.go b/plumbing/format/packfile/fsobject.go index c5edaf5..4aa3c8e 100644 --- a/plumbing/format/packfile/fsobject.go +++ b/plumbing/format/packfile/fsobject.go @@ -7,6 +7,7 @@ import ( "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/cache" "github.com/go-git/go-git/v5/plumbing/format/idxfile" + "github.com/go-git/go-git/v5/utils/ioutil" ) // FSObject is an object from the packfile on the filesystem. @@ -63,6 +64,20 @@ func (o *FSObject) Reader() (io.ReadCloser, error) { } p := NewPackfileWithCache(o.index, nil, f, o.cache) + if o.size > LargeObjectThreshold { + // We have a big object + h, err := p.objectHeaderAtOffset(o.offset) + if err != nil { + return nil, err + } + + r, err := p.getReaderDirect(h) + if err != nil { + _ = f.Close() + return nil, err + } + return ioutil.NewReadCloserWithCloser(r, f.Close), nil + } r, err := p.getObjectContent(o.offset) if err != nil { _ = f.Close() |