Skip to content

Commit 1a4a299

Browse files
fix(server): reverting to a checkpoint restores skip-worktree files (#36308)
1 parent 41e69ff commit 1a4a299

2 files changed

Lines changed: 161 additions & 10 deletions

File tree

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

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,87 @@ it.effect("restores a checkpoint before the first commit without staging its fil
12731273
}).pipe(Effect.scoped, Effect.provide(layerGitContract)),
12741274
);
12751275

1276+
it.effect("restores edits to skip-worktree files whose checkpoint content matches HEAD", () =>
1277+
Effect.gen(function* () {
1278+
const fileSystem = yield* FileSystem.FileSystem;
1279+
const path = yield* Path.Path;
1280+
const driver = yield* GitVcsDriver.makeVcsDriverShape();
1281+
const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-skip-worktree-restore-" });
1282+
const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
1283+
const skippedPath = path.join(cwd, "skipped.txt");
1284+
yield* fileSystem.writeFileString(skippedPath, "original\n");
1285+
yield* git(["add", "skipped.txt"]);
1286+
yield* git(["commit", "-m", "add skipped file"]);
1287+
yield* git(["update-index", "--skip-worktree", "skipped.txt"]);
1288+
yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
1289+
yield* fileSystem.writeFileString(skippedPath, "later\n");
1290+
1291+
assert.isTrue(
1292+
yield* driver.checkpoints.restoreCheckpoint({ cwd, checkpointRef, fallbackToHead: false }),
1293+
);
1294+
1295+
assert.strictEqual(yield* fileSystem.readFileString(skippedPath), "original\n");
1296+
}).pipe(Effect.scoped, Effect.provide(layerGitContract)),
1297+
);
1298+
1299+
it.effect("sparse restore rewrites manual skip-worktree files and keeps exclusions absent", () =>
1300+
Effect.gen(function* () {
1301+
const fs = yield* FileSystem.FileSystem;
1302+
const path = yield* Path.Path;
1303+
const liveProcess = yield* VcsProcess.VcsProcess;
1304+
// One byte per chunk, so the index listing splits inside every record.
1305+
const driver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
1306+
Effect.provideService(VcsProcess.VcsProcess, {
1307+
run: (input) => {
1308+
const onStdoutChunk = input.onStdoutChunk;
1309+
return liveProcess.run(
1310+
onStdoutChunk === undefined
1311+
? input
1312+
: {
1313+
...input,
1314+
onStdoutChunk: (chunk) => {
1315+
for (let index = 0; index < chunk.length; index++)
1316+
onStdoutChunk(chunk.subarray(index, index + 1));
1317+
},
1318+
},
1319+
);
1320+
},
1321+
}),
1322+
);
1323+
const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-sparse-restore-" });
1324+
const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
1325+
for (const name of ["in/normal", "in/skipped", "in/s space", "out/deep/absent", "out/stray"]) {
1326+
yield* fs.makeDirectory(path.dirname(path.join(cwd, name)), { recursive: true });
1327+
yield* fs.writeFileString(path.join(cwd, name), "original\n");
1328+
}
1329+
yield* git(["add", "."]);
1330+
yield* git(["commit", "-m", "sparse fixture"]);
1331+
yield* git(["sparse-checkout", "set", "--cone", "--sparse-index", "in"]);
1332+
// Keeps Git from clearing the bit on files that are present.
1333+
yield* git(["config", "sparse.expectFilesOutsideOfPatterns", "true"]);
1334+
yield* git(["update-index", "--skip-worktree", "in/skipped", "in/s space"]);
1335+
const captureCwd = path.join(cwd, "in");
1336+
yield* driver.checkpoints.captureCheckpoint({ cwd: captureCwd, checkpointRef });
1337+
yield* fs.writeFileString(path.join(cwd, "in/skipped"), "later\n");
1338+
yield* fs.writeFileString(path.join(cwd, "in/s space"), "later\n");
1339+
yield* fs.writeFileString(path.join(cwd, "in/normal"), "later\n");
1340+
1341+
for (const restoreCwd of [captureCwd, cwd]) {
1342+
assert.isTrue(
1343+
yield* driver.checkpoints.restoreCheckpoint({
1344+
cwd: restoreCwd,
1345+
checkpointRef,
1346+
fallbackToHead: false,
1347+
}),
1348+
);
1349+
assert.strictEqual(yield* fs.readFileString(path.join(cwd, "in/skipped")), "original\n");
1350+
assert.strictEqual(yield* fs.readFileString(path.join(cwd, "in/s space")), "original\n");
1351+
assert.strictEqual(yield* fs.readFileString(path.join(cwd, "in/normal")), "original\n");
1352+
assert.isFalse(yield* fs.exists(path.join(cwd, "out")));
1353+
}
1354+
}).pipe(Effect.scoped, Effect.provide(layerGitContract)),
1355+
);
1356+
12761357
// The real VCS process, spawning `command` wherever the driver runs git.
12771358
const vcsProcessSpawning = (command: string) =>
12781359
Layer.effect(

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

Lines changed: 80 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,20 +1390,90 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
13901390
});
13911391
// An empty index and checkpoint have nothing for git restore's pathspec to match.
13921392
if (tracked.stdout.length > 0) {
1393+
const sparseConfig = yield* execute({
1394+
operation,
1395+
cwd: input.cwd,
1396+
args: ["config", "--bool", "core.sparseCheckout"],
1397+
allowNonZeroExit: true,
1398+
});
1399+
const sparseCheckout = sparseConfig.stdout.trim() === "true";
1400+
const restore = [...longPaths, "restore", "--source", commitOid, "--worktree", "--staged"];
1401+
// restore leaves skip-worktree paths alone. Outside a sparse checkout every such bit was
1402+
// set by hand and capture recorded the file, so override them all.
13931403
yield* execute({
13941404
operation,
13951405
cwd: input.cwd,
1396-
args: [
1397-
...longPaths,
1398-
"restore",
1399-
"--source",
1400-
commitOid,
1401-
"--worktree",
1402-
"--staged",
1403-
"--",
1404-
".",
1405-
],
1406+
args: [...restore, ...(sparseCheckout ? [] : ["--ignore-skip-worktree-bits"]), "--", "."],
14061407
});
1408+
if (sparseCheckout) {
1409+
// Sparse exclusions stay excluded; only skip-worktree paths the rules select are manual.
1410+
const manual = yield* Effect.gen(function* () {
1411+
const skipped: string[] = [];
1412+
let recordStart = true;
1413+
let keep = false;
1414+
let record: Uint8Array[] = [];
1415+
yield* vcsProcess.run({
1416+
operation,
1417+
command: "git",
1418+
cwd: input.cwd,
1419+
args: ["ls-files", "--full-name", "--sparse", "-v", "-z", "--", "."],
1420+
maxOutputBytes: 4_096,
1421+
outputMode: "truncate",
1422+
// The index can be huge; keep only the "S "/"s " records of the NUL-delimited list.
1423+
onStdoutChunk: (chunk) => {
1424+
let start = 0;
1425+
while (start < chunk.length) {
1426+
const end = chunk.indexOf(0, start);
1427+
if (recordStart) {
1428+
keep = chunk[start] === 83 || chunk[start] === 115;
1429+
record = [];
1430+
}
1431+
if (keep) {
1432+
record.push(Buffer.from(chunk.subarray(start, end === -1 ? undefined : end)));
1433+
if (end !== -1) {
1434+
const entry = Buffer.concat(record).subarray(2);
1435+
record = [];
1436+
// Sparse directory entries are exclusions; stdin cannot carry non-UTF-8.
1437+
if (entry.at(-1) !== 47 && NodeBuffer.isUtf8(entry))
1438+
skipped.push(entry.toString("utf8"));
1439+
}
1440+
}
1441+
recordStart = end !== -1;
1442+
if (!recordStart) break;
1443+
start = end + 1;
1444+
}
1445+
},
1446+
});
1447+
if (skipped.length === 0) return skipped;
1448+
const stdin = skipped.join("\0") + "\0";
1449+
// check-rules echoes a subset of its input, so the input size bounds its output.
1450+
const rules = yield* execute({
1451+
operation,
1452+
cwd: input.cwd,
1453+
args: ["sparse-checkout", "check-rules", "-z"],
1454+
stdin,
1455+
maxOutputBytes: Buffer.byteLength(stdin),
1456+
outputMode: "error",
1457+
});
1458+
return rules.stdout.split("\0").filter((entry) => entry.length > 0);
1459+
}).pipe(
1460+
// A Git without ls-files --sparse or check-rules keeps the plain restore's result.
1461+
Effect.orElseSucceed((): string[] => []),
1462+
);
1463+
if (manual.length > 0) {
1464+
yield* execute({
1465+
operation,
1466+
cwd: input.cwd,
1467+
args: [
1468+
...restore,
1469+
"--ignore-skip-worktree-bits",
1470+
"--pathspec-from-file=-",
1471+
"--pathspec-file-nul",
1472+
],
1473+
stdin: manual.map((entry) => `:(top,literal)${entry}\0`).join(""),
1474+
});
1475+
}
1476+
}
14071477
}
14081478
// Restoring away the last tracked file can remove a nested workspace directory.
14091479
const recreateWorkspace = fileSystem.makeDirectory(input.cwd, { recursive: true }).pipe(

0 commit comments

Comments
 (0)