aboutsummaryrefslogtreecommitdiff
path: root/object/fetch/object.go
blob: 4c99987131b251b322d5b8269de220363e7be6e1 (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
39
40
41
package fetch

import (
	"fmt"

	"lindenii.org/go/furgit/object"
	oid "lindenii.org/go/furgit/object/id"
	"lindenii.org/go/furgit/object/stored"
)

// ExactObject reads, parses, and wraps the object at id without constraining
// its concrete object kind.
//
// Labels: Life-Parent.
func (fetcher *Fetcher) ExactObject(id oid.ObjectID) (*stored.Stored[object.Object], error) {
	parsed, err := fetcher.parseObject(id)
	if err != nil {
		return nil, err
	}

	return stored.New(id, parsed), nil
}

func (fetcher *Fetcher) parseObject(id oid.ObjectID) (object.Object, error) {
	ty, content, err := fetcher.store.ReadBytesContent(id)
	if err != nil {
		return nil, wrapObjectReadError(id, err)
	}

	parsed, err := object.ParseWithoutHeader(ty, content, id.ObjectFormat())
	if err != nil {
		tyName, ok := ty.Name()
		if !ok {
			tyName = fmt.Sprintf("type %d", ty)
		}

		return nil, fmt.Errorf("object/fetch: parse object %s (%s): %w", id, tyName, err)
	}

	return parsed, nil
}