aboutsummaryrefslogtreecommitdiff
path: root/obj_blob.go
blob: 1f74464e7768fc1cddc9d0383298a433008d5fbe (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
package furgit

// Blob represents the contents of a Git blob.
type Blob[T HashType] struct {
	Hash Hash[T]
	Data []byte
}

// ObjType allows Blob to satisfy the Object interface.
func (*Blob[T]) ObjType() ObjType {
	return ObjBlob
}

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

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