Open will open a repository if it exists.
(baseFS billy.Filesystem, path string)
| 32 | |
| 33 | // Open will open a repository if it exists. |
| 34 | func Open(baseFS billy.Filesystem, path string) (*Repository, error) { |
| 35 | // This lets us sanitize the path and ensure it always has .git on the end. |
| 36 | path = strings.TrimSuffix(path, ".git") + ".git" |
| 37 | |
| 38 | fs, err := baseFS.Chroot(path) |
| 39 | if err != nil { |
| 40 | return nil, err |
| 41 | } |
| 42 | |
| 43 | repoFS := filesystem.NewStorage(fs, cache.NewObjectLRUDefault()) |
| 44 | |
| 45 | // TODO: this probably shouldn't be memfs. |
| 46 | worktreeFS := memfs.New() |
| 47 | |
| 48 | repo, err := git.Open(repoFS, worktreeFS) |
| 49 | if err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | |
| 53 | worktree, err := repo.Worktree() |
| 54 | if err != nil { |
| 55 | return nil, err |
| 56 | } |
| 57 | |
| 58 | return &Repository{ |
| 59 | Repo: repo, |
| 60 | RepoFS: repoFS, |
| 61 | Worktree: worktree, |
| 62 | WorktreeFS: worktreeFS, |
| 63 | }, nil |
| 64 | } |
| 65 | |
| 66 | // EnsureRepo will open a repository if it exists and try to create it if it |
| 67 | // doesn't. |