diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2017-05-04 01:45:11 +0200 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2017-05-04 01:45:11 +0200 |
commit | ff18ce3751ad80cfd0297845872ba1d796c36ca5 (patch) | |
tree | 0b0a72f26482bc61bad7258d10a9a2d1392d2b90 /plumbing | |
parent | 5592dabdf9eed67c92b0e411ad375ae763119fd2 (diff) | |
download | go-git-ff18ce3751ad80cfd0297845872ba1d796c36ca5.tar.gz |
plumbing: MemoryObject.Writer tracks his own size
Diffstat (limited to 'plumbing')
-rw-r--r-- | plumbing/memory.go | 2 | ||||
-rw-r--r-- | plumbing/object/blob.go | 7 | ||||
-rw-r--r-- | plumbing/object/commit.go | 9 | ||||
-rw-r--r-- | plumbing/object/tag_test.go | 35 | ||||
-rw-r--r-- | plumbing/object/tree.go | 14 |
5 files changed, 37 insertions, 30 deletions
diff --git a/plumbing/memory.go b/plumbing/memory.go index c65ce1f..51cbb54 100644 --- a/plumbing/memory.go +++ b/plumbing/memory.go @@ -51,6 +51,8 @@ func (o *MemoryObject) Writer() (io.WriteCloser, error) { func (o *MemoryObject) Write(p []byte) (n int, err error) { o.cont = append(o.cont, p...) + o.sz = int64(len(o.cont)) + return len(p), nil } diff --git a/plumbing/object/blob.go b/plumbing/object/blob.go index 2771416..2608477 100644 --- a/plumbing/object/blob.go +++ b/plumbing/object/blob.go @@ -68,18 +68,23 @@ func (b *Blob) Decode(o plumbing.EncodedObject) error { // Encode transforms a Blob into a plumbing.EncodedObject. func (b *Blob) Encode(o plumbing.EncodedObject) error { + o.SetType(plumbing.BlobObject) + w, err := o.Writer() if err != nil { return err } + defer ioutil.CheckClose(w, &err) + r, err := b.Reader() if err != nil { return err } + defer ioutil.CheckClose(r, &err) + _, err = io.Copy(w, r) - o.SetType(plumbing.BlobObject) return err } diff --git a/plumbing/object/commit.go b/plumbing/object/commit.go index 2e47a2a..adac4f9 100644 --- a/plumbing/object/commit.go +++ b/plumbing/object/commit.go @@ -171,30 +171,39 @@ func (b *Commit) Encode(o plumbing.EncodedObject) error { if err != nil { return err } + defer ioutil.CheckClose(w, &err) + if _, err = fmt.Fprintf(w, "tree %s\n", b.TreeHash.String()); err != nil { return err } + for _, parent := range b.ParentHashes { if _, err = fmt.Fprintf(w, "parent %s\n", parent.String()); err != nil { return err } } + if _, err = fmt.Fprint(w, "author "); err != nil { return err } + if err = b.Author.Encode(w); err != nil { return err } + if _, err = fmt.Fprint(w, "\ncommitter "); err != nil { return err } + if err = b.Committer.Encode(w); err != nil { return err } + if _, err = fmt.Fprintf(w, "\n\n%s", b.Message); err != nil { return err } + return err } diff --git a/plumbing/object/tag_test.go b/plumbing/object/tag_test.go index f121ce9..9f2d28c 100644 --- a/plumbing/object/tag_test.go +++ b/plumbing/object/tag_test.go @@ -234,33 +234,32 @@ func (s *TagSuite) TestString(c *C) { ) } -func (s *TagSuite) TestTagToTagString(c *C) { +func (s *TagSuite) TestStringNonCommit(c *C) { store := memory.NewStorage() - tagOneHash := plumbing.NewHash("TAGONE") - tagTwoHash := plumbing.NewHash("TAGTWO") - - tagOne := &Tag{ - Target: tagTwoHash, - Hash: tagOneHash, + target := &Tag{ + Target: plumbing.NewHash("TAGONE"), Name: "TAG ONE", + Message: "tag one", TargetType: plumbing.TagObject, } - tagTwo := &Tag{ - Target: tagOneHash, - Hash: tagTwoHash, + + targetObj := &plumbing.MemoryObject{} + target.Encode(targetObj) + store.SetEncodedObject(targetObj) + + tag := &Tag{ + Target: targetObj.Hash(), Name: "TAG TWO", + Message: "tag two", TargetType: plumbing.TagObject, } - oOne := &plumbing.MemoryObject{} - tagOne.Encode(oOne) - oTwo := &plumbing.MemoryObject{} - tagTwo.Encode(oTwo) - store.SetEncodedObject(oOne) - store.SetEncodedObject(oTwo) + tagObj := &plumbing.MemoryObject{} + tag.Encode(tagObj) + store.SetEncodedObject(tagObj) - tag, err := GetTag(store, tagOneHash) + tag, err := GetTag(store, tagObj.Hash()) c.Assert(err, IsNil) c.Assert(tag.String(), Equals, @@ -268,7 +267,7 @@ func (s *TagSuite) TestTagToTagString(c *C) { "Tagger: <>\n"+ "Date: Mon Jan 01 00:00:00 0001 +0000\n"+ "\n"+ - "\n") + "tag two\n") } func (s *TagSuite) TestLongTagNameSerialization(c *C) { diff --git a/plumbing/object/tree.go b/plumbing/object/tree.go index d2265a8..25687b0 100644 --- a/plumbing/object/tree.go +++ b/plumbing/object/tree.go @@ -240,29 +240,21 @@ func (t *Tree) Encode(o plumbing.EncodedObject) error { return err } - var size int defer ioutil.CheckClose(w, &err) for _, entry := range t.Entries { - n, err := fmt.Fprintf(w, "%o %s", entry.Mode, entry.Name) - if err != nil { + if _, err := fmt.Fprintf(w, "%o %s", entry.Mode, entry.Name); err != nil { return err } - size += n - n, err = w.Write([]byte{0x00}) - if err != nil { + if _, err = w.Write([]byte{0x00}); err != nil { return err } - size += n - n, err = w.Write([]byte(entry.Hash[:])) - if err != nil { + if _, err = w.Write([]byte(entry.Hash[:])); err != nil { return err } - size += n } - o.SetSize(int64(size)) return err } |