feat: git ignore ralpi

This commit is contained in:
2026-08-09 15:34:12 -04:00
parent d31fca3cb3
commit 25e76679c5
5 changed files with 274 additions and 0 deletions

View File

@@ -0,0 +1,82 @@
/**
* Tests for the tri-state commit-range diff (src/utils.ts getCommitRangeDiff):
* a FAILED range computation (invalid/stale base ref, git error) must be a
* distinct `error` signal, never collapsed into a clean `no-changes` — a
* broken base ref must never be silently treated as a verified task.
*
* Uses a real throwaway git repo so the shell-out behavior is exercised.
*/
import { describe, test, expect, beforeAll, afterAll } from "bun:test";
import * as fs from "node:fs";
import * as path from "node:path";
import * as os from "node:os";
import { execSync } from "node:child_process";
import { getCommitRangeDiff } from "../src/utils";
let repoDir: string;
function sh(cmd: string, cwd: string) {
execSync(cmd, { cwd, stdio: "pipe" });
}
beforeAll(() => {
repoDir = fs.mkdtempSync(path.join(os.tmpdir(), "ralpi-crd-"));
sh("git init -q", repoDir);
sh("git config user.email test@example.com", repoDir);
sh("git config user.name test", repoDir);
fs.writeFileSync(path.join(repoDir, "a.ts"), "one\n", "utf-8");
sh("git add -A", repoDir);
sh("git commit -q -m init", repoDir);
});
afterAll(() => {
fs.rmSync(repoDir, { recursive: true, force: true });
});
describe("getCommitRangeDiff tri-state", () => {
test("ok: a resolvable base with committed changes yields the diff", () => {
fs.writeFileSync(path.join(repoDir, "a.ts"), "one\ntwo\n", "utf-8");
sh("git add -A", repoDir);
sh("git commit -q -m change", repoDir);
const base = execSync("git rev-parse HEAD~1", {
cwd: repoDir,
encoding: "utf-8",
}).trim();
const result = getCommitRangeDiff(repoDir, base);
expect(result.kind).toBe("ok");
if (result.kind === "ok") {
expect(result.diff).toContain("a.ts");
expect(result.hash.length).toBeGreaterThan(0);
}
});
test("error: a fake/unresolvable base ref yields the failure signal, not no-changes", () => {
// 40 hex chars that never existed in this repo.
const fake = "ffffffffffffffffffffffffffffffffffffffff";
const result = getCommitRangeDiff(repoDir, fake);
expect(result.kind).toBe("error");
if (result.kind === "error") {
expect(result.error).toContain("cannot be resolved");
}
});
test("error: a non-hex base ref is rejected before reaching the shell", () => {
const result = getCommitRangeDiff(repoDir, "HEAD~1; rm -rf /");
expect(result.kind).toBe("error");
if (result.kind === "error") {
expect(result.error).toContain("invalid or stale base ref");
}
});
test("no-changes: an empty range (base == HEAD) yields the no-changes signal", () => {
const head = execSync("git rev-parse HEAD", {
cwd: repoDir,
encoding: "utf-8",
}).trim();
const result = getCommitRangeDiff(repoDir, head);
expect(result.kind).toBe("no-changes");
});
});

View File

@@ -0,0 +1,96 @@
import { describe, expect, it } from "bun:test";
import * as fs from "node:fs";
import * as path from "node:path";
import { tempDir } from "./helpers";
import { ensureRalpiIgnored } from "../src/utils";
// ─── Gitignore hygiene: ensureRalpiIgnored ──────────────────────────────────
describe("ensureRalpiIgnored", () => {
it("creates .gitignore with .ralpi/ when absent in a git work tree", () => {
const { dir, cleanup } = tempDir();
try {
fs.mkdirSync(path.join(dir, ".git"));
expect(ensureRalpiIgnored(dir)).toBe(true);
const content = fs.readFileSync(path.join(dir, ".gitignore"), "utf8");
expect(content).toContain(".ralpi/");
} finally {
cleanup();
}
});
it("appends .ralpi/ to an existing .gitignore without the marker", () => {
const { dir, cleanup } = tempDir();
try {
fs.mkdirSync(path.join(dir, ".git"));
fs.writeFileSync(
path.join(dir, ".gitignore"),
"node_modules/\n*.log\n",
"utf8",
);
expect(ensureRalpiIgnored(dir)).toBe(true);
const content = fs.readFileSync(path.join(dir, ".gitignore"), "utf8");
expect(content).toContain("node_modules/");
expect(content).toContain(".ralpi/");
} finally {
cleanup();
}
});
it("leaves a .gitignore with the marker untouched", () => {
const { dir, cleanup } = tempDir();
try {
fs.mkdirSync(path.join(dir, ".git"));
fs.writeFileSync(path.join(dir, ".gitignore"), ".ralpi/\n", "utf8");
expect(ensureRalpiIgnored(dir)).toBe(false);
expect(fs.readFileSync(path.join(dir, ".gitignore"), "utf8")).toBe(
".ralpi/\n",
);
} finally {
cleanup();
}
});
it("is a no-op outside a git work tree", () => {
const { dir, cleanup } = tempDir();
try {
expect(ensureRalpiIgnored(dir)).toBe(false);
expect(fs.existsSync(path.join(dir, ".gitignore"))).toBe(false);
} finally {
cleanup();
}
});
it("is memoized per project dir", () => {
const { dir, cleanup } = tempDir();
try {
fs.mkdirSync(path.join(dir, ".git"));
expect(ensureRalpiIgnored(dir)).toBe(true);
// Second call: same dir already handled → no further work.
expect(ensureRalpiIgnored(dir)).toBe(false);
fs.writeFileSync(path.join(dir, ".gitignore"), "old\n", "utf8");
expect(ensureRalpiIgnored(dir)).toBe(false);
expect(fs.readFileSync(path.join(dir, ".gitignore"), "utf8")).toBe(
"old\n",
);
} finally {
cleanup();
}
});
it("works when .git is a file (linked git worktree)", () => {
const { dir, cleanup } = tempDir();
try {
fs.writeFileSync(
path.join(dir, ".git"),
"gitdir: /some/shared/repo\n",
"utf8",
);
expect(ensureRalpiIgnored(dir)).toBe(true);
const content = fs.readFileSync(path.join(dir, ".gitignore"), "utf8");
expect(content).toContain(".ralpi/");
} finally {
cleanup();
}
});
});