aboutsummaryrefslogtreecommitdiff
path: root/obj_blob.go
blob: c25f88eb304d9a533b7fa1d15cc709c846849e25 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package furgit

// Blob represents the contents of a Git blob.
type Blob struct {
	Hash Hash

	Data []byte
}

// ObjectType allows Blob to satisfy the Object interface.
func (blob *Blob) ObjectType() ObjectType {
	_ = blob
	return ObjectTypeBlob
}

func parseBlob(id Hash, body []byte) (*Blob, error) {
	data := append([]byte(nil), body...)
	return &Blob{
		Hash: id,
		Data: data,
	}, nil
}

// Serialize renders the full "blob size\\0body" representation.
func (blob *Blob) Serialize() ([]byte, error) {
	header, err := headerForType(ObjectTypeBlob, blob.Data)
	if err != nil {
		return nil, err
	}
	raw := make([]byte, len(header)+len(blob.Data))
	copy(raw, header)
	copy(raw[len(header):], blob.Data)
	return raw, nil
}