// Decode transform an core.Object into a Tree struct func (t *Tree) Decode(o core.Object) error { if o.Type() != core.TreeObject { return ErrUnsupportedObject } t.Hash = o.Hash() if o.Size() == 0 { return nil } t.Entries = make(map[string]TreeEntry) r := bufio.NewReader(o.Reader()) for { mode, err := r.ReadString(' ') if err != nil { if err == io.EOF { break } return err } fm, err := strconv.ParseInt(mode[:len(mode)-1], 8, 32) if err != nil && err != io.EOF { return err } name, err := r.ReadString(0) if err != nil && err != io.EOF { return err } var hash core.Hash _, err = r.Read(hash[:]) if err != nil && err != io.EOF { return err } baseName := name[:len(name)-1] t.Entries[baseName] = TreeEntry{ Hash: hash, Mode: os.FileMode(fm), Name: baseName, } } return nil }
// Decode transform an core.Object into a Blob struct func (c *Commit) Decode(o core.Object) error { if o.Type() != core.CommitObject { return ErrUnsupportedObject } c.Hash = o.Hash() r := bufio.NewReader(o.Reader()) var message bool for { line, err := r.ReadSlice('\n') if err != nil && err != io.EOF { return err } line = bytes.TrimSpace(line) if !message { if len(line) == 0 { message = true continue } split := bytes.SplitN(line, []byte{' '}, 2) switch string(split[0]) { case "tree": c.tree = core.NewHash(string(split[1])) case "parent": c.parents = append(c.parents, core.NewHash(string(split[1]))) case "author": c.Author.Decode(split[1]) case "committer": c.Committer.Decode(split[1]) } } else { c.Message += string(line) + "\n" } if err == io.EOF { return nil } } }