Files
oh-my-pi/packages/coding-agent/test/utils/git-clone.test.ts
T
roboomp 56b222f544 fix(coding-agent): clone full repo when plugin source pins a SHA
git clone --depth 1 --single-branch only fetches the tip of the
requested branch, so any subsequent git checkout <sha> for a non-tip
commit fails with 'reference is not a tree'. The error was caught and
rethrown as 'shallow clone may not contain this commit', but the clone
arguments were never adjusted.

Drop --depth 1 (and --single-branch when no ref is requested) when the
caller supplies options.sha so the desired commit is present in the
local object store. The ref-only path remains shallow.

Fixes #1589
2026-05-31 14:47:14 +00:00

79 lines
2.8 KiB
TypeScript

import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import * as fs from "node:fs/promises";
import * as os from "node:os";
import * as path from "node:path";
import * as git from "@oh-my-pi/pi-coding-agent/utils/git";
// Regression coverage for #1589: `git.clone({ sha })` used to hardcode
// `--depth 1`, producing a shallow clone whose object store never contained
// non-tip commits. The subsequent `git checkout <sha>` then failed with
// "shallow clone may not contain this commit".
const GIT_ENV = {
GIT_AUTHOR_NAME: "t",
GIT_AUTHOR_EMAIL: "t@example.com",
GIT_COMMITTER_NAME: "t",
GIT_COMMITTER_EMAIL: "t@example.com",
} as const;
function gitRun(cwd: string, args: string[]): string {
const result = Bun.spawnSync({
cmd: ["git", ...args],
cwd,
env: { ...process.env, ...GIT_ENV },
stdout: "pipe",
stderr: "pipe",
});
if (result.exitCode !== 0) {
throw new Error(`git ${args.join(" ")} failed: ${result.stderr.toString()}`);
}
return result.stdout.toString().trim();
}
describe("git.clone with options.sha", () => {
let tmpRoot: string;
let upstreamUrl: string;
let firstSha: string;
let tipSha: string;
beforeAll(async () => {
tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), "omp-git-clone-test-"));
const upstream = path.join(tmpRoot, "upstream");
await fs.mkdir(upstream, { recursive: true });
// `file://` is required: local-path clones ignore `--depth`, which would
// mask the bug. See git-clone(1) "GIT URLS" / "LOCAL PROTOCOL".
upstreamUrl = `file://${upstream}`;
gitRun(upstream, ["init", "-q", "-b", "main"]);
gitRun(upstream, ["commit", "-q", "--allow-empty", "-m", "first"]);
firstSha = gitRun(upstream, ["rev-parse", "HEAD"]);
gitRun(upstream, ["commit", "-q", "--allow-empty", "-m", "second"]);
gitRun(upstream, ["commit", "-q", "--allow-empty", "-m", "third"]);
tipSha = gitRun(upstream, ["rev-parse", "HEAD"]);
});
afterAll(async () => {
await fs.rm(tmpRoot, { recursive: true, force: true });
});
test("checks out a non-tip SHA (regression for #1589)", async () => {
const target = path.join(tmpRoot, "clone-non-tip");
await git.clone(upstreamUrl, target, { sha: firstSha });
expect(gitRun(target, ["rev-parse", "HEAD"])).toBe(firstSha);
});
test("still succeeds when SHA happens to be the tip", async () => {
const target = path.join(tmpRoot, "clone-tip");
await git.clone(upstreamUrl, target, { sha: tipSha });
expect(gitRun(target, ["rev-parse", "HEAD"])).toBe(tipSha);
});
test("cleans up the target directory when SHA does not exist", async () => {
const target = path.join(tmpRoot, "clone-missing");
await expect(git.clone(upstreamUrl, target, { sha: "0".repeat(40) })).rejects.toThrow(/Failed to checkout SHA/);
await expect(fs.stat(target)).rejects.toMatchObject({ code: "ENOENT" });
});
});