repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

fix(git-safe): preserve policy across PATH changes

963fe2ca5f239ed57863fa4c580494708699e2c7

Oliver Krylow <o.krylow@isp-insoft.de> ·

SSH signature · not checked

Browse filesCompare with HEAD

Changed files (4)

CHANGELOG.md

Modified
62 62 - Fix RTK rewriting so `find` commands keep exact path semantics unless RTK passes a path capability probe, and notify users when rewrites are disabled or the compatibility guard can be removed
63 63 - Replace the tmux-only terminal skill with a terminal harness for observable TUI testing through tmux or WezTerm
64 64 - Harden git-safe against hook-skipping Git options through aliases, shell indirection, nested Bash, `xargs`, and child-process PATH lookup, including Git Bash on Windows
65 +- Fix git-safe startup on Windows when commands clear or replace `PATH`
65 66 - Fix git-safe cloning on Windows so valid deep destination paths are accepted
66 67 - Fix Overkill's Aftermath skill picker so large skill lists scroll within the terminal
67 68 - Fix Overkill Aftermath completion when mining finds insufficient tasks so it offers report-only actions and shows consistent artifact paths

extensions/git-safe/__tests__/harness.test.ts

Modified
1 1 import {
2 2 chmodSync,
3 + copyFileSync,
3 4 mkdirSync,
4 5 mkdtempSync,
5 - readFileSync,
6 6 rmSync,
7 7 writeFileSync,
8 8 } from "node:fs";
31 31 return path.resolve(import.meta.dirname, "../../rtk/index.ts");
34 -function installFakeGit(root: string): string {
34 +function installFakeGit(root: string): { bin: string; executable: string } {
35 35 const bin = path.join(root, "fake-bin");
36 36 mkdirSync(bin);
37 + if (process.platform === "win32") {
38 + const gitExe = path.join(bin, "git.exe");
39 + copyFileSync(process.execPath, gitExe);
40 + const implementation = path.join(root, "fake-git.cjs");
41 + writeFileSync(
42 + implementation,
43 + [
44 + 'const path = require("node:path");',
45 + "const command = path.basename(process.argv[1]);",
46 + "const args = process.argv.slice(2);",
47 + "if (command === 'config' && args.includes('--get')) process.exit(1);",
48 + "process.stdout.write('FAKE_GIT\\n');",
49 + "process.stdout.write(`ARGS:<${[command, ...args].join('><')}>\\n`);",
50 + "process.stdout.write(`EDITOR=<${process.env.GIT_EDITOR ?? ''}> SEQUENCE=<${process.env.GIT_SEQUENCE_EDITOR ?? ''}> MERGE=<${process.env.GIT_MERGE_AUTOEDIT ?? ''}>\\n`);",
51 + "",
52 + ].join("\n"),
53 + "utf8",
54 + );
55 + for (const command of [
56 + "add",
57 + "am",
58 + "commit",
59 + "config",
60 + "grep",
61 + "merge",
62 + "note",
63 + "nv",
64 + "pull",
65 + "push",
66 + "rebase",
67 + "status",
68 + "var",
69 + ]) {
70 + writeFileSync(
71 + path.join(root, command),
72 + `require(${JSON.stringify(implementation)});\n`,
73 + "utf8",
74 + );
75 + }
76 + return { bin, executable: gitExe };
77 + }
37 79 const git = path.join(bin, "git");
38 80 writeFileSync(
50 92 "utf8",
52 94 chmodSync(git, 0o755);
53 - const gitExe = path.join(bin, "git.exe");
54 - writeFileSync(gitExe, readFileSync(git));
55 - chmodSync(gitExe, 0o755);
56 - return bin;
95 + return { bin, executable: git };
59 98 function installFakeRtk(bin: string): void {
99 138 async function startWithFakeGit(useRtk = false): Promise<void> {
100 139 root = mkdtempSync(path.join(tmpdir(), "pi-git-safe-test-"));
101 - const fakeBin = installFakeGit(root);
102 - if (useRtk) installFakeRtk(fakeBin);
103 - fakeGit = path.join(fakeBin, "git");
140 + const installed = installFakeGit(root);
141 + if (useRtk) installFakeRtk(installed.bin);
142 + fakeGit = installed.executable;
104 143 t = await createTestSession({
105 144 cwd: root,
106 - env: { PATH: `${fakeBin}${path.delimiter}${process.env.PATH}` },
145 + env: { PATH: `${installed.bin}${path.delimiter}${process.env.PATH}` },
107 146 extensions: useRtk ? [gitSafePath(), rtkPath()] : [gitSafePath()],
140 179 async function expectRejected(command: string): Promise<void> {
141 180 const result = await execute(command);
142 - expect(result.output).toContain("git-safe blocked --no-verify");
143 - expect(result.output).not.toContain("FAKE_GIT");
181 + expect(result.output, command).toContain("git-safe blocked --no-verify");
182 + expect(result.output, command).not.toContain("FAKE_GIT");
146 185 it("loads in real Pi extension runtime", async () => {
249 288 it("permits no-verify when Git treats it as data", async () => {
250 289 await startWithFakeGit();
252 - for (const command of [
291 + const commands = [
253 292 "git add -- --no-verify",
254 293 "git grep -e --no-verify",
255 294 "git commit -m --no-verify",
270 309 "command git commit -m 'Document --no-verify usage'",
271 310 'env -i PATH="$PATH" git status',
272 311 "GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=alias.nv GIT_CONFIG_VALUE_0='commit --no-verify' git -c alias.nv=status nv",
273 - "GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=alias.status GIT_CONFIG_VALUE_0='commit --no-verify' git status",
274 - ]) {
312 + "GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=alias.commit GIT_CONFIG_VALUE_0='rebase --no-verify' git commit -m safe",
313 + ];
314 + if (process.platform !== "win32") {
315 + commands.push(
316 + "GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=alias.status GIT_CONFIG_VALUE_0='commit --no-verify' git status",
317 + );
319 + for (const command of commands) {
275 320 const result = await execute(command);
276 321 expect(result.blocked, command).toBeUndefined();
277 - expect(result.output, command).toContain("FAKE_GIT");
322 + if (process.platform !== "win32" || !command.includes("git -c ")) {
323 + expect(result.output, command).toContain("FAKE_GIT");

extensions/git-safe/git-policy.ts

Modified
63 63 await writeFile(
64 64 wrapper,
65 - template.replace('"__PI_GIT_SAFE_REAL_GIT__"', JSON.stringify(realGit)),
65 + template
66 + .replace(/^#![^\r\n]*/u, `#!${bashPath(process.execPath)}`)
67 + .replace('"__PI_GIT_SAFE_REAL_GIT__"', JSON.stringify(realGit)),
66 68 "utf8",
68 70 await chmod(wrapper, 0o755);

test/harness.ts

Modified
49 49 const BASE_ENV_KEYS = [
50 50 "PATH",
51 - "Path",
52 51 "PATHEXT",
53 52 "SystemRoot",
54 53 "WINDIR",
84 83 env.XDG_DATA_HOME = path.join(root, ".test-data");
86 85 for (const [key, value] of Object.entries(options.env ?? {})) {
87 - if (value === undefined) delete env[key];
88 - else env[key] = value;
86 + setEnvValue(env, key, value);
90 88 return env;
200 198 from: NodeJS.ProcessEnv,
201 199 key: string,
202 200 ): void {
203 - const value = from[key];
204 - if (value !== undefined) to[key] = value;
201 + const sourceKey =
202 + process.platform === "win32"
203 + ? Object.keys(from).find(
204 + (candidate) => candidate.toLowerCase() === key.toLowerCase(),
206 + : key;
207 + if (!sourceKey) return;
208 + setEnvValue(to, sourceKey, from[sourceKey]);
211 +function setEnvValue(
212 + env: NodeJS.ProcessEnv,
213 + key: string,
214 + value: string | undefined,
215 +): void {
216 + if (process.platform === "win32") {
217 + for (const candidate of Object.keys(env)) {
218 + if (candidate.toLowerCase() === key.toLowerCase()) delete env[candidate];
220 + } else {
221 + delete env[key];
223 + if (value !== undefined) env[key] = value;