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

plumbing: format/packfile, fix crash with cycle deltas #731

Merged
merged 2 commits into from
Jan 25, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion plumbing/format/packfile/delta_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ func (dw *deltaSelector) restoreOriginal(otp *ObjectToPack) error {
return err
}

otp.Original = obj
otp.SetOriginal(obj)

return nil
}

Expand Down
1 change: 1 addition & 0 deletions plumbing/format/packfile/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func (e *Encoder) entry(o *ObjectToPack) error {
// (for example due to a concurrent repack) and a different base
// was chosen, forcing a cycle. Select something other than a
// delta, and write this object.
e.selector.restoreOriginal(o)
o.BackToOriginal()
}

Expand Down
21 changes: 21 additions & 0 deletions plumbing/format/packfile/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,15 @@ func (s *EncoderSuite) deltaOverDeltaCyclicTest(c *C) {
o3 := newObject(plumbing.BlobObject, []byte("011111"))
o4 := newObject(plumbing.BlobObject, []byte("01111100000"))

_, err := s.store.SetEncodedObject(o1)
c.Assert(err, IsNil)
_, err = s.store.SetEncodedObject(o2)
c.Assert(err, IsNil)
_, err = s.store.SetEncodedObject(o3)
c.Assert(err, IsNil)
_, err = s.store.SetEncodedObject(o4)
c.Assert(err, IsNil)

d2, err := GetDelta(o1, o2)
c.Assert(err, IsNil)

Expand All @@ -219,6 +228,18 @@ func (s *EncoderSuite) deltaOverDeltaCyclicTest(c *C) {
pd3.SetDelta(pd4, d3)
pd4.SetDelta(pd3, d4)

// SetOriginal is used by delta selector when generating ObjectToPack.
// It also fills type, hash and size values to be used when Original
// is nil.
po1.SetOriginal(po1.Original)
pd2.SetOriginal(pd2.Original)
pd2.Original = nil

pd3.SetOriginal(pd3.Original)
pd3.Original = nil

pd4.SetOriginal(pd4.Original)

encHash, err := s.enc.encode([]*ObjectToPack{
po1,
pd2,
Expand Down
27 changes: 27 additions & 0 deletions plumbing/format/packfile/object_pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ type ObjectToPack struct {
// offset in pack when object has been already written, or 0 if it
// has not been written yet
Offset int64

// Information from the original object
resolvedOriginal bool
originalType plumbing.ObjectType
originalSize int64
originalHash plumbing.Hash
}

// newObjectToPack creates a correct ObjectToPack based on a non-delta object
Expand Down Expand Up @@ -71,11 +77,24 @@ func (o *ObjectToPack) WantWrite() bool {
return o.Offset == 1
}

// SetOriginal sets both Original and saves size, type and hash
func (o *ObjectToPack) SetOriginal(obj plumbing.EncodedObject) {
o.Original = obj
o.originalSize = obj.Size()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check if obj is nil

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added checks to SetOriginal and BackToOriginal. The test now also uses it to set Original to nil.

o.originalType = obj.Type()
o.originalHash = obj.Hash()
o.resolvedOriginal = true
}

func (o *ObjectToPack) Type() plumbing.ObjectType {
if o.Original != nil {
return o.Original.Type()
}

if o.resolvedOriginal {
return o.originalType
}

if o.Base != nil {
return o.Base.Type()
}
Expand All @@ -92,6 +111,10 @@ func (o *ObjectToPack) Hash() plumbing.Hash {
return o.Original.Hash()
}

if o.resolvedOriginal {
return o.originalHash
}

do, ok := o.Object.(plumbing.DeltaObject)
if ok {
return do.ActualHash()
Expand All @@ -105,6 +128,10 @@ func (o *ObjectToPack) Size() int64 {
return o.Original.Size()
}

if o.resolvedOriginal {
return o.originalSize
}

do, ok := o.Object.(plumbing.DeltaObject)
if ok {
return do.ActualSize()
Expand Down