-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathobject_store.go
More file actions
363 lines (337 loc) · 9.4 KB
/
Copy pathobject_store.go
File metadata and controls
363 lines (337 loc) · 9.4 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package clone
import (
"bufio"
"context"
"encoding/hex"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/osfs"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/cache"
"github.com/go-git/go-git/v5/storage/filesystem"
"github.com/go-git/go-git/v5/storage/filesystem/dotgit"
)
const goGitV5SHA1HexLength = 40
func readBlobWithGoGit(ctx context.Context, dir, commit, blobPath string, maxBytes int64) ([]byte, bool, error) {
if len(commit) > goGitV5SHA1HexLength {
return nil, false, fmt.Errorf("go-git v5 does not support %d-character object IDs", len(commit))
}
store, err := openObjectStore(dir, maxBytes)
if err != nil {
return nil, false, err
}
commitHash, err := resolveCommitHash(ctx, store, commit)
if err != nil {
return nil, false, err
}
treeHash, err := readHeaderHash(ctx, store, plumbing.CommitObject, commitHash, "tree")
if err != nil {
return nil, false, err
}
blob, err := findBlobObject(ctx, store, treeHash, blobPath)
if err != nil {
return nil, false, err
}
if blob.Size() < 0 {
return nil, false, fmt.Errorf("blob %q has invalid size %d", blobPath, blob.Size())
}
readSize := min(blob.Size(), maxBytes)
if uint64(readSize) > uint64(maxInt()) {
return nil, false, fmt.Errorf("blob read size %d exceeds platform limit", readSize)
}
content := make([]byte, int(readSize))
if readSize == 0 {
return content, blob.Size() > maxBytes, nil
}
reader, err := blob.Reader()
if err != nil {
return nil, false, err
}
_, readErr := io.ReadFull(contextReader{ctx: ctx, reader: reader}, content)
closeErr := reader.Close()
if readErr != nil {
return nil, false, readErr
}
if closeErr != nil {
return nil, false, closeErr
}
if err := ctx.Err(); err != nil {
return nil, false, err
}
return content, blob.Size() > maxBytes, nil
}
func maxInt() int {
return int(^uint(0) >> 1)
}
func openObjectStore(dir string, maxBytes int64) (*filesystem.Storage, error) {
gitDir, commonDir, err := findGitDirs(dir)
if err != nil {
return nil, err
}
dotGitFS := osfs.New(gitDir)
var repositoryFS billy.Filesystem
if commonDir != "" && commonDir != gitDir {
repositoryFS = dotgit.NewRepositoryFilesystem(dotGitFS, osfs.New(commonDir))
} else {
repositoryFS = dotGitFS
}
largeObjectThreshold := max(maxBytes, 1)
return filesystem.NewStorageWithOptions(
repositoryFS,
cache.NewObjectLRUDefault(),
filesystem.Options{LargeObjectThreshold: largeObjectThreshold},
), nil
}
func findGitDirs(dir string) (gitDir, commonDir string, err error) {
start, err := filepath.Abs(dir)
if err != nil {
return "", "", err
}
info, err := os.Stat(start)
if err != nil {
return "", "", err
}
if !info.IsDir() {
return "", "", fmt.Errorf("repository path %q is not a directory", dir)
}
for current := start; ; current = filepath.Dir(current) {
dotGit := filepath.Join(current, ".git")
if info, statErr := os.Stat(dotGit); statErr == nil {
if info.IsDir() {
return gitDirectories(dotGit)
}
resolved, resolveErr := resolveGitFile(dotGit)
if resolveErr != nil {
return "", "", resolveErr
}
return gitDirectories(resolved)
} else if !errors.Is(statErr, os.ErrNotExist) {
return "", "", statErr
}
if current == start && isBareGitDir(current) {
return gitDirectories(current)
}
parent := filepath.Dir(current)
if parent == current {
return "", "", fmt.Errorf("no Git repository found from %q", start)
}
}
}
func resolveGitFile(path string) (string, error) {
content, err := os.ReadFile(path)
if err != nil {
return "", err
}
value := strings.TrimSpace(string(content))
const prefix = "gitdir:"
if !strings.HasPrefix(value, prefix) {
return "", fmt.Errorf("invalid gitfile %q", path)
}
gitDir := strings.TrimSpace(strings.TrimPrefix(value, prefix))
if gitDir == "" {
return "", fmt.Errorf("invalid gitfile %q", path)
}
if !filepath.IsAbs(gitDir) {
gitDir = filepath.Join(filepath.Dir(path), gitDir)
}
return filepath.Clean(gitDir), nil
}
func gitDirectories(gitDir string) (string, string, error) {
content, err := os.ReadFile(filepath.Join(gitDir, "commondir"))
if errors.Is(err, os.ErrNotExist) {
return gitDir, "", nil
}
if err != nil {
return "", "", err
}
commonDir := strings.TrimSpace(string(content))
if commonDir == "" {
return "", "", fmt.Errorf("empty commondir in %q", gitDir)
}
if !filepath.IsAbs(commonDir) {
commonDir = filepath.Join(gitDir, commonDir)
}
return gitDir, filepath.Clean(commonDir), nil
}
func isBareGitDir(dir string) bool {
if info, err := os.Stat(filepath.Join(dir, "objects")); err != nil || !info.IsDir() {
return false
}
info, err := os.Stat(filepath.Join(dir, "HEAD"))
return err == nil && !info.IsDir()
}
func resolveCommitHash(ctx context.Context, store *filesystem.Storage, revision string) (plumbing.Hash, error) {
hashes, err := resolveObjectPrefix(store, revision)
if err != nil {
return plumbing.ZeroHash, err
}
var resolved plumbing.Hash
found := false
for _, hash := range hashes {
commit, commitErr := peelCommitHash(ctx, store, hash)
if commitErr != nil {
continue
}
if found {
return plumbing.ZeroHash, fmt.Errorf("ambiguous commit prefix %q", revision)
}
resolved = commit
found = true
}
if !found {
return plumbing.ZeroHash, fmt.Errorf("commit %q not found", revision)
}
return resolved, nil
}
func peelCommitHash(ctx context.Context, store *filesystem.Storage, hash plumbing.Hash) (plumbing.Hash, error) {
const maxTagDepth = 16
for range maxTagDepth {
encoded, err := store.EncodedObject(plumbing.AnyObject, hash)
if err != nil {
return plumbing.ZeroHash, err
}
switch encoded.Type() {
case plumbing.CommitObject:
return hash, nil
case plumbing.TagObject:
hash, err = readEncodedHeaderHash(ctx, encoded, "object")
if err != nil {
return plumbing.ZeroHash, err
}
default:
return plumbing.ZeroHash, fmt.Errorf("object %s is not a commit", hash)
}
}
return plumbing.ZeroHash, fmt.Errorf("tag chain for %s is too deep", hash)
}
func readHeaderHash(
ctx context.Context,
store *filesystem.Storage,
typeName plumbing.ObjectType,
hash plumbing.Hash,
header string,
) (plumbing.Hash, error) {
encoded, err := store.EncodedObject(typeName, hash)
if err != nil {
return plumbing.ZeroHash, err
}
return readEncodedHeaderHash(ctx, encoded, header)
}
func readEncodedHeaderHash(ctx context.Context, encoded plumbing.EncodedObject, header string) (plumbing.Hash, error) {
reader, err := encoded.Reader()
if err != nil {
return plumbing.ZeroHash, err
}
line, readErr := bufio.NewReader(contextReader{ctx: ctx, reader: reader}).ReadString('\n')
closeErr := reader.Close()
if readErr != nil {
return plumbing.ZeroHash, readErr
}
if closeErr != nil {
return plumbing.ZeroHash, closeErr
}
value, ok := strings.CutPrefix(strings.TrimSuffix(line, "\n"), header+" ")
if !ok || !plumbing.IsHash(value) {
return plumbing.ZeroHash, fmt.Errorf("object has invalid %s header", header)
}
return plumbing.NewHash(value), nil
}
func findBlobObject(
ctx context.Context,
store *filesystem.Storage,
treeHash plumbing.Hash,
blobPath string,
) (*storedBlob, error) {
parts := strings.Split(blobPath, "/")
for index, part := range parts {
tree, err := store.EncodedObject(plumbing.TreeObject, treeHash)
if err != nil {
return nil, err
}
mode, entryHash, err := findTreeEntry(ctx, tree, part)
if err != nil {
return nil, err
}
if index == len(parts)-1 {
encoded, encodedErr := store.EncodedObject(plumbing.BlobObject, entryHash)
if encodedErr != nil {
return nil, encodedErr
}
return &storedBlob{EncodedObject: encoded}, nil
}
if mode != "40000" && mode != "040000" {
return nil, fmt.Errorf("path component %q is not a directory", part)
}
treeHash = entryHash
}
return nil, fmt.Errorf("path %q does not exist", blobPath)
}
type storedBlob struct {
plumbing.EncodedObject
}
func findTreeEntry(
ctx context.Context,
encoded plumbing.EncodedObject,
want string,
) (mode string, hash plumbing.Hash, err error) {
reader, err := encoded.Reader()
if err != nil {
return "", plumbing.ZeroHash, err
}
defer func() {
if closeErr := reader.Close(); err == nil {
err = closeErr
}
}()
buffered := bufio.NewReader(contextReader{ctx: ctx, reader: reader})
for {
mode, err = buffered.ReadString(' ')
if errors.Is(err, io.EOF) {
return "", plumbing.ZeroHash, fmt.Errorf("path %q does not exist", want)
}
if err != nil {
return "", plumbing.ZeroHash, err
}
mode = strings.TrimSuffix(mode, " ")
name, nameErr := buffered.ReadString(0)
if nameErr != nil {
return "", plumbing.ZeroHash, nameErr
}
name = strings.TrimSuffix(name, "\x00")
if _, err = io.ReadFull(buffered, hash[:]); err != nil {
return "", plumbing.ZeroHash, err
}
if name == want {
return mode, hash, nil
}
}
}
func resolveObjectPrefix(store *filesystem.Storage, revision string) ([]plumbing.Hash, error) {
if len(revision) == goGitV5SHA1HexLength {
return []plumbing.Hash{plumbing.NewHash(revision)}, nil
}
evenHex := revision[:len(revision)&^1]
prefix, err := hex.DecodeString(evenHex)
if err != nil {
return nil, err
}
hashes, err := store.HashesWithPrefix(prefix)
if err != nil {
return nil, err
}
if len(evenHex) == len(revision) {
return hashes, nil
}
filtered := hashes[:0]
for _, hash := range hashes {
if strings.HasPrefix(hash.String(), revision) {
filtered = append(filtered, hash)
}
}
return filtered, nil
}