aboutsummaryrefslogtreecommitdiff
path: root/object/parse_with_header.go
blob: b2ddfff4839928db4cdd5ace4175b54f6ade2dcd (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
package object

import (
	"fmt"

	objectheader "codeberg.org/lindenii/furgit/object/header"
	objectid "codeberg.org/lindenii/furgit/object/id"
)

// ParseObjectWithHeader parses a loose object in "type size\x00body" format.
//
//nolint:ireturn
func ParseObjectWithHeader(raw []byte, algo objectid.Algorithm) (Object, error) {
	ty, size, headerLen, ok := objectheader.Parse(raw)
	if !ok {
		return nil, fmt.Errorf("object: malformed object header")
	}

	body := raw[headerLen:]
	if int64(len(body)) != size {
		return nil, fmt.Errorf("object: size mismatch: header says %d bytes, body has %d", size, len(body))
	}

	return ParseObjectWithoutHeader(ty, body, algo)
}