MCPcopy Create free account
hub / github.com/belak/gitdir / EnsureRepo

Function EnsureRepo

internal/git/repository.go:68–108  ·  view source on GitHub ↗

EnsureRepo will open a repository if it exists and try to create it if it doesn't.

(baseFS billy.Filesystem, path string)

Source from the content-addressed store, hash-verified

66// EnsureRepo will open a repository if it exists and try to create it if it
67// doesn't.
68func 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.

Callers 7

loadOrgConfigMethod · 0.92
openAdminRepoMethod · 0.92
SetHashMethod · 0.92
SetUserHashMethod · 0.92
SetOrgHashMethod · 0.92
loadUserConfigMethod · 0.92
cmdRepoActionMethod · 0.92

Calls 3

dirExistsFunction · 0.85
OpenFunction · 0.85
ensureHooksFunction · 0.85

Tested by

no test coverage detected