Skip to content

Commit a91c9a4

Browse files
committed
fix(server): skip git status scans while the index is locked
1 parent 0dd5c64 commit a91c9a4

2 files changed

Lines changed: 83 additions & 0 deletions

File tree

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as Layer from "effect/Layer";
1111
import * as Path from "effect/Path";
1212
import * as PlatformError from "effect/PlatformError";
1313
import * as Ref from "effect/Ref";
14+
import * as Result from "effect/Result";
1415
import * as Scope from "effect/Scope";
1516
import * as Sink from "effect/Sink";
1617
import * as Stream from "effect/Stream";
@@ -131,6 +132,51 @@ const initRepoWithCommit = (
131132
return { initialBranch };
132133
});
133134

135+
for (const location of ["root", "nested", "worktree"] as const) {
136+
it.effect(
137+
`skips clean filters while the ${location} index is locked and resumes after unlock`,
138+
() =>
139+
Effect.gen(function* () {
140+
const driver = yield* GitVcsDriver.GitVcsDriver;
141+
const fs = yield* FileSystem.FileSystem;
142+
const path = yield* Path.Path;
143+
const repository = yield* makeTmpDir();
144+
yield* initRepoWithCommit(repository);
145+
const cwd = location === "worktree" ? yield* makeTmpDir() : repository;
146+
if (location === "worktree") {
147+
yield* git(repository, ["worktree", "add", "--detach", cwd]);
148+
}
149+
yield* git(cwd, ["config", "filter.probe.clean", "echo clean >> .filter-runs; cat"]);
150+
yield* writeTextFile(cwd, ".gitattributes", "asset.bin filter=probe\n");
151+
yield* writeTextFile(cwd, ".gitignore", ".filter-runs\n");
152+
yield* writeTextFile(cwd, "asset.bin", "original\n");
153+
yield* git(cwd, ["add", "."]);
154+
yield* git(cwd, ["commit", "-m", "filtered asset"]);
155+
NodeFS.utimesSync(path.join(cwd, "asset.bin"), 1, 1);
156+
const runsPath = path.join(cwd, ".filter-runs");
157+
yield* fs.remove(runsPath, { force: true });
158+
const indexPath = yield* git(cwd, ["rev-parse", "--git-path", "index"]);
159+
const lockPath = `${path.resolve(cwd, indexPath)}.lock`;
160+
yield* fs.writeFileString(lockPath, "");
161+
const statusCwd = location === "nested" ? path.join(cwd, "nested") : cwd;
162+
yield* fs.makeDirectory(statusCwd, { recursive: true });
163+
164+
for (let poll = 0; poll < 3; poll++) {
165+
const result = yield* driver.statusDetailsLocal(statusCwd).pipe(Effect.result);
166+
assert.isTrue(Result.isFailure(result));
167+
if (Result.isFailure(result)) assert.include(result.failure.detail, "index is locked");
168+
}
169+
assert.isFalse(yield* fs.exists(runsPath));
170+
assert.isTrue(yield* fs.exists(lockPath));
171+
172+
yield* fs.remove(lockPath);
173+
const status = yield* driver.statusDetailsLocal(statusCwd);
174+
assert.isFalse(status.hasWorkingTreeChanges);
175+
assert.include(yield* fs.readFileString(runsPath), "clean");
176+
}).pipe(Effect.provide(TestLayer)),
177+
);
178+
}
179+
134180
it.effect("uses stable diagnostics for every parsed non-repository command", () => {
135181
const commands: Array<{ readonly args: ReadonlyArray<string>; readonly lcAll?: string }> = [];
136182
const spawner = ChildProcessSpawner.make((command) =>
@@ -163,6 +209,7 @@ it.effect("uses stable diagnostics for every parsed non-repository command", ()
163209
yield* driver.listRefs({ cwd });
164210

165211
assert.deepStrictEqual(commands, [
212+
{ args: ["rev-parse", "--git-path", "index"], lcAll: "C" },
166213
{ args: ["status", "--porcelain=2", "--branch"], lcAll: "C" },
167214
{ args: ["rev-parse", "--abbrev-ref", "HEAD"], lcAll: "C" },
168215
{ args: ["rev-parse", "--git-common-dir"], lcAll: "C" },

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,6 +1584,42 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
15841584
});
15851585

15861586
const readStatusDetailsLocal = Effect.fn("readStatusDetailsLocal")(function* (cwd: string) {
1587+
const indexResult = yield* executeGitWithStableDiagnostics(
1588+
"GitVcsDriver.statusDetails.indexPath",
1589+
cwd,
1590+
["rev-parse", "--git-path", "index"],
1591+
{ allowNonZeroExit: true },
1592+
).pipe(
1593+
Effect.catchTags({
1594+
GitCommandError: (error) =>
1595+
isMissingGitCwdError(error) ? Effect.succeed(null) : Effect.fail(error),
1596+
}),
1597+
);
1598+
if (indexResult === null) return NON_REPOSITORY_STATUS_DETAILS;
1599+
if (indexResult.exitCode === 0) {
1600+
const lockPath = `${path.resolve(cwd, indexResult.stdout.trim())}.lock`;
1601+
const lockError = new GitCommandError({
1602+
operation: "GitVcsDriver.statusDetails.indexPath",
1603+
command: "git",
1604+
cwd,
1605+
detail: "Git index is locked. Status will resume when the index lock is removed.",
1606+
});
1607+
// Status can succeed while locked, repeatedly running LFS clean filters without caching.
1608+
if (
1609+
yield* fileSystem.exists(lockPath).pipe(
1610+
Effect.mapError(
1611+
(cause) =>
1612+
new GitCommandError({
1613+
...lockError,
1614+
detail: "Failed to check the Git index lock.",
1615+
cause,
1616+
}),
1617+
),
1618+
)
1619+
) {
1620+
return yield* lockError;
1621+
}
1622+
}
15871623
const statusResult = yield* executeGitWithStableDiagnostics(
15881624
"GitVcsDriver.statusDetails.status",
15891625
cwd,

0 commit comments

Comments
 (0)