Skip to content
Closed
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
9 changes: 8 additions & 1 deletion apps/server/src/checkpointing/Layers/CheckpointStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { randomUUID } from "node:crypto";
import { Effect, Layer, FileSystem, Path } from "effect";

import { CheckpointInvariantError } from "../Errors.ts";
import { machineReadableGitDiffArgs } from "../../git/diffArgs.ts";
import { GitCommandError } from "../../git/Errors.ts";
import { GitServiceLive } from "../../git/Layers/GitService.ts";
import { GitService } from "../../git/Services/GitService.ts";
Expand Down Expand Up @@ -244,7 +245,13 @@ const makeCheckpointStore = Effect.gen(function* () {
const result = yield* git.execute({
operation,
cwd: input.cwd,
args: ["diff", "--patch", "--minimal", "--no-color", fromCommitOid, toCommitOid],
args: machineReadableGitDiffArgs(
"--patch",
"--minimal",
"--no-color",
fromCommitOid,
toCommitOid,
),
});

return result.stdout;
Expand Down
66 changes: 65 additions & 1 deletion apps/server/src/git/Layers/GitCore.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { existsSync } from "node:fs";
import { chmodSync, existsSync } from "node:fs";
import path from "node:path";

import * as NodeServices from "@effect/platform-node/NodeServices";
Expand Down Expand Up @@ -223,6 +223,33 @@ function commitWithDate(
});
}

function configureExternalDiff(
cwd: string,
scriptDir: string,
): Effect.Effect<
void,
GitCommandError | PlatformError.PlatformError,
GitService | FileSystem.FileSystem
> {
return Effect.gen(function* () {
const scriptPath = path.join(
scriptDir,
process.platform === "win32" ? "external-diff.cmd" : "external-diff.sh",
);
const scriptContents =
process.platform === "win32"
? "@echo off\r\necho external-diff %*\r\n"
: '#!/bin/sh\necho external-diff "$@"\n';

yield* writeTextFile(scriptPath, scriptContents);
if (process.platform !== "win32") {
chmodSync(scriptPath, 0o755);
}

yield* git(cwd, ["config", "diff.external", scriptPath]);
});
}

// ── Tests ──

it.layer(TestLayer)("git integration", (it) => {
Expand Down Expand Up @@ -1734,6 +1761,43 @@ it.layer(TestLayer)("git integration", (it) => {
}),
);

it.effect("prepareCommitContext ignores configured external diff tools", () =>
Effect.gen(function* () {
const tmp = yield* makeTmpDir();
const externalDiffDir = yield* makeTmpDir("external-diff-test-");
yield* initRepoWithCommit(tmp);
const core = yield* GitCore;

yield* configureExternalDiff(tmp, externalDiffDir);
yield* writeTextFile(path.join(tmp, "README.md"), "new content\n");

const context = yield* core.prepareCommitContext(tmp);
expect(context).not.toBeNull();
expect(context!.stagedPatch).toContain("diff --git a/README.md b/README.md");
expect(context!.stagedPatch).not.toContain("external-diff");
}),
);

it.effect("readRangeContext ignores configured external diff tools", () =>
Effect.gen(function* () {
const tmp = yield* makeTmpDir();
const externalDiffDir = yield* makeTmpDir("external-diff-test-");
const { initialBranch } = yield* initRepoWithCommit(tmp);
const core = yield* GitCore;

yield* git(tmp, ["checkout", "-b", "feature/external-diff"]);
yield* writeTextFile(path.join(tmp, "README.md"), "new content\n");
yield* git(tmp, ["add", "README.md"]);
yield* git(tmp, ["commit", "-m", "update readme"]);
yield* configureExternalDiff(tmp, externalDiffDir);

const context = yield* core.readRangeContext(tmp, initialBranch);
expect(context.diffSummary).toContain("README.md");
expect(context.diffPatch).toContain("diff --git a/README.md b/README.md");
expect(context.diffPatch).not.toContain("external-diff");
}),
);

it.effect("prepareCommitContext stages everything when filePaths is undefined", () =>
Effect.gen(function* () {
const tmp = yield* makeTmpDir();
Expand Down
55 changes: 31 additions & 24 deletions apps/server/src/git/Layers/GitCore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Cache, Data, Duration, Effect, Exit, FileSystem, Layer, Path } from "effect";

import { machineReadableGitDiffArgs } from "../diffArgs.ts";
import { GitCommandError } from "../Errors.ts";
import { GitService } from "../Services/GitService.ts";
import { GitCore, type GitCoreShape } from "../Services/GitCore.ts";
Expand Down Expand Up @@ -663,12 +664,16 @@ const makeGitCore = Effect.gen(function* () {
"--porcelain=2",
"--branch",
]),
runGitStdout("GitCore.statusDetails.unstagedNumstat", cwd, ["diff", "--numstat"]),
runGitStdout("GitCore.statusDetails.stagedNumstat", cwd, [
"diff",
"--cached",
"--numstat",
]),
runGitStdout(
"GitCore.statusDetails.unstagedNumstat",
cwd,
machineReadableGitDiffArgs("--numstat"),
),
runGitStdout(
"GitCore.statusDetails.stagedNumstat",
cwd,
machineReadableGitDiffArgs("--cached", "--numstat"),
),
],
{ concurrency: "unbounded" },
);
Expand Down Expand Up @@ -782,21 +787,20 @@ const makeGitCore = Effect.gen(function* () {
yield* runGit("GitCore.prepareCommitContext.addAll", cwd, ["add", "-A"]);
}

const stagedSummary = yield* runGitStdout("GitCore.prepareCommitContext.stagedSummary", cwd, [
"diff",
"--cached",
"--name-status",
]).pipe(Effect.map((stdout) => stdout.trim()));
const stagedSummary = yield* runGitStdout(
"GitCore.prepareCommitContext.stagedSummary",
cwd,
machineReadableGitDiffArgs("--cached", "--name-status"),
).pipe(Effect.map((stdout) => stdout.trim()));
if (stagedSummary.length === 0) {
return null;
}

const stagedPatch = yield* runGitStdout("GitCore.prepareCommitContext.stagedPatch", cwd, [
"diff",
"--cached",
"--patch",
"--minimal",
]);
const stagedPatch = yield* runGitStdout(
"GitCore.prepareCommitContext.stagedPatch",
cwd,
machineReadableGitDiffArgs("--cached", "--patch", "--minimal"),
);

return {
stagedSummary,
Expand Down Expand Up @@ -970,13 +974,16 @@ const makeGitCore = Effect.gen(function* () {
const [commitSummary, diffSummary, diffPatch] = yield* Effect.all(
[
runGitStdout("GitCore.readRangeContext.log", cwd, ["log", "--oneline", range]),
runGitStdout("GitCore.readRangeContext.diffStat", cwd, ["diff", "--stat", range]),
runGitStdout("GitCore.readRangeContext.diffPatch", cwd, [
"diff",
"--patch",
"--minimal",
range,
]),
runGitStdout(
"GitCore.readRangeContext.diffStat",
cwd,
machineReadableGitDiffArgs("--stat", range),
),
runGitStdout(
"GitCore.readRangeContext.diffPatch",
cwd,
machineReadableGitDiffArgs("--patch", "--minimal", range),
),
],
{ concurrency: "unbounded" },
);
Expand Down
3 changes: 3 additions & 0 deletions apps/server/src/git/diffArgs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function machineReadableGitDiffArgs(...args: ReadonlyArray<string>): ReadonlyArray<string> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this helper really provides value since it increases the overall complexity by making you need to go to a different file to figure out what we're actually calling

imo you should just add the --no-ext-diff flag to the existing diff command call sites rather than trying to decrease duplication with a helper

return ["diff", "--no-ext-diff", ...args];
}