From 5cf20a4edf7803458a1c2ec94e902369bed76f28 Mon Sep 17 00:00:00 2001 From: "Santiago M. Mola" Date: Mon, 29 Aug 2016 22:39:08 +0200 Subject: object: Add Encode method to all objects. (#70) Encode method encodes a typed object (commit, tree, tag, blob) into raw core.Object representation. Additionally, Decode does not trim commit message lines. This is needed for Decode/Encode to be idempotent. --- objects.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'objects.go') diff --git a/objects.go b/objects.go index 6f3549b..fd79deb 100644 --- a/objects.go +++ b/objects.go @@ -4,6 +4,7 @@ import ( "bytes" "errors" "fmt" + "io" "strconv" "time" @@ -39,6 +40,7 @@ type Object interface { ID() core.Hash Type() core.ObjectType Decode(core.Object) error + Encode(core.Object) error } // Blob is used to store file data - it is generally a file. @@ -77,6 +79,23 @@ func (b *Blob) Decode(o core.Object) error { return nil } +// Encode transforms a Blob into a core.Object. +func (b *Blob) Encode(o core.Object) error { + w, err := o.Writer() + if err != nil { + return err + } + defer checkClose(w, &err) + r, err := b.Reader() + if err != nil { + return err + } + defer checkClose(r, &err) + _, err = io.Copy(w, r) + o.SetType(core.BlobObject) + return err +} + // Reader returns a reader allow the access to the content of the blob func (b *Blob) Reader() (core.ObjectReader, error) { return b.obj.Reader() @@ -106,6 +125,17 @@ func (s *Signature) Decode(b []byte) { } } +// Encode encodes a Signature into a writer. +func (s *Signature) Encode(w io.Writer) error { + if _, err := fmt.Fprintf(w, "%s <%s> ", s.Name, s.Email); err != nil { + return err + } + if err := s.encodeTimeAndTimeZone(w); err != nil { + return err + } + return nil +} + var timeZoneLength = 5 func (s *Signature) decodeTimeAndTimeZone(b []byte) { @@ -133,6 +163,11 @@ func (s *Signature) decodeTimeAndTimeZone(b []byte) { s.When = s.When.In(tl.Location()) } +func (s *Signature) encodeTimeAndTimeZone(w io.Writer) error { + _, err := fmt.Fprintf(w, "%d %s", s.When.Unix(), s.When.Format("-0700")) + return err +} + func (s *Signature) String() string { return fmt.Sprintf("%s <%s>", s.Name, s.Email) } -- cgit