Skip to content

Commit 7bd9134

Browse files
fix(server): turn diffs follow branch switches (#36343)
1 parent ca6e09c commit 7bd9134

6 files changed

Lines changed: 345 additions & 14 deletions

File tree

‎apps/server/src/checkpointing/CheckpointStore.test.ts‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,72 @@ it.layer(layerTest)("CheckpointStore.layer", (it) => {
546546
}),
547547
);
548548

549+
it.effect.each([{ committed: "before the turn" }, { committed: "during the turn" }])(
550+
"starts a turn that switched branches from the branch it landed on (committed $committed)",
551+
({ committed }) =>
552+
Effect.gen(function* () {
553+
const tmp = yield* makeTmpDir();
554+
yield* initRepoWithCommit(tmp);
555+
const checkpointStore = yield* CheckpointStore.CheckpointStore;
556+
const threadId = ThreadId.make("checkpoint-branch-switch");
557+
const [first, second, third] = [0, 1, 2].map((turn) =>
558+
checkpointRefForThreadTurn(threadId, turn),
559+
) as [CheckpointRef, CheckpointRef, CheckpointRef];
560+
const changedSince = (fromCheckpointRef: CheckpointRef, toCheckpointRef: CheckpointRef) =>
561+
Effect.gen(function* () {
562+
const refs = { cwd: tmp, fromCheckpointRef, toCheckpointRef };
563+
return {
564+
patch: yield* checkpointStore.diffCheckpoints({ ...refs, ignoreWhitespace: false }),
565+
authored: yield* checkpointStore.listAuthoredPaths(refs),
566+
};
567+
});
568+
const commitOnA = git(tmp, ["commit", "-am", "work on a"]);
569+
570+
yield* git(tmp, ["branch", "b"]);
571+
yield* git(tmp, ["checkout", "-b", "a"]);
572+
yield* writeTextFile(NodePath.join(tmp, "a.txt"), "a\n");
573+
yield* writeTextFile(NodePath.join(tmp, "README.md"), "# on a\n");
574+
yield* git(tmp, ["add", "."]);
575+
if (committed === "before the turn") yield* commitOnA;
576+
yield* checkpointStore.captureCheckpoint({ cwd: tmp, checkpointRef: first });
577+
578+
if (committed === "during the turn") yield* commitOnA;
579+
yield* git(tmp, ["checkout", "b"]);
580+
yield* writeTextFile(NodePath.join(tmp, "README.md"), "# on b\n");
581+
yield* writeTextFile(NodePath.join(tmp, "b.txt"), "b\n");
582+
yield* checkpointStore.captureCheckpoint({
583+
cwd: tmp,
584+
checkpointRef: second,
585+
previousCheckpointRef: first,
586+
});
587+
588+
const switched = yield* changedSince(first, second);
589+
expect(switched.patch).not.toContain("a.txt");
590+
expect(switched.patch).toContain("-# test\n+# on b\n");
591+
expect(switched.patch).toContain("b/b.txt");
592+
expect(switched.authored).toBeNull();
593+
594+
// A reset also moves HEAD off a commit, and what it removed is the turn's doing.
595+
yield* git(tmp, ["add", "."]);
596+
yield* git(tmp, ["commit", "-m", "work on b"]);
597+
yield* checkpointStore.captureCheckpoint({
598+
cwd: tmp,
599+
checkpointRef: second,
600+
previousCheckpointRef: first,
601+
});
602+
yield* git(tmp, ["reset", "--hard", "HEAD~1"]);
603+
yield* checkpointStore.captureCheckpoint({
604+
cwd: tmp,
605+
checkpointRef: third,
606+
previousCheckpointRef: second,
607+
});
608+
609+
const reset = yield* changedSince(second, third);
610+
expect(reset.patch).toContain("--- a/b.txt\n+++ /dev/null");
611+
expect(reset.authored).toEqual(new Set(["README.md", "b.txt"]));
612+
}),
613+
);
614+
549615
it.effect("keeps paths from signed commits under log.showSignature", () =>
550616
Effect.gen(function* () {
551617
const tmp = yield* makeTmpDir();

‎apps/server/src/checkpointing/CheckpointStore.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
1010
export interface CaptureCheckpointInput {
1111
readonly cwd: string;
1212
readonly checkpointRef: CheckpointRef;
13+
/** The checkpoint this one follows, so a turn that switched branches starts where it landed. */
14+
readonly previousCheckpointRef?: CheckpointRef;
1315
}
1416

1517
export interface RestoreCheckpointInput {

‎apps/server/src/orchestration-v2/CheckpointService.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ export const layer: Layer.Layer<
410410
.captureCheckpoint({
411411
cwd: input.scope.cwd,
412412
checkpointRef,
413+
...(input.ordinalWithinScope > 0 ? { previousCheckpointRef } : {}),
413414
})
414415
.pipe(
415416
Effect.as("ready" as const),

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,63 @@ it.effect.each([1_700_000_000, 1_700_000_000.9999])(
861861
}).pipe(Effect.scoped, Effect.provide(layerGitContract)),
862862
);
863863

864+
it.effect("a branch switch landing after the snapshot belongs to the next checkpoint", () =>
865+
Effect.gen(function* () {
866+
const fileSystem = yield* FileSystem.FileSystem;
867+
const path = yield* Path.Path;
868+
const liveProcess = yield* VcsProcess.VcsProcess;
869+
const driver = yield* GitVcsDriver.makeVcsDriverShape();
870+
const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-switch-race-" });
871+
const git = (args: ReadonlyArray<string>) =>
872+
driver.execute({ operation: "checkpoint-test", cwd, args });
873+
yield* git(["init"]);
874+
yield* git(["config", "user.name", "Test"]);
875+
yield* git(["config", "user.email", "[email protected]"]);
876+
yield* fileSystem.writeFileString(path.join(cwd, "f.txt"), "A\n");
877+
yield* git(["add", "."]);
878+
yield* git(["commit", "-m", "a"]);
879+
yield* git(["checkout", "-b", "b"]);
880+
yield* fileSystem.writeFileString(path.join(cwd, "f.txt"), "B\n");
881+
yield* git(["commit", "-am", "b"]);
882+
yield* git(["checkout", "-"]);
883+
const [first, second, third] = ["1", "2", "3"].map((turn) =>
884+
CheckpointRef.make(`refs/t3/checkpoints/switch-race/${turn}`),
885+
) as [CheckpointRef, CheckpointRef, CheckpointRef];
886+
const racedDriver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
887+
Effect.provideService(VcsProcess.VcsProcess, {
888+
run: Effect.fn(function* (input: VcsProcess.VcsProcessInput) {
889+
const result = yield* liveProcess.run(input);
890+
if (input.args.includes("write-tree")) yield* git(["checkout", "b"]).pipe(Effect.orDie);
891+
return result;
892+
}),
893+
}),
894+
);
895+
const changedSince = (fromCheckpointRef: CheckpointRef, toCheckpointRef: CheckpointRef) =>
896+
Effect.gen(function* () {
897+
const refs = { cwd, fromCheckpointRef, toCheckpointRef };
898+
return {
899+
patch: yield* driver.checkpoints.diffCheckpoints({ ...refs, ignoreWhitespace: false }),
900+
authored: yield* driver.checkpoints.listAuthoredPaths(refs),
901+
};
902+
});
903+
904+
yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef: first });
905+
yield* racedDriver.checkpoints.captureCheckpoint({
906+
cwd,
907+
checkpointRef: second,
908+
previousCheckpointRef: first,
909+
});
910+
yield* driver.checkpoints.captureCheckpoint({
911+
cwd,
912+
checkpointRef: third,
913+
previousCheckpointRef: second,
914+
});
915+
916+
assert.deepEqual(yield* changedSince(first, second), { patch: "", authored: null });
917+
assert.deepEqual(yield* changedSince(second, third), { patch: "", authored: null });
918+
}).pipe(Effect.scoped, Effect.provide(layerGitContract)),
919+
);
920+
864921
it.effect("checkpoint capture preserves racy edits made after resetting the index", () =>
865922
Effect.gen(function* () {
866923
const fileSystem = yield* FileSystem.FileSystem;
@@ -1051,6 +1108,7 @@ it.effect.each(["committed", "unborn"] as const)(
10511108

10521109
assert.deepStrictEqual(commands, [
10531110
"rev-parse",
1111+
...(head === "committed" ? ["log"] : []),
10541112
"config",
10551113
...(head === "committed" ? ["read-tree", "ls-files"] : []),
10561114
"add",

0 commit comments

Comments
 (0)