(action?: string)
| 4 | import { getRepoRoot } from "../core/git"; |
| 5 | |
| 6 | export async function hookCommand(action?: string) { |
| 7 | try { |
| 8 | const root = await getRepoRoot(); |
| 9 | const hooksDir = path.join(root, ".git", "hooks"); |
| 10 | const hookPath = path.join(hooksDir, "post-commit"); |
| 11 | |
| 12 | if (action === "remove") { |
| 13 | if (!fs.existsSync(hookPath)) { |
| 14 | console.log(chalk.yellow("⚠ No DevContext git hook found.")); |
| 15 | return; |
| 16 | } |
| 17 | |
| 18 | const content = fs.readFileSync(hookPath, "utf-8"); |
| 19 | if (!content.includes("devctx")) { |
| 20 | console.log(chalk.yellow("⚠ post-commit hook exists but was not created by DevContext.")); |
| 21 | return; |
| 22 | } |
| 23 | |
| 24 | // Remove only the devctx line, preserve other hooks |
| 25 | const lines = content.split("\n").filter((l) => !l.includes("devctx")); |
| 26 | if (lines.filter((l) => l.trim() && !l.startsWith("#!")).length === 0) { |
| 27 | fs.unlinkSync(hookPath); |
| 28 | } else { |
| 29 | fs.writeFileSync(hookPath, lines.join("\n")); |
| 30 | fs.chmodSync(hookPath, "755"); |
| 31 | } |
| 32 | |
| 33 | console.log(chalk.green("✓ Removed DevContext post-commit hook")); |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | // Default: install |
| 38 | fs.mkdirSync(hooksDir, { recursive: true }); |
| 39 | |
| 40 | let hookContent = "#!/bin/sh\n"; |
| 41 | |
| 42 | if (fs.existsSync(hookPath)) { |
| 43 | const existing = fs.readFileSync(hookPath, "utf-8"); |
| 44 | if (existing.includes("devctx")) { |
| 45 | console.log(chalk.yellow("⚠ DevContext post-commit hook already installed.")); |
| 46 | return; |
| 47 | } |
| 48 | // Append to existing hook |
| 49 | hookContent = existing.trimEnd() + "\n"; |
| 50 | } |
| 51 | |
| 52 | hookContent += `\n# DevContext auto-snapshot on commit\ndevctx save "Auto-saved on commit: $(git log -1 --pretty=%B | head -1)" 2>/dev/null || true\n`; |
| 53 | |
| 54 | fs.writeFileSync(hookPath, hookContent); |
| 55 | fs.chmodSync(hookPath, "755"); |
| 56 | |
| 57 | console.log(chalk.green("✓ Installed DevContext post-commit hook")); |
| 58 | console.log(chalk.gray(" Context will be auto-saved on every commit.")); |
| 59 | console.log(chalk.gray(" Remove with: devctx hook remove")); |
| 60 | } catch (err: any) { |
| 61 | console.log(chalk.red(`✗ Error: ${err.message}`)); |
| 62 | } |
| 63 | } |
nothing calls this directly
no test coverage detected