Copy git hooks during worktree init
This commit is contained in:
@@ -1,8 +1,9 @@
|
|||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
|
import { execFileSync } from "node:child_process";
|
||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import { copySeededSecretsKey, rebindWorkspaceCwd } from "../commands/worktree.js";
|
import { copyGitHooksToWorktreeGitDir, copySeededSecretsKey, rebindWorkspaceCwd } from "../commands/worktree.js";
|
||||||
import {
|
import {
|
||||||
buildWorktreeConfig,
|
buildWorktreeConfig,
|
||||||
buildWorktreeEnvEntries,
|
buildWorktreeEnvEntries,
|
||||||
@@ -199,4 +200,52 @@ describe("worktree helpers", () => {
|
|||||||
}),
|
}),
|
||||||
).toBeNull();
|
).toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("copies shared git hooks into a linked worktree git dir", () => {
|
||||||
|
const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), "paperclip-worktree-hooks-"));
|
||||||
|
const repoRoot = path.join(tempRoot, "repo");
|
||||||
|
const worktreePath = path.join(tempRoot, "repo-feature");
|
||||||
|
|
||||||
|
try {
|
||||||
|
fs.mkdirSync(repoRoot, { recursive: true });
|
||||||
|
execFileSync("git", ["init"], { cwd: repoRoot, stdio: "ignore" });
|
||||||
|
execFileSync("git", ["config", "user.email", "test@example.com"], { cwd: repoRoot, stdio: "ignore" });
|
||||||
|
execFileSync("git", ["config", "user.name", "Test User"], { cwd: repoRoot, stdio: "ignore" });
|
||||||
|
fs.writeFileSync(path.join(repoRoot, "README.md"), "# temp\n", "utf8");
|
||||||
|
execFileSync("git", ["add", "README.md"], { cwd: repoRoot, stdio: "ignore" });
|
||||||
|
execFileSync("git", ["commit", "-m", "Initial commit"], { cwd: repoRoot, stdio: "ignore" });
|
||||||
|
|
||||||
|
const sourceHooksDir = path.join(repoRoot, ".git", "hooks");
|
||||||
|
const sourceHookPath = path.join(sourceHooksDir, "pre-commit");
|
||||||
|
const sourceTokensPath = path.join(sourceHooksDir, "forbidden-tokens.txt");
|
||||||
|
fs.writeFileSync(sourceHookPath, "#!/usr/bin/env bash\nexit 0\n", { encoding: "utf8", mode: 0o755 });
|
||||||
|
fs.chmodSync(sourceHookPath, 0o755);
|
||||||
|
fs.writeFileSync(sourceTokensPath, "secret-token\n", "utf8");
|
||||||
|
|
||||||
|
execFileSync("git", ["worktree", "add", "--detach", worktreePath], { cwd: repoRoot, stdio: "ignore" });
|
||||||
|
|
||||||
|
const copied = copyGitHooksToWorktreeGitDir(worktreePath);
|
||||||
|
const worktreeGitDir = execFileSync("git", ["rev-parse", "--git-dir"], {
|
||||||
|
cwd: worktreePath,
|
||||||
|
encoding: "utf8",
|
||||||
|
stdio: ["ignore", "pipe", "ignore"],
|
||||||
|
}).trim();
|
||||||
|
const resolvedSourceHooksDir = fs.realpathSync(sourceHooksDir);
|
||||||
|
const resolvedTargetHooksDir = fs.realpathSync(path.resolve(worktreePath, worktreeGitDir, "hooks"));
|
||||||
|
const targetHookPath = path.join(resolvedTargetHooksDir, "pre-commit");
|
||||||
|
const targetTokensPath = path.join(resolvedTargetHooksDir, "forbidden-tokens.txt");
|
||||||
|
|
||||||
|
expect(copied).toMatchObject({
|
||||||
|
sourceHooksPath: resolvedSourceHooksDir,
|
||||||
|
targetHooksPath: resolvedTargetHooksDir,
|
||||||
|
copied: true,
|
||||||
|
});
|
||||||
|
expect(fs.readFileSync(targetHookPath, "utf8")).toBe("#!/usr/bin/env bash\nexit 0\n");
|
||||||
|
expect(fs.statSync(targetHookPath).mode & 0o111).not.toBe(0);
|
||||||
|
expect(fs.readFileSync(targetTokensPath, "utf8")).toBe("secret-token\n");
|
||||||
|
} finally {
|
||||||
|
execFileSync("git", ["worktree", "remove", "--force", worktreePath], { cwd: repoRoot, stdio: "ignore" });
|
||||||
|
fs.rmSync(tempRoot, { recursive: true, force: true });
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,4 +1,16 @@
|
|||||||
import { chmodSync, copyFileSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
import {
|
||||||
|
chmodSync,
|
||||||
|
copyFileSync,
|
||||||
|
existsSync,
|
||||||
|
mkdirSync,
|
||||||
|
readdirSync,
|
||||||
|
readFileSync,
|
||||||
|
readlinkSync,
|
||||||
|
rmSync,
|
||||||
|
statSync,
|
||||||
|
symlinkSync,
|
||||||
|
writeFileSync,
|
||||||
|
} from "node:fs";
|
||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { execFileSync } from "node:child_process";
|
import { execFileSync } from "node:child_process";
|
||||||
@@ -80,6 +92,14 @@ type EmbeddedPostgresHandle = {
|
|||||||
type GitWorkspaceInfo = {
|
type GitWorkspaceInfo = {
|
||||||
root: string;
|
root: string;
|
||||||
commonDir: string;
|
commonDir: string;
|
||||||
|
gitDir: string;
|
||||||
|
hooksPath: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
type CopiedGitHooksResult = {
|
||||||
|
sourceHooksPath: string;
|
||||||
|
targetHooksPath: string;
|
||||||
|
copied: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
type SeedWorktreeDatabaseResult = {
|
type SeedWorktreeDatabaseResult = {
|
||||||
@@ -162,15 +182,88 @@ function detectGitWorkspaceInfo(cwd: string): GitWorkspaceInfo | null {
|
|||||||
encoding: "utf8",
|
encoding: "utf8",
|
||||||
stdio: ["ignore", "pipe", "ignore"],
|
stdio: ["ignore", "pipe", "ignore"],
|
||||||
}).trim();
|
}).trim();
|
||||||
|
const gitDirRaw = execFileSync("git", ["rev-parse", "--git-dir"], {
|
||||||
|
cwd: root,
|
||||||
|
encoding: "utf8",
|
||||||
|
stdio: ["ignore", "pipe", "ignore"],
|
||||||
|
}).trim();
|
||||||
|
const hooksPathRaw = execFileSync("git", ["rev-parse", "--git-path", "hooks"], {
|
||||||
|
cwd: root,
|
||||||
|
encoding: "utf8",
|
||||||
|
stdio: ["ignore", "pipe", "ignore"],
|
||||||
|
}).trim();
|
||||||
return {
|
return {
|
||||||
root: path.resolve(root),
|
root: path.resolve(root),
|
||||||
commonDir: path.resolve(root, commonDirRaw),
|
commonDir: path.resolve(root, commonDirRaw),
|
||||||
|
gitDir: path.resolve(root, gitDirRaw),
|
||||||
|
hooksPath: path.resolve(root, hooksPathRaw),
|
||||||
};
|
};
|
||||||
} catch {
|
} catch {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function copyDirectoryContents(sourceDir: string, targetDir: string): boolean {
|
||||||
|
if (!existsSync(sourceDir)) return false;
|
||||||
|
|
||||||
|
const entries = readdirSync(sourceDir, { withFileTypes: true });
|
||||||
|
if (entries.length === 0) return false;
|
||||||
|
|
||||||
|
mkdirSync(targetDir, { recursive: true });
|
||||||
|
|
||||||
|
let copied = false;
|
||||||
|
for (const entry of entries) {
|
||||||
|
const sourcePath = path.resolve(sourceDir, entry.name);
|
||||||
|
const targetPath = path.resolve(targetDir, entry.name);
|
||||||
|
|
||||||
|
if (entry.isDirectory()) {
|
||||||
|
mkdirSync(targetPath, { recursive: true });
|
||||||
|
copyDirectoryContents(sourcePath, targetPath);
|
||||||
|
copied = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (entry.isSymbolicLink()) {
|
||||||
|
rmSync(targetPath, { recursive: true, force: true });
|
||||||
|
symlinkSync(readlinkSync(sourcePath), targetPath);
|
||||||
|
copied = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
copyFileSync(sourcePath, targetPath);
|
||||||
|
try {
|
||||||
|
chmodSync(targetPath, statSync(sourcePath).mode & 0o777);
|
||||||
|
} catch {
|
||||||
|
// best effort
|
||||||
|
}
|
||||||
|
copied = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return copied;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function copyGitHooksToWorktreeGitDir(cwd: string): CopiedGitHooksResult | null {
|
||||||
|
const workspace = detectGitWorkspaceInfo(cwd);
|
||||||
|
if (!workspace) return null;
|
||||||
|
|
||||||
|
const sourceHooksPath = workspace.hooksPath;
|
||||||
|
const targetHooksPath = path.resolve(workspace.gitDir, "hooks");
|
||||||
|
|
||||||
|
if (sourceHooksPath === targetHooksPath) {
|
||||||
|
return {
|
||||||
|
sourceHooksPath,
|
||||||
|
targetHooksPath,
|
||||||
|
copied: false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
sourceHooksPath,
|
||||||
|
targetHooksPath,
|
||||||
|
copied: copyDirectoryContents(sourceHooksPath, targetHooksPath),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export function rebindWorkspaceCwd(input: {
|
export function rebindWorkspaceCwd(input: {
|
||||||
sourceRepoRoot: string;
|
sourceRepoRoot: string;
|
||||||
targetRepoRoot: string;
|
targetRepoRoot: string;
|
||||||
@@ -493,6 +586,7 @@ export async function worktreeInitCommand(opts: WorktreeInitOptions): Promise<vo
|
|||||||
mergePaperclipEnvEntries(buildWorktreeEnvEntries(paths), paths.envPath);
|
mergePaperclipEnvEntries(buildWorktreeEnvEntries(paths), paths.envPath);
|
||||||
ensureAgentJwtSecret(paths.configPath);
|
ensureAgentJwtSecret(paths.configPath);
|
||||||
loadPaperclipEnvFile(paths.configPath);
|
loadPaperclipEnvFile(paths.configPath);
|
||||||
|
const copiedGitHooks = copyGitHooksToWorktreeGitDir(cwd);
|
||||||
|
|
||||||
let seedSummary: string | null = null;
|
let seedSummary: string | null = null;
|
||||||
let reboundWorkspaceSummary: SeedWorktreeDatabaseResult["reboundWorkspaces"] = [];
|
let reboundWorkspaceSummary: SeedWorktreeDatabaseResult["reboundWorkspaces"] = [];
|
||||||
@@ -527,6 +621,11 @@ export async function worktreeInitCommand(opts: WorktreeInitOptions): Promise<vo
|
|||||||
p.log.message(pc.dim(`Isolated home: ${paths.homeDir}`));
|
p.log.message(pc.dim(`Isolated home: ${paths.homeDir}`));
|
||||||
p.log.message(pc.dim(`Instance: ${paths.instanceId}`));
|
p.log.message(pc.dim(`Instance: ${paths.instanceId}`));
|
||||||
p.log.message(pc.dim(`Server port: ${serverPort} | DB port: ${databasePort}`));
|
p.log.message(pc.dim(`Server port: ${serverPort} | DB port: ${databasePort}`));
|
||||||
|
if (copiedGitHooks?.copied) {
|
||||||
|
p.log.message(
|
||||||
|
pc.dim(`Mirrored git hooks: ${copiedGitHooks.sourceHooksPath} -> ${copiedGitHooks.targetHooksPath}`),
|
||||||
|
);
|
||||||
|
}
|
||||||
if (seedSummary) {
|
if (seedSummary) {
|
||||||
p.log.message(pc.dim(`Seed mode: ${seedMode}`));
|
p.log.message(pc.dim(`Seed mode: ${seedMode}`));
|
||||||
p.log.message(pc.dim(`Seed snapshot: ${seedSummary}`));
|
p.log.message(pc.dim(`Seed snapshot: ${seedSummary}`));
|
||||||
|
|||||||
@@ -138,6 +138,7 @@ This command:
|
|||||||
|
|
||||||
- writes repo-local files at `.paperclip/config.json` and `.paperclip/.env`
|
- writes repo-local files at `.paperclip/config.json` and `.paperclip/.env`
|
||||||
- creates an isolated instance under `~/.paperclip-worktrees/instances/<worktree-id>/`
|
- creates an isolated instance under `~/.paperclip-worktrees/instances/<worktree-id>/`
|
||||||
|
- when run inside a linked git worktree, mirrors the effective git hooks into that worktree's private git dir
|
||||||
- picks a free app port and embedded PostgreSQL port
|
- picks a free app port and embedded PostgreSQL port
|
||||||
- by default seeds the isolated DB in `minimal` mode from your main instance via a logical SQL snapshot
|
- by default seeds the isolated DB in `minimal` mode from your main instance via a logical SQL snapshot
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user