aboutsummaryrefslogtreecommitdiff
path: root/object/header/append.go
blob: 5b062e058073fdf70897de768c40417cc41c4bc4 (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
package header

import (
	"strconv"

	"codeberg.org/lindenii/furgit/object/typ"
)

// Append appends a canonical loose-object header ("type size\x00") to dst.
func Append(dst []byte, ty typ.Type, size int64) ([]byte, bool) {
	if size < 0 {
		return nil, false
	}

	tyName, ok := ty.Name()
	if !ok {
		return nil, false
	}

	sizeStr := strconv.FormatInt(size, 10)
	out := make([]byte, 0, len(dst)+len(tyName)+len(sizeStr)+2)
	out = append(out, dst...)
	out = append(out, tyName...)
	out = append(out, ' ')
	out = append(out, sizeStr...)
	out = append(out, 0)

	return out, true
}