diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts index d9a661c7b1237..690671e17a728 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.test.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts @@ -2651,8 +2651,8 @@ it.effect("cancels a slow submodule checkout without reporting best-effort succe const spawner = ChildProcessSpawner.make((command) => { if ( ChildProcess.isStandardCommand(command) && - command.args[0] === "submodule" && - command.args[1] === "update" + command.args.includes("submodule") && + command.args.includes("update") ) { return Effect.acquireRelease( Effect.succeed( @@ -7549,8 +7549,8 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { Effect.gen(function* () { if ( ChildProcess.isStandardCommand(command) && - command.args[0] === "submodule" && - command.args[1] === "update" + command.args.includes("submodule") && + command.args.includes("update") ) { yield* Deferred.succeed(updateStarted, undefined); yield* Deferred.await(releaseUpdate); @@ -7664,6 +7664,44 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { }).pipe(withGitFileProtocol), ); + it.effect("borrows submodule objects from the source checkout without keeping a link", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const pathService = yield* Path.Path; + const submoduleRepo = yield* makeTmpDir("git-submodule-"); + yield* initRepoWithCommit(submoduleRepo); + yield* writeTextFile(submoduleRepo, "SHARED.md", "# shared\n"); + yield* git(submoduleRepo, ["add", "."]); + yield* git(submoduleRepo, ["commit", "-m", "shared"]); + const cwd = yield* makeTmpDir(); + const { initialBranch } = yield* initRepoWithCommit(cwd); + yield* git(cwd, ["submodule", "add", NodeURL.pathToFileURL(submoduleRepo).href, "shared"]); + yield* git(cwd, ["commit", "-m", "add submodule"]); + // The remote keeps its refs but can no longer serve objects; only the source has them. + const remoteObjects = pathService.join(submoduleRepo, ".git", "objects"); + yield* fileSystem.remove(remoteObjects, { recursive: true }); + yield* fileSystem.makeDirectory(pathService.join(remoteObjects, "pack"), { + recursive: true, + }); + + const worktreePath = pathService.join(yield* makeTmpDir("git-worktrees-"), "borrowed"); + const driver = yield* GitVcsDriver.GitVcsDriver; + yield* driver.createWorktree( + { cwd, path: worktreePath, refName: initialBranch, newRefName: "feature/borrowed" }, + { submodules: "recursive" }, + ); + + const shared = pathService.join(worktreePath, "shared"); + assert.equal( + yield* fileSystem.readFileString(pathService.join(shared, "SHARED.md")), + "# shared\n", + ); + yield* fileSystem.remove(pathService.join(cwd, ".git", "modules"), { recursive: true }); + yield* git(shared, ["fsck", "--no-progress"]); + assert.equal(yield* git(worktreePath, ["status", "--porcelain"]), ""); + }).pipe(withGitFileProtocol), + ); + it.effect("still creates the worktree when submodule checkout fails", () => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts index 8f3e3e16ca12b..69662f6902dbf 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.ts @@ -6104,26 +6104,38 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* yield* progress.onSubmodulesStarted(); } const onSubmoduleLine = progress?.onSubmoduleLine; - const updateSubmodules = (paths: readonly string[]) => + const updateSubmodules = (paths: readonly string[], borrowEnv?: NodeJS.ProcessEnv) => runGit( "GitVcsDriver.createWorktree.updateSubmodules", worktreePath, [ + ...(borrowEnv + ? [ + "-c", + "submodule.alternateLocation=superproject", + // Git checks each source module and clones the unusable ones (shallow, missing). + "-c", + "submodule.alternateErrorStrategy=ignore", + ] + : []), "submodule", "update", "--init", ...(submoduleMode.value === "recursive" ? ["--recursive"] : []), // Git reports transfer progress to a pipe only when forced. ...(onSubmoduleLine ? ["--progress"] : []), + // Copies the borrowed objects, so the worktree never depends on the source checkout. + ...(borrowEnv ? ["--dissociate"] : []), // A clone left by a failed update already has HEAD at the gitlink, so Git would skip it. ...(paths.length > 0 ? ["--force", "--", ...paths] : []), ], { // Cloning scales with remote size; let it finish or be cancelled, like worktree add. timeoutMs: null, + ...(borrowEnv ? { env: borrowEnv } : {}), ...(onSubmoduleLine ? { - env: { LANGUAGE: "C" }, + env: { ...borrowEnv, LANGUAGE: "C" }, progress: { onStdoutLine: onSubmoduleLine, onStderrLine: onSubmoduleLine }, // Progress for many submodules can pass the output cap; truncate instead of failing. appendTruncationMarker: true, @@ -6160,6 +6172,39 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* yield* updateSubmodules(clonedPaths); } }); + // superproject borrows from each alternate's modules/, but a linked worktree shares + // the source's object store instead of listing it as an alternate. Behind an empty primary + // store it becomes one, so Git clones each submodule from the source's copy and fetches + // only what that lacks. + const borrowSubmodules = Effect.gen(function* () { + const objectsPath = yield* runGitStdout( + "GitVcsDriver.createWorktree.objectsPath", + worktreePath, + ["rev-parse", "--git-path", "objects"], + ); + const objectDirectories = yield* runGitStdout( + "GitVcsDriver.createWorktree.alternates", + worktreePath, + ["count-objects", "-v"], + ); + // Git's relative paths use the physical cwd. + const physicalWorktree = yield* fileSystem.realPath(worktreePath); + const emptyObjects = yield* fileSystem.makeTempDirectoryScoped({ + prefix: "t3code-submodule-objects-", + }); + yield* updateSubmodules( + [], + privateObjectDirectoryEnv( + emptyObjects, + path.resolve(physicalWorktree, objectsPath.trim()), + objectDirectories, + ), + ); + }).pipe( + Effect.scoped, + // Older Git lacks --dissociate, and a failed clone leaves the others unchecked-out. + Effect.catch(() => updateSubmodules([]).pipe(Effect.andThen(checkOutClonedSubmodules))), + ); // Registering submodule URLs writes the shared config; the clones that follow do not. yield* runGit( "GitVcsDriver.createWorktree.initSubmodules", @@ -6167,7 +6212,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* ["submodule", "init"], { writesSharedConfig: true }, ).pipe( - Effect.andThen(updateSubmodules([])), + Effect.andThen(borrowSubmodules), Effect.matchEffect({ onFailure: (cause) => Effect.logWarning("worktree submodule checkout failed; submodules may be incomplete", {