Skip to content

Commit fa6ba12

Browse files
fix(server): refs read while a clone ran are dropped when the clone finishes
A ref list read during a clone caches an empty snapshot and an unborn HEAD. When the clone finished within a few seconds of that read, the client's refresh was coalesced with it, so the picker never saw the real default branch: the draft kept no base and sending in New worktree mode was refused. Every other git operation that changes refs invalidates that snapshot. The clone runs outside the driver, so it now does the same through a new GitVcsDriver.invalidateRefs before it reports done. Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent a2cd749 commit fa6ba12

4 files changed

Lines changed: 70 additions & 1 deletion

File tree

‎apps/server/src/sourceControl/SourceControlRepositoryService.test.ts‎

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ function layer(input: {
7878
Layer.provide(
7979
Layer.mock(GitVcsDriver.GitVcsDriver)({
8080
execute: () => Effect.succeed(processOutput()),
81+
invalidateRefs: () => Effect.void,
8182
ensureRemote: () => Effect.succeed("origin"),
8283
pushCurrentBranch: () =>
8384
Effect.succeed({
@@ -220,7 +221,7 @@ it.effect.each(["--bare", "source.git"])(
220221
});
221222
assert.strictEqual(result.cwd, destinationPath);
222223
assert.isTrue(yield* fs.exists(path.join(destinationPath, ".git")));
223-
}).pipe(Effect.provide(layer({ git: { execute: git.execute } })));
224+
}).pipe(Effect.provide(layer({ git })));
224225
}).pipe(
225226
Effect.provide(
226227
GitVcsDriver.layer.pipe(
@@ -235,6 +236,67 @@ it.effect.each(["--bare", "source.git"])(
235236
),
236237
);
237238

239+
it.effect("lists the cloned branches after a ref list was read while the clone ran", () =>
240+
Effect.gen(function* () {
241+
const fs = yield* FileSystem.FileSystem;
242+
const path = yield* Path.Path;
243+
const git = yield* GitVcsDriver.GitVcsDriver;
244+
const parent = yield* fs.makeTempDirectoryScoped({ prefix: "t3-clone-refs-" });
245+
const source = path.join(parent, "source");
246+
const destinationPath = path.join(parent, "checkout");
247+
const run = (cwd: string, args: ReadonlyArray<string>) =>
248+
git.execute({ operation: "test.git", cwd, args });
249+
yield* run(parent, ["init", "--initial-branch=staging", source]);
250+
yield* run(source, [
251+
"-c",
252+
"user.name=Test",
253+
"-c",
254+
255+
"commit",
256+
"--allow-empty",
257+
"-m",
258+
"init",
259+
]);
260+
const refNames = git
261+
.listRefs({ cwd: destinationPath })
262+
.pipe(Effect.map((result) => result.refs.map((ref) => ref.name)));
263+
let duringClone: ReadonlyArray<string> | null = null;
264+
// Stands in for git clone so the read lands while HEAD is still unborn.
265+
const cloneInTwoSteps = Effect.gen(function* () {
266+
yield* run(parent, ["init", "--initial-branch=staging", destinationPath]);
267+
duringClone = yield* refNames;
268+
return yield* run(destinationPath, ["pull", source, "staging"]);
269+
});
270+
yield* Effect.gen(function* () {
271+
const service = yield* SourceControlRepositoryService.SourceControlRepositoryService;
272+
yield* service.cloneRepository({ remoteUrl: source, destinationPath });
273+
}).pipe(
274+
Effect.provide(
275+
layer({
276+
git: {
277+
...git,
278+
execute: (input) => (input.args[0] === "clone" ? cloneInTwoSteps : git.execute(input)),
279+
},
280+
}),
281+
),
282+
);
283+
284+
assert.deepStrictEqual(duringClone, []);
285+
assert.deepStrictEqual(yield* refNames, ["staging"]);
286+
}).pipe(
287+
Effect.provide(
288+
GitVcsDriver.layer.pipe(
289+
Layer.provide(
290+
ServerConfig.layerTest(process.cwd(), {
291+
prefix: "t3-clone-refs-config-",
292+
}),
293+
),
294+
Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
295+
),
296+
),
297+
),
298+
);
299+
238300
it.effect("reports clone progress from git's stderr and keeps its error text on failure", () =>
239301
Effect.gen(function* () {
240302
const fs = yield* FileSystem.FileSystem;

‎apps/server/src/sourceControl/SourceControlRepositoryService.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,8 @@ export const make = Effect.gen(function* () {
429429
),
430430
);
431431

432+
// A ref list read while the clone ran saw an unborn HEAD and no refs.
433+
yield* git.invalidateRefs(prepared.destinationPath);
432434
return {
433435
cwd: prepared.destinationPath,
434436
remoteUrl,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,8 @@ export class GitVcsDriver extends Context.Service<
458458
input: VcsSwitchRefInput,
459459
) => Effect.Effect<VcsSwitchRefResult, GitCommandError>;
460460
readonly initRepo: (input: VcsInitInput) => Effect.Effect<void, GitCommandError>;
461+
/** Drops cached refs after something outside this driver changed them, such as a clone into `cwd`. */
462+
readonly invalidateRefs: (cwd: string) => Effect.Effect<void>;
461463
readonly listLocalBranchNames: (cwd: string) => Effect.Effect<string[], GitCommandError>;
462464
}
463465
>()("t3/vcs/GitVcsDriver") {}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5578,6 +5578,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
55785578
return yield* withListRefsInvalidation(input.cwd, switchRef(input));
55795579
}),
55805580
initRepo: initRepoWithListRefsInvalidation,
5581+
invalidateRefs: Effect.fn("GitVcsDriver.invalidateRefs")(function* (cwd) {
5582+
return yield* withListRefsInvalidation(cwd, Effect.void);
5583+
}),
55815584
listLocalBranchNames,
55825585
listWorktreePaths,
55835586
});

0 commit comments

Comments
 (0)