Skip to content

Commit c971b34

Browse files
committed
fix(server): pulls in one repository of a multi-repo workspace no longer flood a turn's files
1 parent 29c4b83 commit c971b34

2 files changed

Lines changed: 128 additions & 17 deletions

File tree

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,48 @@ it.layer(layerTest)("CheckpointStore.layer", (it) => {
258258
);
259259
}),
260260
);
261+
262+
it.effect("leaves out what a pull brought into one repository", () =>
263+
Effect.gen(function* () {
264+
const workspace = yield* makeWorkspace;
265+
const checkpointStore = yield* CheckpointStore.CheckpointStore;
266+
const threadId = ThreadId.make("thread-multi-repo-authored");
267+
const refs = {
268+
cwd: workspace,
269+
fromCheckpointRef: checkpointRefForThreadTurn(threadId, 0),
270+
toCheckpointRef: checkpointRefForThreadTurn(threadId, 1),
271+
};
272+
const api = NodePath.join(workspace, "api");
273+
yield* git(api, ["checkout", "-b", "upstream"]);
274+
yield* writeTextFile(NodePath.join(api, "upstream.txt"), "upstream\n");
275+
yield* git(api, ["add", "."]);
276+
yield* git(api, ["commit", "-m", "upstream"], { committedAt: "2020-01-01T00:00:00Z" });
277+
yield* git(api, ["checkout", "-"]);
278+
yield* checkpointStore.captureCheckpoint({
279+
cwd: workspace,
280+
checkpointRef: refs.fromCheckpointRef,
281+
});
282+
283+
// The turn pulls upstream into api and edits web.
284+
yield* git(api, ["merge", "--ff-only", "upstream"]);
285+
yield* writeTextFile(NodePath.join(workspace, "web", "app.ts"), "export {};\n");
286+
yield* checkpointStore.captureCheckpoint({
287+
cwd: workspace,
288+
checkpointRef: refs.toCheckpointRef,
289+
});
290+
291+
expect(yield* checkpointStore.listAuthoredPaths(refs)).toEqual(new Set(["web/app.ts"]));
292+
const numstat = yield* checkpointStore.diffCheckpoints({
293+
...refs,
294+
ignoreWhitespace: false,
295+
format: "numstat",
296+
filePaths: ["web/app.ts"],
297+
});
298+
expect(parseTurnDiffFilesFromNumstat(numstat).map((file) => file.path)).toEqual([
299+
"web/app.ts",
300+
]);
301+
}),
302+
);
261303
});
262304

263305
describe("diffCheckpoints", () => {

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

Lines changed: 86 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import type { CheckpointStoreError } from "./Errors.ts";
2828
import type { VcsCheckpointOps } from "../vcs/VcsDriver.ts";
2929
import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
3030
import * as WorkspaceRepositories from "../workspace/WorkspaceRepositories.ts";
31-
import { prefixNumstatPaths, prefixPatchPaths } from "./Diffs.ts";
31+
import { parseTurnDiffFilesFromNumstat, prefixNumstatPaths, prefixPatchPaths } from "./Diffs.ts";
3232

3333
export interface CaptureCheckpointInput {
3434
readonly cwd: string;
@@ -261,22 +261,36 @@ export const make = Effect.gen(function* () {
261261
"CheckpointStore.diffCheckpoints",
262262
target.cwd,
263263
);
264-
if (target.pathPrefix !== null) {
265-
// A repository added to the workspace after either checkpoint has nothing to compare.
266-
const comparable =
264+
if (target.pathPrefix === null) {
265+
return yield* checkpoints.diffCheckpoints({ ...input, cwd: target.cwd });
266+
}
267+
// A repository added to the workspace after either checkpoint has nothing to compare.
268+
const comparable =
269+
(yield* checkpoints.hasCheckpointRef({
270+
cwd: target.cwd,
271+
checkpointRef: input.toCheckpointRef,
272+
})) &&
273+
(input.fallbackFromToHead === true ||
267274
(yield* checkpoints.hasCheckpointRef({
268275
cwd: target.cwd,
269-
checkpointRef: input.toCheckpointRef,
270-
})) &&
271-
(input.fallbackFromToHead === true ||
272-
(yield* checkpoints.hasCheckpointRef({
273-
cwd: target.cwd,
274-
checkpointRef: input.fromCheckpointRef,
275-
})));
276-
if (!comparable) return "";
277-
}
278-
const diff = yield* checkpoints.diffCheckpoints({ ...input, cwd: target.cwd });
279-
if (target.pathPrefix === null) return diff;
276+
checkpointRef: input.fromCheckpointRef,
277+
})));
278+
if (!comparable) return "";
279+
// Requested paths are relative to the workspace folder.
280+
const repositoryPrefix = `${target.pathPrefix}/`;
281+
const diff = yield* checkpoints.diffCheckpoints({
282+
...input,
283+
cwd: target.cwd,
284+
...(input.filePaths
285+
? {
286+
filePaths: input.filePaths.flatMap((filePath) =>
287+
filePath.startsWith(repositoryPrefix)
288+
? [filePath.slice(repositoryPrefix.length)]
289+
: [],
290+
),
291+
}
292+
: {}),
293+
});
280294
return input.format === "numstat"
281295
? prefixNumstatPaths(diff, target.pathPrefix)
282296
: prefixPatchPaths(diff, target.pathPrefix);
@@ -288,8 +302,63 @@ export const make = Effect.gen(function* () {
288302
const listAuthoredPaths: CheckpointStore["Service"]["listAuthoredPaths"] = Effect.fn(
289303
"listAuthoredPaths",
290304
)(function* (input) {
291-
const checkpoints = yield* resolveCheckpoints("CheckpointStore.listAuthoredPaths", input.cwd);
292-
return yield* checkpoints.listAuthoredPaths(input);
305+
const targets = yield* resolveTargets(input.cwd);
306+
if (targets.length === 1 && targets[0]!.pathPrefix === null) {
307+
const checkpoints = yield* resolveCheckpoints("CheckpointStore.listAuthoredPaths", input.cwd);
308+
return yield* checkpoints.listAuthoredPaths(input);
309+
}
310+
const perRepository = yield* forEachTarget(targets, (target) =>
311+
Effect.gen(function* () {
312+
const checkpoints = yield* resolveCheckpoints(
313+
"CheckpointStore.listAuthoredPaths",
314+
target.cwd,
315+
);
316+
// A repository without both checkpoints has no diff, so nothing of it is listed.
317+
const comparable =
318+
(yield* checkpoints.hasCheckpointRef({
319+
cwd: target.cwd,
320+
checkpointRef: input.fromCheckpointRef,
321+
})) &&
322+
(yield* checkpoints.hasCheckpointRef({
323+
cwd: target.cwd,
324+
checkpointRef: input.toCheckpointRef,
325+
}));
326+
if (!comparable) return { target, paths: new Set<string>() };
327+
return {
328+
target,
329+
paths: yield* checkpoints.listAuthoredPaths({ ...input, cwd: target.cwd }),
330+
};
331+
}),
332+
);
333+
if (perRepository.every(({ paths }) => paths === null)) return null;
334+
// The result is one set for the workspace, so a repository whose HEAD did not move lists
335+
// every path it changed.
336+
const authored = new Set<string>();
337+
for (const { target, paths } of perRepository) {
338+
let repositoryPaths: Iterable<string> | null = paths;
339+
if (repositoryPaths === null) {
340+
const numstat = yield* resolveCheckpoints(
341+
"CheckpointStore.listAuthoredPaths",
342+
target.cwd,
343+
).pipe(
344+
Effect.flatMap((checkpoints) =>
345+
checkpoints.diffCheckpoints({
346+
...input,
347+
cwd: target.cwd,
348+
ignoreWhitespace: false,
349+
format: "numstat",
350+
}),
351+
),
352+
);
353+
repositoryPaths = parseTurnDiffFilesFromNumstat(numstat).flatMap((file) =>
354+
file.previousPath === undefined ? [file.path] : [file.path, file.previousPath],
355+
);
356+
}
357+
for (const repositoryPath of repositoryPaths) {
358+
authored.add(`${target.pathPrefix}/${repositoryPath}`);
359+
}
360+
}
361+
return authored;
293362
});
294363

295364
const deleteCheckpointRefs: CheckpointStore["Service"]["deleteCheckpointRefs"] = Effect.fn(

0 commit comments

Comments
 (0)