// The scripted half of the read additions okfit asked for: the missing `config.worktree` degrade, `ignoredFiles`, // `gitDir`, cone-mode canonical patterns and `GitCommandError.refusal`. Real git runs these in // `integration/GitReadSurface.int.test.ts`. import { assert, describe, it } from "@effect/vitest"; import { ConfigProvider, Effect, Schema } from "effect"; import type { GitShape } from "../src/Git.js"; import { Git, GitCommandError, NotARepositoryError, SparseCheckoutState } from "../src/Git.js"; import type { ScriptResult } from "./fixtures.js"; import { GitOverMemory, scripted } from "./fixtures.js"; const cwd = "/repo"; const run = (f: (git: GitShape) => Effect.Effect, byArgs: (args: ReadonlyArray) => ScriptResult) => Effect.flatMap(Git, f).pipe( Effect.provide(GitOverMemory), Effect.provide(scripted(byArgs)), Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnvRecord({}))), ); const MISSING_WORKTREE_CONFIG = { exit: 128, stderr: "fatal: unable to read config file '.git/config.worktree': No such file or directory\n", }; describe("configList", () => { it.effect("a worktree-scope read with no config.worktree yet is the empty list", () => Effect.gen(function* () { const entries = yield* run( (git) => git.configList(cwd, { scope: "worktree" }), () => MISSING_WORKTREE_CONFIG, ); assert.deepStrictEqual(entries, []); }), ); it.effect("control: the same stderr on another scope, and another 128 on the worktree scope, still fail", () => Effect.gen(function* () { const local = yield* Effect.flip( run( (git) => git.configList(cwd, { scope: "local" }), () => MISSING_WORKTREE_CONFIG, ), ); assert.instanceOf(local, GitCommandError); const other = yield* Effect.flip( run( (git) => git.configList(cwd, { scope: "worktree" }), () => ({ exit: 128, stderr: "fatal: unable to read config file '.git/config': Permission denied\n" }), ), ); assert.instanceOf(other, GitCommandError); }), ); }); describe("ignoredFiles", () => { it.effect("lists the NUL-separated ignored paths, with the flags the options choose", () => Effect.gen(function* () { const seen: Array> = []; const record = (args: ReadonlyArray) => { seen.push(args); return { stdout: "a.log\0dist/\0" }; }; const listed = yield* run((git) => git.ignoredFiles(cwd, { directory: true }), record); yield* run((git) => git.ignoredFiles(cwd, { relative: true }), record); assert.deepStrictEqual(listed, ["a.log", "dist/"]); assert.deepStrictEqual(seen, [ ["ls-files", "--others", "--ignored", "--exclude-standard", "-z", "--directory", "--full-name"], ["ls-files", "--others", "--ignored", "--exclude-standard", "-z"], ]); }), ); }); describe("gitDir", () => { it.effect("answers git's path with only the terminating newline removed", () => Effect.gen(function* () { const dir = yield* run( (git) => git.gitDir(cwd), (args) => args[0] === "rev-parse" && args[1] === "--absolute-git-dir" ? { stdout: "/repo/.git/worktrees/b \n" } : { exit: 99 }, ); assert.strictEqual(dir, "/repo/.git/worktrees/b "); }), ); it.effect("outside a repository is NotARepositoryError; an answer that is not one line is a GitCommandError", () => Effect.gen(function* () { const outside = yield* Effect.flip( run( (git) => git.gitDir(cwd), () => ({ exit: 128, stderr: "fatal: not a git repository (or any of the parent directories): .git\n" }), ), ); assert.instanceOf(outside, NotARepositoryError); const garbled = yield* Effect.flip( run( (git) => git.gitDir(cwd), () => ({ stdout: "a\nb\n" }), ), ); assert.instanceOf(garbled, GitCommandError); }), ); }); describe("sparseCheckoutList", () => { const list = (patterns: string) => run( (git) => git.sparseCheckoutList(cwd), (args) => (args[0] === "sparse-checkout" ? { stdout: patterns } : { exit: 1 }), ); it.effect("cone mode is canonical: trailing slashes removed, duplicates dropped, sorted", () => Effect.gen(function* () { const state = yield* list("src/\ndocs\nsrc\na b/\nsrc/lib\n"); assert.isTrue(state.cone); assert.deepStrictEqual(state.patterns, ["a b", "docs", "src"]); }), ); }); describe("SparseCheckoutState.canonicalCone", () => { it("dedupes, strips trailing slashes, drops a child of a listed parent, and sorts, as git lists them", () => { // Probed against git 2.56: `sparse-checkout set --cone a/b a c/ d/e ab` lists `a`, `ab`, `c`, `d/e`. assert.deepStrictEqual(SparseCheckoutState.canonicalCone(["a/b", "a", "c/", "d/e", "ab"]), ["a", "ab", "c", "d/e"]); assert.deepStrictEqual(SparseCheckoutState.canonicalCone(["x//", "x", "", "/"]), ["x"]); // A deeper descendant goes too; a sibling sharing a name prefix stays. assert.deepStrictEqual(SparseCheckoutState.canonicalCone(["p/q/r", "p", "p-q"]), ["p", "p-q"]); }); it("trims only the trailing run of slashes, keeping inner ones, however long the runs", () => { // The shape a `/\/+$/` replacement retries quadratically on: a long run of slashes a non-slash ends. const run = "/".repeat(50_000); assert.deepStrictEqual(SparseCheckoutState.canonicalCone([`a${run}b${run}`, `c${run}`, "d", run]), [ `a${run}b`, "c", "d", ]); assert.deepStrictEqual(SparseCheckoutState.canonicalCone(["a//b///"]), ["a//b"]); }); it("a desired list compares equal to the listed state when the selection is unchanged", () => { const listed = ["docs", "src"]; assert.deepStrictEqual(SparseCheckoutState.canonicalCone(["src/", "docs", "src/lib"]), listed); }); }); describe("GitCommandError.refusal", () => { const error = (stderr: string) => GitCommandError.make({ kind: "failed", args: ["fetch"], cwd, stderr, exitCode: 128 }); it("is the last fatal:, error: or ssh: line, prefix included", () => { assert.strictEqual( error("hint: something\nerror: pathspec 'x' did not match\nfatal: bad object x\n").refusal, "fatal: bad object x", ); assert.strictEqual(error("warning: a\r\nerror: b\r\n").refusal, "error: b"); }); it("passes over git's transport trailers to the line that says why", () => { assert.strictEqual( error( "ssh: Could not resolve hostname nope: nodename nor servname provided\nfatal: Could not read from remote repository.\n\nPlease make sure you have the correct access rights\n", ).refusal, "ssh: Could not resolve hostname nope: nodename nor servname provided", ); // A trailer alone is still the answer. assert.strictEqual( error("fatal: the remote end hung up unexpectedly\n").refusal, "fatal: the remote end hung up unexpectedly", ); }); it("passes over a submodule add's clone-failed trailer to the line that says why", () => { assert.strictEqual( error("fatal: unable to checkout 'x'\nfatal: clone of 'https://h/o/n' into submodule path '/r/m' failed\n") .refusal, "fatal: unable to checkout 'x'", ); // The trailer alone is still the answer. assert.strictEqual( error("fatal: clone of 'https://h/o/n' into submodule path '/r/m' failed\n").refusal, "fatal: clone of 'https://h/o/n' into submodule path '/r/m' failed", ); }); it("with access set, is the first line that reads as that refusal, git's own lines first", () => { const withAccess = (stderr: string, access: NonNullable) => GitCommandError.make({ kind: "failed", args: ["fetch"], cwd, stderr, exitCode: 128, access }).refusal; assert.strictEqual( withAccess( "remote: Repository not found.\nfatal: repository 'https://h/o/n/' not found\nfatal: later\n", "not-found", ), "fatal: repository 'https://h/o/n/' not found", ); assert.strictEqual( withAccess("remote: Repository not found.\nfatal: Could not read from remote repository.\n", "not-found"), "remote: Repository not found.", ); // An access value no line supports (a hand-built error) falls back to the plain rule. assert.strictEqual(withAccess("fatal: something else\n", "unreachable"), "fatal: something else"); }); it("is undefined with no such line, never picks a remote: line, and is not part of the encoded error", () => { assert.isUndefined(error("").refusal); assert.isUndefined(error("remote: fatal: denied\n").refusal); const encoded = Schema.encodeSync(GitCommandError)(error("fatal: x\n")); assert.isFalse("refusal" in encoded); }); it("reads the masked stderr: a credential never reaches it", () => { const masked = GitCommandError.make({ kind: "failed", args: ["fetch"], cwd, stderr: "fatal: unable to access 'https://@example.com/r.git/': Could not resolve host\n", }); assert.notInclude(masked.refusal ?? "", "tok"); assert.include(masked.refusal ?? "", "@"); }); });