Skip to content
TypeScript

fix(server): reverting files before the first commit no longer leaves them staged (#28647)

Abhishek B Rac976026a900
diff --git a/apps/server/src/vcs/GitVcsDriver.test.ts b/apps/server/src/vcs/GitVcsDriver.test.ts
index 9262688fd10..fd85564a7da 100644
--- a/apps/server/src/vcs/GitVcsDriver.test.ts
+++ b/apps/server/src/vcs/GitVcsDriver.test.ts
@@ -1134,6 +1134,47 @@ it.effect("restores empty checkpoints without changing paths outside the workspa
}).pipe(Effect.scoped, Effect.provide(layerGitContract)),
);
+it.effect("restores a checkpoint before the first commit without staging its files", () =>
+ Effect.gen(function* () {
+ const fileSystem = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
+ const driver = yield* GitVcsDriver.makeVcsDriverShape();
+ for (const nested of [false, true]) {
+ const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-unborn-restore-" });
+ yield* runGit(root, ["init"]);
+ yield* runGit(root, ["config", "user.email", "[email protected]"]);
+ yield* runGit(root, ["config", "user.name", "Test"]);
+ if (nested) {
+ yield* fileSystem.writeFileString(path.join(root, "outside.txt"), "staged\n");
+ yield* runGit(root, ["add", "outside.txt"]);
+ }
+ const cwd = nested ? path.join(root, "nested") : root;
+ yield* fileSystem.makeDirectory(cwd, { recursive: true });
+ yield* fileSystem.writeFileString(path.join(cwd, "file.txt"), "original\n");
+ const checkpointRef = CheckpointRef.make("refs/t3/checkpoints/unborn");
+ yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
+ yield* fileSystem.writeFileString(path.join(cwd, "file.txt"), "changed\n");
+ yield* fileSystem.writeFileString(path.join(cwd, "added.txt"), "new\n");
+
+ assert.isTrue(
+ yield* driver.checkpoints.restoreCheckpoint({ cwd, checkpointRef, fallbackToHead: false }),
+ );
+
+ assert.strictEqual(
+ yield* fileSystem.readFileString(path.join(cwd, "file.txt")),
+ "original\n",
+ );
+ assert.isFalse(yield* fileSystem.exists(path.join(cwd, "added.txt")));
+ const staged = yield* driver.execute({
+ operation: "test",
+ cwd: root,
+ args: ["diff", "--cached", "--name-only"],
+ });
+ assert.strictEqual(staged.stdout.trim(), nested ? "outside.txt" : "");
+ }
+ }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
+);
+
// The real VCS process, spawning `command` wherever the driver runs git.
const vcsProcessSpawning = (command: string) =>
Layer.effect(
diff --git a/apps/server/src/vcs/GitVcsDriver.ts b/apps/server/src/vcs/GitVcsDriver.ts
index 15392cb96ce..d3ecfcfa95d 100644
--- a/apps/server/src/vcs/GitVcsDriver.ts
+++ b/apps/server/src/vcs/GitVcsDriver.ts
@@ -1367,14 +1367,11 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
// On Windows the same clean can remove './' and exit 0.
yield* recreateWorkspace;
- const headExists = yield* hasHeadCommit(input.cwd);
- if (headExists) {
- yield* execute({
- operation,
- cwd: input.cwd,
- args: [...longPaths, "reset", "--quiet", "--", "."],
- });
- }
+ yield* execute({
+ operation,
+ cwd: input.cwd,
+ args: [...longPaths, "reset", "--quiet", "--", "."],
+ });
return true;
}),