aboutsummaryrefslogtreecommitdiff
path: root/object/object.go
blob: ee75b903653be5f06c28189a942bcd50d06f7654 (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
35
36
37
38
// Package object provides Git object models and codecs.
package object

import (
	"bytes"
	"errors"
	"strconv"

	"codeberg.org/lindenii/furgit/objecttype"
)

var (
	// ErrInvalidObject indicates malformed serialized data.
	ErrInvalidObject = errors.New("object: invalid object encoding")
	// ErrNotFound indicates missing entries in in-memory lookups.
	ErrNotFound = errors.New("object: not found")
)

// Object is a Git object that can serialize itself.
type Object interface {
	ObjectType() objecttype.Type
	Serialize() ([]byte, error)
}

func headerForType(ty objecttype.Type, body []byte) ([]byte, error) {
	tyStr, ok := objecttype.Name(ty)
	if !ok {
		return nil, ErrInvalidObject
	}
	size := strconv.Itoa(len(body))
	var buf bytes.Buffer
	buf.Grow(len(tyStr) + len(size) + 2)
	buf.WriteString(tyStr)
	buf.WriteByte(' ')
	buf.WriteString(size)
	buf.WriteByte(0)
	return buf.Bytes(), nil
}