Exemplo n.º 1
0
// Decode transform an core.Object into a Blob struct
func (b *Blob) Decode(o core.Object) error {
	if o.Type() != core.BlobObject {
		return ErrUnsupportedObject
	}

	b.Hash = o.Hash()
	b.Size = o.Size()
	b.obj = o

	return nil
}
Exemplo n.º 2
0
// 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
}