Skip to content

Commit 07fc4cf

Browse files
committed
fix(server): stabilize non-repository Git diagnostics
1 parent fdca154 commit 07fc4cf

2 files changed

Lines changed: 90 additions & 3 deletions

File tree

‎apps/server/src/vcs/GitVcsDriverCore.test.ts‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import * as Layer from "effect/Layer";
66
import * as Path from "effect/Path";
77
import * as PlatformError from "effect/PlatformError";
88
import * as Scope from "effect/Scope";
9+
import * as Sink from "effect/Sink";
10+
import * as Stream from "effect/Stream";
11+
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
912

1013
import { GitCommandError } from "@t3tools/contracts";
1114
import { ServerConfig } from "../config.ts";
@@ -20,6 +23,21 @@ const TestLayer = GitVcsDriver.layer.pipe(
2023
Layer.provideMerge(NodeServices.layer),
2124
);
2225

26+
const makeNonRepositoryHandle = () =>
27+
ChildProcessSpawner.makeHandle({
28+
pid: ChildProcessSpawner.ProcessId(1),
29+
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(128)),
30+
isRunning: Effect.succeed(false),
31+
kill: () => Effect.void,
32+
unref: Effect.succeed(Effect.void),
33+
stdin: Sink.drain,
34+
stdout: Stream.empty,
35+
stderr: Stream.encodeText(Stream.make("fatal: not a git repository")),
36+
all: Stream.empty,
37+
getInputFd: () => Sink.drain,
38+
getOutputFd: () => Stream.empty,
39+
});
40+
2341
const makeTmpDir = (
2442
prefix = "git-vcs-driver-test-",
2543
): Effect.Effect<string, PlatformError.PlatformError, FileSystem.FileSystem | Scope.Scope> =>
@@ -77,7 +95,62 @@ const initRepoWithCommit = (
7795
return { initialBranch };
7896
});
7997

98+
it.effect("uses stable diagnostics for every parsed non-repository command", () => {
99+
const commands: Array<{ readonly args: ReadonlyArray<string>; readonly lcAll?: string }> = [];
100+
const spawner = ChildProcessSpawner.make((command) =>
101+
Effect.sync(() => {
102+
if (!ChildProcess.isStandardCommand(command)) {
103+
return assert.fail("expected a standard Git command");
104+
}
105+
commands.push({
106+
args: command.args,
107+
...(command.options.env?.LC_ALL ? { lcAll: command.options.env.LC_ALL } : {}),
108+
});
109+
return makeNonRepositoryHandle();
110+
}),
111+
);
112+
const nodeServicesLayer = Layer.merge(
113+
NodeServices.layer,
114+
Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner),
115+
);
116+
const layer = GitVcsDriver.layer.pipe(
117+
Layer.provide(ServerConfigLayer),
118+
Layer.provideMerge(nodeServicesLayer),
119+
);
120+
121+
return Effect.gen(function* () {
122+
const driver = yield* GitVcsDriver.GitVcsDriver;
123+
const cwd = "/repo";
124+
125+
yield* driver.statusDetailsLocal(cwd);
126+
yield* driver.statusDetailsRemote(cwd, { refreshUpstream: false });
127+
yield* driver.listRefs({ cwd });
128+
129+
assert.deepStrictEqual(commands, [
130+
{ args: ["status", "--porcelain=2", "--branch"], lcAll: "C" },
131+
{ args: ["rev-parse", "--abbrev-ref", "HEAD"], lcAll: "C" },
132+
{ args: ["branch", "--no-color", "--no-column"], lcAll: "C" },
133+
]);
134+
}).pipe(Effect.provide(layer));
135+
});
136+
80137
it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
138+
describe("process environment", () => {
139+
it.effect("preserves the caller locale for general Git subprocesses", () =>
140+
Effect.gen(function* () {
141+
const cwd = yield* makeTmpDir();
142+
143+
const locale = yield* git(
144+
cwd,
145+
["-c", 'alias.print-locale=!printf "%s" "$LC_ALL"', "print-locale"],
146+
{ LC_ALL: "zh_CN.UTF-8" },
147+
);
148+
149+
assert.equal(locale, "zh_CN.UTF-8");
150+
}),
151+
);
152+
});
153+
81154
describe("structured errors", () => {
82155
it.effect("preserves structured spawn context and the platform cause", () =>
83156
Effect.gen(function* () {

‎apps/server/src/vcs/GitVcsDriverCore.ts‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,20 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
830830
}),
831831
);
832832

833+
const executeGitWithStableDiagnostics = (
834+
operation: string,
835+
cwd: string,
836+
args: readonly string[],
837+
options: ExecuteGitOptions = {},
838+
): Effect.Effect<GitVcsDriver.ExecuteGitResult, GitCommandError> =>
839+
executeGit(operation, cwd, args, {
840+
...options,
841+
env: {
842+
...options.env,
843+
LC_ALL: "C",
844+
},
845+
});
846+
833847
const runGit = (
834848
operation: string,
835849
cwd: string,
@@ -1184,7 +1198,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
11841198
});
11851199

11861200
const readStatusDetailsRemote = Effect.fn("readStatusDetailsRemote")(function* (cwd: string) {
1187-
const branchResult = yield* executeGit(
1201+
const branchResult = yield* executeGitWithStableDiagnostics(
11881202
"GitVcsDriver.statusDetailsRemote.branch",
11891203
cwd,
11901204
["rev-parse", "--abbrev-ref", "HEAD"],
@@ -1304,7 +1318,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
13041318
});
13051319

13061320
const readStatusDetailsLocal = Effect.fn("readStatusDetailsLocal")(function* (cwd: string) {
1307-
const statusResult = yield* executeGit(
1321+
const statusResult = yield* executeGitWithStableDiagnostics(
13081322
"GitVcsDriver.statusDetails.status",
13091323
cwd,
13101324
["status", "--porcelain=2", "--branch"],
@@ -1985,7 +1999,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
19851999
const branchRecencyPromise = readBranchRecency(input.cwd).pipe(
19862000
Effect.orElseSucceed(() => new Map<string, number>()),
19872001
);
1988-
const localBranchResult = yield* executeGit(
2002+
const localBranchResult = yield* executeGitWithStableDiagnostics(
19892003
"GitVcsDriver.listRefs.branchNoColor",
19902004
input.cwd,
19912005
["branch", "--no-color", "--no-column"],

0 commit comments

Comments
 (0)