diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2020-05-24 19:25:08 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-24 19:25:08 +0200 |
commit | 8019144b6534ff58ad234a355e5b143f1c99b45e (patch) | |
tree | 5a67682c34bb20a826ebf430f11181d6892fc0e7 | |
parent | e7f544844d6d736acfd9d75ee0d4a9d37f450103 (diff) | |
parent | 10199949b9e5a71f72241c4bb23f3d733287065c (diff) | |
download | go-git-8019144b6534ff58ad234a355e5b143f1c99b45e.tar.gz |
Merge pull request #73 from WKBae/close_objectv5.1.0
Close Reader & Writer of EncodedObject after use
-rw-r--r-- | plumbing/format/packfile/diff_delta.go | 10 | ||||
-rw-r--r-- | plumbing/format/packfile/encoder.go | 10 | ||||
-rw-r--r-- | plumbing/format/packfile/encoder_test.go | 6 | ||||
-rw-r--r-- | plumbing/format/packfile/packfile.go | 5 | ||||
-rw-r--r-- | plumbing/format/packfile/parser.go | 15 | ||||
-rw-r--r-- | plumbing/format/packfile/patch_delta.go | 7 | ||||
-rw-r--r-- | storage/filesystem/object.go | 2 | ||||
-rw-r--r-- | storage/test/storage_suite.go | 14 |
8 files changed, 55 insertions, 14 deletions
diff --git a/plumbing/format/packfile/diff_delta.go b/plumbing/format/packfile/diff_delta.go index 1d4b384..1951b34 100644 --- a/plumbing/format/packfile/diff_delta.go +++ b/plumbing/format/packfile/diff_delta.go @@ -4,6 +4,7 @@ import ( "bytes" "github.com/go-git/go-git/v5/plumbing" + "github.com/go-git/go-git/v5/utils/ioutil" ) // See https://github.com/jelmer/dulwich/blob/master/dulwich/pack.py and @@ -27,17 +28,20 @@ func GetDelta(base, target plumbing.EncodedObject) (plumbing.EncodedObject, erro return getDelta(new(deltaIndex), base, target) } -func getDelta(index *deltaIndex, base, target plumbing.EncodedObject) (plumbing.EncodedObject, error) { +func getDelta(index *deltaIndex, base, target plumbing.EncodedObject) (o plumbing.EncodedObject, err error) { br, err := base.Reader() if err != nil { return nil, err } - defer br.Close() + + defer ioutil.CheckClose(br, &err) + tr, err := target.Reader() if err != nil { return nil, err } - defer tr.Close() + + defer ioutil.CheckClose(tr, &err) bb := bufPool.Get().(*bytes.Buffer) defer bufPool.Put(bb) diff --git a/plumbing/format/packfile/encoder.go b/plumbing/format/packfile/encoder.go index 65fae52..5501f88 100644 --- a/plumbing/format/packfile/encoder.go +++ b/plumbing/format/packfile/encoder.go @@ -9,6 +9,7 @@ import ( "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/storer" "github.com/go-git/go-git/v5/utils/binary" + "github.com/go-git/go-git/v5/utils/ioutil" ) // Encoder gets the data from the storage and write it into the writer in PACK @@ -80,7 +81,7 @@ func (e *Encoder) head(numEntries int) error { ) } -func (e *Encoder) entry(o *ObjectToPack) error { +func (e *Encoder) entry(o *ObjectToPack) (err error) { if o.WantWrite() { // A cycle exists in this delta chain. This should only occur if a // selected object representation disappeared during writing @@ -119,17 +120,22 @@ func (e *Encoder) entry(o *ObjectToPack) error { } e.zw.Reset(e.w) + + defer ioutil.CheckClose(e.zw, &err) + or, err := o.Object.Reader() if err != nil { return err } + defer ioutil.CheckClose(or, &err) + _, err = io.Copy(e.zw, or) if err != nil { return err } - return e.zw.Close() + return nil } func (e *Encoder) writeBaseIfDelta(o *ObjectToPack) error { diff --git a/plumbing/format/packfile/encoder_test.go b/plumbing/format/packfile/encoder_test.go index 80d1e73..2689762 100644 --- a/plumbing/format/packfile/encoder_test.go +++ b/plumbing/format/packfile/encoder_test.go @@ -287,6 +287,12 @@ func objectsEqual(c *C, o1, o2 plumbing.EncodedObject) { c.Assert(err, IsNil) c.Assert(bytes.Compare(b1, b2), Equals, 0) + + err = r2.Close() + c.Assert(err, IsNil) + + err = r1.Close() + c.Assert(err, IsNil) } func packfileFromReader(c *C, buf *bytes.Buffer) (*Packfile, func()) { diff --git a/plumbing/format/packfile/packfile.go b/plumbing/format/packfile/packfile.go index d060041..ddd7f62 100644 --- a/plumbing/format/packfile/packfile.go +++ b/plumbing/format/packfile/packfile.go @@ -10,6 +10,7 @@ import ( "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/plumbing/storer" + "github.com/go-git/go-git/v5/utils/ioutil" ) var ( @@ -307,12 +308,14 @@ func (p *Packfile) getNextMemoryObject(h *ObjectHeader) (plumbing.EncodedObject, return obj, nil } -func (p *Packfile) fillRegularObjectContent(obj plumbing.EncodedObject) error { +func (p *Packfile) fillRegularObjectContent(obj plumbing.EncodedObject) (err error) { w, err := obj.Writer() if err != nil { return err } + defer ioutil.CheckClose(w, &err) + _, _, err = p.s.NextObject(w) p.cachePut(obj) diff --git a/plumbing/format/packfile/parser.go b/plumbing/format/packfile/parser.go index d411c5b..4b5a570 100644 --- a/plumbing/format/packfile/parser.go +++ b/plumbing/format/packfile/parser.go @@ -4,11 +4,12 @@ import ( "bytes" "errors" "io" - "io/ioutil" + stdioutil "io/ioutil" "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/storer" + "github.com/go-git/go-git/v5/utils/ioutil" ) var ( @@ -283,7 +284,7 @@ func (p *Parser) resolveDeltas() error { if !obj.IsDelta() && len(obj.Children) > 0 { for _, child := range obj.Children { - if err := p.resolveObject(ioutil.Discard, child, content); err != nil { + if err := p.resolveObject(stdioutil.Discard, child, content); err != nil { return err } } @@ -298,7 +299,7 @@ func (p *Parser) resolveDeltas() error { return nil } -func (p *Parser) get(o *objectInfo, buf *bytes.Buffer) error { +func (p *Parser) get(o *objectInfo, buf *bytes.Buffer) (err error) { if !o.ExternalRef { // skip cache check for placeholder parents b, ok := p.cache.Get(o.Offset) if ok { @@ -310,17 +311,21 @@ func (p *Parser) get(o *objectInfo, buf *bytes.Buffer) error { // If it's not on the cache and is not a delta we can try to find it in the // storage, if there's one. External refs must enter here. if p.storage != nil && !o.Type.IsDelta() { - e, err := p.storage.EncodedObject(plumbing.AnyObject, o.SHA1) + var e plumbing.EncodedObject + e, err = p.storage.EncodedObject(plumbing.AnyObject, o.SHA1) if err != nil { return err } o.Type = e.Type() - r, err := e.Reader() + var r io.ReadCloser + r, err = e.Reader() if err != nil { return err } + defer ioutil.CheckClose(r, &err) + _, err = buf.ReadFrom(io.LimitReader(r, e.Size())) return err } diff --git a/plumbing/format/packfile/patch_delta.go b/plumbing/format/packfile/patch_delta.go index 57c9da7..1dc8b8b 100644 --- a/plumbing/format/packfile/patch_delta.go +++ b/plumbing/format/packfile/patch_delta.go @@ -6,6 +6,7 @@ import ( "io" "github.com/go-git/go-git/v5/plumbing" + "github.com/go-git/go-git/v5/utils/ioutil" ) // See https://github.com/git/git/blob/49fa3dc76179e04b0833542fa52d0f287a4955ac/delta.h @@ -16,17 +17,21 @@ import ( const deltaSizeMin = 4 // ApplyDelta writes to target the result of applying the modification deltas in delta to base. -func ApplyDelta(target, base plumbing.EncodedObject, delta []byte) error { +func ApplyDelta(target, base plumbing.EncodedObject, delta []byte) (err error) { r, err := base.Reader() if err != nil { return err } + defer ioutil.CheckClose(r, &err) + w, err := target.Writer() if err != nil { return err } + defer ioutil.CheckClose(w, &err) + buf := bufPool.Get().(*bytes.Buffer) defer bufPool.Put(buf) buf.Reset() diff --git a/storage/filesystem/object.go b/storage/filesystem/object.go index 862cc1b..7437174 100644 --- a/storage/filesystem/object.go +++ b/storage/filesystem/object.go @@ -408,6 +408,8 @@ func (s *ObjectStorage) getFromUnpacked(h plumbing.Hash) (obj plumbing.EncodedOb return nil, err } + defer ioutil.CheckClose(w, &err) + s.objectCache.Put(obj) _, err = io.Copy(w, r) diff --git a/storage/test/storage_suite.go b/storage/test/storage_suite.go index d87a7dd..2c00e75 100644 --- a/storage/test/storage_suite.go +++ b/storage/test/storage_suite.go @@ -494,12 +494,12 @@ func objectEquals(a plumbing.EncodedObject, b plumbing.EncodedObject) error { ra, err := a.Reader() if err != nil { - return fmt.Errorf("can't get reader on b: %q", err) + return fmt.Errorf("can't get reader on a: %q", err) } rb, err := b.Reader() if err != nil { - return fmt.Errorf("can't get reader on a: %q", err) + return fmt.Errorf("can't get reader on b: %q", err) } ca, err := ioutil.ReadAll(ra) @@ -516,5 +516,15 @@ func objectEquals(a plumbing.EncodedObject, b plumbing.EncodedObject) error { return errors.New("content does not match") } + err = rb.Close() + if err != nil { + return fmt.Errorf("can't close reader on b: %q", err) + } + + err = ra.Close() + if err != nil { + return fmt.Errorf("can't close reader on a: %q", err) + } + return nil } |