Debian Codegraph @

plumbing/transport/server/loader.go

raw
1 package server
2
3 import (
4 "github.com/go-git/go-git/v5/plumbing/cache"
5 "github.com/go-git/go-git/v5/plumbing/storer"
6 "github.com/go-git/go-git/v5/plumbing/transport"
7 "github.com/go-git/go-git/v5/storage/filesystem"
8
9 "github.com/go-git/go-billy/v5"
10 "github.com/go-git/go-billy/v5/osfs"
11 )
12
13 // DefaultLoader is a filesystem loader ignoring host and resolving paths to /.
14 var DefaultLoader = NewFilesystemLoader(osfs.New(""))
15
16 // Loader loads repository's storer.Storer based on an optional host and a path.
17 type Loader interface {
18 // Load loads a storer.Storer given a transport.Endpoint.
19 // Returns transport.ErrRepositoryNotFound if the repository does not
20 // exist.
21 Load(ep *transport.Endpoint) (storer.Storer, error)
22 }
23
24 type fsLoader struct {
25 base billy.Filesystem
26 }
27
28 // NewFilesystemLoader creates a Loader that ignores host and resolves paths
29 // with a given base filesystem.
30 func NewFilesystemLoader(base billy.Filesystem) Loader {
31 return &fsLoader{base}
32 }
33
34 // Load looks up the endpoint's path in the base file system and returns a
35 // storer for it. Returns transport.ErrRepositoryNotFound if a repository does
36 // not exist in the given path.
37 func (l *fsLoader) Load(ep *transport.Endpoint) (storer.Storer, error) {
38 fs, err := l.base.Chroot(ep.Path)
39 if err != nil {
40 return nil, err
41 }
42
43 var bare bool
44 if _, err := fs.Stat("config"); err == nil {
45 bare = true
46 }
47
48 if !bare {
49 // do not use git.GitDirName due to import cycle
50 if _, err := fs.Stat(".git"); err != nil {
51 return nil, transport.ErrRepositoryNotFound
52 }
53 }
54
55 return filesystem.NewStorage(fs, cache.NewObjectLRUDefault()), nil
56 }
57
58 // MapLoader is a Loader that uses a lookup map of storer.Storer by
59 // transport.Endpoint.
60 type MapLoader map[string]storer.Storer
61
62 // Load returns a storer.Storer for given a transport.Endpoint by looking it up
63 // in the map. Returns transport.ErrRepositoryNotFound if the endpoint does not
64 // exist.
65 func (l MapLoader) Load(ep *transport.Endpoint) (storer.Storer, error) {
66 s, ok := l[ep.String()]
67 if !ok {
68 return nil, transport.ErrRepositoryNotFound
69 }
70
71 return s, nil
72 }