Skip to content

Commit e7ac911

Browse files
committed
fix(server): preserve nested repositories during checkpoint cleanup
1 parent 6bd5263 commit e7ac911

2 files changed

Lines changed: 52 additions & 15 deletions

File tree

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ it.effect("checkpoint capture skips untracked nested repositories without a comm
193193
`160000 commit ${nestedHead}\tcommitted\n`,
194194
);
195195
assert.deepEqual(yield* fileSystem.readFile(path.join(cwd, ".git", "index")), originalIndex);
196+
assert.isTrue(yield* driver.checkpoints.restoreCheckpoint({ cwd, checkpointRef }));
196197
assert.strictEqual(
197198
yield* fileSystem.readFileString(path.join(cwd, nested, "private.txt")),
198199
"nested\n",
@@ -1102,6 +1103,9 @@ it.effect("restores empty checkpoints without changing paths outside the workspa
11021103
});
11031104
assert.strictEqual(staged.stdout.trim(), "outside.txt");
11041105
}
1106+
yield* fileSystem.writeFileString(path.join(cwd, "checkpoint.txt"), "checkpoint\n");
1107+
yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
1108+
yield* fileSystem.writeFileString(path.join(cwd, "checkpoint.txt"), "later\n");
11051109
let deepest = cwd;
11061110
for (let depth = 0; depth < 4; depth++) {
11071111
yield* fileSystem.writeFileString(path.join(deepest, ".gitignore"), "d/.gitignore\n");
@@ -1114,6 +1118,28 @@ it.effect("restores empty checkpoints without changing paths outside the workspa
11141118
.restoreCheckpoint({ cwd, checkpointRef, fallbackToHead: false })
11151119
.pipe(Effect.flip);
11161120
assert.strictEqual(incomplete._tag, "VcsProcessExitError");
1121+
assert.strictEqual(
1122+
yield* fileSystem.readFileString(path.join(cwd, "checkpoint.txt")),
1123+
"checkpoint\n",
1124+
);
1125+
assert.strictEqual(
1126+
(yield* driver.execute({
1127+
operation: "test",
1128+
cwd,
1129+
args: ["diff", "--cached", "--name-only", "--", "."],
1130+
})).stdout,
1131+
"",
1132+
);
1133+
if (nested) {
1134+
assert.strictEqual(
1135+
(yield* driver.execute({
1136+
operation: "test",
1137+
cwd: root,
1138+
args: ["diff", "--cached", "--name-only"],
1139+
})).stdout.trim(),
1140+
"outside.txt",
1141+
);
1142+
}
11171143
}
11181144
}).pipe(Effect.scoped, Effect.provide(layerGitContract)),
11191145
);

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

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,34 +1295,53 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
12951295
}),
12961296
),
12971297
);
1298+
const resetIndex = Effect.gen(function* () {
1299+
// On Windows clean can remove './' and exit 0.
1300+
yield* recreateWorkspace;
1301+
if (yield* hasHeadCommit(input.cwd)) {
1302+
yield* execute({
1303+
operation,
1304+
cwd: input.cwd,
1305+
args: [...longPaths, "reset", "--quiet", "--", "."],
1306+
});
1307+
}
1308+
});
12981309
yield* recreateWorkspace;
12991310
const clean = execute({
13001311
operation,
13011312
cwd: input.cwd,
13021313
args: [...longPaths, "clean", "-fd", "--", "."],
1314+
env: { LC_ALL: "C" },
13031315
allowNonZeroExit: true,
13041316
});
13051317
let cleaned = yield* clean;
13061318
// Removing a new .gitignore exposes the files it hid; clean again until nothing is left.
1307-
for (let pass = 1; pass < 4 && cleaned.exitCode === 0 && cleaned.stdout.length > 0; pass++) {
1319+
for (
1320+
let pass = 1;
1321+
pass < 4 && cleaned.exitCode === 0 && /^Removing /m.test(cleaned.stdout);
1322+
pass++
1323+
) {
13081324
yield* recreateWorkspace;
13091325
cleaned = yield* clean;
13101326
}
1311-
if (cleaned.exitCode === 0 && cleaned.stdout.length > 0) {
1327+
if (cleaned.exitCode === 0 && /^Removing /m.test(cleaned.stdout)) {
13121328
yield* recreateWorkspace;
13131329
const remaining = yield* execute({
13141330
operation,
13151331
cwd: input.cwd,
13161332
args: [...longPaths, "clean", "-dn", "--", "."],
1333+
env: { LC_ALL: "C" },
13171334
});
1318-
if (remaining.stdout.length > 0)
1335+
if (/^Would remove /m.test(remaining.stdout)) {
1336+
yield* resetIndex;
13191337
return yield* new VcsProcessExitError({
13201338
operation,
13211339
command: "git clean",
13221340
cwd: input.cwd,
13231341
exitCode: 0,
13241342
detail: "Could not clean the checkpoint workspace: untracked files remain.",
13251343
});
1344+
}
13261345
}
13271346
if (cleaned.exitCode !== 0) {
13281347
// Git can remove every child, then fail trying to remove './' itself.
@@ -1333,26 +1352,18 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
13331352
Effect.map((entries) => entries.length === 0),
13341353
Effect.orElseSucceed(() => false),
13351354
));
1336-
if (!emptiedWorkspace)
1355+
if (!emptiedWorkspace) {
1356+
yield* resetIndex;
13371357
return yield* new VcsProcessExitError({
13381358
operation,
13391359
command: "git clean",
13401360
cwd: input.cwd,
13411361
exitCode: cleaned.exitCode,
13421362
detail: cleaned.stderr.trim() || "Could not clean the checkpoint workspace.",
13431363
});
1364+
}
13441365
}
1345-
// On Windows the same clean can remove './' and exit 0.
1346-
yield* recreateWorkspace;
1347-
1348-
const headExists = yield* hasHeadCommit(input.cwd);
1349-
if (headExists) {
1350-
yield* execute({
1351-
operation,
1352-
cwd: input.cwd,
1353-
args: [...longPaths, "reset", "--quiet", "--", "."],
1354-
});
1355-
}
1366+
yield* resetIndex;
13561367

13571368
return true;
13581369
}),

0 commit comments

Comments
 (0)