(opts: InitWithDeps)
| 41 | } |
| 42 | |
| 43 | export async function initWith(opts: InitWithDeps): Promise<void> { |
| 44 | const dir = opts.dir ?? "/"; |
| 45 | const defaultBranch = opts.defaultBranch ?? "main"; |
| 46 | const bare = opts.bare ?? false; |
| 47 | |
| 48 | // Detect "already a repo" up front so we can throw the typed |
| 49 | // error. isomorphic-git's `init` is documented as idempotent, |
| 50 | // so without this probe a second call would silently succeed. |
| 51 | if (!bare) { |
| 52 | const probe = (opts.fs as InitFsProbe).promises?.stat; |
| 53 | if (typeof probe === "function") { |
| 54 | try { |
| 55 | await probe(joinPath(dir, ".git")); |
| 56 | throw new AlreadyInitializedError(dir); |
| 57 | } catch (cause) { |
| 58 | if (cause instanceof AlreadyInitializedError) throw cause; |
| 59 | // anything else — including the expected ENOENT — |
| 60 | // means "no repo here; safe to init". |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | try { |
| 66 | await opts.git.init({ fs: opts.fs, dir, bare, defaultBranch }); |
| 67 | } catch (cause) { |
| 68 | throw new GitError("EINITFAIL", `git init failed: ${errorMessage(cause)}`, { cause }); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | function joinPath(base: string, segment: string): string { |
| 73 | if (base.endsWith("/")) return `${base}${segment}`; |
no test coverage detected