loader.go 1.9 KB

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