Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions src/sources/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ vi.mock("../utils/exec.js", () => ({
ExecError: Error,
}));

import { clone, headCommitDate, findCommitOlderThan } from "./git.js";
import { clone, fetchAndReset, fetchRef, headCommitDate, findCommitOlderThan } from "./git.js";
import { exec } from "../utils/exec.js";

const mockExec = vi.mocked(exec);
Expand Down Expand Up @@ -59,6 +59,7 @@ describe("clone", () => {
// Second call: fetch the specific SHA
expect(mockExec).toHaveBeenNthCalledWith(2, "git", [
"fetch",
"--force",
"--depth=1",
"--",
"origin",
Expand Down Expand Up @@ -146,6 +147,44 @@ describe("clone", () => {
});
});

describe("fetchAndReset", () => {
it("force-fetches origin before resetting to FETCH_HEAD", async () => {
await fetchAndReset("/tmp/repo");

expect(mockExec).toHaveBeenNthCalledWith(1, "git", [
"fetch",
"--force",
"--depth=1",
"--",
"origin",
], { cwd: "/tmp/repo" });
expect(mockExec).toHaveBeenNthCalledWith(2, "git", [
"reset",
"--hard",
"FETCH_HEAD",
], { cwd: "/tmp/repo" });
});
});

describe("fetchRef", () => {
it("force-fetches the requested ref before checkout", async () => {
await fetchRef("/tmp/repo", "v0");

expect(mockExec).toHaveBeenNthCalledWith(1, "git", [
"fetch",
"--force",
"--depth=1",
"--",
"origin",
"v0",
], { cwd: "/tmp/repo" });
expect(mockExec).toHaveBeenNthCalledWith(2, "git", [
"checkout",
"FETCH_HEAD",
], { cwd: "/tmp/repo" });
});
});

describe("headCommitDate", () => {
it("returns the committer date of HEAD", async () => {
mockExec.mockResolvedValueOnce({ stdout: "2026-03-15T10:30:00+00:00\n", stderr: "" });
Expand Down Expand Up @@ -175,7 +214,7 @@ describe("findCommitOlderThan", () => {
expect(mockExec).toHaveBeenNthCalledWith(
1,
"git",
["fetch", "--unshallow", "--", "origin"],
["fetch", "--force", "--unshallow", "--", "origin"],
{ cwd: "/tmp/repo" },
);
expect(mockExec).toHaveBeenNthCalledWith(
Expand Down
6 changes: 3 additions & 3 deletions src/sources/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export async function clone(
*/
export async function fetchAndReset(repoDir: string): Promise<void> {
try {
await exec("git", ["fetch", "--depth=1", "--", "origin"], { cwd: repoDir });
await exec("git", ["fetch", "--force", "--depth=1", "--", "origin"], { cwd: repoDir });
await exec("git", ["reset", "--hard", "FETCH_HEAD"], { cwd: repoDir });
} catch (err) {
if (err instanceof ExecError) {
Expand All @@ -92,7 +92,7 @@ export async function fetchAndReset(repoDir: string): Promise<void> {
*/
export async function fetchRef(repoDir: string, ref: string): Promise<void> {
try {
await exec("git", ["fetch", "--depth=1", "--", "origin", ref], {
await exec("git", ["fetch", "--force", "--depth=1", "--", "origin", ref], {
cwd: repoDir,
});
await exec("git", ["checkout", "FETCH_HEAD"], { cwd: repoDir });
Expand Down Expand Up @@ -141,7 +141,7 @@ export async function findCommitOlderThan(
// Unshallow to get full history — needed to find commits older than the cutoff.
// Only called when HEAD is too new, so the extra fetch is acceptable.
try {
await exec("git", ["fetch", "--unshallow", "--", "origin"], { cwd: repoDir });
await exec("git", ["fetch", "--force", "--unshallow", "--", "origin"], { cwd: repoDir });
} catch (err) {
if (!(err instanceof ExecError)) {throw err;}
// --unshallow fails on a complete (non-shallow) repository — that's fine
Expand Down
Loading