Skip to content

Commit c1679eb

Browse files
fix(server): branch-switch replay ignores later commits and submodules
The carried-edit filter compared the baseline with each switch's source commit, so a commit between two switches made the switch back look like a local edit and left branch content in the baseline. It now uses the previous checkpoint's own edits over its HEAD, computed once. Gitlinks are left out of the replay because a plain switch does not move a submodule. AI-assisted; verified with the tests and tools listed above.
1 parent 232e596 commit c1679eb

2 files changed

Lines changed: 91 additions & 22 deletions

File tree

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,71 @@ it.layer(layerTest)("CheckpointStore.layer", (it) => {
748748
}),
749749
);
750750

751+
it.effect("follows a switch back after committing on the other branch", () =>
752+
Effect.gen(function* () {
753+
const turn = yield* captureTurnAcrossHeadMove("checkpoint-switch-commit-back", (cwd) =>
754+
Effect.gen(function* () {
755+
yield* git(cwd, ["switch", "branch-b"]);
756+
yield* writeTextFile(NodePath.join(cwd, "shared.txt"), "C content\n");
757+
yield* git(cwd, ["commit", "-am", "branch B commit"]);
758+
yield* git(cwd, ["switch", "branch-a"]);
759+
}),
760+
);
761+
762+
expect(turn.changedLines).toEqual([]);
763+
expect(turn.files).toEqual([]);
764+
}),
765+
);
766+
767+
it.effect("leaves a submodule a plain switch did not move", () =>
768+
Effect.gen(function* () {
769+
const tmp = yield* makeTmpDir();
770+
yield* initRepoWithCommit(tmp);
771+
const sub = NodePath.join(tmp, "sub");
772+
yield* git(tmp, ["init", "sub"]);
773+
yield* initRepoWithCommit(sub);
774+
const s1 = yield* git(sub, ["rev-parse", "HEAD"]);
775+
yield* writeTextFile(NodePath.join(sub, "s.txt"), "s2\n");
776+
yield* git(sub, ["add", "."]);
777+
yield* git(sub, ["commit", "-m", "s2"]);
778+
yield* git(tmp, ["checkout", "-b", "branch-b"]);
779+
yield* git(tmp, ["add", "sub"]);
780+
yield* git(tmp, ["commit", "-m", "pin S2"]);
781+
yield* git(sub, ["checkout", "-q", s1]);
782+
yield* git(tmp, ["checkout", "-b", "branch-a", "HEAD~1"]);
783+
yield* git(tmp, ["add", "sub"]);
784+
yield* git(tmp, ["commit", "-m", "pin S1"]);
785+
const checkpointStore = yield* CheckpointStore.CheckpointStore;
786+
const id = ThreadId.make("checkpoint-switch-submodule");
787+
const refs = {
788+
cwd: tmp,
789+
fromCheckpointRef: checkpointRefForThreadTurn(id, 1),
790+
toCheckpointRef: checkpointRefForThreadTurn(id, 2),
791+
};
792+
yield* checkpointStore.captureCheckpoint({
793+
cwd: tmp,
794+
checkpointRef: refs.fromCheckpointRef,
795+
});
796+
797+
yield* git(tmp, ["switch", "branch-b"]);
798+
yield* writeTextFile(NodePath.join(tmp, "turn.txt"), "turn\n");
799+
yield* checkpointStore.captureCheckpoint({
800+
cwd: tmp,
801+
checkpointRef: refs.toCheckpointRef,
802+
previousCheckpointRef: refs.fromCheckpointRef,
803+
});
804+
805+
const numstat = yield* checkpointStore.diffCheckpoints({
806+
...refs,
807+
ignoreWhitespace: false,
808+
format: "numstat",
809+
});
810+
expect(parseTurnDiffFilesFromNumstat(numstat).map((file) => file.path)).toEqual([
811+
"turn.txt",
812+
]);
813+
}),
814+
);
815+
751816
it.effect("keeps local edits that git switch -m carried to the new branch", () =>
752817
Effect.gen(function* () {
753818
const tmp = yield* makeTmpDir();

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

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,6 +1447,26 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
14471447
args: [...indexConfig, "read-tree", previousTree],
14481448
env: commitEnv,
14491449
});
1450+
// Local edits the previous checkpoint held over its HEAD were merged by a switch
1451+
// (git switch -m), not replaced, so those paths keep the baseline's content.
1452+
const edited = new Set(
1453+
(yield* execute({
1454+
operation,
1455+
cwd: input.cwd,
1456+
args: [
1457+
"-c",
1458+
"core.quotePath=true",
1459+
"diff-tree",
1460+
"-r",
1461+
"--no-renames",
1462+
"--name-only",
1463+
`${previousCommit}^1`,
1464+
previousCommit,
1465+
],
1466+
maxOutputBytes: VcsDriver.CHECKPOINT_DIFF_MAX_OUTPUT_BYTES,
1467+
outputMode: "error",
1468+
})).stdout.split("\n"),
1469+
);
14501470
for (const { from, to } of switches) {
14511471
const changes = yield* execute({
14521472
operation,
@@ -1463,27 +1483,6 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
14631483
maxOutputBytes: VcsDriver.CHECKPOINT_DIFF_MAX_OUTPUT_BYTES,
14641484
outputMode: "error",
14651485
});
1466-
// A path with local edits was merged by the switch (git switch -m), not
1467-
// replaced, so it keeps the baseline's content.
1468-
const edited = new Set(
1469-
(yield* execute({
1470-
operation,
1471-
cwd: input.cwd,
1472-
args: [
1473-
...indexConfig,
1474-
"-c",
1475-
"core.quotePath=true",
1476-
"diff-index",
1477-
"--cached",
1478-
"--no-renames",
1479-
"--name-only",
1480-
from,
1481-
],
1482-
env: commitEnv,
1483-
maxOutputBytes: VcsDriver.CHECKPOINT_DIFF_MAX_OUTPUT_BYTES,
1484-
outputMode: "error",
1485-
})).stdout.split("\n"),
1486-
);
14871486
// Git's quoted paths round-trip through index-info, including non-UTF-8 names,
14881487
// and a deletion's zero mode drops the path.
14891488
yield* execute({
@@ -1492,7 +1491,12 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
14921491
args: [...indexConfig, "update-index", "--index-info"],
14931492
stdin: changes.stdout
14941493
.split("\n")
1495-
.filter((line) => !edited.has(line.slice(line.indexOf("\t") + 1)))
1494+
.filter(
1495+
(line) =>
1496+
// A plain switch leaves submodules at their old commits.
1497+
!/^:(?:160000 \d{6}|\d{6} 160000) /.test(line) &&
1498+
!edited.has(line.slice(line.indexOf("\t") + 1)),
1499+
)
14961500
.join("\n")
14971501
.replace(/^:[0-7]{6} ([0-7]{6}) [0-9a-f]+ ([0-9a-f]+) [A-Z]\t/gm, "$1 $2\t"),
14981502
env: commitEnv,

0 commit comments

Comments
 (0)