-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
browser.go
487 lines (423 loc) · 11.9 KB
/
browser.go
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
// Copyright 2018 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package libgit
import (
"context"
"io"
"os"
"path"
"strings"
"time"
"github.com/keybase/client/go/kbfs/libfs"
"github.com/keybase/client/go/kbfs/libkbfs"
"github.com/pkg/errors"
billy "gopkg.in/src-d/go-billy.v4"
gogit "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"gopkg.in/src-d/go-git.v4/storage"
)
const (
// LFSSubdir is the prefix for the LFS directory under .kbfs_git
LFSSubdir = "kbfs_lfs"
lfsEntryMinSize = 120
lfsEntryMaxSize = 150
)
func translateGitError(err *error) {
if *err == nil {
return
}
switch errors.Cause(*err) {
case object.ErrEntryNotFound:
*err = os.ErrNotExist
default:
return
}
}
// Browser presents the contents of a git repo as a read-only file
// system, using only the dotgit directory of the repo.
type Browser struct {
repo *gogit.Repository
tree *object.Tree
root string
mtime time.Time
commitHash plumbing.Hash
lfsFS billy.Filesystem
sharedCache sharedInBrowserCache
}
var _ billy.Filesystem = (*Browser)(nil)
// NewBrowser makes a new Browser instance, browsing the given branch
// of the given repo. If `gitBranchName` is empty,
// "refs/heads/master" is used. If `gitBranchName` is not empty, but
// it doesn't begin with "refs/", then "refs/heads/" is prepended to
// it.
func NewBrowser(
repoFS *libfs.FS, clock libkbfs.Clock,
gitBranchName plumbing.ReferenceName,
sharedCache sharedInBrowserCache) (*Browser, error) {
var storage storage.Storer
storage, err := NewGitConfigWithoutRemotesStorer(repoFS)
if err != nil {
return nil, err
}
const masterBranch = "refs/heads/master"
if gitBranchName == "" {
gitBranchName = masterBranch
} else if !strings.HasPrefix(string(gitBranchName), "refs/") {
gitBranchName = "refs/heads/" + gitBranchName
}
repo, err := gogit.Open(storage, nil)
if errors.Cause(err) == gogit.ErrWorktreeNotProvided {
// This is not a bare repo (it might be for a test). So we
// need to pass in a working tree, but since `Browser` is
// read-only and doesn't even use the worktree, it doesn't
// matter what we pass in.
repo, err = gogit.Open(storage, repoFS)
}
if err == gogit.ErrRepositoryNotExists && gitBranchName == masterBranch {
// This repo is not initialized yet, so pretend it's empty.
return &Browser{
root: string(gitBranchName),
sharedCache: sharedCache,
}, nil
} else if err != nil {
return nil, err
}
ref, err := repo.Reference(gitBranchName, true)
if err == plumbing.ErrReferenceNotFound && gitBranchName == masterBranch {
// This branch has no commits, so pretend it's empty.
return &Browser{
root: string(gitBranchName),
sharedCache: sharedCache,
}, nil
} else if err != nil {
return nil, err
}
if ref.Type() != plumbing.HashReference {
return nil, errors.Errorf("can't browse reference type %s", ref.Type())
}
c, err := repo.CommitObject(ref.Hash())
if err != nil {
return nil, err
}
tree, err := c.Tree()
if err != nil {
return nil, err
}
lfsFS, err := repoFS.Chroot(LFSSubdir)
if os.IsNotExist(err) {
lfsFS = nil
} else if err != nil {
return nil, err
}
return &Browser{
repo: repo,
tree: tree,
root: string(gitBranchName),
mtime: c.Author.When,
commitHash: c.Hash,
lfsFS: lfsFS,
sharedCache: sharedCache,
}, nil
}
func (b *Browser) getCommitFile(
ctx context.Context, hash plumbing.Hash) (*diffFile, error) {
if b.repo == nil {
return nil, errors.New("Empty repo")
}
commit, err := b.repo.CommitObject(hash)
if err != nil {
return nil, err
}
return newCommitFile(ctx, commit)
}
///// Read-only functions:
const (
maxSymlinkLevels = 40 // same as Linux
)
func (b *Browser) readLink(filename string) (string, error) {
f, err := b.tree.File(filename)
if err != nil {
return "", err
}
r, err := f.Reader()
if err != nil {
return "", err
}
defer r.Close()
data, err := io.ReadAll(r)
if err != nil {
return "", err
}
return string(data), nil
}
func (b *Browser) followSymlink(filename string) (string, error) {
// Otherwise, resolve the symlink and return the underlying FileInfo.
link, err := b.readLink(filename)
if err != nil {
return "", err
}
if path.IsAbs(link) {
return "", errors.Errorf("can't follow absolute link: %s", link)
}
parts := strings.Split(filename, "/")
var parentPath string
if len(parts) > 0 {
parentPath = path.Join(parts[:len(parts)-1]...)
}
newPath := path.Clean(path.Join(parentPath, link))
if strings.HasPrefix(newPath, "..") {
return "", errors.Errorf(
"cannot follow symlink out of chroot: %s", newPath)
}
return newPath, nil
}
// Open implements the billy.Filesystem interface for Browser.
func (b *Browser) Open(filename string) (f billy.File, err error) {
if b.tree == nil {
return nil, errors.New("Empty repo")
}
defer translateGitError(&err)
for i := 0; i < maxSymlinkLevels; i++ {
fi, err := b.Lstat(filename)
if err != nil {
return nil, err
}
// Check if this is a submodule.
if sfi, ok := fi.(*submoduleFileInfo); ok {
return sfi.sf, nil
}
// Check if this is LFS.
if lfsFI, ok := fi.(*lfsFileInfo); ok {
return b.lfsFS.Open(lfsFI.oid)
}
// If it's not a symlink, we can return right away.
if fi.Mode()&os.ModeSymlink == 0 {
f, err := b.tree.File(filename)
if err != nil {
return nil, err
}
return newBrowserFile(f)
}
filename, err = b.followSymlink(filename)
if err != nil {
return nil, err
}
}
return nil, errors.New("cannot resolve deep symlink chain")
}
// OpenFile implements the billy.Filesystem interface for Browser.
func (b *Browser) OpenFile(filename string, flag int, _ os.FileMode) (
f billy.File, err error) {
if b.tree == nil {
return nil, errors.New("Empty repo")
}
if flag&os.O_CREATE != 0 {
return nil, errors.New("browser can't create files")
}
return b.Open(filename)
}
func (b *Browser) fileInfoForLFS(
filename string, oidLine string, fi os.FileInfo) (
newFi os.FileInfo, err error) {
fields := strings.Fields(oidLine)
// An OID line looks like:
// oid sha256:588b3683...
if len(fields) < 2 || fields[0] != "oid" {
return fi, nil
}
s := strings.Split(fields[1], ":")
if len(s) < 2 {
return fi, nil
}
oid := s[1]
// Now look that OID up and make sure it exists.
lfsFI, err := b.lfsFS.Stat(oid)
if err != nil {
return nil, err
}
return &lfsFileInfo{
filename, oid, lfsFI.Size(), b.mtime}, nil
}
// Lstat implements the billy.Filesystem interface for Browser.
func (b *Browser) Lstat(filename string) (fi os.FileInfo, err error) {
if b.tree == nil {
return nil, errors.New("Empty repo")
}
if strings.HasPrefix(filename, AutogitCommitPrefix) {
commit := strings.TrimPrefix(filename, AutogitCommitPrefix)
hash := plumbing.NewHash(commit)
f, err := b.getCommitFile(context.Background(), hash)
if err != nil {
return nil, err
}
return f.GetInfo(), nil
}
cachePath := path.Join(b.root, filename)
if fi, ok := b.sharedCache.getFileInfo(b.commitHash, cachePath); ok {
return fi, nil
}
defer translateGitError(&err)
entry, err := b.tree.FindEntry(filename)
if err != nil {
return nil, errors.WithStack(err)
}
size, err := b.tree.Size(filename)
switch errors.Cause(err) {
case nil:
// Git doesn't keep track of the mtime of individual files
// anywhere, so just use the timestamp from the commit.
fi = &browserFileInfo{entry, size, b.mtime}
case plumbing.ErrObjectNotFound:
// This is likely a git submodule.
sf := newSubmoduleFile(entry.Hash, filename, b.mtime)
fi = sf.GetInfo()
default:
return nil, errors.WithStack(err)
}
// If this repo has an LFS subdirectory, check and see if the size
// of this file is within the size bounds for an LFS object. If
// so, read the object and see if it points to LFS or not.
if b.lfsFS != nil && size >= lfsEntryMinSize && size <= lfsEntryMaxSize {
f, err := b.tree.File(filename)
if err != nil {
return nil, err
}
lines, err := f.Lines()
if err != nil {
return nil, err
}
if len(lines) >= 2 {
fi, err = b.fileInfoForLFS(filename, lines[1], fi)
if err != nil {
return nil, err
}
}
}
b.sharedCache.setFileInfo(b.commitHash, cachePath, fi)
return fi, nil
}
// Stat implements the billy.Filesystem interface for Browser.
func (b *Browser) Stat(filename string) (fi os.FileInfo, err error) {
defer translateGitError(&err)
for i := 0; i < maxSymlinkLevels; i++ {
fi, err := b.Lstat(filename)
if err != nil {
return nil, err
}
// If it's not a symlink, we can return right away.
if fi.Mode()&os.ModeSymlink == 0 {
return fi, nil
}
filename, err = b.followSymlink(filename)
if err != nil {
return nil, err
}
}
return nil, errors.New("cannot resolve deep symlink chain")
}
// Join implements the billy.Filesystem interface for Browser.
func (b *Browser) Join(elem ...string) string {
return path.Clean(path.Join(elem...))
}
// ReadDir implements the billy.Filesystem interface for Browser.
func (b *Browser) ReadDir(p string) (fis []os.FileInfo, err error) {
if p == "" {
p = "."
}
if b.tree == nil {
if p == "." {
// Branch with no commits.
return nil, nil
}
return nil, errors.New("Empty repo")
}
cachePath := path.Join(b.root, p)
if fis, ok := b.sharedCache.getChildrenFileInfos(
b.commitHash, cachePath); ok {
return fis, nil
}
defer translateGitError(&err)
var dirTree *object.Tree
if p == "." {
dirTree = b.tree
} else {
dirTree, err = b.tree.Tree(p)
if err != nil {
return nil, err
}
}
childrenPathsToCache := make([]string, 0, len(dirTree.Entries))
for _, e := range dirTree.Entries {
fi, err := b.Lstat(path.Join(p, e.Name))
if err != nil {
return nil, err
}
fis = append(fis, fi)
childrenPathsToCache = append(childrenPathsToCache, path.Join(cachePath, e.Name))
}
b.sharedCache.setChildrenPaths(
b.commitHash, cachePath, childrenPathsToCache)
return fis, nil
}
// Readlink implements the billy.Filesystem interface for Browser.
func (b *Browser) Readlink(link string) (target string, err error) {
defer translateGitError(&err)
fi, err := b.Lstat(link)
if err != nil {
return "", err
}
// If it's not a symlink, error right away.
if fi.Mode()&os.ModeSymlink == 0 {
return "", errors.New("not a symlink")
}
return b.readLink(link)
}
// Chroot implements the billy.Filesystem interface for Browser.
func (b *Browser) Chroot(p string) (newFS billy.Filesystem, err error) {
if b.tree == nil {
return nil, errors.New("Empty repo")
}
defer translateGitError(&err)
newTree, err := b.tree.Tree(p)
if err != nil {
return nil, err
}
return &Browser{
tree: newTree,
root: b.Join(b.root, p),
mtime: b.mtime,
commitHash: b.commitHash,
sharedCache: b.sharedCache,
}, nil
}
// Root implements the billy.Filesystem interface for Browser.
func (b *Browser) Root() string {
return b.root
}
///// Modifying functions (not supported):
// Create implements the billy.Filesystem interface for Browser.
func (b *Browser) Create(_ string) (billy.File, error) {
return nil, errors.New("browser cannot create files")
}
// Rename implements the billy.Filesystem interface for Browser.
func (b *Browser) Rename(_, _ string) (err error) {
return errors.New("browser cannot rename files")
}
// Remove implements the billy.Filesystem interface for Browser.
func (b *Browser) Remove(_ string) (err error) {
return errors.New("browser cannot remove files")
}
// TempFile implements the billy.Filesystem interface for Browser.
func (b *Browser) TempFile(_, _ string) (billy.File, error) {
return nil, errors.New("browser cannot make temp files")
}
// MkdirAll implements the billy.Filesystem interface for Browser.
func (b *Browser) MkdirAll(_ string, _ os.FileMode) (err error) {
return errors.New("browser cannot mkdir")
}
// Symlink implements the billy.Filesystem interface for Browser.
func (b *Browser) Symlink(_, _ string) (err error) {
return errors.New("browser cannot make symlinks")
}