Skip to content

Commit 9517d57

Browse files
committed
fix(server): reverting files before the first commit no longer leaves them staged
restoreCheckpoint restores with --staged and then resets the index, but skipped the reset when HEAD was unborn, so files from the checkpoint stayed staged. git reset works on an unborn HEAD, so always run it.
1 parent 8760d06 commit 9517d57

2 files changed

Lines changed: 46 additions & 8 deletions

File tree

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,47 @@ it.effect("restores empty checkpoints without changing paths outside the workspa
11031103
}).pipe(Effect.scoped, Effect.provide(layerGitContract)),
11041104
);
11051105

1106+
it.effect("restores a checkpoint before the first commit without staging its files", () =>
1107+
Effect.gen(function* () {
1108+
const fileSystem = yield* FileSystem.FileSystem;
1109+
const path = yield* Path.Path;
1110+
const driver = yield* GitVcsDriver.makeVcsDriverShape();
1111+
for (const nested of [false, true]) {
1112+
const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-unborn-restore-" });
1113+
yield* runGit(root, ["init"]);
1114+
yield* runGit(root, ["config", "user.email", "[email protected]"]);
1115+
yield* runGit(root, ["config", "user.name", "Test"]);
1116+
if (nested) {
1117+
yield* fileSystem.writeFileString(path.join(root, "outside.txt"), "staged\n");
1118+
yield* runGit(root, ["add", "outside.txt"]);
1119+
}
1120+
const cwd = nested ? path.join(root, "nested") : root;
1121+
yield* fileSystem.makeDirectory(cwd, { recursive: true });
1122+
yield* fileSystem.writeFileString(path.join(cwd, "file.txt"), "original\n");
1123+
const checkpointRef = CheckpointRef.make("refs/t3/checkpoints/unborn");
1124+
yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
1125+
yield* fileSystem.writeFileString(path.join(cwd, "file.txt"), "changed\n");
1126+
yield* fileSystem.writeFileString(path.join(cwd, "added.txt"), "new\n");
1127+
1128+
assert.isTrue(
1129+
yield* driver.checkpoints.restoreCheckpoint({ cwd, checkpointRef, fallbackToHead: false }),
1130+
);
1131+
1132+
assert.strictEqual(
1133+
yield* fileSystem.readFileString(path.join(cwd, "file.txt")),
1134+
"original\n",
1135+
);
1136+
assert.isFalse(yield* fileSystem.exists(path.join(cwd, "added.txt")));
1137+
const staged = yield* driver.execute({
1138+
operation: "test",
1139+
cwd: root,
1140+
args: ["diff", "--cached", "--name-only"],
1141+
});
1142+
assert.strictEqual(staged.stdout.trim(), nested ? "outside.txt" : "");
1143+
}
1144+
}).pipe(Effect.scoped, Effect.provide(layerGitContract)),
1145+
);
1146+
11061147
// The real VCS process, spawning `command` wherever the driver runs git.
11071148
const vcsProcessSpawning = (command: string) =>
11081149
Layer.effect(

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,14 +1315,11 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
13151315
// On Windows the same clean can remove './' and exit 0.
13161316
yield* recreateWorkspace;
13171317

1318-
const headExists = yield* hasHeadCommit(input.cwd);
1319-
if (headExists) {
1320-
yield* execute({
1321-
operation,
1322-
cwd: input.cwd,
1323-
args: [...longPaths, "reset", "--quiet", "--", "."],
1324-
});
1325-
}
1318+
yield* execute({
1319+
operation,
1320+
cwd: input.cwd,
1321+
args: [...longPaths, "reset", "--quiet", "--", "."],
1322+
});
13261323

13271324
return true;
13281325
}),

0 commit comments

Comments
 (0)