aboutsummaryrefslogtreecommitdiff
path: root/obj.go
blob: 41400985e657e0e9377e7b1cc7e5bcb6cc31eb50 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package furgit

import (
	"bytes"
	"errors"
	"fmt"
	"strconv"
)

// ObjectType mirrors Git's object type tags.
type ObjectType uint8

const (
	// An invalid object.
	ObjectTypeInvalid ObjectType = 0
	// A commit object.
	ObjectTypeCommit ObjectType = 1
	// A tree object.
	ObjectTypeTree ObjectType = 2
	// A blob object.
	ObjectTypeBlob ObjectType = 3
	// An annotated tag object.
	ObjectTypeTag ObjectType = 4
	// An object type reserved for future use.
	ObjectTypeFuture ObjectType = 5
	// A packfile offset delta object. This is not typically exposed.
	ObjectTypeOfsDelta ObjectType = 6
	// A packfile reference delta object. This is not typically exposed.
	ObjectTypeRefDelta ObjectType = 7
)

const (
	objectTypeNameBlob   = "blob"
	objectTypeNameTree   = "tree"
	objectTypeNameCommit = "commit"
	objectTypeNameTag    = "tag"
)

// Object represents a Git object.
type Object interface {
	// ObjectType returns the object's type.
	ObjectType() ObjectType
}

// StoredObject describes a Git object with a known hash, such as
// one read from storage.
type StoredObject interface {
	Object
	// Hash returns the object's hash.
	Hash() Hash
}

func headerForType(ty ObjectType, body []byte) ([]byte, error) {
	var tyStr string
	switch ty {
	case ObjectTypeBlob:
		tyStr = objectTypeNameBlob
	case ObjectTypeTree:
		tyStr = objectTypeNameTree
	case ObjectTypeCommit:
		tyStr = objectTypeNameCommit
	case ObjectTypeTag:
		tyStr = objectTypeNameTag
	case ObjectTypeInvalid, ObjectTypeFuture, ObjectTypeOfsDelta, ObjectTypeRefDelta:
		return nil, fmt.Errorf("furgit: object: unsupported type %d", ty)
	default:
		return nil, fmt.Errorf("furgit: object: unsupported type %d", ty)
	}
	size := strconv.Itoa(len(body))
	var buf bytes.Buffer
	buf.Grow(len(tyStr) + len(size) + 1)
	buf.WriteString(tyStr)
	buf.WriteByte(' ')
	buf.WriteString(size)
	buf.WriteByte(0)
	return buf.Bytes(), nil
}

func parseObjectBody(ty ObjectType, id Hash, body []byte, repo *Repository) (StoredObject, error) {
	switch ty {
	case ObjectTypeBlob:
		return parseBlob(id, body)
	case ObjectTypeTree:
		return parseTree(id, body, repo)
	case ObjectTypeCommit:
		return parseCommit(id, body, repo)
	case ObjectTypeTag:
		return parseTag(id, body, repo)
	case ObjectTypeInvalid, ObjectTypeFuture, ObjectTypeOfsDelta, ObjectTypeRefDelta:
		return nil, fmt.Errorf("furgit: object: unsupported type %d", ty)
	default:
		return nil, fmt.Errorf("furgit: object: unknown type %d", ty)
	}
}

// ReadObject resolves an ID.
func (repo *Repository) ReadObject(id Hash) (StoredObject, error) {
	ty, body, err := repo.looseRead(id)
	if err == nil {
		obj, parseErr := parseObjectBody(ty, id, body.Bytes(), repo)
		body.Release()
		return obj, parseErr
	}
	if !errors.Is(err, ErrNotFound) {
		return nil, err
	}
	ty, body, err = repo.packRead(id)
	if errors.Is(err, ErrNotFound) {
		return nil, ErrNotFound
	}
	if err != nil {
		return nil, err
	}
	obj, parseErr := parseObjectBody(ty, id, body.Bytes(), repo)
	body.Release()
	return obj, parseErr
}

// ReadObjectTypeRaw reads the object type and raw body.
func (repo *Repository) ReadObjectTypeRaw(id Hash) (ObjectType, []byte, error) {
	ty, body, err := repo.looseRead(id)
	if err == nil {
		return ty, body.Bytes(), nil
	}
	if !errors.Is(err, ErrNotFound) {
		return ObjectTypeInvalid, nil, err
	}
	ty, body, err = repo.packRead(id)
	if errors.Is(err, ErrNotFound) {
		return ObjectTypeInvalid, nil, ErrNotFound
	}
	if err != nil {
		return ObjectTypeInvalid, nil, err
	}
	return ty, body.Bytes(), nil
	// note to self: It always feels wrong to not call .Release in places like
	// this but this is actually correct; we're returning the underlying buffer
	// to the user who should not be aware of our internal buffer pooling.
	// Releasing this buffer back to the pool would lead to a use-after-free;
	// not releasing it as we do here, means it gets GC'ed.
	// Copying into a newly allocated buffer is even worse as it incurs
	// unnecessary copy overhead.
}

// ReadObjectTypeSize reports the object type and size.
//
// Typicall, this is more efficient than reading the full object,
// as it avoids decompressing the entire object body.
func (repo *Repository) ReadObjectTypeSize(id Hash) (ObjectType, int64, error) {
	ty, size, err := repo.looseTypeSize(id)
	if err == nil {
		return ty, size, nil
	}
	if !errors.Is(err, ErrNotFound) {
		return ObjectTypeInvalid, 0, err
	}
	loc, err := repo.packIndexFind(id)
	if err != nil {
		if errors.Is(err, ErrNotFound) {
			return ObjectTypeInvalid, 0, ErrInvalidObject
		}
		return ObjectTypeInvalid, 0, err
	}
	return repo.packTypeSizeAtLocation(loc, nil)
}