Skip to content
TypeScript

perf(server): avoid repeated Git directory discovery during worktree creation (#21337)

Adamulek12398a735373523
diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts
index cd4b6949fad..3566a48fc4c 100644
--- a/apps/server/src/vcs/GitVcsDriverCore.test.ts
+++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts
@@ -5,8 +5,10 @@ import * as NodeURL from "node:url";
import * as NodeServices from "@effect/platform-node/NodeServices";
import * as HostProcess from "@t3tools/shared/HostProcess";
import { assert, it, describe } from "@effect/vitest";
+import * as Cause from "effect/Cause";
import * as Deferred from "effect/Deferred";
import * as Effect from "effect/Effect";
+import * as Exit from "effect/Exit";
import * as FileSystem from "effect/FileSystem";
import * as Fiber from "effect/Fiber";
import * as Layer from "effect/Layer";
@@ -49,7 +51,7 @@ const layerServerConfig = ServerConfig.layerTest(process.cwd(), {
prefix: "t3-git-vcs-driver-test-",
});
const layerTest = GitVcsDriver.layer.pipe(
- Layer.provide(layerServerConfig),
+ Layer.provideMerge(layerServerConfig),
Layer.provideMerge(NodeServices.layer),
);
@@ -330,6 +332,415 @@ it.effect("invalidates origin remote cache when a driver mutation adds origin",
}).pipe(Effect.provide(layerTest)),
);
+it.effect.each([
+ { hookExitCode: 0, commonDirFailure: "none" },
+ { hookExitCode: 1, commonDirFailure: "none" },
+ { hookExitCode: 0, commonDirFailure: "error" },
+ { hookExitCode: 1, commonDirFailure: "error" },
+ { hookExitCode: 0, commonDirFailure: "non-repository" },
+ { hookExitCode: 1, commonDirFailure: "non-repository" },
+])(
+ "invalidates sibling worktree refs and status after checkout hook exit $hookExitCode with common-dir failure $commonDirFailure",
+ ({ hookExitCode, commonDirFailure }) =>
+ Effect.scoped(
+ Effect.gen(function* () {
+ const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
+ const spawnedArgs: Array<ReadonlyArray<string>> = [];
+ let invalidationFailure = "none";
+ const spawner = ChildProcessSpawner.make((command) =>
+ Effect.gen(function* () {
+ if (!ChildProcess.isStandardCommand(command)) {
+ return yield* Effect.die("expected a standard Git command");
+ }
+ spawnedArgs.push(command.args);
+ if (invalidationFailure !== "none" && command.args.includes("--git-common-dir")) {
+ if (invalidationFailure === "non-repository") return makeNonRepositoryHandle();
+ return ChildProcessSpawner.makeHandle({
+ ...makeNonRepositoryHandle(),
+ stderr: Stream.encodeText(Stream.make("fatal: could not read repository metadata")),
+ });
+ }
+ return yield* delegate.spawn(command);
+ }),
+ );
+ const driver = yield* makeGitVcsDriverCore().pipe(
+ Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
+ );
+ const cwd = yield* makeTmpDir();
+ const { initialBranch } = yield* initRepoWithCommit(cwd);
+ const path = yield* Path.Path;
+ const fs = yield* FileSystem.FileSystem;
+ const siblingPath = path.join(yield* makeTmpDir("git-worktrees-"), "sibling");
+ yield* git(cwd, ["worktree", "add", "-b", "cache-sibling", siblingPath, "HEAD"]);
+
+ const before = yield* driver.listRefs({ cwd: siblingPath, refresh: true });
+ assert.isTrue(before.refs.find((ref) => ref.name === "cache-sibling")?.current);
+ const statusBefore = yield* driver.statusDetailsLocal(siblingPath);
+ assert.isFalse(statusBefore.hasOriginRemote);
+ assert.isFalse(statusBefore.isDefaultBranch);
+ yield* driver.listRefs({ cwd, refresh: true });
+ yield* git(cwd, ["checkout", "-b", "creator-current"]);
+ yield* writeTextFile(
+ cwd,
+ ".git/hooks/post-checkout",
+ "#!/bin/sh\n" +
+ "git remote add origin https://example.invalid/repo.git || exit 2\n" +
+ "git update-ref refs/remotes/origin/cache-sibling HEAD || exit 2\n" +
+ "git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/cache-sibling || exit 2\n" +
+ `exit ${hookExitCode}\n`,
+ );
+ yield* fs.chmod(path.join(cwd, ".git/hooks/post-checkout"), 0o755);
+ const worktreePath = path.join(yield* makeTmpDir("git-worktrees-"), "created");
+ spawnedArgs.length = 0;
+ invalidationFailure = commonDirFailure;
+
+ const created = yield* driver
+ .createWorktree({
+ cwd,
+ path: worktreePath,
+ refName: initialBranch,
+ newRefName: "feature/cache-created",
+ })
+ .pipe(Effect.result);
+
+ assert.equal(Result.isSuccess(created), hookExitCode === 0);
+ if (Result.isFailure(created)) {
+ assert.equal(created.failure.operation, "GitVcsDriver.createWorktree");
+ assert.equal(created.failure.detail, "git worktree add failed");
+ assert.equal(created.failure.exitCode, hookExitCode);
+ }
+ const metadataQueries = spawnedArgs.filter((args) => args.includes("--git-common-dir"));
+ assert.deepStrictEqual(metadataQueries, [["rev-parse", "--git-common-dir"]]);
+ assert.isFalse(spawnedArgs.some((args) => args.includes("symbolic-ref")));
+ invalidationFailure = "none";
+
+ spawnedArgs.length = 0;
+ const creatorStatus = yield* driver.statusDetailsLocal(cwd);
+ assert.equal(creatorStatus.branch, "creator-current");
+ assert.isTrue(spawnedArgs.some((args) => args.includes("--git-common-dir")));
+ const siblingRefs = yield* driver.listRefs({ cwd: siblingPath });
+ const createdRef = siblingRefs.refs.find((ref) => ref.name === "feature/cache-created");
+ assert.isDefined(createdRef?.worktreePath);
+ assert.equal(
+ NodeFS.realpathSync.native(createdRef!.worktreePath!),
+ NodeFS.realpathSync.native(worktreePath),
+ );
+ assert.isTrue(siblingRefs.hasPrimaryRemote);
+ assert.isTrue(siblingRefs.refs.find((ref) => ref.name === "cache-sibling")?.current);
+ const statusAfter = yield* driver.statusDetailsLocal(siblingPath);
+ assert.isTrue(statusAfter.hasOriginRemote);
+ assert.isTrue(statusAfter.isDefaultBranch);
+ const creatorRefs = yield* driver.listRefs({ cwd });
+ assert.isTrue(creatorRefs.refs.find((ref) => ref.name === "creator-current")?.current);
+ assert.isFalse(creatorRefs.refs.find((ref) => ref.name === initialBranch)?.current);
+ }),
+ ).pipe(Effect.provide(layerTest)),
+);
+
+it.effect.each(["branch", "detached", "nested", "aliased-root", "aliased-nested"] as const)(
+ "discovers only the common directory during worktree creation from %s",
+ (location) =>
+ Effect.scoped(
+ Effect.gen(function* () {
+ const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
+ const spawnedArgs: Array<ReadonlyArray<string>> = [];
+ const spawner = ChildProcessSpawner.make((command) => {
+ if (ChildProcess.isStandardCommand(command)) spawnedArgs.push(command.args);
+ return delegate.spawn(command);
+ });
+ const driver = yield* makeGitVcsDriverCore().pipe(
+ Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
+ );
+ const cwd = yield* makeTmpDir();
+ const { initialBranch } = yield* initRepoWithCommit(cwd);
+ const path = yield* Path.Path;
+ if (location === "detached") yield* git(cwd, ["checkout", "--detach"]);
+ const isNested = location === "nested" || location === "aliased-nested";
+ const physicalCwd = isNested ? path.join(cwd, "nested") : cwd;
+ const fs = yield* FileSystem.FileSystem;
+ yield* fs.makeDirectory(physicalCwd, { recursive: true });
+ const isAliased = location === "aliased-root" || location === "aliased-nested";
+ const creationCwd = isAliased
+ ? path.join(yield* makeTmpDir("git-alias-"), "link")
+ : physicalCwd;
+ if (isAliased) NodeFS.symlinkSync(physicalCwd, creationCwd, "junction");
+ const worktreePath = path.join(yield* makeTmpDir("git-worktrees-"), "cold");
+
+ yield* driver.createWorktree({
+ cwd: creationCwd,
+ path: worktreePath,
+ refName: initialBranch,
+ newRefName: "feature/cold",
+ });
+
+ assert.equal(spawnedArgs.filter((args) => args.includes("--git-common-dir")).length, 1);
+ assert.isFalse(spawnedArgs.some((args) => args.includes("--show-toplevel")));
+ assert.isFalse(spawnedArgs.some((args) => args.includes("symbolic-ref")));
+ spawnedArgs.length = 0;
+ const originalStatus = yield* driver.statusDetailsLocal(creationCwd);
+ assert.equal(originalStatus.branch, location === "detached" ? null : initialBranch);
+ const originalRefs = yield* driver.listRefs({ cwd: creationCwd });
+ assert.equal(originalRefs.isAtRepositoryRoot, !isNested);
+ assert.deepStrictEqual(
+ originalRefs.refs.filter((ref) => ref.current).map((ref) => ref.name),
+ location === "detached" ? [] : [initialBranch],
+ );
+ assert.equal(spawnedArgs.filter((args) => args.includes("--git-common-dir")).length, 1);
+ const refs = yield* driver.listRefs({ cwd: worktreePath });
+ assert.isTrue(refs.refs.find((ref) => ref.name === "feature/cold")?.current);
+ assert.equal(
+ yield* git(worktreePath, ["rev-parse", "HEAD"]),
+ yield* git(cwd, ["rev-parse", "HEAD"]),
+ );
+ }),
+ ).pipe(Effect.provide(layerTest)),
+);
+
+it.effect.each(["bare", "unborn"] as const)(
+ "invalidates a %s repository after worktree creation",
+ (state) =>
+ Effect.scoped(
+ Effect.gen(function* () {
+ const source = yield* makeTmpDir();
+ const cwd = yield* makeTmpDir("git-discovery-fallback-");
+ if (state === "bare") {
+ yield* initRepoWithCommit(source);
+ yield* git(source, ["clone", "--bare", source, cwd]);
+ } else {
+ yield* git(cwd, ["init"]);
+ }
+ const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
+ const spawnedArgs: Array<ReadonlyArray<string>> = [];
+ const spawner = ChildProcessSpawner.make((command) => {
+ if (ChildProcess.isStandardCommand(command)) spawnedArgs.push(command.args);
+ return delegate.spawn(
+ state === "bare" ? ChildProcess.setEnv(command, { GIT_DIR: cwd }) : command,
+ );
+ });
+ const driver = yield* makeGitVcsDriverCore().pipe(
+ Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
+ );
+ const path = yield* Path.Path;
+ const worktreePath = path.join(yield* makeTmpDir("git-worktrees-"), "fallback");
+ const result = yield* driver
+ .createWorktree({
+ cwd,
+ path: worktreePath,
+ refName: "HEAD",
+ newRefName: "feature/fallback",
+ })
+ .pipe(Effect.result);
+ assert.equal(Result.isSuccess(result), state === "bare");
+ if (Result.isFailure(result)) {
+ assert.equal(result.failure.operation, "GitVcsDriver.createWorktree.createBranch");
+ assert.equal(result.failure.detail, "git worktree add failed");
+ }
+ assert.equal(spawnedArgs.filter((args) => args.includes("--git-common-dir")).length, 1);
+ assert.isFalse(spawnedArgs.some((args) => args[0] === "symbolic-ref"));
+ spawnedArgs.length = 0;
+ const refs = yield* driver.listRefs({ cwd });
+ assert.isTrue(refs.isRepo);
+ if (state === "bare") {
+ assert.isTrue(refs.refs.some((ref) => ref.name === "feature/fallback"));
+ } else {
+ assert.isEmpty(refs.refs);
+ }
+ assert.equal(spawnedArgs.filter((args) => args.includes("--git-common-dir")).length, 1);
+ }),
+ ).pipe(Effect.provide(layerTest)),
+);
+
+it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
+ "preserves embedded newlines in repository paths after worktree creation",
+ () =>
+ Effect.scoped(
+ Effect.gen(function* () {
+ const path = yield* Path.Path;
+ const fs = yield* FileSystem.FileSystem;
+ const cwd = path.join(yield* makeTmpDir(), "repo\nwith-newline");
+ yield* fs.makeDirectory(cwd);
+ const { initialBranch } = yield* initRepoWithCommit(cwd);
+ const driver = yield* GitVcsDriver.GitVcsDriver;
+ const worktreePath = path.join(yield* makeTmpDir("git-worktrees-"), "newline");
+ yield* driver.createWorktree({
+ cwd,
+ path: worktreePath,
+ refName: initialBranch,
+ newRefName: "feature/newline",
+ });
+ const status = yield* driver.statusDetailsLocal(cwd);
+ assert.equal(status.branch, initialBranch);
+ const refs = yield* driver.listRefs({ cwd });
+ assert.isTrue(refs.refs.find((ref) => ref.name === initialBranch)?.current);
+ assert.isTrue(refs.refs.some((ref) => ref.name === "feature/newline"));
+ }),
+ ).pipe(Effect.provide(layerTest)),
+);
+
+it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
+ "preserves a trailing carriage return in the repository root after creation through an alias",
+ () =>
+ Effect.scoped(
+ Effect.gen(function* () {
+ const path = yield* Path.Path;
+ const fs = yield* FileSystem.FileSystem;
+ const sandbox = yield* makeTmpDir();
+ const repository = path.join(sandbox, "repo\r");
+ const cwd = path.join(sandbox, "alias");
+ yield* fs.makeDirectory(repository);
+ const { initialBranch } = yield* initRepoWithCommit(repository);
+ NodeFS.symlinkSync(repository, cwd, "junction");
+ const driver = yield* GitVcsDriver.GitVcsDriver;
+ yield* driver.createWorktree({
+ cwd,
+ path: path.join(yield* makeTmpDir("git-worktrees-"), "created"),
+ refName: initialBranch,
+ newRefName: "feature/carriage-return",
+ });
+ const status = yield* driver.statusDetailsLocal(cwd);
+ assert.equal(status.workingTree.rootPath, NodeFS.realpathSync.native(repository));
+ assert.equal(status.branch, initialBranch);
+ const refs = yield* driver.listRefs({ cwd });
+ assert.isTrue(refs.isAtRepositoryRoot);
+ assert.isTrue(refs.refs.some((ref) => ref.name === "feature/carriage-return"));
+ }),
+ ).pipe(Effect.provide(layerTest)),
+);
+
+it.effect(
+ "reads fresh metadata when another mutation completes during common-directory discovery",
+ () =>
+ Effect.scoped(
+ Effect.gen(function* () {
+ const cwd = yield* makeTmpDir();
+ yield* initRepoWithCommit(cwd);
+ const commit = yield* git(cwd, ["rev-parse", "HEAD"]);
+ const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
+ const discoveryComplete = yield* Deferred.make<void>();
+ const releaseDiscovery = yield* Deferred.make<void>();
+ let holdDiscovery = false;
+ const spawner = ChildProcessSpawner.make((command) =>
+ Effect.gen(function* () {
+ const handle = yield* delegate.spawn(command);
+ if (
+ !holdDiscovery ||
+ !ChildProcess.isStandardCommand(command) ||
+ !command.args.includes("--git-common-dir")
+ )
+ return handle;
+ holdDiscovery = false;
+ const output = yield* Stream.runCollect(handle.stdout);
+ yield* handle.exitCode;
+ yield* Deferred.succeed(discoveryComplete, undefined);
+ yield* Deferred.await(releaseDiscovery);
+ return ChildProcessSpawner.makeHandle({
+ ...handle,
+ stdout: Stream.fromIterable(output),
+ });
+ }),
+ );
+ const driver = yield* makeGitVcsDriverCore().pipe(
+ Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
+ );
+ yield* driver.listRefs({ cwd, refresh: true });
+ const path = yield* Path.Path;
+ const worktreePath = path.join(yield* makeTmpDir("git-worktrees-"), "concurrent");
+ holdDiscovery = true;
+ const creation = yield* driver
+ .createWorktree({
+ cwd,
+ path: worktreePath,
+ refName: "HEAD",
+ newRefName: "feature/concurrent",
+ })
+ .pipe(Effect.forkChild({ startImmediately: true }));
+ yield* Effect.gen(function* () {
+ yield* Deferred.await(discoveryComplete);
+ yield* driver.switchRef({ cwd, refName: commit });
+ }).pipe(Effect.ensuring(Deferred.succeed(releaseDiscovery, undefined)));
+ const created = yield* Fiber.join(creation);
+ assert.equal(created.worktree.path, worktreePath);
+ const refs = yield* driver.listRefs({ cwd });
+ assert.isEmpty(refs.refs.filter((ref) => ref.current));
+ assert.isTrue(refs.refs.some((ref) => ref.name === "feature/concurrent"));
+ assert.equal((yield* driver.statusDetailsLocal(cwd)).branch, null);
+ }),
+ ).pipe(Effect.provide(layerTest)),
+);
+
+it.effect("invalidates cached metadata after interrupted worktree creation", () =>
+ Effect.scoped(
+ Effect.gen(function* () {
+ const cwd = yield* makeTmpDir();
+ yield* initRepoWithCommit(cwd);
+ const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
+ const discoveryComplete = yield* Deferred.make<void>();
+ const releaseDiscovery = yield* Deferred.make<void>();
+ let holdDiscovery = false;
+ const spawner = ChildProcessSpawner.make((command) =>
+ Effect.gen(function* () {
+ const handle = yield* delegate.spawn(command);
+ if (
+ !holdDiscovery ||
+ !ChildProcess.isStandardCommand(command) ||
+ !command.args.includes("--git-common-dir")
+ )
+ return handle;
+ holdDiscovery = false;
+ const output = yield* Stream.runCollect(handle.stdout);
+ yield* handle.exitCode;
+ yield* Deferred.succeed(discoveryComplete, undefined);
+ yield* Deferred.await(releaseDiscovery);
+ return ChildProcessSpawner.makeHandle({ ...handle, stdout: Stream.fromIterable(output) });
+ }),
+ );
+ const driver = yield* makeGitVcsDriverCore().pipe(
+ Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
+ );
+ yield* driver.listRefs({ cwd, refresh: true });
+ yield* driver.statusDetailsLocal(cwd);
+ yield* git(cwd, ["checkout", "-b", "creator-current"]);
+ const path = yield* Path.Path;
+ const worktreePath = path.join(yield* makeTmpDir("git-worktrees-"), "interrupted");
+ holdDiscovery = true;
+ const creation = yield* driver
+ .createWorktree({
+ cwd,
+ path: worktreePath,
+ refName: "HEAD",
+ newRefName: "feature/interrupted",
+ })
+ .pipe(Effect.forkChild({ startImmediately: true }));
+ const cancellation = yield* Effect.gen(function* () {
+ yield* Deferred.await(discoveryComplete);
+ return yield* Fiber.interrupt(creation).pipe(Effect.forkChild({ startImmediately: true }));
+ }).pipe(Effect.ensuring(Deferred.succeed(releaseDiscovery, undefined)));
+ yield* Fiber.join(cancellation);
+ const exit = yield* Fiber.await(creation);
+ assert.isTrue(Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause));
+ const refs = yield* driver.listRefs({ cwd });
+ assert.isTrue(refs.refs.find((ref) => ref.name === "creator-current")?.current);
+ const interruptedRef = refs.refs.find((ref) => ref.name === "feature/interrupted");
+ assert.isDefined(interruptedRef?.worktreePath);
+ assert.equal(
+ NodeFS.realpathSync.native(interruptedRef!.worktreePath!),
+ NodeFS.realpathSync.native(worktreePath),
+ );
+ assert.equal((yield* driver.statusDetailsLocal(cwd)).branch, "creator-current");
+ const nextPath = path.join(yield* makeTmpDir("git-worktrees-"), "after-interruption");
+ yield* driver.createWorktree({
+ cwd,
+ path: nextPath,
+ refName: "HEAD",
+ newRefName: "feature/after",
+ });
+ assert.isTrue(
+ (yield* driver.listRefs({ cwd })).refs.some((ref) => ref.name === "feature/after"),
+ );
+ }),
+ ).pipe(Effect.provide(layerTest)),
+);
+
describe("pullCurrentBranch", () => {
// A fast-forward that prints over 1 MB takes thousands of files, so the real pull's output is
// padded past the cap instead.
@@ -2624,10 +3035,23 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
yield* git(repo, ["checkout", "-b", "real-only"]);
// The decoy must exist before the first lookup, which caches repository paths.
- const refs = yield* (yield* GitVcsDriver.GitVcsDriver).listRefs({ cwd: link });
+ const driver = yield* GitVcsDriver.GitVcsDriver;
+ const refs = yield* driver.listRefs({ cwd: link });
assert.isTrue(refs.isRepo);
assert.isFalse(refs.refs.some((ref) => ref.name === "decoy-only"));
assert.isTrue(refs.refs.some((ref) => ref.name === "real-only"));
+
+ yield* driver.createWorktree({
+ cwd: link,
+ path: pathService.join(yield* makeTmpDir("git-worktrees-"), "real"),
+ refName: "real-only",
+ newRefName: "feature/real-created",
+ });
+ const createdRefs = yield* driver.listRefs({ cwd: link });
+ assert.isFalse(createdRefs.isAtRepositoryRoot);
+ assert.isFalse(createdRefs.refs.some((ref) => ref.name === "decoy-only"));
+ assert.isTrue(createdRefs.refs.some((ref) => ref.name === "real-only" && ref.current));
+ assert.isTrue(createdRefs.refs.some((ref) => ref.name === "feature/real-created"));
}),
);
diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts
index c5bdf3d068e..79086ab9e9f 100644
--- a/apps/server/src/vcs/GitVcsDriverCore.ts
+++ b/apps/server/src/vcs/GitVcsDriverCore.ts
@@ -1371,9 +1371,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
}
});
- const resolveRepositoryPathsUncached = Effect.fn("resolveRepositoryPathsUncached")(function* (
- cwd: string,
- ) {
+ const readGitCommonDir = Effect.fn("readGitCommonDir")(function* (cwd: string) {
// Git reports relative paths from the physical cwd, which can differ from a symlink.
const commonDirResult = yield* executeGitWithStableDiagnostics(
"GitVcsDriver.resolveRepositoryPaths.commonDir",
@@ -1407,9 +1405,16 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
const resolvedGitCommonDir = path.isAbsolute(commonDirOutput)
? path.normalize(commonDirOutput)
: path.resolve(physicalCwd, commonDirOutput);
- const gitCommonDir = yield* fileSystem
+ return yield* fileSystem
.realPath(resolvedGitCommonDir)
.pipe(Effect.orElseSucceed(() => resolvedGitCommonDir));
+ });
+
+ const resolveRepositoryPathsUncached = Effect.fn("resolveRepositoryPathsUncached")(function* (
+ cwd: string,
+ ) {
+ const gitCommonDir = yield* readGitCommonDir(cwd);
+ if (gitCommonDir === null) return null;
const [worktreeRootResult, currentBranchResult] = yield* Effect.all(
[
executeGit(
@@ -1547,11 +1552,14 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
},
);
const invalidateStatusStaticCaches = Effect.fn("GitVcsDriverCore.invalidateStatusStaticCaches")(
- function* (cwd: string) {
- const repositoryPaths = yield* resolveRepositoryPaths(cwd).pipe(
- Effect.catchTags({ GitCommandError: () => Effect.succeed(null) }),
- );
- const cacheKey = repositoryPaths?.gitCommonDir ?? normalizeRepositoryPathsCacheKey(cwd);
+ function* (cwd: string, gitCommonDir?: string | null) {
+ const commonDir =
+ gitCommonDir === undefined
+ ? (yield* resolveRepositoryPaths(cwd).pipe(
+ Effect.catchTags({ GitCommandError: () => Effect.succeed(null) }),
+ ))?.gitCommonDir
+ : gitCommonDir;
+ const cacheKey = commonDir ?? normalizeRepositoryPathsCacheKey(cwd);
yield* Cache.invalidate(defaultBranchCache, cacheKey);
yield* Cache.invalidate(originExistsCache, cacheKey);
},
@@ -3943,17 +3951,21 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
});
const invalidateListRefsSnapshot = Effect.fn("invalidateListRefsSnapshot")(function* (
cwd: string,
+ gitCommonDir?: string | null,
) {
const repositoryPathsCacheKey = normalizeRepositoryPathsCacheKey(cwd);
- const repositoryPaths = yield* Cache.get(repositoryPathsCache, repositoryPathsCacheKey);
- if (repositoryPaths === null) return;
- const previousGeneration = currentListRefsGeneration(repositoryPaths.gitCommonDir);
- bumpListRefsGeneration(repositoryPaths.gitCommonDir);
- bumpListRefsEpoch(repositoryPaths.gitCommonDir);
+ const commonDir =
+ gitCommonDir === undefined
+ ? (yield* Cache.get(repositoryPathsCache, repositoryPathsCacheKey))?.gitCommonDir
+ : gitCommonDir;
+ if (commonDir == null) return;
+ const previousGeneration = currentListRefsGeneration(commonDir);
+ bumpListRefsGeneration(commonDir);
+ bumpListRefsEpoch(commonDir);
yield* Cache.invalidate(
listRefsRefreshSnapshotCache,
new GitRefsRefreshCacheKey({
- gitCommonDir: repositoryPaths.gitCommonDir,
+ gitCommonDir: commonDir,
generation: previousGeneration,
}),
);
@@ -4824,6 +4836,26 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
]),
),
);
+ const invalidateWorktreeCreationCaches = Effect.fn(
+ "GitVcsDriverCore.invalidateWorktreeCreationCaches",
+ )(function* (cwd: string) {
+ let gitCommonDir = yield* readGitCommonDir(cwd).pipe(
+ Effect.catchTags({ GitCommandError: () => Effect.succeed(null) }),
+ );
+ if (gitCommonDir === null) {
+ gitCommonDir = yield* Cache.getOption(
+ repositoryPathsCache,
+ normalizeRepositoryPathsCacheKey(cwd),
+ ).pipe(
+ Effect.map((cached) => Option.getOrNull(cached)?.gitCommonDir ?? null),
+ Effect.orElseSucceed(() => null),
+ );
+ }
+ yield* Effect.all([
+ invalidateListRefsSnapshot(cwd, gitCommonDir).pipe(Effect.ignore),
+ invalidateStatusStaticCaches(cwd, gitCommonDir).pipe(Effect.ignore),
+ ]);
+ });
const initRepoWithListRefsInvalidation: GitVcsDriver.GitVcsDriver["Service"]["initRepo"] = (
input,
) =>
@@ -4864,7 +4896,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
resolveBranchName,
listRefs,
createWorktree: Effect.fnUntraced(function* (input, options) {
- return yield* withListRefsInvalidation(input.cwd, createWorktree(input, options));
+ return yield* createWorktree(input, options).pipe(
+ Effect.ensuring(invalidateWorktreeCreationCaches(input.cwd)),
+ );
}),
fetchRefIntoBranch: Effect.fnUntraced(function* (input) {
return yield* withListRefsInvalidation(input.cwd, fetchRefIntoBranch(input));