diff options
author | Santiago M. Mola <santi@mola.io> | 2016-08-29 22:39:08 +0200 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-08-29 22:39:08 +0200 |
commit | 5cf20a4edf7803458a1c2ec94e902369bed76f28 (patch) | |
tree | b6a0276ab12f82383818892064038fb0b79e161b /commit.go | |
parent | a97ca42cbce377b5725ecc41e4539fc7e263b90d (diff) | |
download | go-git-5cf20a4edf7803458a1c2ec94e902369bed76f28.tar.gz |
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.
Diffstat (limited to 'commit.go')
-rw-r--r-- | commit.go | 38 |
1 files changed, 36 insertions, 2 deletions
@@ -97,8 +97,8 @@ func (c *Commit) Decode(o core.Object) (err error) { return err } - line = bytes.TrimSpace(line) if !message { + line = bytes.TrimSpace(line) if len(line) == 0 { message = true continue @@ -116,7 +116,7 @@ func (c *Commit) Decode(o core.Object) (err error) { c.Committer.Decode(split[1]) } } else { - c.Message += string(line) + "\n" + c.Message += string(line) } if err == io.EOF { @@ -137,6 +137,40 @@ func (c *Commit) History() ([]*Commit, error) { return commits, err } +// Encode transforms a Commit into a core.Object. +func (b *Commit) Encode(o core.Object) error { + o.SetType(core.CommitObject) + w, err := o.Writer() + if err != nil { + return err + } + defer checkClose(w, &err) + if _, err = fmt.Fprintf(w, "tree %s\n", b.tree.String()); err != nil { + return err + } + for _, parent := range b.parents { + 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 +} + func (c *Commit) String() string { return fmt.Sprintf( "%s %s\nAuthor: %s\nDate: %s\n", |