EnsureRepo will open a repository if it exists and try to create it if it doesn't.
(baseFS billy.Filesystem, path string)
| 66 | // EnsureRepo will open a repository if it exists and try to create it if it |
| 67 | // doesn't. |
| 68 | func EnsureRepo(baseFS billy.Filesystem, path string) (*Repository, error) { |
| 69 | // This lets us sanitize the path and ensure it always has .git on the end. |
| 70 | path = strings.TrimSuffix(path, ".git") + ".git" |
| 71 | |
| 72 | if !dirExists(baseFS, path) { |
| 73 | oldPath := strings.TrimSuffix(path, ".git") |
| 74 | |
| 75 | // If the old dir exists, rename it. Otherwize, init the repo. |
| 76 | if dirExists(baseFS, oldPath) { |
| 77 | err := baseFS.Rename(oldPath, path) |
| 78 | if err != nil { |
| 79 | return nil, err |
| 80 | } |
| 81 | } else { |
| 82 | fs, err := baseFS.Chroot(path) |
| 83 | if err != nil { |
| 84 | return nil, err |
| 85 | } |
| 86 | |
| 87 | repoFS := filesystem.NewStorage(fs, cache.NewObjectLRUDefault()) |
| 88 | |
| 89 | // Init the repo without a worktree so it's a bare repo. |
| 90 | _, err = git.Init(repoFS, nil) |
| 91 | if err != nil { |
| 92 | return nil, err |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | repo, err := Open(baseFS, path) |
| 98 | if err != nil { |
| 99 | return nil, err |
| 100 | } |
| 101 | |
| 102 | err = ensureHooks(repo.RepoFS.Filesystem()) |
| 103 | if err != nil { |
| 104 | return nil, err |
| 105 | } |
| 106 | |
| 107 | return repo, nil |
| 108 | } |
| 109 | |
| 110 | // Checkout will checkout the given hash to the worktreeFS. If an empty string |
| 111 | // is given, we checkout master. |
no test coverage detected