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 /tree.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 'tree.go')
-rw-r--r-- | tree.go | 23 |
1 files changed, 23 insertions, 0 deletions
@@ -3,6 +3,7 @@ package git import ( "bufio" "errors" + "fmt" "io" "os" "strconv" @@ -210,6 +211,28 @@ func (t *Tree) decodeFileMode(mode string) (os.FileMode, error) { return m, nil } +// Encode transforms a Tree into a core.Object. +func (t *Tree) Encode(o core.Object) error { + o.SetType(core.TreeObject) + w, err := o.Writer() + if err != nil { + return err + } + defer checkClose(w, &err) + for _, entry := range t.Entries { + if _, err = fmt.Fprintf(w, "%o %s", entry.Mode, entry.Name); err != nil { + return err + } + if _, err = w.Write([]byte{0x00}); err != nil { + return err + } + if _, err = w.Write([]byte(entry.Hash[:])); err != nil { + return err + } + } + return err +} + func (t *Tree) buildMap() { t.m = make(map[string]*TreeEntry) for i := 0; i < len(t.Entries); i++ { |