diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2017-11-19 18:56:33 +0100 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2017-11-19 18:56:33 +0100 |
commit | 07adc3a19b288c539903dc126d20998e17137508 (patch) | |
tree | 65bbfc79f492dc929d5d67eeb0e13330d728bfb8 /plumbing | |
parent | 63f480619b2795a6bb4f7d7d4ebbcc385688c0b4 (diff) | |
download | go-git-07adc3a19b288c539903dc126d20998e17137508.tar.gz |
format: packfile fix DecodeObjectAt when Decoder has type
Signed-off-by: Máximo Cuadros <mcuadros@gmail.com>
Diffstat (limited to 'plumbing')
-rw-r--r-- | plumbing/format/packfile/decoder.go | 12 | ||||
-rw-r--r-- | plumbing/format/packfile/decoder_test.go | 21 |
2 files changed, 29 insertions, 4 deletions
diff --git a/plumbing/format/packfile/decoder.go b/plumbing/format/packfile/decoder.go index 3d475b2..26a2a01 100644 --- a/plumbing/format/packfile/decoder.go +++ b/plumbing/format/packfile/decoder.go @@ -207,12 +207,16 @@ func (d *Decoder) decodeObjectsWithObjectStorerTx(count int) error { // constructor, if the object decoded is not equals to the specified one, nil will // be returned func (d *Decoder) DecodeObject() (plumbing.EncodedObject, error) { + return d.doDecodeObject(d.decoderType) +} + +func (d *Decoder) doDecodeObject(t plumbing.ObjectType) (plumbing.EncodedObject, error) { h, err := d.s.NextObjectHeader() if err != nil { return nil, err } - if d.decoderType == plumbing.AnyObject { + if t == plumbing.AnyObject { return d.decodeByHeader(h) } @@ -279,6 +283,7 @@ func (d *Decoder) decodeByHeader(h *ObjectHeader) (plumbing.EncodedObject, error obj := d.newObject() obj.SetSize(h.Length) obj.SetType(h.Type) + var crc uint32 var err error switch h.Type { @@ -315,7 +320,8 @@ func (d *Decoder) newObject() plumbing.EncodedObject { // returned is added into a internal index. This is intended to be able to regenerate // objects from deltas (offset deltas or reference deltas) without an package index // (.idx file). If Decode wasn't called previously objects offset should provided -// using the SetOffsets method. +// using the SetOffsets method. It decodes the object regardless of the Decoder +// type. func (d *Decoder) DecodeObjectAt(offset int64) (plumbing.EncodedObject, error) { if !d.s.IsSeekable { return nil, ErrNonSeekable @@ -333,7 +339,7 @@ func (d *Decoder) DecodeObjectAt(offset int64) (plumbing.EncodedObject, error) { } }() - return d.DecodeObject() + return d.doDecodeObject(plumbing.AnyObject) } func (d *Decoder) fillRegularObjectContent(obj plumbing.EncodedObject) (uint32, error) { diff --git a/plumbing/format/packfile/decoder_test.go b/plumbing/format/packfile/decoder_test.go index ecf7c81..5d1530c 100644 --- a/plumbing/format/packfile/decoder_test.go +++ b/plumbing/format/packfile/decoder_test.go @@ -293,7 +293,7 @@ func (s *ReaderSuite) TestDecodeCRCs(c *C) { c.Assert(int(sum), Equals, 78022211966) } -func (s *ReaderSuite) TestReadObjectAt(c *C) { +func (s *ReaderSuite) TestDecodeObjectAt(c *C) { f := fixtures.Basic().One() scanner := packfile.NewScanner(f.Packfile()) d, err := packfile.NewDecoder(scanner, nil) @@ -311,6 +311,25 @@ func (s *ReaderSuite) TestReadObjectAt(c *C) { c.Assert(obj.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5") } +func (s *ReaderSuite) TestDecodeObjectAtForType(c *C) { + f := fixtures.Basic().One() + scanner := packfile.NewScanner(f.Packfile()) + d, err := packfile.NewDecoderForType(scanner, nil, plumbing.TreeObject) + c.Assert(err, IsNil) + + // when the packfile is ref-delta based, the offsets are required + if f.Is("ref-delta") { + d.SetIndex(getIndexFromIdxFile(f.Idx())) + } + + // the objects at reference 186, is a delta, so should be recall, + // without being read before. + obj, err := d.DecodeObjectAt(186) + c.Assert(err, IsNil) + c.Assert(obj.Type(), Equals, plumbing.CommitObject) + c.Assert(obj.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5") +} + func (s *ReaderSuite) TestIndex(c *C) { f := fixtures.Basic().One() scanner := packfile.NewScanner(f.Packfile()) |