From 1e1315d5e587d481e73e295dd18e50026af48c79 Mon Sep 17 00:00:00 2001 From: Jeremy Stribling Date: Thu, 11 Oct 2018 16:24:25 -0700 Subject: object: get object size without reading whole object Signed-off-by: Jeremy Stribling --- plumbing/format/packfile/packfile.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'plumbing/format/packfile/packfile.go') diff --git a/plumbing/format/packfile/packfile.go b/plumbing/format/packfile/packfile.go index 852a834..dbd5d4b 100644 --- a/plumbing/format/packfile/packfile.go +++ b/plumbing/format/packfile/packfile.go @@ -90,6 +90,22 @@ func (p *Packfile) GetByOffset(o int64) (plumbing.EncodedObject, error) { return p.nextObject() } +func (p *Packfile) GetSizeByOffset(o int64) (size int64, err error) { + if _, err := p.s.SeekFromStart(o); err != nil { + if err == io.EOF || isInvalid(err) { + return 0, plumbing.ErrObjectNotFound + } + + return 0, err + } + + h, err := p.nextObjectHeader() + if err != nil { + return 0, err + } + return h.Length, nil +} + func (p *Packfile) nextObjectHeader() (*ObjectHeader, error) { h, err := p.s.NextObjectHeader() p.s.pendingObject = nil -- cgit