| 1 |
// Package filesystem is a storage backend base on filesystems |
| 2 |
package filesystem |
| 3 |
|
| 4 |
import ( |
| 5 |
"github.com/go-git/go-git/v5/plumbing/cache" |
| 6 |
"github.com/go-git/go-git/v5/storage/filesystem/dotgit" |
| 7 |
|
| 8 |
"github.com/go-git/go-billy/v5" |
| 9 |
) |
| 10 |
|
| 11 |
// Storage is an implementation of git.Storer that stores data on disk in the |
| 12 |
// standard git format (this is, the .git directory). Zero values of this type |
| 13 |
// are not safe to use, see the NewStorage function below. |
| 14 |
type Storage struct { |
| 15 |
fs billy.Filesystem |
| 16 |
dir *dotgit.DotGit |
| 17 |
|
| 18 |
ObjectStorage |
| 19 |
ReferenceStorage |
| 20 |
IndexStorage |
| 21 |
ShallowStorage |
| 22 |
ConfigStorage |
| 23 |
ModuleStorage |
| 24 |
} |
| 25 |
|
| 26 |
// Options holds configuration for the storage. |
| 27 |
type Options struct { |
| 28 |
// ExclusiveAccess means that the filesystem is not modified externally |
| 29 |
// while the repo is open. |
| 30 |
ExclusiveAccess bool |
| 31 |
// KeepDescriptors makes the file descriptors to be reused but they will |
| 32 |
// need to be manually closed calling Close(). |
| 33 |
KeepDescriptors bool |
| 34 |
// MaxOpenDescriptors is the max number of file descriptors to keep |
| 35 |
// open. If KeepDescriptors is true, all file descriptors will remain open. |
| 36 |
MaxOpenDescriptors int |
| 37 |
// LargeObjectThreshold maximum object size (in bytes) that will be read in to memory. |
| 38 |
// If left unset or set to 0 there is no limit |
| 39 |
LargeObjectThreshold int64 |
| 40 |
// AlternatesFS provides the billy filesystem to be used for Git Alternates. |
| 41 |
// If none is provided, it falls back to using the underlying instance used for |
| 42 |
// DotGit. |
| 43 |
AlternatesFS billy.Filesystem |
| 44 |
} |
| 45 |
|
| 46 |
// NewStorage returns a new Storage backed by a given `fs.Filesystem` and cache. |
| 47 |
func NewStorage(fs billy.Filesystem, cache cache.Object) *Storage { |
| 48 |
return NewStorageWithOptions(fs, cache, Options{}) |
| 49 |
} |
| 50 |
|
| 51 |
// NewStorageWithOptions returns a new Storage with extra options, |
| 52 |
// backed by a given `fs.Filesystem` and cache. |
| 53 |
func NewStorageWithOptions(fs billy.Filesystem, cache cache.Object, ops Options) *Storage { |
| 54 |
dirOps := dotgit.Options{ |
| 55 |
ExclusiveAccess: ops.ExclusiveAccess, |
| 56 |
AlternatesFS: ops.AlternatesFS, |
| 57 |
} |
| 58 |
dir := dotgit.NewWithOptions(fs, dirOps) |
| 59 |
|
| 60 |
return &Storage{ |
| 61 |
fs: fs, |
| 62 |
dir: dir, |
| 63 |
|
| 64 |
ObjectStorage: *NewObjectStorageWithOptions(dir, cache, ops), |
| 65 |
ReferenceStorage: ReferenceStorage{dir: dir}, |
| 66 |
IndexStorage: IndexStorage{dir: dir}, |
| 67 |
ShallowStorage: ShallowStorage{dir: dir}, |
| 68 |
ConfigStorage: ConfigStorage{dir: dir}, |
| 69 |
ModuleStorage: ModuleStorage{dir: dir}, |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
// Filesystem returns the underlying filesystem |
| 74 |
func (s *Storage) Filesystem() billy.Filesystem { |
| 75 |
return s.fs |
| 76 |
} |
| 77 |
|
| 78 |
// Init initializes .git directory |
| 79 |
func (s *Storage) Init() error { |
| 80 |
return s.dir.Initialize() |
| 81 |
} |
| 82 |
|
| 83 |
func (s *Storage) AddAlternate(remote string) error { |
| 84 |
return s.dir.AddAlternate(remote) |
| 85 |
} |