diff options
Diffstat (limited to 'formats/packfile/decoder.go')
-rw-r--r-- | formats/packfile/decoder.go | 48 |
1 files changed, 25 insertions, 23 deletions
diff --git a/formats/packfile/decoder.go b/formats/packfile/decoder.go index 6a0aa71..23a8e1a 100644 --- a/formats/packfile/decoder.go +++ b/formats/packfile/decoder.go @@ -91,20 +91,17 @@ func (d *Decoder) doDecode() error { func (d *Decoder) readObjects(count uint32) error { for i := 0; i < int(count); i++ { - obj, err := d.readObject() + _, err := d.ReadObject() if err != nil { return err } - - if _, err := d.tx.Set(obj); err != nil { - return err - } } return nil } -func (d *Decoder) readObject() (core.Object, error) { +// ReadObject reads a object from the stream and return it +func (d *Decoder) ReadObject() (core.Object, error) { h, err := d.s.NextObjectHeader() if err != nil { return nil, err @@ -130,9 +127,31 @@ func (d *Decoder) readObject() (core.Object, error) { } d.remember(obj, h.Offset, crc) + + if _, err := d.tx.Set(obj); err != nil { + return nil, err + } + return obj, nil } +// ReadObjectAt reads an object at the given location +func (d *Decoder) ReadObjectAt(offset int64) (core.Object, error) { + beforeJump, err := d.s.Seek(offset) + if err != nil { + return nil, err + } + + defer func() { + _, seekErr := d.s.Seek(beforeJump) + if err == nil { + err = seekErr + } + }() + + return d.ReadObject() +} + func (d *Decoder) fillRegularObjectContent(obj core.Object) (uint32, error) { w, err := obj.Writer() if err != nil { @@ -195,23 +214,6 @@ func (d *Decoder) recallByHash(h core.Hash) (core.Object, error) { return d.tx.Get(core.AnyObject, h) } -// ReadObjectAt reads an object at the given location -func (d *Decoder) ReadObjectAt(offset int64) (core.Object, error) { - beforeJump, err := d.s.Seek(offset) - if err != nil { - return nil, err - } - - defer func() { - _, seekErr := d.s.Seek(beforeJump) - if err == nil { - err = seekErr - } - }() - - return d.readObject() -} - // Offsets returns the objects read offset func (d *Decoder) Offsets() map[core.Hash]int64 { i := make(map[core.Hash]int64, len(d.offsets)) |