Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions apps/server/src/vcs/GitVcsDriverCore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,49 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
}),
);

it.effect("shows branches a background fetch brought in to the next fresh listing", () =>
Effect.gen(function* () {
const cwd = yield* makeTmpDir();
const remote = yield* makeTmpDir("git-vcs-driver-remote-");
const { initialBranch } = yield* initRepoWithCommit(cwd);
const driver = yield* GitVcsDriver.GitVcsDriver;
yield* git(remote, ["init", "--bare"]);
yield* git(cwd, ["remote", "add", "origin", remote]);
yield* git(cwd, ["push", "-u", "origin", initialBranch]);
yield* pushRemoteBranchFromPeer({
remote,
baseBranch: initialBranch,
branch: "fetched-later",
subject: "remote branch",
fileName: "later.txt",
});

// A fresh listing starts the background fetch and is shared for a few
// seconds with the next fresh one. (It may already include the branch
// if the fetch beat its read.)
yield* driver.listRefs({ cwd, query: "fetched-later", refresh: true });
yield* waitForGitOutput(
cwd,
["branch", "--remotes", "--list", "origin/fetched-later"],
(output) => output.trim().length > 0,
);

// The fetch has landed (the server notes it a moment after git does):
// the shared listing from before it isn't handed out again, which
// would otherwise last until its few seconds ran out.
const startedAtMs = Date.now();
let after = yield* driver.listRefs({ cwd, query: "fetched-later", refresh: true });
while (after.refs.length === 0 && Date.now() - startedAtMs < 5_000) {
yield* Effect.promise(() => sleepRealTime(50));
after = yield* driver.listRefs({ cwd, query: "fetched-later", refresh: true });
}
assert.deepStrictEqual(
after.refs.map((ref) => ref.name),
["origin/fetched-later"],
);
}),
);

it.effect("puts the branch named exactly what was searched for on the first page", () =>
Effect.gen(function* () {
const cwd = yield* makeTmpDir();
Expand Down
23 changes: 22 additions & 1 deletion apps/server/src/vcs/GitVcsDriverCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,8 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
// the snapshot implementation's ordering in this service constructor.
let invalidateListRefsSnapshotByCommonDir = (_gitCommonDir: string): Effect.Effect<void> =>
Effect.void;
let markRemoteRefsFetchedByCommonDir = (_gitCommonDir: string): Effect.Effect<void> =>
Effect.void;

const executeRaw: GitVcsDriver.GitVcsDriverShape["execute"] = Effect.fnUntraced(
function* (input) {
Expand Down Expand Up @@ -1874,7 +1876,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
includeTags: cacheKey.includeTags,
}),
{ discard: true, concurrency: "unbounded" },
).pipe(Effect.ensuring(invalidateListRefsSnapshotByCommonDir(cacheKey.gitCommonDir)));
).pipe(Effect.ensuring(markRemoteRefsFetchedByCommonDir(cacheKey.gitCommonDir)));
return true as const;
});

Expand Down Expand Up @@ -4985,11 +4987,30 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
}
});

// A change made here (a branch made, switched or deleted): a listing read
// while it landed may predate it, so it reads again (resolveListRefsSnapshot).
invalidateListRefsSnapshotByCommonDir = (gitCommonDir) =>
Effect.sync(() => {
bumpListRefsGeneration(gitCommonDir);
bumpListRefsEpoch(gitCommonDir);
});
// The background fetch of remote branches finished. The next listing reads
// them, but one already reading keeps its answer, as if it had come a
// moment sooner: reading again doubled the wait (seconds on a busy repository)
// for every listing a fetch landed during, roughly every two minutes.
markRemoteRefsFetchedByCommonDir = (gitCommonDir) =>
Effect.suspend(() => {
bumpListRefsEpoch(gitCommonDir);
// Fresh reads are shared for a few seconds per generation; the one from
// before the fetch mustn't be handed out again.
return Cache.invalidate(
listRefsRefreshCache,
new ListRefsRefreshCacheKey({
gitCommonDir,
generation: currentListRefsGeneration(gitCommonDir),
}),
);
});
const invalidateListRefsSnapshot = (cwd: string): Effect.Effect<void> =>
resolveGitCommonDir(cwd).pipe(
Effect.flatMap(invalidateListRefsSnapshotByCommonDir),
Expand Down
Loading