|
| 1 | +//! `runseal :init [--force]` — validate the repo and install git hooks. |
| 2 | +//! |
| 3 | +//! Installs `.runseal/hooks/{pre-commit,commit-msg}` into the repo's hooks |
| 4 | +//! directory. Existing hooks generated by a prior `:init` are replaced; foreign |
| 5 | +//! hooks are left alone unless `--force` is given (then backed up first). |
| 6 | + |
| 7 | +import { capture } from "@/lib/std/cmd.ts"; |
| 8 | +import { exists, join } from "@/lib/std/fs.ts"; |
| 9 | + |
| 10 | +const MARKER = "santi :init hook"; |
| 11 | +const HOOKS = ["pre-commit", "commit-msg"]; |
| 12 | +const REQUIRED_TOOLS = ["git", "deno", "cargo", "runseal"]; |
| 13 | +const REQUIRED_PATHS = [ |
| 14 | + "runseal.toml", |
| 15 | + ".runseal/deno.json", |
| 16 | + ".runseal/hooks/pre-commit", |
| 17 | + ".runseal/hooks/commit-msg", |
| 18 | + ".runseal/wrappers/guard.ts", |
| 19 | +]; |
| 20 | + |
| 21 | +export async function init(argv: string[]): Promise<number> { |
| 22 | + if (argv.includes("-h") || argv.includes("--help")) { |
| 23 | + usage(); |
| 24 | + return 0; |
| 25 | + } |
| 26 | + const force = argv.includes("--force"); |
| 27 | + const positional = argv.find((arg) => !arg.startsWith("-")); |
| 28 | + if (positional !== undefined) return fail(`unexpected argument: ${positional}`); |
| 29 | + |
| 30 | + console.log("==> resolving repository"); |
| 31 | + const root = (await capture("git", ["rev-parse", "--show-toplevel"])).stdout.trim(); |
| 32 | + if (root === "") return fail("not inside a git repository"); |
| 33 | + const gitDir = (await capture("git", ["rev-parse", "--absolute-git-dir"])).stdout.trim(); |
| 34 | + const hooksDir = join(gitDir, "hooks"); |
| 35 | + console.log(`repository: ${root}`); |
| 36 | + |
| 37 | + console.log("==> checking required tools"); |
| 38 | + for (const tool of REQUIRED_TOOLS) { |
| 39 | + if (!(await toolExists(tool))) return fail(`missing required tool: ${tool}`); |
| 40 | + } |
| 41 | + console.log(`ok: ${REQUIRED_TOOLS.join(", ")}`); |
| 42 | + |
| 43 | + console.log("==> checking repository entrypoints"); |
| 44 | + for (const rel of REQUIRED_PATHS) { |
| 45 | + if (!exists(join(root, rel))) return fail(`missing required path: ${rel}`); |
| 46 | + } |
| 47 | + console.log("ok: repository entrypoints"); |
| 48 | + |
| 49 | + console.log("==> installing git hooks"); |
| 50 | + Deno.mkdirSync(hooksDir, { recursive: true }); |
| 51 | + for (const name of HOOKS) { |
| 52 | + const dest = join(hooksDir, name); |
| 53 | + const prepared = prepareHook(dest, force); |
| 54 | + if (prepared !== 0) return prepared; |
| 55 | + Deno.copyFileSync(join(root, ".runseal/hooks", name), dest); |
| 56 | + chmod755(dest); |
| 57 | + console.log(`installed ${dest}`); |
| 58 | + } |
| 59 | + console.log("hooks installed — commits now refuse main and run `runseal :guard`"); |
| 60 | + return 0; |
| 61 | +} |
| 62 | + |
| 63 | +function prepareHook(dest: string, force: boolean): number { |
| 64 | + if (!exists(dest)) return 0; |
| 65 | + let content = ""; |
| 66 | + try { |
| 67 | + content = Deno.readTextFileSync(dest); |
| 68 | + } catch { |
| 69 | + content = ""; |
| 70 | + } |
| 71 | + if (content.includes(MARKER)) return 0; // generated by us; safe to replace |
| 72 | + if (!force) { |
| 73 | + return fail( |
| 74 | + `${dest} exists and was not generated by :init — rerun with --force to back it up and replace it`, |
| 75 | + ); |
| 76 | + } |
| 77 | + const backup = backupNumbered(dest); |
| 78 | + console.log(`backed up existing hook to ${backup}`); |
| 79 | + return 0; |
| 80 | +} |
| 81 | + |
| 82 | +function backupNumbered(path: string): string { |
| 83 | + let candidate = `${path}.bak`; |
| 84 | + let n = 1; |
| 85 | + while (exists(candidate)) candidate = `${path}.bak.${n++}`; |
| 86 | + Deno.renameSync(path, candidate); |
| 87 | + return candidate; |
| 88 | +} |
| 89 | + |
| 90 | +async function toolExists(tool: string): Promise<boolean> { |
| 91 | + const result = await capture("sh", ["-c", `command -v ${tool}`]); |
| 92 | + return result.code === 0 && result.stdout.trim() !== ""; |
| 93 | +} |
| 94 | + |
| 95 | +function chmod755(path: string): void { |
| 96 | + try { |
| 97 | + Deno.chmodSync(path, 0o755); |
| 98 | + } catch { |
| 99 | + // non-unix platform; git uses its own executable handling |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +function usage(): void { |
| 104 | + console.log("Usage: runseal :init [--force]"); |
| 105 | + console.log(""); |
| 106 | + console.log("Validate the repo and install git hooks (pre-commit, commit-msg)."); |
| 107 | + console.log(" --force back up a non-generated existing hook before replacing it"); |
| 108 | +} |
| 109 | + |
| 110 | +function fail(message: string): number { |
| 111 | + console.error(`:init: ${message}`); |
| 112 | + return 1; |
| 113 | +} |
0 commit comments