Skip to content

Commit 6dbb73b

Browse files
committed
fix(server): preserve sparse deletions and racy edits
1 parent 61be1af commit 6dbb73b

2 files changed

Lines changed: 78 additions & 17 deletions

File tree

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

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,13 @@ for (const nested of [false, true]) {
276276
}
277277

278278
for (const sparseIndex of [false, true]) {
279-
for (const indexState of ["normal", "assumed", "missing", "invalid"] as const) {
279+
for (const indexState of [
280+
"normal",
281+
"assumed",
282+
"assumed-deleted",
283+
"missing",
284+
"invalid",
285+
] as const) {
280286
it.effect(
281287
`checkpoint capture preserves sparse files (sparseIndex=${sparseIndex}, index=${indexState})`,
282288
() =>
@@ -290,7 +296,7 @@ for (const sparseIndex of [false, true]) {
290296
const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
291297
const write = (name: string, contents: string) =>
292298
fileSystem.writeFileString(path.join(cwd, name), contents);
293-
for (const directory of ["included", "excluded"]) {
299+
for (const directory of ["included", "included/empty", "excluded"]) {
294300
yield* fileSystem.makeDirectory(path.join(cwd, directory));
295301
yield* write(`${directory}/tracked`, "original\n");
296302
}
@@ -299,13 +305,25 @@ for (const sparseIndex of [false, true]) {
299305
yield* git(["commit", "-m", "sparse fixture"]);
300306
yield* git([
301307
"sparse-checkout",
302-
"set",
308+
"init",
303309
"--cone",
304310
sparseIndex ? "--sparse-index" : "--no-sparse-index",
305-
"included",
306311
]);
312+
yield* git(["sparse-checkout", "set", "included"]);
307313
if (indexState === "assumed") {
314+
yield* git(["config", "core.trustctime", "false"]);
315+
yield* fileSystem.utimes(
316+
path.join(cwd, "included/tracked"),
317+
1_700_000_000,
318+
1_700_000_000,
319+
);
320+
yield* git(["add", "included/tracked"]);
321+
}
322+
if (indexState === "assumed" || indexState === "assumed-deleted") {
308323
yield* git(["update-index", "--assume-unchanged", "included/tracked"]);
324+
if (indexState === "assumed-deleted") {
325+
yield* git(["update-index", "--assume-unchanged", "included/deleted"]);
326+
}
309327
} else {
310328
yield* write("included/tracked", "staged\n");
311329
yield* git(["add", "included/tracked"]);
@@ -314,11 +332,24 @@ for (const sparseIndex of [false, true]) {
314332
if (indexState === "missing") yield* fileSystem.remove(indexPath);
315333
if (indexState === "invalid")
316334
yield* fileSystem.writeFileString(indexPath, "invalid index");
335+
if (indexState === "assumed") {
336+
yield* fileSystem.utimes(indexPath, 1_700_000_000, 1_700_000_000);
337+
}
317338
const originalIndex = yield* fileSystem
318339
.readFile(indexPath)
319340
.pipe(Effect.orElseSucceed(() => null));
320-
yield* write("included/tracked", "working\n");
321-
yield* fileSystem.remove(path.join(cwd, "included/deleted"));
341+
yield* write("included/tracked", "modified\n");
342+
if (indexState === "assumed") {
343+
yield* fileSystem.utimes(
344+
path.join(cwd, "included/tracked"),
345+
1_700_000_000,
346+
1_700_000_000,
347+
);
348+
}
349+
if (indexState !== "assumed") {
350+
yield* fileSystem.remove(path.join(cwd, "included/deleted"));
351+
yield* fileSystem.remove(path.join(cwd, "included/empty"), { recursive: true });
352+
}
322353
yield* write("included/new", "new\n");
323354
yield* fileSystem.makeDirectory(path.join(cwd, "excluded"), { recursive: true });
324355
yield* write("excluded/artifact", "artifact\n");
@@ -327,7 +358,7 @@ for (const sparseIndex of [false, true]) {
327358

328359
assert.strictEqual(
329360
(yield* git(["show", `${checkpointRef}:included/tracked`])).stdout,
330-
"working\n",
361+
"modified\n",
331362
);
332363
assert.strictEqual(
333364
(yield* git(["show", `${checkpointRef}:included/new`])).stdout,
@@ -341,9 +372,17 @@ for (const sparseIndex of [false, true]) {
341372
(yield* git(["show", `${checkpointRef}:excluded/artifact`])).stdout,
342373
"artifact\n",
343374
);
344-
assert.notInclude(
345-
(yield* git(["ls-tree", "-r", "--name-only", checkpointRef])).stdout,
346-
"included/deleted",
375+
assert.strictEqual(
376+
(yield* git(["ls-tree", "-r", "--name-only", checkpointRef])).stdout.includes(
377+
"included/deleted",
378+
),
379+
indexState === "assumed",
380+
);
381+
assert.strictEqual(
382+
(yield* git(["ls-tree", "-r", "--name-only", checkpointRef])).stdout.includes(
383+
"included/empty/tracked",
384+
),
385+
indexState === "assumed",
347386
);
348387
assert.isFalse(yield* fileSystem.exists(path.join(cwd, "excluded/tracked")));
349388
assert.deepEqual(

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

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -782,8 +782,9 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
782782
}).pipe(Effect.map((result) => result.stdout.trim() === "true")),
783783
).pipe(Effect.map((values) => values.every(Boolean)));
784784
const headExists = yield* hasHeadCommit(input.cwd);
785+
let reusedIndex = false;
785786
if (headExists) {
786-
const reusedIndex = yield* Effect.gen(function* () {
787+
reusedIndex = yield* Effect.gen(function* () {
787788
const indexPath = yield* execute({
788789
operation,
789790
cwd: input.cwd,
@@ -803,6 +804,7 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
803804
env: commitEnv,
804805
});
805806
if (sparseCone) {
807+
yield* fileSystem.utimes(tempIndexPath, indexTime, indexTime);
806808
// Refresh assumed-unchanged files without listing or discarding sparse entries.
807809
yield* execute({
808810
operation,
@@ -814,10 +816,18 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
814816
"update-index",
815817
"-q",
816818
"--really-refresh",
817-
"--ignore-missing",
818819
],
819820
env: commitEnv,
820821
});
822+
// Refresh leaves missing assumed-unchanged entries hidden from add.
823+
const deleted = yield* execute({
824+
operation,
825+
cwd: input.cwd,
826+
args: ["ls-files", "--deleted", "-z", "--", "."],
827+
env: commitEnv,
828+
maxOutputBytes: 1,
829+
});
830+
if (deleted.stdout.length > 0 || deleted.stdoutTruncated) return false;
821831
}
822832
// read-tree can rewrite the index, so restore its racy timestamp afterward.
823833
yield* fileSystem.utimes(tempIndexPath, indexTime, indexTime);
@@ -837,10 +847,7 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
837847
yield* execute({
838848
operation,
839849
cwd: input.cwd,
840-
// Sparse-index conversion reapplies cone flags without touching working files.
841-
args: sparseCone
842-
? ["-c", "index.sparse=true", "read-tree", "--reset", "HEAD"]
843-
: ["read-tree", "HEAD"],
850+
args: ["read-tree", "HEAD"],
844851
env: commitEnv,
845852
});
846853
}
@@ -849,9 +856,24 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
849856
yield* execute({
850857
operation,
851858
cwd: input.cwd,
852-
args: ["add", "-A", ...(sparseCone ? ["--sparse"] : []), "--", "."],
859+
args: [
860+
"add",
861+
...(sparseCone ? ["--sparse"] : []),
862+
sparseCone && !reusedIndex ? "--ignore-removal" : "-A",
863+
"--",
864+
".",
865+
],
853866
env: commitEnv,
854867
});
868+
if (sparseCone && !reusedIndex) {
869+
// Outside-cone files are now indexed; only apply deletions inside the cone.
870+
yield* execute({
871+
operation,
872+
cwd: input.cwd,
873+
args: ["add", "-A", "--", "."],
874+
env: commitEnv,
875+
});
876+
}
855877

856878
const writeTreeResult = yield* execute({
857879
operation,

0 commit comments

Comments
 (0)