|
| 1 | +// @effect-diagnostics nodeBuiltinImport:off - The fixup fold check is Git plumbing; fixtures need real repositories. |
| 2 | + |
| 3 | +import * as NodeChildProcess from "node:child_process"; |
| 4 | +import * as NodeFS from "node:fs"; |
| 5 | +import * as NodeOS from "node:os"; |
| 6 | + |
| 7 | +import { assert, it } from "@effect/vitest"; |
| 8 | + |
| 9 | +import { fixupFoldFailures } from "./fork-fixup-fold.ts"; |
| 10 | + |
| 11 | +const git = (root: string, args: ReadonlyArray<string>): string => |
| 12 | + NodeChildProcess.execFileSync("git", args, { cwd: root, encoding: "utf8" }).trim(); |
| 13 | + |
| 14 | +/** Repo-local identity, so the replayed rebase commits without ambient global config. */ |
| 15 | +const identity = (root: string): void => { |
| 16 | + git(root, ["config", "user.name", "Fixup Fold Test"]); |
| 17 | + git(root, ["config", "user.email", "[email protected]"]); |
| 18 | + git(root, ["config", "commit.gpgsign", "false"]); |
| 19 | +}; |
| 20 | + |
| 21 | +const commit = (root: string, subject: string, files: Record<string, string>): void => { |
| 22 | + for (const [file, text] of Object.entries(files)) NodeFS.writeFileSync(`${root}/${file}`, text); |
| 23 | + git(root, ["add", "."]); |
| 24 | + git(root, ["commit", "--quiet", "--allow-empty", "-m", subject]); |
| 25 | +}; |
| 26 | + |
| 27 | +const worktreeCount = (root: string): number => |
| 28 | + git(root, ["worktree", "list", "--porcelain"]) |
| 29 | + .split("\n") |
| 30 | + .filter((line) => line.startsWith("worktree ")).length; |
| 31 | + |
| 32 | +it("passes a stack whose fixups fold at their owners, squash suffix included", () => { |
| 33 | + const root = NodeFS.mkdtempSync(NodeOS.tmpdir() + "/fork-fixup-fold-"); |
| 34 | + try { |
| 35 | + git(root, ["init", "--quiet", "--initial-branch", "main"]); |
| 36 | + identity(root); |
| 37 | + commit(root, "upstream base", { "file.txt": "one\ntwo\nthree\n" }); |
| 38 | + git(root, ["update-ref", "refs/remotes/upstream/main", "main"]); |
| 39 | + const base = git(root, ["rev-parse", "main"]); |
| 40 | + commit(root, "owner edit", { "file.txt": "ONE\ntwo\nthree\n" }); |
| 41 | + commit(root, "later edit", { "file.txt": "ONE\ntwo\nTHREE\n" }); |
| 42 | + commit(root, "fixup! owner edit (#2)", { "file.txt": "ONE!\ntwo\nTHREE\n" }); |
| 43 | + assert.deepStrictEqual(fixupFoldFailures(root, base, "HEAD"), []); |
| 44 | + // The throwaway replay worktree is gone again. |
| 45 | + assert.strictEqual(worktreeCount(root), 1); |
| 46 | + } finally { |
| 47 | + NodeFS.rmSync(root, { recursive: true, force: true }); |
| 48 | + } |
| 49 | +}); |
| 50 | + |
| 51 | +it("refuses a fixup that only folds at the tip, naming fixup and owner", () => { |
| 52 | + const root = NodeFS.mkdtempSync(NodeOS.tmpdir() + "/fork-fixup-fold-"); |
| 53 | + try { |
| 54 | + git(root, ["init", "--quiet", "--initial-branch", "main"]); |
| 55 | + identity(root); |
| 56 | + commit(root, "upstream base", { "file.txt": "a\ntail\n" }); |
| 57 | + git(root, ["update-ref", "refs/remotes/upstream/main", "main"]); |
| 58 | + const base = git(root, ["rev-parse", "main"]); |
| 59 | + commit(root, "owner edit", { "file.txt": "b\ntail\n" }); |
| 60 | + commit(root, "later edit", { "file.txt": "c\ntail\n" }); |
| 61 | + // The fixup edit builds on the later commit's content, so it applies at |
| 62 | + // the tip and nowhere near its owner: the proof must stop there. |
| 63 | + commit(root, "fixup! owner edit", { "file.txt": "c!\ntail\n" }); |
| 64 | + const [failure] = fixupFoldFailures(root, base, "HEAD"); |
| 65 | + assert.ok(failure?.includes('fixup "fixup! owner edit"'), failure); |
| 66 | + assert.ok(failure?.includes('owner "owner edit"'), failure); |
| 67 | + // The live checkout is untouched: the tip stands and the replay worktree is gone. |
| 68 | + assert.strictEqual(git(root, ["rev-parse", "HEAD"]), git(root, ["rev-parse", "main"])); |
| 69 | + assert.strictEqual(worktreeCount(root), 1); |
| 70 | + } finally { |
| 71 | + NodeFS.rmSync(root, { recursive: true, force: true }); |
| 72 | + } |
| 73 | +}); |
| 74 | + |
| 75 | +it("refuses a fixup that names no fork commit", () => { |
| 76 | + const root = NodeFS.mkdtempSync(NodeOS.tmpdir() + "/fork-fixup-fold-"); |
| 77 | + try { |
| 78 | + git(root, ["init", "--quiet", "--initial-branch", "main"]); |
| 79 | + identity(root); |
| 80 | + commit(root, "upstream base", { "file.txt": "a\n" }); |
| 81 | + git(root, ["update-ref", "refs/remotes/upstream/main", "main"]); |
| 82 | + const base = git(root, ["rev-parse", "main"]); |
| 83 | + commit(root, "fixup! nothing names this", { "file.txt": "b\n" }); |
| 84 | + assert.deepStrictEqual(fixupFoldFailures(root, base, "HEAD"), [ |
| 85 | + 'fixup "fixup! nothing names this" names no fork commit above the target', |
| 86 | + ]); |
| 87 | + } finally { |
| 88 | + NodeFS.rmSync(root, { recursive: true, force: true }); |
| 89 | + } |
| 90 | +}); |
| 91 | + |
| 92 | +it("skips the replay when the stack has no fixups", () => { |
| 93 | + const root = NodeFS.mkdtempSync(NodeOS.tmpdir() + "/fork-fixup-fold-"); |
| 94 | + try { |
| 95 | + git(root, ["init", "--quiet", "--initial-branch", "main"]); |
| 96 | + identity(root); |
| 97 | + commit(root, "upstream base", { "file.txt": "a\n" }); |
| 98 | + git(root, ["update-ref", "refs/remotes/upstream/main", "main"]); |
| 99 | + const base = git(root, ["rev-parse", "main"]); |
| 100 | + commit(root, "plain fork commit", { "file.txt": "b\n" }); |
| 101 | + assert.deepStrictEqual(fixupFoldFailures(root, base, "HEAD"), []); |
| 102 | + assert.strictEqual(worktreeCount(root), 1); |
| 103 | + } finally { |
| 104 | + NodeFS.rmSync(root, { recursive: true, force: true }); |
| 105 | + } |
| 106 | +}); |
0 commit comments