Skip to content

Commit 115cf97

Browse files
fix(git): disable external diff tools for app diffs
1 parent 2bb71f4 commit 115cf97

4 files changed

Lines changed: 107 additions & 26 deletions

File tree

‎apps/server/src/checkpointing/Layers/CheckpointStore.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { randomUUID } from "node:crypto";
1414
import { Effect, Layer, FileSystem, Path } from "effect";
1515

1616
import { CheckpointInvariantError } from "../Errors.ts";
17+
import { machineReadableGitDiffArgs } from "../../git/diffArgs.ts";
1718
import { GitCommandError } from "../../git/Errors.ts";
1819
import { GitServiceLive } from "../../git/Layers/GitService.ts";
1920
import { GitService } from "../../git/Services/GitService.ts";
@@ -244,7 +245,13 @@ const makeCheckpointStore = Effect.gen(function* () {
244245
const result = yield* git.execute({
245246
operation,
246247
cwd: input.cwd,
247-
args: ["diff", "--patch", "--minimal", "--no-color", fromCommitOid, toCommitOid],
248+
args: machineReadableGitDiffArgs(
249+
"--patch",
250+
"--minimal",
251+
"--no-color",
252+
fromCommitOid,
253+
toCommitOid,
254+
),
248255
});
249256

250257
return result.stdout;

‎apps/server/src/git/Layers/GitCore.test.ts‎

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { existsSync } from "node:fs";
1+
import { chmodSync, existsSync } from "node:fs";
22
import path from "node:path";
33

44
import * as NodeServices from "@effect/platform-node/NodeServices";
@@ -223,6 +223,33 @@ function commitWithDate(
223223
});
224224
}
225225

226+
function configureExternalDiff(
227+
cwd: string,
228+
scriptDir: string,
229+
): Effect.Effect<
230+
void,
231+
GitCommandError | PlatformError.PlatformError,
232+
GitService | FileSystem.FileSystem
233+
> {
234+
return Effect.gen(function* () {
235+
const scriptPath = path.join(
236+
scriptDir,
237+
process.platform === "win32" ? "external-diff.cmd" : "external-diff.sh",
238+
);
239+
const scriptContents =
240+
process.platform === "win32"
241+
? "@echo off\r\necho external-diff %*\r\n"
242+
: '#!/bin/sh\necho external-diff "$@"\n';
243+
244+
yield* writeTextFile(scriptPath, scriptContents);
245+
if (process.platform !== "win32") {
246+
chmodSync(scriptPath, 0o755);
247+
}
248+
249+
yield* git(cwd, ["config", "diff.external", scriptPath]);
250+
});
251+
}
252+
226253
// ── Tests ──
227254

228255
it.layer(TestLayer)("git integration", (it) => {
@@ -1734,6 +1761,43 @@ it.layer(TestLayer)("git integration", (it) => {
17341761
}),
17351762
);
17361763

1764+
it.effect("prepareCommitContext ignores configured external diff tools", () =>
1765+
Effect.gen(function* () {
1766+
const tmp = yield* makeTmpDir();
1767+
const externalDiffDir = yield* makeTmpDir("external-diff-test-");
1768+
yield* initRepoWithCommit(tmp);
1769+
const core = yield* GitCore;
1770+
1771+
yield* configureExternalDiff(tmp, externalDiffDir);
1772+
yield* writeTextFile(path.join(tmp, "README.md"), "new content\n");
1773+
1774+
const context = yield* core.prepareCommitContext(tmp);
1775+
expect(context).not.toBeNull();
1776+
expect(context!.stagedPatch).toContain("diff --git a/README.md b/README.md");
1777+
expect(context!.stagedPatch).not.toContain("external-diff");
1778+
}),
1779+
);
1780+
1781+
it.effect("readRangeContext ignores configured external diff tools", () =>
1782+
Effect.gen(function* () {
1783+
const tmp = yield* makeTmpDir();
1784+
const externalDiffDir = yield* makeTmpDir("external-diff-test-");
1785+
const { initialBranch } = yield* initRepoWithCommit(tmp);
1786+
const core = yield* GitCore;
1787+
1788+
yield* git(tmp, ["checkout", "-b", "feature/external-diff"]);
1789+
yield* writeTextFile(path.join(tmp, "README.md"), "new content\n");
1790+
yield* git(tmp, ["add", "README.md"]);
1791+
yield* git(tmp, ["commit", "-m", "update readme"]);
1792+
yield* configureExternalDiff(tmp, externalDiffDir);
1793+
1794+
const context = yield* core.readRangeContext(tmp, initialBranch);
1795+
expect(context.diffSummary).toContain("README.md");
1796+
expect(context.diffPatch).toContain("diff --git a/README.md b/README.md");
1797+
expect(context.diffPatch).not.toContain("external-diff");
1798+
}),
1799+
);
1800+
17371801
it.effect("prepareCommitContext stages everything when filePaths is undefined", () =>
17381802
Effect.gen(function* () {
17391803
const tmp = yield* makeTmpDir();

‎apps/server/src/git/Layers/GitCore.ts‎

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Cache, Data, Duration, Effect, Exit, FileSystem, Layer, Path } from "effect";
22

3+
import { machineReadableGitDiffArgs } from "../diffArgs.ts";
34
import { GitCommandError } from "../Errors.ts";
45
import { GitService } from "../Services/GitService.ts";
56
import { GitCore, type GitCoreShape } from "../Services/GitCore.ts";
@@ -663,12 +664,16 @@ const makeGitCore = Effect.gen(function* () {
663664
"--porcelain=2",
664665
"--branch",
665666
]),
666-
runGitStdout("GitCore.statusDetails.unstagedNumstat", cwd, ["diff", "--numstat"]),
667-
runGitStdout("GitCore.statusDetails.stagedNumstat", cwd, [
668-
"diff",
669-
"--cached",
670-
"--numstat",
671-
]),
667+
runGitStdout(
668+
"GitCore.statusDetails.unstagedNumstat",
669+
cwd,
670+
machineReadableGitDiffArgs("--numstat"),
671+
),
672+
runGitStdout(
673+
"GitCore.statusDetails.stagedNumstat",
674+
cwd,
675+
machineReadableGitDiffArgs("--cached", "--numstat"),
676+
),
672677
],
673678
{ concurrency: "unbounded" },
674679
);
@@ -782,21 +787,20 @@ const makeGitCore = Effect.gen(function* () {
782787
yield* runGit("GitCore.prepareCommitContext.addAll", cwd, ["add", "-A"]);
783788
}
784789

785-
const stagedSummary = yield* runGitStdout("GitCore.prepareCommitContext.stagedSummary", cwd, [
786-
"diff",
787-
"--cached",
788-
"--name-status",
789-
]).pipe(Effect.map((stdout) => stdout.trim()));
790+
const stagedSummary = yield* runGitStdout(
791+
"GitCore.prepareCommitContext.stagedSummary",
792+
cwd,
793+
machineReadableGitDiffArgs("--cached", "--name-status"),
794+
).pipe(Effect.map((stdout) => stdout.trim()));
790795
if (stagedSummary.length === 0) {
791796
return null;
792797
}
793798

794-
const stagedPatch = yield* runGitStdout("GitCore.prepareCommitContext.stagedPatch", cwd, [
795-
"diff",
796-
"--cached",
797-
"--patch",
798-
"--minimal",
799-
]);
799+
const stagedPatch = yield* runGitStdout(
800+
"GitCore.prepareCommitContext.stagedPatch",
801+
cwd,
802+
machineReadableGitDiffArgs("--cached", "--patch", "--minimal"),
803+
);
800804

801805
return {
802806
stagedSummary,
@@ -970,13 +974,16 @@ const makeGitCore = Effect.gen(function* () {
970974
const [commitSummary, diffSummary, diffPatch] = yield* Effect.all(
971975
[
972976
runGitStdout("GitCore.readRangeContext.log", cwd, ["log", "--oneline", range]),
973-
runGitStdout("GitCore.readRangeContext.diffStat", cwd, ["diff", "--stat", range]),
974-
runGitStdout("GitCore.readRangeContext.diffPatch", cwd, [
975-
"diff",
976-
"--patch",
977-
"--minimal",
978-
range,
979-
]),
977+
runGitStdout(
978+
"GitCore.readRangeContext.diffStat",
979+
cwd,
980+
machineReadableGitDiffArgs("--stat", range),
981+
),
982+
runGitStdout(
983+
"GitCore.readRangeContext.diffPatch",
984+
cwd,
985+
machineReadableGitDiffArgs("--patch", "--minimal", range),
986+
),
980987
],
981988
{ concurrency: "unbounded" },
982989
);

‎apps/server/src/git/diffArgs.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function machineReadableGitDiffArgs(...args: ReadonlyArray<string>): ReadonlyArray<string> {
2+
return ["diff", "--no-ext-diff", ...args];
3+
}

0 commit comments

Comments
 (0)