aboutsummaryrefslogtreecommitdiffstats
path: root/tree.go
diff options
context:
space:
mode:
authorSantiago M. Mola <santi@mola.io>2016-08-29 22:39:08 +0200
committerMáximo Cuadros <mcuadros@gmail.com>2016-08-29 22:39:08 +0200
commit5cf20a4edf7803458a1c2ec94e902369bed76f28 (patch)
treeb6a0276ab12f82383818892064038fb0b79e161b /tree.go
parenta97ca42cbce377b5725ecc41e4539fc7e263b90d (diff)
downloadgo-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.go23
1 files changed, 23 insertions, 0 deletions
diff --git a/tree.go b/tree.go
index cfe457e..86f1aaa 100644
--- a/tree.go
+++ b/tree.go
@@ -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++ {