From e58175d381c34044696a6d0e9dfcaa95d6fe4fce Mon Sep 17 00:00:00 2001 From: Neil Mazumdar <84065477+NeilMazumdar@users.noreply.github.com> Date: Sat, 7 Nov 2026 09:13:21 +1030 Subject: [PATCH 01/14] feat(git): show file diffs inside submodules --- apps/server/src/vcs/GitVcsDriverCore.test.ts | 235 ++++++++ apps/server/src/vcs/GitVcsDriverCore.ts | 529 ++++++++++++++----- docs/user/source-control.md | 12 + 3 files changed, 633 insertions(+), 143 deletions(-) diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts index d9a5e7d2b9fde..7ab94e02cf687 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.test.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts @@ -3028,6 +3028,241 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { }), ); + it.effect("reviews files recursively inside submodules without changing their indexes", () => + withGitFileProtocol( + Effect.gen(function* () { + const cwd = yield* makeTmpDir(); + const inner = yield* makeTmpDir(); + const nested = yield* makeTmpDir(); + const { initialBranch } = yield* initRepoWithCommit(cwd); + yield* initRepoWithCommit(inner); + yield* initRepoWithCommit(nested); + yield* git(inner, ["submodule", "add", nested, "nested"]); + yield* git(inner, ["commit", "-am", "add nested module"]); + const modulePath = "modules/space repo"; + yield* git(cwd, ["submodule", "add", inner, modulePath]); + yield* git(cwd, ["submodule", "add", inner, "modules/clean repo"]); + yield* git(cwd, ["commit", "-am", "add module"]); + yield* git(cwd, ["checkout", "-b", "feature/submodule-review"]); + const path = yield* Path.Path; + const moduleCwd = path.join(cwd, modulePath); + yield* git(moduleCwd, ["submodule", "update", "--init", "--recursive"]); + yield* git(moduleCwd, ["config", "user.name", "Test"]); + yield* git(moduleCwd, ["config", "user.email", "test@test.com"]); + yield* writeTextFile(moduleCwd, "README.md", "# committed in module\n"); + yield* git(moduleCwd, ["commit", "-am", "change module file"]); + yield* writeTextFile(moduleCwd, "staged.txt", "staged\n"); + yield* git(moduleCwd, ["add", "staged.txt"]); + yield* writeTextFile(moduleCwd, "untracked.txt", "untracked\n"); + yield* writeTextFile(moduleCwd, "nested/README.md", "# nested dirty\n"); + yield* git(cwd, ["config", "diff.ignoreSubmodules", "all"]); + yield* git(cwd, ["config", `submodule.${modulePath}.ignore`, "all"]); + const indexBefore = yield* git(moduleCwd, ["diff", "--cached"]); + const commands: { cwd: string | undefined; args: ReadonlyArray }[] = []; + const realSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; + const driver = yield* makeGitVcsDriverCore().pipe( + Effect.provideService( + ChildProcessSpawner.ChildProcessSpawner, + ChildProcessSpawner.make((command) => { + if (ChildProcess.isStandardCommand(command)) + commands.push({ cwd: command.options.cwd, args: command.args.map(String) }); + return realSpawner.spawn(command); + }), + ), + ); + for (const metadataOnly of [true, false]) { + const preview = yield* driver.getReviewDiffPreview({ + cwd, + baseRef: initialBranch, + metadataOnly, + }); + const changes = preview.sources.find((source) => source.kind === "branch-range")!; + const status = yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true }); + assert.equal( + status.branchChanges?.insertions, + changes.files!.reduce((n, file) => n + file.additions, 0), + ); + assert.equal( + status.branchChanges?.deletions, + changes.files!.reduce((n, file) => n + file.deletions, 0), + ); + for (const source of preview.sources) { + assert.deepStrictEqual( + source.files?.map((file) => file.path).toSorted(), + [ + `${modulePath}/README.md`, + `${modulePath}/nested/README.md`, + `${modulePath}/staged.txt`, + `${modulePath}/untracked.txt`, + ].toSorted(), + ); + for (const file of source.files ?? []) { + const scoped = yield* driver.getReviewDiffPreview({ + cwd, + baseRef: initialBranch, + file: { ...file, sourceKind: source.kind }, + }); + const patch = scoped.sources.find((candidate) => candidate.kind === source.kind)!; + assert.deepStrictEqual(patch.files, [file]); + assert.include(patch.diff, `b/${file.path}`); + } + } + } + assert.isFalse( + commands.some( + (command) => command.cwd?.includes("clean repo") && command.args.includes("diff"), + ), + ); + const projectDirectories = [cwd, path.join(cwd, "modules")]; + if (symlinksSupported) { + const alias = path.join(yield* makeTmpDir(), "alias"); + yield* (yield* FileSystem.FileSystem).symlink(cwd, alias); + projectDirectories.push(alias); + } + for (const projectCwd of projectDirectories) { + assert.deepStrictEqual( + yield* driver.getReviewDiffFileContents( + makeReviewDiffFileContentsInput(projectCwd, { + oldPath: `${modulePath}/README.md`, + newPath: `${modulePath}/README.md`, + }), + ), + { oldContents: "# test\n", newContents: "# committed in module\n" }, + ); + } + assert.equal(yield* git(moduleCwd, ["diff", "--cached"]), indexBefore); + assert.equal(yield* git(cwd, ["diff", "--cached"]), ""); + yield* git(cwd, ["add", modulePath]); + yield* git(cwd, ["commit", "-m", "advance module pin"]); + const commitSha = yield* git(cwd, ["rev-parse", "HEAD"]); + const committed = yield* driver.getReviewDiffPreview({ cwd, commitSha }); + assert.deepStrictEqual( + committed.sources[0]?.files?.map((file) => file.path), + [`${modulePath}/README.md`], + ); + assert.notInclude(committed.sources[0]!.diff, "nested dirty"); + const fileSystem = yield* FileSystem.FileSystem; + const manifest = path.join(cwd, ".gitmodules"); + const originalManifest = yield* fileSystem.readFileString(manifest); + yield* fileSystem.remove(manifest); + const noManifest = yield* driver.getReviewDiffPreview({ + cwd, + sourceKind: "working-tree", + metadataOnly: true, + }); + assert.isTrue( + noManifest.sources[0]!.files!.some( + (file) => file.path === `${modulePath}/untracked.txt`, + ), + ); + yield* fileSystem.writeFileString(manifest, originalManifest); + yield* fileSystem.rename(manifest, `${manifest}.saved`); + const renamedManifest = yield* driver.getReviewDiffPreview({ + cwd, + sourceKind: "working-tree", + metadataOnly: true, + }); + assert.isTrue( + renamedManifest.sources[0]!.files!.some( + (file) => file.path === `${modulePath}/untracked.txt`, + ), + ); + yield* fileSystem.rename(`${manifest}.saved`, manifest); + yield* writeTextFile(inner, "README.md", "# not fetched locally\n"); + yield* git(inner, ["commit", "-am", "advance upstream only"]); + const missingSha = yield* git(inner, ["rev-parse", "HEAD"]); + yield* git(cwd, ["update-index", "--cacheinfo", `160000,${missingSha},${modulePath}`]); + yield* git(cwd, ["commit", "-m", "record an unfetched module commit"]); + const missingCommit = yield* git(cwd, ["rev-parse", "HEAD"]); + for (const metadataOnly of [true, false]) { + const fallback = yield* driver.getReviewDiffPreview({ + cwd, + commitSha: missingCommit, + metadataOnly, + }); + assert.deepStrictEqual( + fallback.sources[0]!.files!.map((file) => file.path), + [modulePath], + ); + if (!metadataOnly) assert.include(fallback.sources[0]!.diff, "Subproject commit"); + } + }), + ), + ); + + it.effect( + "reviews a small commit in a parent tree whose complete listing exceeds the metadata cap", + () => + withGitFileProtocol( + Effect.gen(function* () { + const cwd = yield* makeTmpDir(); + const child = yield* makeTmpDir(); + yield* initRepoWithCommit(cwd); + yield* initRepoWithCommit(child); + yield* git(cwd, ["submodule", "add", child, "mod"]); + yield* git(cwd, ["commit", "-am", "add module"]); + const driver = yield* GitVcsDriver.GitVcsDriver; + const original = yield* git(cwd, ["rev-parse", "HEAD"]); + const blob = (yield* driver.execute({ + operation: "test.blob", + cwd, + args: ["hash-object", "-w", "--stdin"], + stdin: "", + })).stdout.trim(); + const env = { GIT_INDEX_FILE: (yield* Path.Path).join(yield* makeTmpDir(), "index") }; + yield* git(cwd, ["read-tree", "HEAD"], env); + const directory = `d/${"x".repeat(180)}`; + const info = Array.from( + { length: 70_000 }, + (_, index) => `100644 ${blob}\t${directory}/${String(index).padStart(6, "0")}.txt\n`, + ).join(""); + yield* driver.execute({ + operation: "test.largeIndex", + cwd, + args: ["update-index", "--index-info"], + env, + stdin: info, + }); + const tree = yield* git(cwd, ["write-tree"], env); + const big = yield* git(cwd, ["commit-tree", tree, "-p", original, "-m", "large tree"]); + yield* git( + cwd, + ["update-index", "--add", "--cacheinfo", `100644,${blob},small.txt`], + env, + ); + const nextTree = yield* git(cwd, ["write-tree"], env); + const small = yield* git(cwd, [ + "commit-tree", + nextTree, + "-p", + big, + "-m", + "small change", + ]); + const preview = yield* driver.getReviewDiffPreview({ + cwd, + commitSha: small, + metadataOnly: true, + }); + assert.deepStrictEqual( + preview.sources[0]!.files!.map((file) => file.path), + ["small.txt"], + ); + const contents = yield* driver.getReviewDiffFileContents( + makeReviewDiffFileContentsInput(cwd, { + sourceKind: "commit", + changeType: "new", + baseRef: original, + headRef: big, + oldPath: `${directory}/000000.txt`, + newPath: `${directory}/000000.txt`, + }), + ); + assert.deepStrictEqual(contents, { oldContents: "", newContents: "" }); + }), + ), + ); + it.effect("loads full file contents for working-tree diff expansion", () => Effect.gen(function* () { const cwd = yield* makeTmpDir(); diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts index 4dabf3b493ebe..1b0e215d36871 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.ts @@ -3260,47 +3260,25 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* roots.length <= 128 && roots.reduce((length, root) => length + root.length + 16, 0) < 8_000 ? roots.map((root) => `:(top,literal)${root}`) : []; - // Empty status can mean untracked files are hidden; retain the full scan. - const untracked = yield* prepareUntrackedReviewIndex(cwd, pathspecs); - if (untracked === null) { - return yield* new GitCommandError({ - operation: "GitVcsDriver.readBranchChangeTotals", - command: "git ls-files", - cwd, - detail: "Too many untracked files to count.", - }); - } - const readNumstat = (ref: string) => - executeGit( - "GitVcsDriver.readBranchChangeTotals", - cwd, - [...REVIEW_DIFF_ARGS, "--numstat", "-z", ref, "--"], - { - allowNonZeroExit: true, - maxOutputBytes: REVIEW_METADATA_MAX_OUTPUT_BYTES, - env: untracked.env, - }, - ); - let result = yield* readNumstat(mergeBase); - if (result.exitCode !== 0 && mergeBase === "HEAD" && isUnbornHeadStderr(result.stderr)) { - result = yield* readNumstat(yield* readEmptyTreeHash(cwd)); - } - if (result.exitCode !== 0) { + const result = yield* readReviewComparison({ + cwd, + baseRef: mergeBase, + metadataOnly: true, + minimal: false, + untrackedPathspecs: pathspecs, + }); + if (!result.files) return yield* new GitCommandError({ operation: "GitVcsDriver.readBranchChangeTotals", command: "git diff --numstat", cwd, - detail: "Could not read Changes totals.", - exitCode: result.exitCode, + detail: "Could not read complete Changes totals.", }); - } - let insertions = 0; - let deletions = 0; - for (const file of parseReviewNumstat(result.stdout)) { - insertions += file.additions; - deletions += file.deletions; - } - return { baseRef, insertions, deletions }; + return { + baseRef, + insertions: result.files.reduce((total, file) => total + file.additions, 0), + deletions: result.files.reduce((total, file) => total + file.deletions, 0), + }; }, Effect.scoped); const reviewCommitError = (cwd: string, detail: string) => @@ -3438,17 +3416,326 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* }; }); + const readReviewGitlinks = Effect.fn("readReviewGitlinks")(function* ( + cwd: string, + revision: string | undefined, + paths: ReadonlyArray, + ) { + if (paths.length === 0) return new Map(); + const pathspecs = paths.map((value) => `:(top,literal)${value}`); + const result = yield* executeGit( + "GitVcsDriver.review.gitlinks", + cwd, + revision + ? ["ls-tree", "--full-name", "-z", "--end-of-options", revision, "--", ...pathspecs] + : ["ls-files", "--stage", "-z", "--", ...pathspecs], + { maxOutputBytes: REVIEW_METADATA_MAX_OUTPUT_BYTES }, + ); + const links = new Map(); + for (const record of result.stdout.split("\0")) { + const match = /^160000 (?:commit )?([a-f0-9]+)(?: 0)?\t([\s\S]+)$/.exec(record); + if (match) links.set(match[2]!, match[1]!); + } + return links; + }); + + const readReviewSubmodulePaths = Effect.fn("readReviewSubmodulePaths")(function* ( + cwd: string, + revision: string | undefined, + ) { + const result = yield* executeGit( + "GitVcsDriver.review.submodulePaths", + cwd, + [ + "config", + "-z", + revision !== undefined ? `--blob=${revision}:.gitmodules` : "--file=.gitmodules", + "--get-regexp", + "^submodule\\..*\\.path$", + ], + { + allowNonZeroExit: true, + maxOutputBytes: REVIEW_METADATA_MAX_OUTPUT_BYTES, + env: { GIT_NO_LAZY_FETCH: "1" }, + }, + ); + return result.exitCode === 0 + ? result.stdout.split("\0").flatMap((record) => { + const separator = record.indexOf("\n"); + return separator < 0 ? [] : [record.slice(separator + 1)]; + }) + : []; + }); + + const readDirtyReviewSubmodules = Effect.fn("readDirtyReviewSubmodules")(function* ( + cwd: string, + paths: ReadonlyArray, + ) { + const result = yield* executeGit( + "GitVcsDriver.review.submoduleStatus", + cwd, + [ + "status", + "--porcelain=2", + "-z", + "--ignore-submodules=none", + "--untracked-files=no", + "--", + ...paths.map((value) => `:(top,literal)${value}`), + ], + { maxOutputBytes: REVIEW_METADATA_MAX_OUTPUT_BYTES }, + ); + const dirty = new Set(); + for (const record of result.stdout.split("\0")) { + const sub = record.split(" ", 3)[2]; + if (sub?.startsWith("S") && sub !== "S...") { + const value = parsePorcelainPath(record); + if (value) dirty.add(value); + } + } + return dirty; + }); + + const resolveReviewSubmodule = Effect.fn("resolveReviewSubmodule")(function* ( + cwd: string, + relativePath: string, + ) { + const candidate = path.resolve(cwd, relativePath); + if (!isPathWithinRoot(cwd, candidate) || candidate === cwd) return null; + if ( + !(yield* fileSystem + .exists(path.join(candidate, ".git")) + .pipe(Effect.orElseSucceed(() => false))) + ) + return null; + const [root, target] = yield* Effect.all([ + fileSystem.realPath(cwd), + fileSystem.realPath(candidate), + ]).pipe(Effect.orElseSucceed(() => [null, null] as const)); + if (!root || !target || !isPathWithinRoot(root, target) || root === target) return null; + if (yield* isServerInternalPath(target)) return null; + const actualRoot = yield* runGitStdout("GitVcsDriver.review.submoduleRoot", candidate, [ + "rev-parse", + "--show-toplevel", + ]); + return path.resolve(actualRoot.trim()) === target ? target : null; + }); + + const readReviewComparison = Effect.fn("readReviewComparison")(function* (options: { + cwd: string; + baseRef: string; + headRef?: string | undefined; + file?: ReviewDiffPreviewInput["file"]; + metadataOnly: boolean; + trackedOnly?: boolean | undefined; + ignoreWhitespace?: boolean | undefined; + minimal?: boolean | undefined; + prefix?: string; + indexes?: Map> | undefined; + statuses?: Map> | undefined; + untrackedPathspecs?: ReadonlyArray | undefined; + }): Effect.fn.Return< + { + stdout: string; + stdoutTruncated: boolean; + files: ReadonlyArray | undefined; + }, + GitCommandError, + Scope.Scope + > { + const { cwd, file, headRef } = options; + const prefix = options.prefix ?? ""; + const paths = file ? [file.path, ...(file.previousPath ? [file.previousPath] : [])] : []; + const pathArgs = paths.map((value) => `:(top,literal)${value}`); + let preparedIndex = options.indexes?.get(cwd); + if (!preparedIndex && !headRef && !options.trackedOnly) { + preparedIndex = yield* Effect.cached( + prepareUntrackedReviewIndex(cwd, options.untrackedPathspecs ?? pathArgs, file?.path), + ); + options.indexes?.set(cwd, preparedIndex); + } + const untracked = preparedIndex ? yield* preparedIndex : { env: undefined }; + const metadataOnly = options.metadataOnly && untracked !== null; + const args = [ + ...REVIEW_DIFF_ARGS, + "--ignore-submodules=dirty", + ...(options.minimal === false ? [] : ["--minimal"]), + ...(options.ignoreWhitespace ? ["--ignore-all-space"] : []), + `--src-prefix=a/${prefix}`, + `--dst-prefix=b/${prefix}`, + ]; + let baseRef = options.baseRef; + const readStat = () => + executeGit( + headRef + ? "GitVcsDriver.getReviewDiffPreview.commitStat" + : "GitVcsDriver.getReviewDiffPreview.stat", + cwd, + [...args, "--numstat", "-z", baseRef, ...(headRef ? [headRef] : []), "--", ...pathArgs], + { + allowNonZeroExit: true, + maxOutputBytes: REVIEW_METADATA_MAX_OUTPUT_BYTES, + env: untracked?.env, + }, + ); + let stat = yield* readStat(); + if (stat.exitCode !== 0 && baseRef === "HEAD" && isUnbornHeadStderr(stat.stderr)) { + baseRef = yield* readEmptyTreeHash(cwd); + stat = yield* readStat(); + } + if (stat.exitCode !== 0) + return yield* new GitCommandError({ + operation: headRef + ? "GitVcsDriver.getReviewDiffPreview.commitStat" + : "GitVcsDriver.getReviewDiffPreview.stat", + command: "git diff --numstat", + cwd, + detail: "Could not read complete diff statistics.", + exitCode: stat.exitCode, + }); + const expanded = new Map< + string, + { + stdout: string; + stdoutTruncated: boolean; + files: ReadonlyArray | undefined; + } + >(); + const ownStats = parseReviewNumstat(stat.stdout); + const manifestExists = yield* fileSystem + .exists(path.join(cwd, ".gitmodules")) + .pipe(Effect.orElseSucceed(() => false)); + if ( + manifestExists || + headRef !== undefined || + file !== undefined || + ownStats.some((entry) => entry.path === ".gitmodules" || entry.previousPath === ".gitmodules") + ) { + const declarations = yield* Effect.all( + [ + readReviewSubmodulePaths(cwd, baseRef), + readReviewSubmodulePaths(cwd, headRef ?? ""), + manifestExists ? readReviewSubmodulePaths(cwd, undefined) : Effect.succeed([]), + ], + { concurrency: 3 }, + ); + const modulePaths = Array.from(new Set(declarations.flat())).filter( + (value) => + value.length > 0 && value !== "." && isPathWithinRoot(cwd, path.resolve(cwd, value)), + ); + const statusKey = `${cwd}\0${modulePaths.join("\0")}`; + let status = options.statuses?.get(statusKey); + if (!status && !headRef && modulePaths.length > 0) { + status = yield* Effect.cached(readDirtyReviewSubmodules(cwd, modulePaths)); + options.statuses?.set(statusKey, status); + } + const dirty = status ? yield* status : new Set(); + const candidates = modulePaths.filter((value) => + file + ? paths.some((candidate) => candidate.startsWith(`${value}/`)) + : dirty.has(value) || ownStats.some((entry) => entry.path === value), + ); + const [before, after] = yield* Effect.all( + [ + readReviewGitlinks(cwd, baseRef, candidates), + readReviewGitlinks(cwd, headRef, candidates), + ], + { concurrency: 2 }, + ); + const children = yield* Effect.forEach( + candidates, + (modulePath) => + Effect.gen(function* () { + const moduleCwd = yield* resolveReviewSubmodule(cwd, modulePath); + if (!moduleCwd || !after.has(modulePath)) return null; + const base = before.get(modulePath) ?? (yield* readEmptyTreeHash(moduleCwd)); + const endpoints = [base, ...(headRef ? [after.get(modulePath)!] : [])]; + const available = yield* Effect.forEach( + endpoints, + (sha) => + executeGit( + "GitVcsDriver.review.submoduleBase", + moduleCwd, + ["cat-file", "-e", `${sha}^{tree}`], + { allowNonZeroExit: true, env: { GIT_NO_LAZY_FETCH: "1" } }, + ), + { concurrency: 2 }, + ); + if (available.some((result) => result.exitCode !== 0)) return null; + const child = yield* readReviewComparison({ + ...options, + cwd: moduleCwd, + baseRef: base, + ...(headRef ? { headRef: after.get(modulePath)! } : {}), + prefix: `${prefix}${modulePath}/`, + untrackedPathspecs: undefined, + file: file + ? { + ...file, + path: file.path.slice(modulePath.length + 1), + previousPath: file.previousPath?.startsWith(`${modulePath}/`) + ? file.previousPath.slice(modulePath.length + 1) + : null, + } + : undefined, + }); + return child.files?.length === 0 ? null : ([modulePath, child] as const); + }), + { concurrency: 4 }, + ); + for (const child of children) if (child) expanded.set(child[0], child[1]); + } + const ownFiles = ownStats + .filter((entry) => !expanded.has(entry.path)) + .map((entry) => ({ + ...entry, + path: `${prefix}${entry.path}`, + previousPath: entry.previousPath === null ? null : `${prefix}${entry.previousPath}`, + })); + const limit = file ? REVIEW_DIFF_FILE_MAX_OUTPUT_BYTES : REVIEW_DIFF_PATCH_MAX_OUTPUT_BYTES; + const patch = + metadataOnly || ownFiles.length === 0 + ? { stdout: "", stdoutTruncated: false } + : yield* executeGit( + headRef + ? "GitVcsDriver.getReviewDiffPreview.commitPatch" + : "GitVcsDriver.getReviewDiffPreview.patch", + cwd, + [ + ...args, + "--patch", + baseRef, + ...(headRef ? [headRef] : []), + "--", + ...pathArgs, + ...Array.from(expanded.keys(), (value) => `:(top,exclude,literal)${value}`), + ], + { maxOutputBytes: limit, appendTruncationMarker: true, env: untracked?.env }, + ); + const children = Array.from(expanded.values()); + const bytes = new TextEncoder().encode( + [patch.stdout, ...children.map((child) => child.stdout)].join(""), + ); + const truncated = + untracked === null || + patch.stdoutTruncated || + children.some((child) => child.stdoutTruncated) || + bytes.length > limit; + return { + stdout: + new TextDecoder().decode(bytes.subarray(0, limit)) + + (bytes.length > limit ? OUTPUT_TRUNCATED_MARKER : ""), + stdoutTruncated: truncated, + files: + untracked === null || children.some((child) => child.files === undefined) + ? undefined + : [...ownFiles, ...children.flatMap((child) => child.files ?? [])], + }; + }); + const getReviewDiffPreview = Effect.fn("getReviewDiffPreview")(function* ( input: ReviewDiffPreviewInput, ) { - const pathArgs = input.file - ? [input.file.path, ...(input.file.previousPath ? [input.file.previousPath] : [])].map( - (path) => `:(top,literal)${path}`, - ) - : []; - const patchLimit = input.file - ? REVIEW_DIFF_FILE_MAX_OUTPUT_BYTES - : REVIEW_DIFF_PATCH_MAX_OUTPUT_BYTES; const sourceKind = input.file?.sourceKind ?? input.sourceKind; const needsCurrentBranch = input.commitSha === undefined && @@ -3473,6 +3760,8 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* } const cwd = repository.worktreeRoot; + const indexes = new Map>(); + const statuses = new Map>(); const hashDiff = (diff: string, files: ReadonlyArray) => digest.digest(Digest.sha256, new TextEncoder().encode(JSON.stringify([diff, files]))).pipe( Effect.map(Hex.encode), @@ -3493,26 +3782,17 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* } const commit = yield* resolveReviewCommit(cwd, input.commitSha); const { subject } = yield* readReviewCommitMessage(cwd, commit.sha); - const diffArgs = [ - ...REVIEW_DIFF_ARGS, - ...(input.ignoreWhitespace ? ["--ignore-all-space"] : []), - ]; - const stat = yield* executeGit( - "GitVcsDriver.getReviewDiffPreview.commitStat", + const patch = yield* readReviewComparison({ + indexes, + statuses, cwd, - [...diffArgs, "--numstat", "-z", commit.parent, commit.sha, "--", ...pathArgs], - { maxOutputBytes: REVIEW_METADATA_MAX_OUTPUT_BYTES }, - ); - const files = parseReviewNumstat(stat.stdout); - const patch = - input.metadataOnly && !input.file - ? { stdout: "", stdoutTruncated: false } - : yield* executeGit( - "GitVcsDriver.getReviewDiffPreview.commitPatch", - cwd, - [...diffArgs, "--patch", commit.parent, commit.sha, "--", ...pathArgs], - { maxOutputBytes: patchLimit, appendTruncationMarker: true }, - ); + baseRef: commit.parent, + headRef: commit.sha, + file: input.file, + metadataOnly: input.metadataOnly === true && !input.file, + ignoreWhitespace: input.ignoreWhitespace, + }); + const files = patch.files ?? []; const diffHash = yield* hashDiff(patch.stdout, files); return { cwd: input.cwd, @@ -3541,81 +3821,29 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* ? { baseRef: input.baseRef ?? null, mergeBase: null } : yield* resolveReviewMergeBase(cwd, repository.currentBranch, input.baseRef); - const diffArgs = [ - ...REVIEW_DIFF_ARGS, - "--minimal", - ...(input.ignoreWhitespace ? ["--ignore-all-space"] : []), - ]; - const readStats = Effect.fn("GitVcsDriver.getReviewDiffPreview.stat")(function* ( - ref: string, - env?: NodeJS.ProcessEnv, - ) { - const args = [...diffArgs, "--numstat", "-z"]; - const result = yield* executeGit( - "GitVcsDriver.getReviewDiffPreview.stat", - cwd, - [...args, ref, "--", ...pathArgs], - { allowNonZeroExit: true, maxOutputBytes: REVIEW_METADATA_MAX_OUTPUT_BYTES, env }, - ); - if (result.exitCode === 0) return { ref, files: parseReviewNumstat(result.stdout) }; - if (ref === "HEAD" && isUnbornHeadStderr(result.stderr)) { - const emptyTree = yield* readEmptyTreeHash(cwd); - const stdout = yield* runGitStdoutWithOptions( - "GitVcsDriver.getReviewDiffPreview.unbornStat", - cwd, - [...args, emptyTree, "--", ...pathArgs], - { maxOutputBytes: REVIEW_METADATA_MAX_OUTPUT_BYTES, env }, - ); - return { ref: emptyTree, files: parseReviewNumstat(stdout) }; - } - return yield* new GitCommandError({ - operation: "GitVcsDriver.getReviewDiffPreview.stat", - cwd, - command: "git diff --numstat", - detail: "Could not read complete diff statistics.", - exitCode: result.exitCode, - }); - }); - const readTrackedDiff = Effect.fn("GitVcsDriver.getReviewDiffPreview.tracked")(function* ( - ref: string | null, - env?: NodeJS.ProcessEnv, - metadataOnly = false, - ) { - if (ref === null) return { stdout: "", stdoutTruncated: false, files: [] }; - const stat = yield* readStats(ref, env); - if (metadataOnly || stat.files.length === 0) { - return { stdout: "", stdoutTruncated: false, files: stat.files }; - } - const patch = yield* executeGit( - "GitVcsDriver.getReviewDiffPreview.patch", - cwd, - [...diffArgs, "--patch", stat.ref, "--", ...pathArgs], - { maxOutputBytes: patchLimit, appendTruncationMarker: true, env }, - ); - return { ...patch, files: stat.files }; - }); - const [dirtyTrackedResult, baseResult] = yield* Effect.gen(function* () { - const untracked = input.trackedOnly - ? { env: undefined } - : yield* prepareUntrackedReviewIndex(cwd, pathArgs, input.file?.path); - const metadataOnly = input.metadataOnly === true && !input.file && untracked !== null; - const [dirty, base] = - review.mergeBase === dirtyRef - ? yield* readTrackedDiff(dirtyRef, untracked?.env, metadataOnly).pipe( - Effect.map((result) => [result, result] as const), - ) - : yield* Effect.all( - [ - readTrackedDiff(dirtyRef, untracked?.env, metadataOnly), - readTrackedDiff(review.mergeBase, untracked?.env, metadataOnly), - ], - { concurrency: 2 }, - ); - if (untracked !== null) return [dirty, base] as const; - const incomplete = (ref: string | null, result: typeof dirty) => - ref === null ? result : { ...result, files: undefined, stdoutTruncated: true }; - return [incomplete(dirtyRef, dirty), incomplete(review.mergeBase, base)] as const; - }).pipe(Effect.scoped); + const readTrackedDiff = (ref: string | null) => + ref === null + ? Effect.succeed({ + stdout: "", + stdoutTruncated: false, + files: [] as ReadonlyArray, + }) + : readReviewComparison({ + indexes, + statuses, + cwd, + baseRef: ref, + file: input.file, + metadataOnly: input.metadataOnly === true && !input.file, + ignoreWhitespace: input.ignoreWhitespace, + trackedOnly: input.trackedOnly, + }); + const [dirtyTrackedResult, baseResult] = + review.mergeBase === dirtyRef + ? yield* readTrackedDiff(dirtyRef).pipe(Effect.map((result) => [result, result] as const)) + : yield* Effect.all([readTrackedDiff(dirtyRef), readTrackedDiff(review.mergeBase)], { + concurrency: 2, + }); const dirtyFiles = dirtyTrackedResult.files; const baseFiles = baseResult.files; const dirtyDiff = dirtyTrackedResult.stdout; @@ -3664,7 +3892,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* : sources, ...(input.trackedOnly ? { incomplete: true } : {}), }; - }); + }, Effect.scoped); const reviewDiffFileError = ( input: ReviewDiffFileContentsInput, @@ -3691,7 +3919,21 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* input: ReviewDiffFileContentsInput, revision: string, relativePath: string, - ) { + ): Effect.fn.Return { + const parts = relativePath.split("/"); + const ancestors = parts.slice(0, -1).map((_, index) => parts.slice(0, index + 1).join("/")); + const links = yield* readReviewGitlinks(input.cwd, revision, ancestors); + for (const [modulePath, sha] of links) { + if (!relativePath.startsWith(`${modulePath}/`)) continue; + const moduleCwd = yield* resolveReviewSubmodule(input.cwd, modulePath); + if (!moduleCwd) + return yield* reviewDiffFileError(input, "Submodule is not available locally."); + return yield* readReviewFileAtRevision( + { ...input, cwd: moduleCwd }, + sha, + relativePath.slice(modulePath.length + 1), + ); + } const result = yield* executeGit( "GitVcsDriver.getReviewDiffFileContents.revision", input.cwd, @@ -3783,6 +4025,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* if (repositoryRoot.length === 0) { return yield* reviewDiffFileError(input, "Could not resolve the Git repository root."); } + const revisionInput = { ...input, cwd: repositoryRoot }; if (input.sourceKind === "commit") { if (!input.headRef) { return yield* reviewDiffFileError(input, "Selected commit is no longer available."); @@ -3798,10 +4041,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* [ input.changeType === "new" ? Effect.succeed("") - : readReviewFileAtRevision(input, commit.parent, input.oldPath), + : readReviewFileAtRevision(revisionInput, commit.parent, input.oldPath), input.changeType === "deleted" ? Effect.succeed("") - : readReviewFileAtRevision(input, commit.sha, input.newPath), + : readReviewFileAtRevision(revisionInput, commit.sha, input.newPath), ], { concurrency: 2 }, ); @@ -3815,7 +4058,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* [ input.changeType === "new" ? Effect.succeed("") - : readReviewFileAtRevision(input, oldRevision, input.oldPath), + : readReviewFileAtRevision(revisionInput, oldRevision, input.oldPath), input.changeType === "deleted" ? Effect.succeed("") : readWorkingTreeReviewFile(input, repositoryRoot), diff --git a/docs/user/source-control.md b/docs/user/source-control.md index 39731cb3d35b6..a517853e3b20d 100644 --- a/docs/user/source-control.md +++ b/docs/user/source-control.md @@ -20,6 +20,18 @@ commit links follow your **Open links in** setting; on web, when that opens a ne shows an **Open link** button. Command/Ctrl+Click always opens the link in your browser. Commit links follow the **Open pull requests in T3 Code** setting in **Settings → Diffs**. +## Review submodule changes + +Changes and Uncommitted include files inside initialized submodules, including nested submodules. +Paths include the submodule directory, so files with the same name in different repositories stay +distinct. Comparisons use the submodule commit recorded by the parent repository at the selected +base. Commit reviews compare the recorded submodule commits without including local edits. + +Initialize submodules and fetch their recorded commits to review their file contents. When those +are unavailable, the comparison shows the submodule commit pointer. Turn diffs, checkpoint restore, +staging, and committing still operate on the thread's repository. Commit submodule changes inside each +submodule before recording the updated pointer in the parent. + ## Connect an account Install Git and configure authentication on the machine running your T3 Code server. For a remote From e014d3c55a326f23e65acabfc6894ff5415d0e12 Mon Sep 17 00:00:00 2001 From: Neil Mazumdar <84065477+NeilMazumdar@users.noreply.github.com> Date: Sat, 7 Nov 2026 09:50:39 +1030 Subject: [PATCH 02/14] fix(git): preserve submodule paths and bounded pointer fallbacks --- apps/server/src/vcs/GitVcsDriverCore.test.ts | 131 +++++++++++++++- apps/server/src/vcs/GitVcsDriverCore.ts | 148 ++++++++++++++----- docs/user/source-control.md | 5 +- 3 files changed, 246 insertions(+), 38 deletions(-) diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts index 7ab94e02cf687..a370f551056e7 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.test.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts @@ -3037,7 +3037,9 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { const { initialBranch } = yield* initRepoWithCommit(cwd); yield* initRepoWithCommit(inner); yield* initRepoWithCommit(nested); + yield* writeTextFile(inner, "before.ts", "export const unchanged = true;\n"); yield* git(inner, ["submodule", "add", nested, "nested"]); + yield* git(inner, ["add", "before.ts"]); yield* git(inner, ["commit", "-am", "add nested module"]); const modulePath = "modules/space repo"; yield* git(cwd, ["submodule", "add", inner, modulePath]); @@ -3049,6 +3051,7 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { yield* git(moduleCwd, ["submodule", "update", "--init", "--recursive"]); yield* git(moduleCwd, ["config", "user.name", "Test"]); yield* git(moduleCwd, ["config", "user.email", "test@test.com"]); + yield* git(moduleCwd, ["mv", "before.ts", "renamed λ.ts"]); yield* writeTextFile(moduleCwd, "README.md", "# committed in module\n"); yield* git(moduleCwd, ["commit", "-am", "change module file"]); yield* writeTextFile(moduleCwd, "staged.txt", "staged\n"); @@ -3092,11 +3095,13 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { [ `${modulePath}/README.md`, `${modulePath}/nested/README.md`, + `${modulePath}/renamed λ.ts`, `${modulePath}/staged.txt`, `${modulePath}/untracked.txt`, ].toSorted(), ); for (const file of source.files ?? []) { + const start = commands.length; const scoped = yield* driver.getReviewDiffPreview({ cwd, baseRef: initialBranch, @@ -3104,7 +3109,14 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { }); const patch = scoped.sources.find((candidate) => candidate.kind === source.kind)!; assert.deepStrictEqual(patch.files, [file]); - assert.include(patch.diff, `b/${file.path}`); + assert.isFalse( + commands.slice(start).some((command) => command.args.includes("status")), + ); + if (file.previousPath) { + assert.equal(file.previousPath, `${modulePath}/before.ts`); + assert.include(patch.diff, `rename from ${file.previousPath}\n`); + assert.include(patch.diff, `rename to ${file.path}\n`); + } else assert.include(patch.diff, `b/${file.path}`); } } } @@ -3138,8 +3150,10 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { const committed = yield* driver.getReviewDiffPreview({ cwd, commitSha }); assert.deepStrictEqual( committed.sources[0]?.files?.map((file) => file.path), - [`${modulePath}/README.md`], + [`${modulePath}/README.md`, `${modulePath}/renamed λ.ts`], ); + assert.include(committed.sources[0]!.diff, `rename from ${modulePath}/before.ts\n`); + assert.include(committed.sources[0]!.diff, `rename to ${modulePath}/renamed λ.ts\n`); assert.notInclude(committed.sources[0]!.diff, "nested dirty"); const fileSystem = yield* FileSystem.FileSystem; const manifest = path.join(cwd, ".gitmodules"); @@ -3190,6 +3204,119 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { ), ); + it.effect("keeps parent changes when a submodule exceeds the untracked file cap", () => + withGitFileProtocol( + Effect.gen(function* () { + const cwd = yield* makeTmpDir(); + const inner = yield* makeTmpDir(); + yield* initRepoWithCommit(cwd); + yield* initRepoWithCommit(inner); + yield* git(cwd, ["submodule", "add", inner, "mod"]); + yield* git(cwd, ["commit", "-am", "add module"]); + yield* writeTextFile(cwd, "README.md", "# parent edit\n"); + yield* Effect.forEach( + Array.from({ length: 5_001 }, (_, index) => `mod/build/${index}.txt`), + (file) => writeTextFile(cwd, file, "untracked\n"), + { concurrency: 32, discard: true }, + ); + const commands: ReadonlyArray[] = []; + const realSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; + const driver = yield* makeGitVcsDriverCore().pipe( + Effect.provideService( + ChildProcessSpawner.ChildProcessSpawner, + ChildProcessSpawner.make((command) => { + if (ChildProcess.isStandardCommand(command)) + commands.push(command.args.map(String)); + return realSpawner.spawn(command); + }), + ), + ); + for (const metadataOnly of [true, false]) { + const preview = yield* driver.getReviewDiffPreview({ + cwd, + sourceKind: "working-tree", + metadataOnly, + }); + const source = preview.sources[0]!; + assert.deepStrictEqual( + source.files?.map((file) => file.path), + ["README.md", "mod"], + ); + assert.isTrue(source.truncated); + if (!metadataOnly) { + assert.include(source.diff, "+# parent edit"); + assert.include(source.diff, "Subproject commit"); + } + const status = yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true }); + assert.equal( + status.branchChanges?.insertions, + source.files!.reduce((n, f) => n + f.additions, 0), + ); + assert.equal( + status.branchChanges?.deletions, + source.files!.reduce((n, f) => n + f.deletions, 0), + ); + } + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + yield* fileSystem.rename(path.join(cwd, "mod/build"), path.join(cwd, "build")); + const start = commands.length; + const status = yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true }); + assert.isUndefined(status.branchChanges); + assert.isFalse(commands.slice(start).some((args) => args.includes("diff"))); + }), + ), + ); + + it.effect("keeps pointer diffs for added submodules and undeclared embedded repositories", () => + withGitFileProtocol( + Effect.gen(function* () { + const cwd = yield* makeTmpDir(); + const inner = yield* makeTmpDir(); + const { initialBranch } = yield* initRepoWithCommit(cwd); + yield* initRepoWithCommit(inner); + yield* git(cwd, ["checkout", "-b", "feature/add-module"]); + yield* git(cwd, ["submodule", "add", inner, "mod"]); + yield* git(cwd, ["commit", "-am", "add module"]); + const moduleCwd = (yield* Path.Path).join(cwd, "mod"); + const commands: { cwd: string | undefined; args: ReadonlyArray }[] = []; + const realSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; + const driver = yield* makeGitVcsDriverCore().pipe( + Effect.provideService( + ChildProcessSpawner.ChildProcessSpawner, + ChildProcessSpawner.make((command) => { + if (ChildProcess.isStandardCommand(command)) + commands.push({ cwd: command.options.cwd, args: command.args.map(String) }); + return realSpawner.spawn(command); + }), + ), + ); + const preview = yield* driver.getReviewDiffPreview({ cwd, baseRef: initialBranch }); + const source = preview.sources.find((source) => source.kind === "branch-range")!; + assert.deepStrictEqual( + source.files?.map((file) => file.path), + [".gitmodules", "mod"], + ); + assert.include(source.diff, "Subproject commit"); + assert.isDefined( + (yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true })).branchChanges, + ); + assert.isFalse( + commands.some((command) => command.cwd === moduleCwd && command.args.includes("diff")), + ); + yield* git(cwd, ["rm", ".gitmodules"]); + yield* git(cwd, ["commit", "-m", "leave an embedded gitlink"]); + yield* writeTextFile(moduleCwd, "README.md", "# embedded edit\n"); + const embedded = yield* driver.getReviewDiffPreview({ cwd, sourceKind: "working-tree" }); + assert.deepStrictEqual( + embedded.sources[0]!.files?.map((file) => file.path), + ["mod"], + ); + assert.include(embedded.sources[0]!.diff, "-dirty"); + }), + ), + ); + it.effect( "reviews a small commit in a parent tree whose complete listing exceeds the metadata cap", () => diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts index 1b0e215d36871..bfb30fd71d351 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.ts @@ -33,6 +33,7 @@ import { type VcsRef, } from "@t3tools/contracts"; import { dedupeRemoteBranchesWithLocalMatches } from "@t3tools/shared/git"; +import { quoteGitPatchPath, unquoteGitPatchPath } from "@t3tools/shared/gitPatchPath"; import * as HostProcess from "@t3tools/shared/HostProcess"; import { compactTraceAttributes } from "@t3tools/shared/observability"; import { decodeJsonResult } from "@t3tools/shared/schemaJson"; @@ -3264,6 +3265,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* cwd, baseRef: mergeBase, metadataOnly: true, + completeFilesOnly: true, minimal: false, untrackedPathspecs: pathspecs, }); @@ -3527,6 +3529,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* headRef?: string | undefined; file?: ReviewDiffPreviewInput["file"]; metadataOnly: boolean; + completeFilesOnly?: boolean | undefined; trackedOnly?: boolean | undefined; ignoreWhitespace?: boolean | undefined; minimal?: boolean | undefined; @@ -3547,31 +3550,42 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* const prefix = options.prefix ?? ""; const paths = file ? [file.path, ...(file.previousPath ? [file.previousPath] : [])] : []; const pathArgs = paths.map((value) => `:(top,literal)${value}`); - let preparedIndex = options.indexes?.get(cwd); - if (!preparedIndex && !headRef && !options.trackedOnly) { + const includeUntracked = !headRef && !options.trackedOnly; + let preparedIndex = includeUntracked ? options.indexes?.get(cwd) : undefined; + if (!preparedIndex && includeUntracked) { preparedIndex = yield* Effect.cached( prepareUntrackedReviewIndex(cwd, options.untrackedPathspecs ?? pathArgs, file?.path), ); options.indexes?.set(cwd, preparedIndex); } const untracked = preparedIndex ? yield* preparedIndex : { env: undefined }; + if (untracked === null && options.completeFilesOnly) + return { stdout: "", stdoutTruncated: true, files: undefined }; const metadataOnly = options.metadataOnly && untracked !== null; const args = [ ...REVIEW_DIFF_ARGS, - "--ignore-submodules=dirty", ...(options.minimal === false ? [] : ["--minimal"]), ...(options.ignoreWhitespace ? ["--ignore-all-space"] : []), `--src-prefix=a/${prefix}`, `--dst-prefix=b/${prefix}`, ]; let baseRef = options.baseRef; - const readStat = () => + const readStat = (selectedPaths = pathArgs, extraArgs: ReadonlyArray = []) => executeGit( headRef ? "GitVcsDriver.getReviewDiffPreview.commitStat" : "GitVcsDriver.getReviewDiffPreview.stat", cwd, - [...args, "--numstat", "-z", baseRef, ...(headRef ? [headRef] : []), "--", ...pathArgs], + [ + ...args, + ...extraArgs, + "--numstat", + "-z", + baseRef, + ...(headRef ? [headRef] : []), + "--", + ...selectedPaths, + ], { allowNonZeroExit: true, maxOutputBytes: REVIEW_METADATA_MAX_OUTPUT_BYTES, @@ -3601,7 +3615,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* files: ReadonlyArray | undefined; } >(); - const ownStats = parseReviewNumstat(stat.stdout); + let submodulesTruncated = false; + let fallbackPaths: string[] = []; + let ownStats = parseReviewNumstat(stat.stdout); const manifestExists = yield* fileSystem .exists(path.join(cwd, ".gitmodules")) .pipe(Effect.orElseSucceed(() => false)); @@ -3625,30 +3641,35 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* ); const statusKey = `${cwd}\0${modulePaths.join("\0")}`; let status = options.statuses?.get(statusKey); - if (!status && !headRef && modulePaths.length > 0) { + if (!status && !headRef && !file && modulePaths.length > 0) { status = yield* Effect.cached(readDirtyReviewSubmodules(cwd, modulePaths)); options.statuses?.set(statusKey, status); } const dirty = status ? yield* status : new Set(); - const candidates = modulePaths.filter((value) => - file - ? paths.some((candidate) => candidate.startsWith(`${value}/`)) - : dirty.has(value) || ownStats.some((entry) => entry.path === value), + const possiblePaths = modulePaths.filter( + (value) => !file || paths.some((candidate) => candidate.startsWith(`${value}/`)), ); const [before, after] = yield* Effect.all( [ - readReviewGitlinks(cwd, baseRef, candidates), - readReviewGitlinks(cwd, headRef, candidates), + readReviewGitlinks(cwd, baseRef, possiblePaths), + readReviewGitlinks(cwd, headRef, possiblePaths), ], { concurrency: 2 }, ); + const candidates = possiblePaths.filter( + (value) => + file || + dirty.has(value) || + ownStats.some((entry) => entry.path === value) || + before.get(value) !== after.get(value), + ); const children = yield* Effect.forEach( candidates, (modulePath) => Effect.gen(function* () { const moduleCwd = yield* resolveReviewSubmodule(cwd, modulePath); - if (!moduleCwd || !after.has(modulePath)) return null; - const base = before.get(modulePath) ?? (yield* readEmptyTreeHash(moduleCwd)); + if (!moduleCwd || !before.has(modulePath) || !after.has(modulePath)) return null; + const base = before.get(modulePath)!; const endpoints = [base, ...(headRef ? [after.get(modulePath)!] : [])]; const available = yield* Effect.forEach( endpoints, @@ -3656,7 +3677,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* executeGit( "GitVcsDriver.review.submoduleBase", moduleCwd, - ["cat-file", "-e", `${sha}^{tree}`], + ["cat-file", "-e", `${sha}^{commit}`], { allowNonZeroExit: true, env: { GIT_NO_LAZY_FETCH: "1" } }, ), { concurrency: 2 }, @@ -3664,6 +3685,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* if (available.some((result) => result.exitCode !== 0)) return null; const child = yield* readReviewComparison({ ...options, + completeFilesOnly: true, cwd: moduleCwd, baseRef: base, ...(headRef ? { headRef: after.get(modulePath)! } : {}), @@ -3683,7 +3705,30 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* }), { concurrency: 4 }, ); - for (const child of children) if (child) expanded.set(child[0], child[1]); + for (const child of children) { + if (!child) continue; + if (child[1].files === undefined) submodulesTruncated = true; + else expanded.set(child[0], child[1]); + } + fallbackPaths = candidates.filter((value) => !expanded.has(value)); + if (fallbackPaths.length > 0) { + const fallback = yield* readStat( + fallbackPaths.map((value) => `:(top,literal)${value}`), + ["--ignore-submodules=none"], + ); + if (fallback.exitCode !== 0) + return yield* new GitCommandError({ + operation: "GitVcsDriver.review.submoduleStat", + command: "git diff --numstat", + cwd, + detail: "Could not read submodule pointer statistics.", + exitCode: fallback.exitCode, + }); + ownStats = [ + ...ownStats.filter((entry) => !fallbackPaths.includes(entry.path)), + ...parseReviewNumstat(fallback.stdout), + ]; + } } const ownFiles = ownStats .filter((entry) => !expanded.has(entry.path)) @@ -3693,32 +3738,65 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* previousPath: entry.previousPath === null ? null : `${prefix}${entry.previousPath}`, })); const limit = file ? REVIEW_DIFF_FILE_MAX_OUTPUT_BYTES : REVIEW_DIFF_PATCH_MAX_OUTPUT_BYTES; - const patch = + const readPatch = ( + selectedPaths: ReadonlyArray, + extraArgs: ReadonlyArray = [], + ) => + executeGit( + headRef + ? "GitVcsDriver.getReviewDiffPreview.commitPatch" + : "GitVcsDriver.getReviewDiffPreview.patch", + cwd, + [ + ...args, + ...extraArgs, + "--patch", + baseRef, + ...(headRef ? [headRef] : []), + "--", + ...selectedPaths, + ], + { maxOutputBytes: limit, appendTruncationMarker: true, env: untracked?.env }, + ); + const patches = metadataOnly || ownFiles.length === 0 - ? { stdout: "", stdoutTruncated: false } - : yield* executeGit( - headRef - ? "GitVcsDriver.getReviewDiffPreview.commitPatch" - : "GitVcsDriver.getReviewDiffPreview.patch", - cwd, + ? [] + : yield* Effect.all( [ - ...args, - "--patch", - baseRef, - ...(headRef ? [headRef] : []), - "--", - ...pathArgs, - ...Array.from(expanded.keys(), (value) => `:(top,exclude,literal)${value}`), + readPatch([ + ...pathArgs, + ...Array.from( + [...expanded.keys(), ...fallbackPaths], + (value) => `:(top,exclude,literal)${value}`, + ), + ]), + ...(fallbackPaths.length > 0 + ? [ + readPatch( + fallbackPaths.map((value) => `:(top,literal)${value}`), + ["--ignore-submodules=none"], + ), + ] + : []), ], - { maxOutputBytes: limit, appendTruncationMarker: true, env: untracked?.env }, + { concurrency: 2 }, ); const children = Array.from(expanded.values()); + const patchText = patches.map((patch) => patch.stdout).join(""); + // Git leaves extended rename/copy headers relative to the child despite src/dst prefixes. + const ownPatch = prefix + ? patchText.replace( + /^(rename (?:from|to)|copy (?:from|to)) (.+)$/gm, + (_, kind, value) => `${kind} ${quoteGitPatchPath(prefix + unquoteGitPatchPath(value))}`, + ) + : patchText; const bytes = new TextEncoder().encode( - [patch.stdout, ...children.map((child) => child.stdout)].join(""), + [ownPatch, ...children.map((child) => child.stdout)].join(""), ); const truncated = untracked === null || - patch.stdoutTruncated || + submodulesTruncated || + patches.some((patch) => patch.stdoutTruncated) || children.some((child) => child.stdoutTruncated) || bytes.length > limit; return { @@ -3727,7 +3805,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* (bytes.length > limit ? OUTPUT_TRUNCATED_MARKER : ""), stdoutTruncated: truncated, files: - untracked === null || children.some((child) => child.files === undefined) + untracked === null ? undefined : [...ownFiles, ...children.flatMap((child) => child.files ?? [])], }; diff --git a/docs/user/source-control.md b/docs/user/source-control.md index a517853e3b20d..a41d6a3d13547 100644 --- a/docs/user/source-control.md +++ b/docs/user/source-control.md @@ -26,9 +26,12 @@ Changes and Uncommitted include files inside initialized submodules, including n Paths include the submodule directory, so files with the same name in different repositories stay distinct. Comparisons use the submodule commit recorded by the parent repository at the selected base. Commit reviews compare the recorded submodule commits without including local edits. +Review includes declared submodules even when Git's submodule ignore settings hide them from summaries. Initialize submodules and fetch their recorded commits to review their file contents. When those -are unavailable, the comparison shows the submodule commit pointer. Turn diffs, checkpoint restore, +are unavailable, or a comparison adds or removes a submodule, it shows the submodule commit pointer. +If a submodule has too many untracked files to list, its pointer remains and the comparison is marked +as truncated. Files in the parent and other submodules remain available. Turn diffs, checkpoint restore, staging, and committing still operate on the thread's repository. Commit submodule changes inside each submodule before recording the updated pointer in the parent. From 37edc711d0e6e23f4e70ffbc2983800934c605e5 Mon Sep 17 00:00:00 2001 From: Neil Mazumdar <84065477+NeilMazumdar@users.noreply.github.com> Date: Sat, 7 Nov 2026 10:09:45 +1030 Subject: [PATCH 03/14] fix(git): keep submodule patches in bounded review fallbacks --- apps/server/src/vcs/GitVcsDriverCore.test.ts | 40 +++++++++++++++++++- apps/server/src/vcs/GitVcsDriverCore.ts | 6 ++- docs/user/source-control.md | 4 +- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts index a370f551056e7..e97367132b47c 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.test.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts @@ -3199,6 +3199,13 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { [modulePath], ); if (!metadataOnly) assert.include(fallback.sources[0]!.diff, "Subproject commit"); + const pointer = yield* driver.getReviewDiffPreview({ + cwd, + commitSha: missingCommit, + file: { path: modulePath, previousPath: null, sourceKind: "commit" }, + }); + assert.deepStrictEqual(pointer.sources[0]!.files, fallback.sources[0]!.files); + assert.include(pointer.sources[0]!.diff, "Subproject commit"); } }), ), @@ -3209,7 +3216,7 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { Effect.gen(function* () { const cwd = yield* makeTmpDir(); const inner = yield* makeTmpDir(); - yield* initRepoWithCommit(cwd); + const { initialBranch } = yield* initRepoWithCommit(cwd); yield* initRepoWithCommit(inner); yield* git(cwd, ["submodule", "add", inner, "mod"]); yield* git(cwd, ["commit", "-am", "add module"]); @@ -3257,9 +3264,40 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { source.files!.reduce((n, f) => n + f.deletions, 0), ); } + for (const [key, value] of [ + ["diff.ignoreSubmodules", "dirty"], + ["submodule.mod.ignore", "all"], + ] as const) { + yield* git(cwd, ["config", key, value]); + const pointer = yield* driver.getReviewDiffPreview({ + cwd, + file: { path: "mod", previousPath: null, sourceKind: "working-tree" }, + }); + assert.deepStrictEqual( + pointer.sources[0]!.files?.map((file) => file.path), + ["mod"], + ); + assert.include(pointer.sources[0]!.diff, "Subproject commit"); + yield* git(cwd, ["config", "--unset", key]); + } const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; yield* fileSystem.rename(path.join(cwd, "mod/build"), path.join(cwd, "build")); + yield* writeTextFile(cwd, "mod/README.md", "# module edit\n"); + for (const sourceKind of ["working-tree", "branch-range"] as const) { + const preview = yield* driver.getReviewDiffPreview({ + cwd, + baseRef: initialBranch, + sourceKind, + metadataOnly: true, + }); + const source = preview.sources[0]!; + assert.isUndefined(source.files); + assert.isTrue(source.truncated); + assert.include(source.diff, "+# parent edit"); + assert.include(source.diff, "diff --git a/mod/README.md"); + assert.include(source.diff, "+# module edit"); + } const start = commands.length; const status = yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true }); assert.isUndefined(status.branchChanges); diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts index bfb30fd71d351..c04b7c8a43f3c 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.ts @@ -3647,7 +3647,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* } const dirty = status ? yield* status : new Set(); const possiblePaths = modulePaths.filter( - (value) => !file || paths.some((candidate) => candidate.startsWith(`${value}/`)), + (value) => + !file || + paths.some((candidate) => candidate === value || candidate.startsWith(`${value}/`)), ); const [before, after] = yield* Effect.all( [ @@ -3667,6 +3669,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* candidates, (modulePath) => Effect.gen(function* () { + if (file?.path === modulePath) return null; const moduleCwd = yield* resolveReviewSubmodule(cwd, modulePath); if (!moduleCwd || !before.has(modulePath) || !after.has(modulePath)) return null; const base = before.get(modulePath)!; @@ -3685,6 +3688,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* if (available.some((result) => result.exitCode !== 0)) return null; const child = yield* readReviewComparison({ ...options, + metadataOnly, completeFilesOnly: true, cwd: moduleCwd, baseRef: base, diff --git a/docs/user/source-control.md b/docs/user/source-control.md index a41d6a3d13547..40c356f27a768 100644 --- a/docs/user/source-control.md +++ b/docs/user/source-control.md @@ -30,8 +30,8 @@ Review includes declared submodules even when Git's submodule ignore settings hi Initialize submodules and fetch their recorded commits to review their file contents. When those are unavailable, or a comparison adds or removes a submodule, it shows the submodule commit pointer. -If a submodule has too many untracked files to list, its pointer remains and the comparison is marked -as truncated. Files in the parent and other submodules remain available. Turn diffs, checkpoint restore, +If a submodule has too many untracked files to list, the review shows its pointer instead of its files. +Files in the parent and other submodules remain available. Turn diffs, checkpoint restore, staging, and committing still operate on the thread's repository. Commit submodule changes inside each submodule before recording the updated pointer in the parent. From 84b02ed37cbfa1230c399f13ce8706674d40afb8 Mon Sep 17 00:00:00 2001 From: Neil Mazumdar <84065477+NeilMazumdar@users.noreply.github.com> Date: Sat, 7 Nov 2026 10:30:39 +1030 Subject: [PATCH 04/14] fix(git): align submodule fallback metadata with patch paths --- apps/server/src/vcs/GitVcsDriverCore.test.ts | 125 +++++++++++++++++-- apps/server/src/vcs/GitVcsDriverCore.ts | 66 +++++++--- docs/user/source-control.md | 2 + 3 files changed, 163 insertions(+), 30 deletions(-) diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts index e97367132b47c..fa913c9111da2 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.test.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts @@ -3313,10 +3313,23 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { const inner = yield* makeTmpDir(); const { initialBranch } = yield* initRepoWithCommit(cwd); yield* initRepoWithCommit(inner); + const modulePath = "mod[ules]"; + yield* writeTextFile(cwd, `${modulePath}/x.txt`, "x\n"); + yield* writeTextFile(cwd, `${modulePath}/y.txt`, "y\n"); + yield* writeTextFile( + cwd, + "outside.txt", + Array.from({ length: 20 }, (_, i) => `line ${i}\n`).join(""), + ); + yield* git(cwd, ["add", "."]); + yield* git(cwd, ["commit", "-m", "track vendored files"]); yield* git(cwd, ["checkout", "-b", "feature/add-module"]); - yield* git(cwd, ["submodule", "add", inner, "mod"]); + yield* git(cwd, ["rm", "-r", modulePath]); + yield* git(cwd, ["submodule", "add", inner, modulePath]); yield* git(cwd, ["commit", "-am", "add module"]); - const moduleCwd = (yield* Path.Path).join(cwd, "mod"); + const moduleCwd = (yield* Path.Path).join(cwd, modulePath); + const commitSha = yield* git(cwd, ["rev-parse", "HEAD"]); + const manifest = yield* git(cwd, ["show", "HEAD:.gitmodules"]); const commands: { cwd: string | undefined; args: ReadonlyArray }[] = []; const realSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; const driver = yield* makeGitVcsDriverCore().pipe( @@ -3329,16 +3342,66 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { }), ), ); - const preview = yield* driver.getReviewDiffPreview({ cwd, baseRef: initialBranch }); - const source = preview.sources.find((source) => source.kind === "branch-range")!; - assert.deepStrictEqual( - source.files?.map((file) => file.path), - [".gitmodules", "mod"], - ); - assert.include(source.diff, "Subproject commit"); - assert.isDefined( - (yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true })).branchChanges, + const assertPatchPaths = (source: ReviewDiffPreviewResult["sources"][number]) => { + const paths = source.files!.map((file) => file.path); + assert.equal(new Set(paths).size, paths.length); + assert.deepStrictEqual( + Array.from( + source.diff.matchAll(/^diff --git a\/.+ b\/(.+)$/gm), + (match) => match[1]!, + ).toSorted(), + paths.toSorted(), + ); + }; + for (const input of [ + { baseRef: initialBranch, sourceKind: "branch-range" }, + { commitSha }, + ] as const) { + const preview = yield* driver.getReviewDiffPreview({ cwd, ...input }); + const source = preview.sources.find( + (source) => source.kind === ("commitSha" in input ? "commit" : "branch-range"), + )!; + assert.deepStrictEqual( + source.files?.map((file) => file.path).toSorted(), + [".gitmodules", modulePath, `${modulePath}/x.txt`, `${modulePath}/y.txt`].toSorted(), + ); + assertPatchPaths(source); + assert.include(source.diff, "Subproject commit"); + const metadata = yield* driver.getReviewDiffPreview({ + cwd, + ...input, + metadataOnly: true, + }); + assert.deepStrictEqual( + metadata.sources.find((item) => item.kind === source.kind)!.files, + source.files, + ); + for (const file of source.files!) { + const scoped = yield* driver.getReviewDiffPreview({ + cwd, + ...input, + file: { ...file, sourceKind: source.kind }, + }); + const patch = scoped.sources.find((item) => item.kind === source.kind)!; + assert.deepStrictEqual(patch.files, [file]); + assertPatchPaths(patch); + } + } + const native = (yield* git(cwd, ["diff", "--numstat", initialBranch])).split("\n").reduce( + (sum, line) => { + const [added, removed] = line.split("\t"); + return { + insertions: sum.insertions + Number(added), + deletions: sum.deletions + Number(removed), + }; + }, + { insertions: 0, deletions: 0 }, ); + assert.deepStrictEqual(native, { insertions: 4, deletions: 2 }); + const totals = (yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true })) + .branchChanges; + assert.equal(totals?.insertions, native.insertions); + assert.equal(totals?.deletions, native.deletions); assert.isFalse( commands.some((command) => command.cwd === moduleCwd && command.args.includes("diff")), ); @@ -3348,9 +3411,47 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { const embedded = yield* driver.getReviewDiffPreview({ cwd, sourceKind: "working-tree" }); assert.deepStrictEqual( embedded.sources[0]!.files?.map((file) => file.path), - ["mod"], + [modulePath], ); assert.include(embedded.sources[0]!.diff, "-dirty"); + yield* writeTextFile(moduleCwd, "README.md", "# test\n"); + yield* writeTextFile(cwd, ".gitmodules", `${manifest}\n`); + yield* git(cwd, ["add", ".gitmodules"]); + yield* git(cwd, ["commit", "-m", "restore submodule declaration"]); + yield* git(cwd, ["rm", "-f", modulePath]); + yield* writeTextFile(cwd, ".gitmodules", ""); + yield* writeTextFile(cwd, `${modulePath}/replacement.txt`, "replacement\n"); + yield* git(cwd, ["mv", "outside.txt", `${modulePath}/moved.txt`]); + for (const staged of [false, true]) { + if (staged) yield* git(cwd, ["add", modulePath]); + const preview = yield* driver.getReviewDiffPreview({ cwd, sourceKind: "working-tree" }); + const source = preview.sources[0]!; + assert.deepStrictEqual( + source.files?.map((file) => file.path).toSorted(), + [ + ".gitmodules", + modulePath, + `${modulePath}/moved.txt`, + `${modulePath}/replacement.txt`, + "outside.txt", + ].toSorted(), + ); + assertPatchPaths(source); + const metadata = yield* driver.getReviewDiffPreview({ + cwd, + sourceKind: "working-tree", + metadataOnly: true, + }); + assert.deepStrictEqual(metadata.sources[0]!.files, source.files); + for (const file of source.files!) { + const scoped = yield* driver.getReviewDiffPreview({ + cwd, + file: { ...file, sourceKind: "working-tree" }, + }); + assert.deepStrictEqual(scoped.sources[0]!.files, [file]); + assertPatchPaths(scoped.sources[0]!); + } + } }), ), ); diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts index c04b7c8a43f3c..a8275e9f321ac 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.ts @@ -3570,7 +3570,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* `--dst-prefix=b/${prefix}`, ]; let baseRef = options.baseRef; - const readStat = (selectedPaths = pathArgs, extraArgs: ReadonlyArray = []) => + const readStat = ( + selectedPaths: ReadonlyArray = pathArgs, + extraArgs: ReadonlyArray = [], + ) => executeGit( headRef ? "GitVcsDriver.getReviewDiffPreview.commitStat" @@ -3617,6 +3620,8 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* >(); let submodulesTruncated = false; let fallbackPaths: string[] = []; + let ownPathArgs: ReadonlyArray = pathArgs; + let fallbackPathArgs: ReadonlyArray = []; let ownStats = parseReviewNumstat(stat.stdout); const manifestExists = yield* fileSystem .exists(path.join(cwd, ".gitmodules")) @@ -3715,11 +3720,47 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* else expanded.set(child[0], child[1]); } fallbackPaths = candidates.filter((value) => !expanded.has(value)); - if (fallbackPaths.length > 0) { - const fallback = yield* readStat( - fallbackPaths.map((value) => `:(top,literal)${value}`), - ["--ignore-submodules=none"], + ownPathArgs = [ + ...pathArgs, + ...Array.from( + [...expanded.keys(), ...fallbackPaths], + (value) => `:(top,exclude,literal)${value}`, + ), + ]; + const isFallbackPath = (value: string) => + fallbackPaths.some( + (modulePath) => value === modulePath || value.startsWith(`${modulePath}/`), ); + fallbackPathArgs = file + ? paths.filter(isFallbackPath).flatMap((value) => [ + `:(top,literal)${value}`, + // A literal trailing-slash exclusion also hides the gitlink; glob matches descendants only. + ...(fallbackPaths.includes(value) + ? [`:(top,exclude,glob)${value.replace(/[\\*?[\]]/g, "\\$&")}/**`] + : []), + ]) + : fallbackPaths.map((value) => `:(top,literal)${value}`); + if (fallbackPaths.length > 0) { + if ( + ownStats.some( + (entry) => + (isFallbackPath(entry.path) && !fallbackPaths.includes(entry.path)) || + (entry.previousPath !== null && + (isFallbackPath(entry.path) || isFallbackPath(entry.previousPath))), + ) + ) { + const own = yield* readStat(ownPathArgs); + if (own.exitCode !== 0) + return yield* new GitCommandError({ + operation: "GitVcsDriver.review.partitionedStat", + command: "git diff --numstat", + cwd, + detail: "Could not read diff statistics outside submodule paths.", + exitCode: own.exitCode, + }); + ownStats = parseReviewNumstat(own.stdout); + } + const fallback = yield* readStat(fallbackPathArgs, ["--ignore-submodules=none"]); if (fallback.exitCode !== 0) return yield* new GitCommandError({ operation: "GitVcsDriver.review.submoduleStat", @@ -3767,20 +3808,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* ? [] : yield* Effect.all( [ - readPatch([ - ...pathArgs, - ...Array.from( - [...expanded.keys(), ...fallbackPaths], - (value) => `:(top,exclude,literal)${value}`, - ), - ]), + readPatch(ownPathArgs), ...(fallbackPaths.length > 0 - ? [ - readPatch( - fallbackPaths.map((value) => `:(top,literal)${value}`), - ["--ignore-submodules=none"], - ), - ] + ? [readPatch(fallbackPathArgs, ["--ignore-submodules=none"])] : []), ], { concurrency: 2 }, diff --git a/docs/user/source-control.md b/docs/user/source-control.md index 40c356f27a768..ed06d900de310 100644 --- a/docs/user/source-control.md +++ b/docs/user/source-control.md @@ -27,6 +27,8 @@ Paths include the submodule directory, so files with the same name in different distinct. Comparisons use the submodule commit recorded by the parent repository at the selected base. Commit reviews compare the recorded submodule commits without including local edits. Review includes declared submodules even when Git's submodule ignore settings hide them from summaries. +This override applies to direct submodules. If a nested submodule is hidden by its parent's own ignore +settings, unset those settings in that parent submodule to review it. Initialize submodules and fetch their recorded commits to review their file contents. When those are unavailable, or a comparison adds or removes a submodule, it shows the submodule commit pointer. From fbcf0b2415d5fc2a473d1577412eb483d22c64bf Mon Sep 17 00:00:00 2001 From: Neil Mazumdar <84065477+NeilMazumdar@users.noreply.github.com> Date: Sun, 8 Nov 2026 09:27:34 +1030 Subject: [PATCH 05/14] feat(git): select a repository for diff review --- .../src/features/review/ReviewSheet.tsx | 40 +- .../src/features/review/reviewModel.test.ts | 5 + .../mobile/src/features/review/reviewModel.ts | 9 +- .../useReviewCommentSelectionController.ts | 10 +- .../src/features/review/useReviewDiffData.ts | 12 +- .../review/useReviewHeaderPresentation.tsx | 4 +- .../src/features/review/useReviewSections.ts | 39 +- apps/server/src/auth/RpcAuthorization.ts | 1 + .../src/observability/RpcInstrumentation.ts | 1 + apps/server/src/review/ReviewService.test.ts | 6 + apps/server/src/review/ReviewService.ts | 16 + apps/server/src/vcs/GitVcsDriver.ts | 5 + apps/server/src/vcs/GitVcsDriverCore.test.ts | 580 ++------------ apps/server/src/vcs/GitVcsDriverCore.ts | 730 ++++++------------ apps/server/src/ws.ts | 1 + .../src/components/DiffPanel.browser.test.tsx | 5 + apps/web/src/components/DiffPanel.tsx | 143 +++- apps/web/src/components/DiffPanelShell.tsx | 4 +- .../AnnotatableCodeView.browser.test.tsx | 73 +- .../components/diffs/AnnotatableCodeView.tsx | 10 +- apps/web/src/diffPanelStore.test.ts | 33 + apps/web/src/diffPanelStore.ts | 37 +- .../lib/openPullRequestLink.browser.test.tsx | 49 +- apps/web/src/lib/openPullRequestLink.ts | 1 + docs/user/source-control.md | 27 +- packages/client-runtime/src/state/review.ts | 5 + packages/contracts/src/review.ts | 15 + packages/contracts/src/rpc.ts | 10 + 28 files changed, 770 insertions(+), 1101 deletions(-) diff --git a/apps/mobile/src/features/review/ReviewSheet.tsx b/apps/mobile/src/features/review/ReviewSheet.tsx index a953ca80901fa..a328a5c6a5ec5 100644 --- a/apps/mobile/src/features/review/ReviewSheet.tsx +++ b/apps/mobile/src/features/review/ReviewSheet.tsx @@ -1,6 +1,6 @@ import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; import { AsyncResult } from "effect/reactivity"; -import type { EnvironmentId, ThreadId } from "@t3tools/contracts"; +import type { ReviewRepositoriesResult, EnvironmentId, ThreadId } from "@t3tools/contracts"; import { formatDiffCount } from "@t3tools/client-runtime/diff-count"; import { useNavigation, type StaticScreenProps } from "@react-navigation/native"; import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator"; @@ -63,6 +63,9 @@ import { useSelectWorkspaceDetail } from "../layout/workspace/useSelectWorkspace function ReviewHeader( props: Parameters[0] & { + readonly repositories: ReviewRepositoriesResult["repositories"]; + readonly selectedRepository: ReviewRepositoriesResult["repositories"][number] | undefined; + readonly onSelectRepository: (cwd: string) => void; readonly sectionMenu: ReturnType; readonly showSectionToolbar: boolean; readonly showChangedFilesToggle: boolean; @@ -104,6 +107,21 @@ function ReviewHeader( : undefined } menus={[ + ...(props.repositories.length > 1 + ? [ + { + title: "Select repository", + icon: "folder" as const, + items: props.repositories.map((repo) => ({ + id: repo.cwd, + title: repo.name + ": " + (repo.branch ?? "HEAD"), + subtitle: repo.path || "Workspace repository", + selected: repo.cwd === props.selectedRepository?.cwd, + onPress: () => props.onSelectRepository(repo.cwd), + })), + }, + ] + : []), ...(presentation.gitMenu ? [presentation.gitMenu] : []), ...(props.showSectionToolbar ? [ @@ -388,6 +406,11 @@ export function ReviewSheet(props: ReviewSheetProps) { refreshSelectedSection, selectSection, isSelectedSectionPending, + repositories, + selectedRepository, + selectRepository, + reviewCwd, + repositoryPath, } = useReviewSections({ enabled: isEnvironmentReady, environmentId, @@ -409,7 +432,8 @@ export function ReviewSheet(props: ReviewSheetProps) { } = useReviewDiffData({ threadKey: reviewCache.threadKey, environmentId, - cwd: selectedThreadCwd, + cwd: reviewCwd, + repositoryPath, selectedSection, draftMessage, draftContext, @@ -452,6 +476,7 @@ export function ReviewSheet(props: ReviewSheetProps) { environmentId, threadId, selectedSection, + repositoryPath, nativeReviewDiffData, }); const nativeBridge = useNativeReviewDiffBridge({ @@ -555,6 +580,9 @@ export function ReviewSheet(props: ReviewSheetProps) { }); }; const androidHeaderSubtitle = [ + selectedRepository + ? selectedRepository.name + ": " + (selectedRepository.branch ?? "HEAD") + : null, selectedSection?.title, headerDiffSummary.additions, headerDiffSummary.deletions, @@ -598,6 +626,9 @@ export function ReviewSheet(props: ReviewSheetProps) { return <>{children}; })(); const headerSubtitle = [ + selectedRepository + ? selectedRepository.name + ": " + (selectedRepository.branch ?? "HEAD") + : null, headerDiffSummary.additions, headerDiffSummary.deletions, pendingReviewCommentCount > 0 @@ -622,7 +653,10 @@ export function ReviewSheet(props: ReviewSheetProps) { title={headerTitleText} subtitle={headerSubtitle} androidSubtitle={androidHeaderSubtitle} - selectedThreadCwd={selectedThreadCwd} + selectedThreadCwd={repositoryPath ? null : selectedThreadCwd} + repositories={repositories} + selectedRepository={selectedRepository} + onSelectRepository={selectRepository} sectionMenu={sectionMenu} selectedSection={selectedSection} showSectionToolbar={showSectionToolbar} diff --git a/apps/mobile/src/features/review/reviewModel.test.ts b/apps/mobile/src/features/review/reviewModel.test.ts index ef366dc33d409..e238b8acf563f 100644 --- a/apps/mobile/src/features/review/reviewModel.test.ts +++ b/apps/mobile/src/features/review/reviewModel.test.ts @@ -24,6 +24,11 @@ describe("resolveReviewSelectedSectionId", () => { it("shows Changes before anything is picked, not the first section", () => { expect(resolveReviewSelectedSectionId([turn, changes], null)).toBe("git:branch-range"); + const childChanges = { ...changes, id: "git:branch-range:packages/auth" }; + expect(resolveReviewSelectedSectionId([turn, childChanges], null)).toBe(childChanges.id); + expect( + resolveReviewSelectedSectionId([turn, childChanges], "git:branch-range:packages/ui"), + ).toBe(childChanges.id); }); it("shows the default while the picked section loads and returns to the pick once it does", () => { diff --git a/apps/mobile/src/features/review/reviewModel.ts b/apps/mobile/src/features/review/reviewModel.ts index df3c7c51324b4..ffb48412c6056 100644 --- a/apps/mobile/src/features/review/reviewModel.ts +++ b/apps/mobile/src/features/review/reviewModel.ts @@ -515,6 +515,7 @@ export function reviewTurnDiffsBySectionId( } export function buildReviewSectionItems(input: { + readonly repositoryPath?: string; readonly checkpoints: ReadonlyArray; readonly gitSections: ReadonlyArray; readonly turnDiffById: Readonly>; @@ -540,7 +541,7 @@ export function buildReviewSectionItems(input: { section.kind !== "commit", ) .map((section) => ({ - id: `git:${section.kind}`, + id: `git:${section.kind}${input.repositoryPath ? ":" + input.repositoryPath : ""}`, kind: section.kind, title: section.title, subtitle: gitSubtitle(section), @@ -550,12 +551,12 @@ export function buildReviewSectionItems(input: { truncated: section.truncated, isLoading: false, })); - const hasChangesItem = gitItems.some((item) => item.id === CHANGES_SECTION_ID); + const hasChangesItem = gitItems.some((item) => item.kind === "branch-range"); const visibleGitItems = input.loadingGitSections && !hasChangesItem ? [ { - id: CHANGES_SECTION_ID, + id: CHANGES_SECTION_ID + (input.repositoryPath ? ":" + input.repositoryPath : ""), kind: "branch-range", title: CHANGES_TITLE, subtitle: null, @@ -572,7 +573,7 @@ export function buildReviewSectionItems(input: { export function getDefaultReviewSectionId( sections: ReadonlyArray, ): string | null { - return (sections.find((section) => section.id === CHANGES_SECTION_ID) ?? sections[0])?.id ?? null; + return (sections.find((section) => section.kind === "branch-range") ?? sections[0])?.id ?? null; } /** diff --git a/apps/mobile/src/features/review/useReviewCommentSelectionController.ts b/apps/mobile/src/features/review/useReviewCommentSelectionController.ts index 4a68af28ca831..91f07c7f15009 100644 --- a/apps/mobile/src/features/review/useReviewCommentSelectionController.ts +++ b/apps/mobile/src/features/review/useReviewCommentSelectionController.ts @@ -26,6 +26,7 @@ export function useReviewCommentSelectionController(input: { readonly environmentId?: EnvironmentId; readonly threadId?: ThreadId; readonly selectedSection: ReviewSectionItem | null; + readonly repositoryPath?: string; readonly nativeReviewDiffData: NativeReviewDiffData; }) { const { environmentId, nativeReviewDiffData, selectedSection, threadId } = input; @@ -99,11 +100,14 @@ export function useReviewCommentSelectionController(input: { return; } + const filePath = input.repositoryPath + ? input.repositoryPath + "/" + target.filePath + : target.filePath; if (gesture === "longPress") { startReviewCommentRange(scope, { sectionId: selectedSection.id, sectionTitle: selectedSection.title, - filePath: target.filePath, + filePath, lines: target.lines, lineIndex: target.lineIndex, rowId, @@ -111,7 +115,7 @@ export function useReviewCommentSelectionController(input: { return; } - if (pendingRange && pendingRange.filePath === target.filePath) { + if (pendingRange && pendingRange.filePath === filePath) { completeReviewCommentRange( scope, buildReviewCommentTarget(pendingRange, pendingRange.lineIndex, target.lineIndex), @@ -122,7 +126,7 @@ export function useReviewCommentSelectionController(input: { setReviewCommentTarget(scope, { sectionTitle: selectedSection.title, sectionId: selectedSection.id, - filePath: target.filePath, + filePath, lines: target.lines, startIndex: target.lineIndex, endIndex: target.lineIndex, diff --git a/apps/mobile/src/features/review/useReviewDiffData.ts b/apps/mobile/src/features/review/useReviewDiffData.ts index d9ec656cbc773..8c44e0d1dee5e 100644 --- a/apps/mobile/src/features/review/useReviewDiffData.ts +++ b/apps/mobile/src/features/review/useReviewDiffData.ts @@ -125,6 +125,7 @@ export function useReviewDiffData(input: { readonly threadKey: string | null; readonly environmentId: EnvironmentId | undefined; readonly cwd: string | null; + readonly repositoryPath?: string; readonly selectedSection: ReviewSectionItem | null; readonly draftMessage: string; readonly draftContext: OrchestrationMessageContext | undefined; @@ -237,8 +238,15 @@ export function useReviewDiffData(input: { if (!selectedSectionId || inlineReviewComments.length === 0) { return EMPTY_INLINE_REVIEW_COMMENTS; } - return inlineReviewComments.filter((comment) => comment.sectionId === selectedSectionId); - }, [inlineReviewComments, selectedSectionId]); + const prefix = input.repositoryPath ? input.repositoryPath + "/" : ""; + return inlineReviewComments + .filter((comment) => comment.sectionId === selectedSectionId) + .map((comment) => + prefix && comment.filePath.startsWith(prefix) + ? { ...comment, filePath: comment.filePath.slice(prefix.length) } + : comment, + ); + }, [inlineReviewComments, selectedSectionId, input.repositoryPath]); const nativeReviewDiffData = getCachedNativeReviewDiffData({ parsedDiff, comments: selectedSectionInlineComments, diff --git a/apps/mobile/src/features/review/useReviewHeaderPresentation.tsx b/apps/mobile/src/features/review/useReviewHeaderPresentation.tsx index d0fda18cf60cd..ad580656c2ef9 100644 --- a/apps/mobile/src/features/review/useReviewHeaderPresentation.tsx +++ b/apps/mobile/src/features/review/useReviewHeaderPresentation.tsx @@ -40,7 +40,9 @@ export function useReviewHeaderPresentation(props: { ); // Selection-based git hooks apply only to reviews of the selected thread. const gitMenuAvailable = - selectedThread !== null && String(selectedThread.id) === String(props.threadId); + selectedThread !== null && + String(selectedThread.id) === String(props.threadId) && + props.selectedThreadCwd !== null; const gitMenu = useThreadGitMenuDefinition({ environmentId: props.environmentId, threadId: props.threadId, diff --git a/apps/mobile/src/features/review/useReviewSections.ts b/apps/mobile/src/features/review/useReviewSections.ts index 4fd506732e5d3..f24d98610aa37 100644 --- a/apps/mobile/src/features/review/useReviewSections.ts +++ b/apps/mobile/src/features/review/useReviewSections.ts @@ -1,3 +1,4 @@ +import { useState } from "react"; import { useAtomValue } from "@effect/atom-react"; import { resolveFilesystemReadAccess } from "@t3tools/client-runtime/state/filesystem"; import { Atom } from "effect/reactivity"; @@ -53,17 +54,34 @@ export function useReviewSections(input: { const { canReadFiles } = fileAccess; const selectedThread = useSelectedThreadProjection(); const { selectedThreadCwd } = useSelectedThreadWorktree(); + const [repositoryChoice, setRepositoryChoice] = useState<{ + threadKey: string | null; + cwd: string; + } | null>(null); + const repositoriesQuery = useEnvironmentQuery( + canReadFiles && enabled && environmentId && selectedThreadCwd + ? reviewEnvironment.repositories({ environmentId, input: { cwd: selectedThreadCwd } }) + : null, + ); + const repositories = repositoriesQuery.data?.repositories ?? []; + const selectedRepository = + repositories.find( + (repo) => + repositoryChoice?.threadKey === reviewCache.threadKey && repo.cwd === repositoryChoice.cwd, + ) ?? repositories[0]; + const reviewCwd = selectedRepository?.cwd ?? selectedThreadCwd; + const repositoryPath = selectedRepository?.path ?? ""; const diffPreview = useEnvironmentQuery( - canReadFiles && enabled && environmentId !== undefined && selectedThreadCwd !== null + canReadFiles && enabled && environmentId !== undefined && reviewCwd !== null ? reviewEnvironment.diffPreview({ environmentId, - input: { cwd: selectedThreadCwd }, + input: { cwd: reviewCwd }, }) : null, ); const lastGitSections = useAtomValue( - environmentId !== undefined && selectedThreadCwd !== null - ? lastReviewGitSectionsAtom(environmentId, selectedThreadCwd) + environmentId !== undefined && reviewCwd !== null + ? lastReviewGitSectionsAtom(environmentId, reviewCwd) : NO_GIT_SECTIONS_ATOM, ); @@ -87,6 +105,7 @@ export function useReviewSections(input: { canReadFiles || fileAccess.isPending ? (diffPreview.data?.sources ?? lastGitSections) : [], turnDiffById: reviewTurnDiffsBySectionId(readyCheckpoints, lastTurnDiffs), loadingGitSections: fileAccess.isPending || diffPreview.isPending, + repositoryPath, }); const selectedSectionId = resolveReviewSelectedSectionId( cachedSections, @@ -133,6 +152,7 @@ export function useReviewSections(input: { return; } diffPreview.refresh(); + repositoriesQuery.refresh(); }; const selectSection = (sectionId: string) => { @@ -141,7 +161,18 @@ export function useReviewSections(input: { } }; + const selectRepository = (cwd: string) => { + if (!repositories.some((repo) => repo.cwd === cwd)) return; + setRepositoryChoice({ threadKey: reviewCache.threadKey, cwd }); + if (reviewCache.threadKey) setReviewSelectedSectionId(reviewCache.threadKey, null); + }; + return { + repositories, + selectedRepository, + selectRepository, + reviewCwd: selectedSection?.kind === "turn" ? selectedThreadCwd : reviewCwd, + repositoryPath: selectedSection?.kind === "turn" ? "" : repositoryPath, error: diffPreview.error ?? activeTurnDiff.error ?? diff --git a/apps/server/src/auth/RpcAuthorization.ts b/apps/server/src/auth/RpcAuthorization.ts index 180f053f111f4..ebf7e8a8074dd 100644 --- a/apps/server/src/auth/RpcAuthorization.ts +++ b/apps/server/src/auth/RpcAuthorization.ts @@ -179,6 +179,7 @@ export const RPC_REQUIRED_SCOPES = { [WS_METHODS.vcsRefreshStatus]: AuthOrchestrationReadScope, [WS_METHODS.gitResolvePullRequest]: AuthOrchestrationReadScope, [WS_METHODS.vcsListRefs]: AuthOrchestrationReadScope, + [WS_METHODS.reviewGetRepositories]: AuthFilesystemReadScope, [WS_METHODS.reviewGetBranchCommits]: AuthFilesystemReadScope, [WS_METHODS.reviewGetDiffPreview]: AuthFilesystemReadScope, [WS_METHODS.reviewGetDiffFileContents]: AuthFilesystemReadScope, diff --git a/apps/server/src/observability/RpcInstrumentation.ts b/apps/server/src/observability/RpcInstrumentation.ts index f9a861cca23fb..9f513a158f853 100644 --- a/apps/server/src/observability/RpcInstrumentation.ts +++ b/apps/server/src/observability/RpcInstrumentation.ts @@ -193,6 +193,7 @@ const RPC_AGGREGATES = { [WS_METHODS.vcsCreateRef]: "vcs", [WS_METHODS.vcsSwitchRef]: "vcs", [WS_METHODS.vcsInit]: "vcs", + [WS_METHODS.reviewGetRepositories]: "review", [WS_METHODS.reviewGetBranchCommits]: "review", [WS_METHODS.reviewGetDiffPreview]: "review", [WS_METHODS.reviewGetDiffFileContents]: "review", diff --git a/apps/server/src/review/ReviewService.test.ts b/apps/server/src/review/ReviewService.test.ts index 4df56454cfc58..2bfd04843caf9 100644 --- a/apps/server/src/review/ReviewService.test.ts +++ b/apps/server/src/review/ReviewService.test.ts @@ -109,6 +109,12 @@ describe("ReviewService", () => { return yield* review.getDiffPreview({ cwd: outsideRoot }).pipe(Effect.flip); }).pipe(Effect.provide(layer({ workspaceRoot, baseDir, detectCalls }))); + const repositoryError = yield* Effect.gen(function* () { + const review = yield* ReviewService.ReviewService; + return yield* review.getRepositories({ cwd: outsideRoot }).pipe(Effect.flip); + }).pipe(Effect.provide(layer({ workspaceRoot, baseDir, detectCalls }))); + assert.strictEqual(repositoryError._tag, "VcsRepositoryDetectionError"); + assert.strictEqual(repositoryError.operation, "ReviewService.getRepositories"); assert.strictEqual(error._tag, "VcsRepositoryDetectionError"); assert.strictEqual(error.operation, "ReviewService.getDiffPreview"); assert.match( diff --git a/apps/server/src/review/ReviewService.ts b/apps/server/src/review/ReviewService.ts index 39ce1554cea61..2169224f3cb6f 100644 --- a/apps/server/src/review/ReviewService.ts +++ b/apps/server/src/review/ReviewService.ts @@ -8,6 +8,8 @@ import * as Path from "effect/Path"; import { VcsRepositoryDetectionError, VcsUnsupportedOperationError, + type ReviewRepositoriesInput, + type ReviewRepositoriesResult, type ReviewBranchCommitsInput, type ReviewBranchCommitsResult, type ReviewDiffFileContentsInput, @@ -23,6 +25,7 @@ import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts"; import * as KnownWorkspaces from "../workspace/KnownWorkspaces.ts"; const WORKSPACE_BOUND_SUBJECTS = { + "ReviewService.getRepositories": "repository list", "ReviewService.getBranchCommits": "commit list", "ReviewService.getDiffPreview": "diff preview", "ReviewService.getDiffFileContents": "diff file contents", @@ -31,6 +34,9 @@ const WORKSPACE_BOUND_SUBJECTS = { export class ReviewService extends Context.Service< ReviewService, { + readonly getRepositories: ( + input: ReviewRepositoriesInput, + ) => Effect.Effect; readonly getBranchCommits: ( input: ReviewBranchCommitsInput, ) => Effect.Effect; @@ -93,6 +99,15 @@ export const make = Effect.gen(function* () { }); }); + const getRepositories: ReviewService["Service"]["getRepositories"] = Effect.fn( + "ReviewService.getRepositories", + )(function* (input) { + yield* assertWorkspaceBoundCwd("ReviewService.getRepositories", input.cwd); + const handle = yield* vcsRegistry.detect({ cwd: input.cwd, requestedKind: "auto" }); + if (handle?.kind !== "git") return { repositories: [], truncated: false }; + return yield* git.getReviewRepositories(input); + }); + const getBranchCommits: ReviewService["Service"]["getBranchCommits"] = Effect.fn( "ReviewService.getBranchCommits", )(function* (input) { @@ -145,6 +160,7 @@ export const make = Effect.gen(function* () { }); return ReviewService.of({ + getRepositories, getBranchCommits, getDiffPreview, getDiffFileContents, diff --git a/apps/server/src/vcs/GitVcsDriver.ts b/apps/server/src/vcs/GitVcsDriver.ts index ff129690b7b24..97cb7ace9ce38 100644 --- a/apps/server/src/vcs/GitVcsDriver.ts +++ b/apps/server/src/vcs/GitVcsDriver.ts @@ -20,6 +20,8 @@ import { type VcsCreateRefResult, type VcsCreateWorktreeInput, type VcsCreateWorktreeResult, + type ReviewRepositoriesInput, + type ReviewRepositoriesResult, type ReviewBranchCommitsInput, type ReviewBranchCommitsResult, type ReviewDiffPreviewInput, @@ -353,6 +355,9 @@ export class GitVcsDriver extends Context.Service< cwd: string, baseRef: string, ) => Effect.Effect; + readonly getReviewRepositories: ( + input: ReviewRepositoriesInput, + ) => Effect.Effect; readonly getReviewBranchCommits: ( input: ReviewBranchCommitsInput, ) => Effect.Effect; diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts index fa913c9111da2..ca7fe2931783a 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.test.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts @@ -2218,6 +2218,85 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { }); describe("review diff previews", () => { + it.effect("lists initialized repositories and keeps each review inside its repository", () => + withGitFileProtocol( + Effect.gen(function* () { + const cwd = yield* makeTmpDir(); + const upstream = yield* makeTmpDir(); + const nested = yield* makeTmpDir(); + const { initialBranch } = yield* initRepoWithCommit(cwd); + yield* initRepoWithCommit(upstream); + yield* initRepoWithCommit(nested); + yield* git(upstream, ["submodule", "add", nested, "vendor/schema"]); + yield* git(upstream, ["commit", "-am", "Add schema"]); + yield* git(cwd, ["submodule", "add", upstream, "packages/auth service"]); + yield* git(cwd, ["submodule", "add", nested, "uninitialized"]); + yield* git(cwd, ["commit", "-am", "Add modules"]); + yield* git(cwd, ["submodule", "update", "--init", "--recursive"]); + yield* git(cwd, ["submodule", "deinit", "-f", "--", "uninitialized"]); + const child = NodePath.join(cwd, "packages/auth service"); + yield* writeTextFile(child, "committed.txt", "child commit\n"); + yield* git(child, ["add", "committed.txt"]); + yield* git(child, ["commit", "-m", "Child change"]); + yield* git(child, ["checkout", "-b", "feature/auth"]); + yield* writeTextFile(child, "README.md", "# changed auth\n"); + yield* writeTextFile(cwd, "README.md", "# changed parent\n"); + const driver = yield* GitVcsDriver.GitVcsDriver; + const repos = yield* driver.getReviewRepositories({ cwd }); + assert.deepStrictEqual( + repos.repositories.map(({ path, branch }) => ({ path, branch })), + [ + { path: "", branch: initialBranch }, + { path: "packages/auth service", branch: "feature/auth" }, + { path: "packages/auth service/vendor/schema", branch: null }, + ], + ); + assert.isFalse(repos.truncated); + const commits = yield* driver.getReviewBranchCommits({ cwd: child, baseRef: "HEAD~1" }); + assert.equal(commits.commits.length, 1); + assert.equal(commits.commits[0]!.subject, "Child change"); + const committed = (yield* driver.getReviewDiffPreview({ + cwd: child, + commitSha: commits.commits[0]!.sha, + })).sources[0]!; + assert.deepStrictEqual( + committed.files?.map((file) => file.path), + ["committed.txt"], + ); + assert.include(committed.diff, "+child commit"); + const parent = (yield* driver.getReviewDiffPreview({ cwd, sourceKind: "working-tree" })) + .sources[0]!; + assert.include(parent.diff, "Subproject commit"); + assert.notInclude(parent.diff, "changed auth"); + const auth = (yield* driver.getReviewDiffPreview({ + cwd: child, + sourceKind: "working-tree", + })).sources[0]!; + assert.include(auth.diff, "+# changed auth"); + assert.notInclude(auth.diff, "changed parent"); + assert.deepStrictEqual( + auth.files?.map((file) => file.path), + ["README.md"], + ); + const file = yield* driver.getReviewDiffFileContents( + makeReviewDiffFileContentsInput(child), + ); + assert.equal(file.newContents, "# changed auth\n"); + // A hostile manifest must not turn discovery into an arbitrary filesystem scan. + yield* git(cwd, ["config", "-f", ".gitmodules", "submodule.escape.path", upstream]); + yield* git(cwd, ["config", "-f", ".gitmodules", "submodule.parent.path", ".."]); + if (symlinksSupported) { + const fs = yield* FileSystem.FileSystem; + yield* fs.symlink(upstream, NodePath.join(cwd, "escape-link")); + yield* git(cwd, ["config", "-f", ".gitmodules", "submodule.link.path", "escape-link"]); + } + assert.deepStrictEqual( + (yield* driver.getReviewRepositories({ cwd })).repositories, + repos.repositories, + ); + }), + ), + ); it.effect("diffs a root commit against the empty tree but rejects a shallow boundary", () => Effect.gen(function* () { const upstream = yield* makeTmpDir(); @@ -3028,507 +3107,6 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { }), ); - it.effect("reviews files recursively inside submodules without changing their indexes", () => - withGitFileProtocol( - Effect.gen(function* () { - const cwd = yield* makeTmpDir(); - const inner = yield* makeTmpDir(); - const nested = yield* makeTmpDir(); - const { initialBranch } = yield* initRepoWithCommit(cwd); - yield* initRepoWithCommit(inner); - yield* initRepoWithCommit(nested); - yield* writeTextFile(inner, "before.ts", "export const unchanged = true;\n"); - yield* git(inner, ["submodule", "add", nested, "nested"]); - yield* git(inner, ["add", "before.ts"]); - yield* git(inner, ["commit", "-am", "add nested module"]); - const modulePath = "modules/space repo"; - yield* git(cwd, ["submodule", "add", inner, modulePath]); - yield* git(cwd, ["submodule", "add", inner, "modules/clean repo"]); - yield* git(cwd, ["commit", "-am", "add module"]); - yield* git(cwd, ["checkout", "-b", "feature/submodule-review"]); - const path = yield* Path.Path; - const moduleCwd = path.join(cwd, modulePath); - yield* git(moduleCwd, ["submodule", "update", "--init", "--recursive"]); - yield* git(moduleCwd, ["config", "user.name", "Test"]); - yield* git(moduleCwd, ["config", "user.email", "test@test.com"]); - yield* git(moduleCwd, ["mv", "before.ts", "renamed λ.ts"]); - yield* writeTextFile(moduleCwd, "README.md", "# committed in module\n"); - yield* git(moduleCwd, ["commit", "-am", "change module file"]); - yield* writeTextFile(moduleCwd, "staged.txt", "staged\n"); - yield* git(moduleCwd, ["add", "staged.txt"]); - yield* writeTextFile(moduleCwd, "untracked.txt", "untracked\n"); - yield* writeTextFile(moduleCwd, "nested/README.md", "# nested dirty\n"); - yield* git(cwd, ["config", "diff.ignoreSubmodules", "all"]); - yield* git(cwd, ["config", `submodule.${modulePath}.ignore`, "all"]); - const indexBefore = yield* git(moduleCwd, ["diff", "--cached"]); - const commands: { cwd: string | undefined; args: ReadonlyArray }[] = []; - const realSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; - const driver = yield* makeGitVcsDriverCore().pipe( - Effect.provideService( - ChildProcessSpawner.ChildProcessSpawner, - ChildProcessSpawner.make((command) => { - if (ChildProcess.isStandardCommand(command)) - commands.push({ cwd: command.options.cwd, args: command.args.map(String) }); - return realSpawner.spawn(command); - }), - ), - ); - for (const metadataOnly of [true, false]) { - const preview = yield* driver.getReviewDiffPreview({ - cwd, - baseRef: initialBranch, - metadataOnly, - }); - const changes = preview.sources.find((source) => source.kind === "branch-range")!; - const status = yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true }); - assert.equal( - status.branchChanges?.insertions, - changes.files!.reduce((n, file) => n + file.additions, 0), - ); - assert.equal( - status.branchChanges?.deletions, - changes.files!.reduce((n, file) => n + file.deletions, 0), - ); - for (const source of preview.sources) { - assert.deepStrictEqual( - source.files?.map((file) => file.path).toSorted(), - [ - `${modulePath}/README.md`, - `${modulePath}/nested/README.md`, - `${modulePath}/renamed λ.ts`, - `${modulePath}/staged.txt`, - `${modulePath}/untracked.txt`, - ].toSorted(), - ); - for (const file of source.files ?? []) { - const start = commands.length; - const scoped = yield* driver.getReviewDiffPreview({ - cwd, - baseRef: initialBranch, - file: { ...file, sourceKind: source.kind }, - }); - const patch = scoped.sources.find((candidate) => candidate.kind === source.kind)!; - assert.deepStrictEqual(patch.files, [file]); - assert.isFalse( - commands.slice(start).some((command) => command.args.includes("status")), - ); - if (file.previousPath) { - assert.equal(file.previousPath, `${modulePath}/before.ts`); - assert.include(patch.diff, `rename from ${file.previousPath}\n`); - assert.include(patch.diff, `rename to ${file.path}\n`); - } else assert.include(patch.diff, `b/${file.path}`); - } - } - } - assert.isFalse( - commands.some( - (command) => command.cwd?.includes("clean repo") && command.args.includes("diff"), - ), - ); - const projectDirectories = [cwd, path.join(cwd, "modules")]; - if (symlinksSupported) { - const alias = path.join(yield* makeTmpDir(), "alias"); - yield* (yield* FileSystem.FileSystem).symlink(cwd, alias); - projectDirectories.push(alias); - } - for (const projectCwd of projectDirectories) { - assert.deepStrictEqual( - yield* driver.getReviewDiffFileContents( - makeReviewDiffFileContentsInput(projectCwd, { - oldPath: `${modulePath}/README.md`, - newPath: `${modulePath}/README.md`, - }), - ), - { oldContents: "# test\n", newContents: "# committed in module\n" }, - ); - } - assert.equal(yield* git(moduleCwd, ["diff", "--cached"]), indexBefore); - assert.equal(yield* git(cwd, ["diff", "--cached"]), ""); - yield* git(cwd, ["add", modulePath]); - yield* git(cwd, ["commit", "-m", "advance module pin"]); - const commitSha = yield* git(cwd, ["rev-parse", "HEAD"]); - const committed = yield* driver.getReviewDiffPreview({ cwd, commitSha }); - assert.deepStrictEqual( - committed.sources[0]?.files?.map((file) => file.path), - [`${modulePath}/README.md`, `${modulePath}/renamed λ.ts`], - ); - assert.include(committed.sources[0]!.diff, `rename from ${modulePath}/before.ts\n`); - assert.include(committed.sources[0]!.diff, `rename to ${modulePath}/renamed λ.ts\n`); - assert.notInclude(committed.sources[0]!.diff, "nested dirty"); - const fileSystem = yield* FileSystem.FileSystem; - const manifest = path.join(cwd, ".gitmodules"); - const originalManifest = yield* fileSystem.readFileString(manifest); - yield* fileSystem.remove(manifest); - const noManifest = yield* driver.getReviewDiffPreview({ - cwd, - sourceKind: "working-tree", - metadataOnly: true, - }); - assert.isTrue( - noManifest.sources[0]!.files!.some( - (file) => file.path === `${modulePath}/untracked.txt`, - ), - ); - yield* fileSystem.writeFileString(manifest, originalManifest); - yield* fileSystem.rename(manifest, `${manifest}.saved`); - const renamedManifest = yield* driver.getReviewDiffPreview({ - cwd, - sourceKind: "working-tree", - metadataOnly: true, - }); - assert.isTrue( - renamedManifest.sources[0]!.files!.some( - (file) => file.path === `${modulePath}/untracked.txt`, - ), - ); - yield* fileSystem.rename(`${manifest}.saved`, manifest); - yield* writeTextFile(inner, "README.md", "# not fetched locally\n"); - yield* git(inner, ["commit", "-am", "advance upstream only"]); - const missingSha = yield* git(inner, ["rev-parse", "HEAD"]); - yield* git(cwd, ["update-index", "--cacheinfo", `160000,${missingSha},${modulePath}`]); - yield* git(cwd, ["commit", "-m", "record an unfetched module commit"]); - const missingCommit = yield* git(cwd, ["rev-parse", "HEAD"]); - for (const metadataOnly of [true, false]) { - const fallback = yield* driver.getReviewDiffPreview({ - cwd, - commitSha: missingCommit, - metadataOnly, - }); - assert.deepStrictEqual( - fallback.sources[0]!.files!.map((file) => file.path), - [modulePath], - ); - if (!metadataOnly) assert.include(fallback.sources[0]!.diff, "Subproject commit"); - const pointer = yield* driver.getReviewDiffPreview({ - cwd, - commitSha: missingCommit, - file: { path: modulePath, previousPath: null, sourceKind: "commit" }, - }); - assert.deepStrictEqual(pointer.sources[0]!.files, fallback.sources[0]!.files); - assert.include(pointer.sources[0]!.diff, "Subproject commit"); - } - }), - ), - ); - - it.effect("keeps parent changes when a submodule exceeds the untracked file cap", () => - withGitFileProtocol( - Effect.gen(function* () { - const cwd = yield* makeTmpDir(); - const inner = yield* makeTmpDir(); - const { initialBranch } = yield* initRepoWithCommit(cwd); - yield* initRepoWithCommit(inner); - yield* git(cwd, ["submodule", "add", inner, "mod"]); - yield* git(cwd, ["commit", "-am", "add module"]); - yield* writeTextFile(cwd, "README.md", "# parent edit\n"); - yield* Effect.forEach( - Array.from({ length: 5_001 }, (_, index) => `mod/build/${index}.txt`), - (file) => writeTextFile(cwd, file, "untracked\n"), - { concurrency: 32, discard: true }, - ); - const commands: ReadonlyArray[] = []; - const realSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; - const driver = yield* makeGitVcsDriverCore().pipe( - Effect.provideService( - ChildProcessSpawner.ChildProcessSpawner, - ChildProcessSpawner.make((command) => { - if (ChildProcess.isStandardCommand(command)) - commands.push(command.args.map(String)); - return realSpawner.spawn(command); - }), - ), - ); - for (const metadataOnly of [true, false]) { - const preview = yield* driver.getReviewDiffPreview({ - cwd, - sourceKind: "working-tree", - metadataOnly, - }); - const source = preview.sources[0]!; - assert.deepStrictEqual( - source.files?.map((file) => file.path), - ["README.md", "mod"], - ); - assert.isTrue(source.truncated); - if (!metadataOnly) { - assert.include(source.diff, "+# parent edit"); - assert.include(source.diff, "Subproject commit"); - } - const status = yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true }); - assert.equal( - status.branchChanges?.insertions, - source.files!.reduce((n, f) => n + f.additions, 0), - ); - assert.equal( - status.branchChanges?.deletions, - source.files!.reduce((n, f) => n + f.deletions, 0), - ); - } - for (const [key, value] of [ - ["diff.ignoreSubmodules", "dirty"], - ["submodule.mod.ignore", "all"], - ] as const) { - yield* git(cwd, ["config", key, value]); - const pointer = yield* driver.getReviewDiffPreview({ - cwd, - file: { path: "mod", previousPath: null, sourceKind: "working-tree" }, - }); - assert.deepStrictEqual( - pointer.sources[0]!.files?.map((file) => file.path), - ["mod"], - ); - assert.include(pointer.sources[0]!.diff, "Subproject commit"); - yield* git(cwd, ["config", "--unset", key]); - } - const fileSystem = yield* FileSystem.FileSystem; - const path = yield* Path.Path; - yield* fileSystem.rename(path.join(cwd, "mod/build"), path.join(cwd, "build")); - yield* writeTextFile(cwd, "mod/README.md", "# module edit\n"); - for (const sourceKind of ["working-tree", "branch-range"] as const) { - const preview = yield* driver.getReviewDiffPreview({ - cwd, - baseRef: initialBranch, - sourceKind, - metadataOnly: true, - }); - const source = preview.sources[0]!; - assert.isUndefined(source.files); - assert.isTrue(source.truncated); - assert.include(source.diff, "+# parent edit"); - assert.include(source.diff, "diff --git a/mod/README.md"); - assert.include(source.diff, "+# module edit"); - } - const start = commands.length; - const status = yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true }); - assert.isUndefined(status.branchChanges); - assert.isFalse(commands.slice(start).some((args) => args.includes("diff"))); - }), - ), - ); - - it.effect("keeps pointer diffs for added submodules and undeclared embedded repositories", () => - withGitFileProtocol( - Effect.gen(function* () { - const cwd = yield* makeTmpDir(); - const inner = yield* makeTmpDir(); - const { initialBranch } = yield* initRepoWithCommit(cwd); - yield* initRepoWithCommit(inner); - const modulePath = "mod[ules]"; - yield* writeTextFile(cwd, `${modulePath}/x.txt`, "x\n"); - yield* writeTextFile(cwd, `${modulePath}/y.txt`, "y\n"); - yield* writeTextFile( - cwd, - "outside.txt", - Array.from({ length: 20 }, (_, i) => `line ${i}\n`).join(""), - ); - yield* git(cwd, ["add", "."]); - yield* git(cwd, ["commit", "-m", "track vendored files"]); - yield* git(cwd, ["checkout", "-b", "feature/add-module"]); - yield* git(cwd, ["rm", "-r", modulePath]); - yield* git(cwd, ["submodule", "add", inner, modulePath]); - yield* git(cwd, ["commit", "-am", "add module"]); - const moduleCwd = (yield* Path.Path).join(cwd, modulePath); - const commitSha = yield* git(cwd, ["rev-parse", "HEAD"]); - const manifest = yield* git(cwd, ["show", "HEAD:.gitmodules"]); - const commands: { cwd: string | undefined; args: ReadonlyArray }[] = []; - const realSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; - const driver = yield* makeGitVcsDriverCore().pipe( - Effect.provideService( - ChildProcessSpawner.ChildProcessSpawner, - ChildProcessSpawner.make((command) => { - if (ChildProcess.isStandardCommand(command)) - commands.push({ cwd: command.options.cwd, args: command.args.map(String) }); - return realSpawner.spawn(command); - }), - ), - ); - const assertPatchPaths = (source: ReviewDiffPreviewResult["sources"][number]) => { - const paths = source.files!.map((file) => file.path); - assert.equal(new Set(paths).size, paths.length); - assert.deepStrictEqual( - Array.from( - source.diff.matchAll(/^diff --git a\/.+ b\/(.+)$/gm), - (match) => match[1]!, - ).toSorted(), - paths.toSorted(), - ); - }; - for (const input of [ - { baseRef: initialBranch, sourceKind: "branch-range" }, - { commitSha }, - ] as const) { - const preview = yield* driver.getReviewDiffPreview({ cwd, ...input }); - const source = preview.sources.find( - (source) => source.kind === ("commitSha" in input ? "commit" : "branch-range"), - )!; - assert.deepStrictEqual( - source.files?.map((file) => file.path).toSorted(), - [".gitmodules", modulePath, `${modulePath}/x.txt`, `${modulePath}/y.txt`].toSorted(), - ); - assertPatchPaths(source); - assert.include(source.diff, "Subproject commit"); - const metadata = yield* driver.getReviewDiffPreview({ - cwd, - ...input, - metadataOnly: true, - }); - assert.deepStrictEqual( - metadata.sources.find((item) => item.kind === source.kind)!.files, - source.files, - ); - for (const file of source.files!) { - const scoped = yield* driver.getReviewDiffPreview({ - cwd, - ...input, - file: { ...file, sourceKind: source.kind }, - }); - const patch = scoped.sources.find((item) => item.kind === source.kind)!; - assert.deepStrictEqual(patch.files, [file]); - assertPatchPaths(patch); - } - } - const native = (yield* git(cwd, ["diff", "--numstat", initialBranch])).split("\n").reduce( - (sum, line) => { - const [added, removed] = line.split("\t"); - return { - insertions: sum.insertions + Number(added), - deletions: sum.deletions + Number(removed), - }; - }, - { insertions: 0, deletions: 0 }, - ); - assert.deepStrictEqual(native, { insertions: 4, deletions: 2 }); - const totals = (yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true })) - .branchChanges; - assert.equal(totals?.insertions, native.insertions); - assert.equal(totals?.deletions, native.deletions); - assert.isFalse( - commands.some((command) => command.cwd === moduleCwd && command.args.includes("diff")), - ); - yield* git(cwd, ["rm", ".gitmodules"]); - yield* git(cwd, ["commit", "-m", "leave an embedded gitlink"]); - yield* writeTextFile(moduleCwd, "README.md", "# embedded edit\n"); - const embedded = yield* driver.getReviewDiffPreview({ cwd, sourceKind: "working-tree" }); - assert.deepStrictEqual( - embedded.sources[0]!.files?.map((file) => file.path), - [modulePath], - ); - assert.include(embedded.sources[0]!.diff, "-dirty"); - yield* writeTextFile(moduleCwd, "README.md", "# test\n"); - yield* writeTextFile(cwd, ".gitmodules", `${manifest}\n`); - yield* git(cwd, ["add", ".gitmodules"]); - yield* git(cwd, ["commit", "-m", "restore submodule declaration"]); - yield* git(cwd, ["rm", "-f", modulePath]); - yield* writeTextFile(cwd, ".gitmodules", ""); - yield* writeTextFile(cwd, `${modulePath}/replacement.txt`, "replacement\n"); - yield* git(cwd, ["mv", "outside.txt", `${modulePath}/moved.txt`]); - for (const staged of [false, true]) { - if (staged) yield* git(cwd, ["add", modulePath]); - const preview = yield* driver.getReviewDiffPreview({ cwd, sourceKind: "working-tree" }); - const source = preview.sources[0]!; - assert.deepStrictEqual( - source.files?.map((file) => file.path).toSorted(), - [ - ".gitmodules", - modulePath, - `${modulePath}/moved.txt`, - `${modulePath}/replacement.txt`, - "outside.txt", - ].toSorted(), - ); - assertPatchPaths(source); - const metadata = yield* driver.getReviewDiffPreview({ - cwd, - sourceKind: "working-tree", - metadataOnly: true, - }); - assert.deepStrictEqual(metadata.sources[0]!.files, source.files); - for (const file of source.files!) { - const scoped = yield* driver.getReviewDiffPreview({ - cwd, - file: { ...file, sourceKind: "working-tree" }, - }); - assert.deepStrictEqual(scoped.sources[0]!.files, [file]); - assertPatchPaths(scoped.sources[0]!); - } - } - }), - ), - ); - - it.effect( - "reviews a small commit in a parent tree whose complete listing exceeds the metadata cap", - () => - withGitFileProtocol( - Effect.gen(function* () { - const cwd = yield* makeTmpDir(); - const child = yield* makeTmpDir(); - yield* initRepoWithCommit(cwd); - yield* initRepoWithCommit(child); - yield* git(cwd, ["submodule", "add", child, "mod"]); - yield* git(cwd, ["commit", "-am", "add module"]); - const driver = yield* GitVcsDriver.GitVcsDriver; - const original = yield* git(cwd, ["rev-parse", "HEAD"]); - const blob = (yield* driver.execute({ - operation: "test.blob", - cwd, - args: ["hash-object", "-w", "--stdin"], - stdin: "", - })).stdout.trim(); - const env = { GIT_INDEX_FILE: (yield* Path.Path).join(yield* makeTmpDir(), "index") }; - yield* git(cwd, ["read-tree", "HEAD"], env); - const directory = `d/${"x".repeat(180)}`; - const info = Array.from( - { length: 70_000 }, - (_, index) => `100644 ${blob}\t${directory}/${String(index).padStart(6, "0")}.txt\n`, - ).join(""); - yield* driver.execute({ - operation: "test.largeIndex", - cwd, - args: ["update-index", "--index-info"], - env, - stdin: info, - }); - const tree = yield* git(cwd, ["write-tree"], env); - const big = yield* git(cwd, ["commit-tree", tree, "-p", original, "-m", "large tree"]); - yield* git( - cwd, - ["update-index", "--add", "--cacheinfo", `100644,${blob},small.txt`], - env, - ); - const nextTree = yield* git(cwd, ["write-tree"], env); - const small = yield* git(cwd, [ - "commit-tree", - nextTree, - "-p", - big, - "-m", - "small change", - ]); - const preview = yield* driver.getReviewDiffPreview({ - cwd, - commitSha: small, - metadataOnly: true, - }); - assert.deepStrictEqual( - preview.sources[0]!.files!.map((file) => file.path), - ["small.txt"], - ); - const contents = yield* driver.getReviewDiffFileContents( - makeReviewDiffFileContentsInput(cwd, { - sourceKind: "commit", - changeType: "new", - baseRef: original, - headRef: big, - oldPath: `${directory}/000000.txt`, - newPath: `${directory}/000000.txt`, - }), - ); - assert.deepStrictEqual(contents, { oldContents: "", newContents: "" }); - }), - ), - ); - it.effect("loads full file contents for working-tree diff expansion", () => Effect.gen(function* () { const cwd = yield* makeTmpDir(); diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts index a8275e9f321ac..6589b3c83eaf7 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.ts @@ -26,6 +26,8 @@ import { type GitCommandFailureReason, T3_PROJECT_FILE_NAME, type ReviewBranchCommitsInput, + type ReviewRepositoriesInput, + type ReviewRepositoriesResult, type ReviewDiffFileContentsInput, type ReviewDiffPreviewInput, type ReviewDiffFileStat, @@ -33,7 +35,6 @@ import { type VcsRef, } from "@t3tools/contracts"; import { dedupeRemoteBranchesWithLocalMatches } from "@t3tools/shared/git"; -import { quoteGitPatchPath, unquoteGitPatchPath } from "@t3tools/shared/gitPatchPath"; import * as HostProcess from "@t3tools/shared/HostProcess"; import { compactTraceAttributes } from "@t3tools/shared/observability"; import { decodeJsonResult } from "@t3tools/shared/schemaJson"; @@ -3261,26 +3262,47 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* roots.length <= 128 && roots.reduce((length, root) => length + root.length + 16, 0) < 8_000 ? roots.map((root) => `:(top,literal)${root}`) : []; - const result = yield* readReviewComparison({ - cwd, - baseRef: mergeBase, - metadataOnly: true, - completeFilesOnly: true, - minimal: false, - untrackedPathspecs: pathspecs, - }); - if (!result.files) + // Empty status can mean untracked files are hidden; retain the full scan. + const untracked = yield* prepareUntrackedReviewIndex(cwd, pathspecs); + if (untracked === null) { + return yield* new GitCommandError({ + operation: "GitVcsDriver.readBranchChangeTotals", + command: "git ls-files", + cwd, + detail: "Too many untracked files to count.", + }); + } + const readNumstat = (ref: string) => + executeGit( + "GitVcsDriver.readBranchChangeTotals", + cwd, + [...REVIEW_DIFF_ARGS, "--numstat", "-z", ref, "--"], + { + allowNonZeroExit: true, + maxOutputBytes: REVIEW_METADATA_MAX_OUTPUT_BYTES, + env: untracked.env, + }, + ); + let result = yield* readNumstat(mergeBase); + if (result.exitCode !== 0 && mergeBase === "HEAD" && isUnbornHeadStderr(result.stderr)) { + result = yield* readNumstat(yield* readEmptyTreeHash(cwd)); + } + if (result.exitCode !== 0) { return yield* new GitCommandError({ operation: "GitVcsDriver.readBranchChangeTotals", command: "git diff --numstat", cwd, - detail: "Could not read complete Changes totals.", + detail: "Could not read Changes totals.", + exitCode: result.exitCode, }); - return { - baseRef, - insertions: result.files.reduce((total, file) => total + file.additions, 0), - deletions: result.files.reduce((total, file) => total + file.deletions, 0), - }; + } + let insertions = 0; + let deletions = 0; + for (const file of parseReviewNumstat(result.stdout)) { + insertions += file.additions; + deletions += file.deletions; + } + return { baseRef, insertions, deletions }; }, Effect.scoped); const reviewCommitError = (cwd: string, detail: string) => @@ -3345,6 +3367,92 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* }; }); + const getReviewRepositories = Effect.fn("getReviewRepositories")(function* ( + input: ReviewRepositoriesInput, + ) { + const root = yield* resolveRepositoryPathsUncached(input.cwd); + if (!root?.worktreeRoot) return { repositories: [], truncated: false }; + const canonicalRoot = yield* fileSystem + .realPath(root.worktreeRoot) + .pipe(Effect.orElseSucceed(() => root.worktreeRoot!)); + const withinRoot = (candidate: string) => { + const relative = path.relative(canonicalRoot, candidate); + return ( + relative === "" || + (relative !== ".." && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative)) + ); + }; + const repositories: Array = []; + const seen = new Set(); + let pending = [canonicalRoot]; + let truncated = false; + // Discovery is separate from diff/status refreshes and bounded for large umbrellas. + while (pending.length > 0) { + const batch = pending.splice(0, 8); + const found = yield* Effect.forEach( + batch, + (candidate) => + Effect.gen(function* () { + const real = yield* fileSystem + .realPath(candidate) + .pipe(Effect.orElseSucceed(() => null)); + if (!real || !withinRoot(real) || seen.has(real)) return null; + seen.add(real); + const repo = yield* resolveRepositoryPathsUncached(real); + if (!repo?.worktreeRoot) return null; + const actualRoot = yield* fileSystem + .realPath(repo.worktreeRoot) + .pipe(Effect.orElseSucceed(() => null)); + // Git otherwise walks out of an uninitialized module into its parent repository. + if (actualRoot !== real) return null; + const manifest = yield* executeGit( + "GitVcsDriver.review.repositories", + real, + ["config", "-z", "--file=.gitmodules", "--get-regexp", "^submodule\\..*\\.path$"], + { allowNonZeroExit: true, maxOutputBytes: 1024 * 1024 }, + ); + const children = + manifest.exitCode === 0 + ? manifest.stdout.split("\0").flatMap((record) => { + const separator = record.indexOf("\n"); + if (separator < 0) return []; + const relative = record.slice(separator + 1); + if (!relative || path.isAbsolute(relative)) return []; + const child = path.resolve(real, relative); + const local = path.relative(real, child); + return local && + local !== ".." && + !local.startsWith(`..${path.sep}`) && + withinRoot(child) + ? [child] + : []; + }) + : []; + return { + repository: { + cwd: real, + path: path.relative(canonicalRoot, real).split(path.sep).join("/"), + name: path.basename(real) || "Repository", + branch: repo.currentBranch, + }, + children, + }; + }), + { concurrency: 8 }, + ); + for (const item of found) { + if (!item) continue; + repositories.push(item.repository); + pending.push(...item.children.filter((child) => !seen.has(child))); + } + if (seen.size + pending.length > 256) { + pending = pending.slice(0, Math.max(0, 256 - seen.size)); + truncated = true; + } + } + return { repositories: repositories.sort((a, b) => a.path.localeCompare(b.path)), truncated }; + }); + const getReviewBranchCommits = Effect.fn("getReviewBranchCommits")(function* ( input: ReviewBranchCommitsInput, ) { @@ -3418,436 +3526,17 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* }; }); - const readReviewGitlinks = Effect.fn("readReviewGitlinks")(function* ( - cwd: string, - revision: string | undefined, - paths: ReadonlyArray, - ) { - if (paths.length === 0) return new Map(); - const pathspecs = paths.map((value) => `:(top,literal)${value}`); - const result = yield* executeGit( - "GitVcsDriver.review.gitlinks", - cwd, - revision - ? ["ls-tree", "--full-name", "-z", "--end-of-options", revision, "--", ...pathspecs] - : ["ls-files", "--stage", "-z", "--", ...pathspecs], - { maxOutputBytes: REVIEW_METADATA_MAX_OUTPUT_BYTES }, - ); - const links = new Map(); - for (const record of result.stdout.split("\0")) { - const match = /^160000 (?:commit )?([a-f0-9]+)(?: 0)?\t([\s\S]+)$/.exec(record); - if (match) links.set(match[2]!, match[1]!); - } - return links; - }); - - const readReviewSubmodulePaths = Effect.fn("readReviewSubmodulePaths")(function* ( - cwd: string, - revision: string | undefined, - ) { - const result = yield* executeGit( - "GitVcsDriver.review.submodulePaths", - cwd, - [ - "config", - "-z", - revision !== undefined ? `--blob=${revision}:.gitmodules` : "--file=.gitmodules", - "--get-regexp", - "^submodule\\..*\\.path$", - ], - { - allowNonZeroExit: true, - maxOutputBytes: REVIEW_METADATA_MAX_OUTPUT_BYTES, - env: { GIT_NO_LAZY_FETCH: "1" }, - }, - ); - return result.exitCode === 0 - ? result.stdout.split("\0").flatMap((record) => { - const separator = record.indexOf("\n"); - return separator < 0 ? [] : [record.slice(separator + 1)]; - }) - : []; - }); - - const readDirtyReviewSubmodules = Effect.fn("readDirtyReviewSubmodules")(function* ( - cwd: string, - paths: ReadonlyArray, - ) { - const result = yield* executeGit( - "GitVcsDriver.review.submoduleStatus", - cwd, - [ - "status", - "--porcelain=2", - "-z", - "--ignore-submodules=none", - "--untracked-files=no", - "--", - ...paths.map((value) => `:(top,literal)${value}`), - ], - { maxOutputBytes: REVIEW_METADATA_MAX_OUTPUT_BYTES }, - ); - const dirty = new Set(); - for (const record of result.stdout.split("\0")) { - const sub = record.split(" ", 3)[2]; - if (sub?.startsWith("S") && sub !== "S...") { - const value = parsePorcelainPath(record); - if (value) dirty.add(value); - } - } - return dirty; - }); - - const resolveReviewSubmodule = Effect.fn("resolveReviewSubmodule")(function* ( - cwd: string, - relativePath: string, - ) { - const candidate = path.resolve(cwd, relativePath); - if (!isPathWithinRoot(cwd, candidate) || candidate === cwd) return null; - if ( - !(yield* fileSystem - .exists(path.join(candidate, ".git")) - .pipe(Effect.orElseSucceed(() => false))) - ) - return null; - const [root, target] = yield* Effect.all([ - fileSystem.realPath(cwd), - fileSystem.realPath(candidate), - ]).pipe(Effect.orElseSucceed(() => [null, null] as const)); - if (!root || !target || !isPathWithinRoot(root, target) || root === target) return null; - if (yield* isServerInternalPath(target)) return null; - const actualRoot = yield* runGitStdout("GitVcsDriver.review.submoduleRoot", candidate, [ - "rev-parse", - "--show-toplevel", - ]); - return path.resolve(actualRoot.trim()) === target ? target : null; - }); - - const readReviewComparison = Effect.fn("readReviewComparison")(function* (options: { - cwd: string; - baseRef: string; - headRef?: string | undefined; - file?: ReviewDiffPreviewInput["file"]; - metadataOnly: boolean; - completeFilesOnly?: boolean | undefined; - trackedOnly?: boolean | undefined; - ignoreWhitespace?: boolean | undefined; - minimal?: boolean | undefined; - prefix?: string; - indexes?: Map> | undefined; - statuses?: Map> | undefined; - untrackedPathspecs?: ReadonlyArray | undefined; - }): Effect.fn.Return< - { - stdout: string; - stdoutTruncated: boolean; - files: ReadonlyArray | undefined; - }, - GitCommandError, - Scope.Scope - > { - const { cwd, file, headRef } = options; - const prefix = options.prefix ?? ""; - const paths = file ? [file.path, ...(file.previousPath ? [file.previousPath] : [])] : []; - const pathArgs = paths.map((value) => `:(top,literal)${value}`); - const includeUntracked = !headRef && !options.trackedOnly; - let preparedIndex = includeUntracked ? options.indexes?.get(cwd) : undefined; - if (!preparedIndex && includeUntracked) { - preparedIndex = yield* Effect.cached( - prepareUntrackedReviewIndex(cwd, options.untrackedPathspecs ?? pathArgs, file?.path), - ); - options.indexes?.set(cwd, preparedIndex); - } - const untracked = preparedIndex ? yield* preparedIndex : { env: undefined }; - if (untracked === null && options.completeFilesOnly) - return { stdout: "", stdoutTruncated: true, files: undefined }; - const metadataOnly = options.metadataOnly && untracked !== null; - const args = [ - ...REVIEW_DIFF_ARGS, - ...(options.minimal === false ? [] : ["--minimal"]), - ...(options.ignoreWhitespace ? ["--ignore-all-space"] : []), - `--src-prefix=a/${prefix}`, - `--dst-prefix=b/${prefix}`, - ]; - let baseRef = options.baseRef; - const readStat = ( - selectedPaths: ReadonlyArray = pathArgs, - extraArgs: ReadonlyArray = [], - ) => - executeGit( - headRef - ? "GitVcsDriver.getReviewDiffPreview.commitStat" - : "GitVcsDriver.getReviewDiffPreview.stat", - cwd, - [ - ...args, - ...extraArgs, - "--numstat", - "-z", - baseRef, - ...(headRef ? [headRef] : []), - "--", - ...selectedPaths, - ], - { - allowNonZeroExit: true, - maxOutputBytes: REVIEW_METADATA_MAX_OUTPUT_BYTES, - env: untracked?.env, - }, - ); - let stat = yield* readStat(); - if (stat.exitCode !== 0 && baseRef === "HEAD" && isUnbornHeadStderr(stat.stderr)) { - baseRef = yield* readEmptyTreeHash(cwd); - stat = yield* readStat(); - } - if (stat.exitCode !== 0) - return yield* new GitCommandError({ - operation: headRef - ? "GitVcsDriver.getReviewDiffPreview.commitStat" - : "GitVcsDriver.getReviewDiffPreview.stat", - command: "git diff --numstat", - cwd, - detail: "Could not read complete diff statistics.", - exitCode: stat.exitCode, - }); - const expanded = new Map< - string, - { - stdout: string; - stdoutTruncated: boolean; - files: ReadonlyArray | undefined; - } - >(); - let submodulesTruncated = false; - let fallbackPaths: string[] = []; - let ownPathArgs: ReadonlyArray = pathArgs; - let fallbackPathArgs: ReadonlyArray = []; - let ownStats = parseReviewNumstat(stat.stdout); - const manifestExists = yield* fileSystem - .exists(path.join(cwd, ".gitmodules")) - .pipe(Effect.orElseSucceed(() => false)); - if ( - manifestExists || - headRef !== undefined || - file !== undefined || - ownStats.some((entry) => entry.path === ".gitmodules" || entry.previousPath === ".gitmodules") - ) { - const declarations = yield* Effect.all( - [ - readReviewSubmodulePaths(cwd, baseRef), - readReviewSubmodulePaths(cwd, headRef ?? ""), - manifestExists ? readReviewSubmodulePaths(cwd, undefined) : Effect.succeed([]), - ], - { concurrency: 3 }, - ); - const modulePaths = Array.from(new Set(declarations.flat())).filter( - (value) => - value.length > 0 && value !== "." && isPathWithinRoot(cwd, path.resolve(cwd, value)), - ); - const statusKey = `${cwd}\0${modulePaths.join("\0")}`; - let status = options.statuses?.get(statusKey); - if (!status && !headRef && !file && modulePaths.length > 0) { - status = yield* Effect.cached(readDirtyReviewSubmodules(cwd, modulePaths)); - options.statuses?.set(statusKey, status); - } - const dirty = status ? yield* status : new Set(); - const possiblePaths = modulePaths.filter( - (value) => - !file || - paths.some((candidate) => candidate === value || candidate.startsWith(`${value}/`)), - ); - const [before, after] = yield* Effect.all( - [ - readReviewGitlinks(cwd, baseRef, possiblePaths), - readReviewGitlinks(cwd, headRef, possiblePaths), - ], - { concurrency: 2 }, - ); - const candidates = possiblePaths.filter( - (value) => - file || - dirty.has(value) || - ownStats.some((entry) => entry.path === value) || - before.get(value) !== after.get(value), - ); - const children = yield* Effect.forEach( - candidates, - (modulePath) => - Effect.gen(function* () { - if (file?.path === modulePath) return null; - const moduleCwd = yield* resolveReviewSubmodule(cwd, modulePath); - if (!moduleCwd || !before.has(modulePath) || !after.has(modulePath)) return null; - const base = before.get(modulePath)!; - const endpoints = [base, ...(headRef ? [after.get(modulePath)!] : [])]; - const available = yield* Effect.forEach( - endpoints, - (sha) => - executeGit( - "GitVcsDriver.review.submoduleBase", - moduleCwd, - ["cat-file", "-e", `${sha}^{commit}`], - { allowNonZeroExit: true, env: { GIT_NO_LAZY_FETCH: "1" } }, - ), - { concurrency: 2 }, - ); - if (available.some((result) => result.exitCode !== 0)) return null; - const child = yield* readReviewComparison({ - ...options, - metadataOnly, - completeFilesOnly: true, - cwd: moduleCwd, - baseRef: base, - ...(headRef ? { headRef: after.get(modulePath)! } : {}), - prefix: `${prefix}${modulePath}/`, - untrackedPathspecs: undefined, - file: file - ? { - ...file, - path: file.path.slice(modulePath.length + 1), - previousPath: file.previousPath?.startsWith(`${modulePath}/`) - ? file.previousPath.slice(modulePath.length + 1) - : null, - } - : undefined, - }); - return child.files?.length === 0 ? null : ([modulePath, child] as const); - }), - { concurrency: 4 }, - ); - for (const child of children) { - if (!child) continue; - if (child[1].files === undefined) submodulesTruncated = true; - else expanded.set(child[0], child[1]); - } - fallbackPaths = candidates.filter((value) => !expanded.has(value)); - ownPathArgs = [ - ...pathArgs, - ...Array.from( - [...expanded.keys(), ...fallbackPaths], - (value) => `:(top,exclude,literal)${value}`, - ), - ]; - const isFallbackPath = (value: string) => - fallbackPaths.some( - (modulePath) => value === modulePath || value.startsWith(`${modulePath}/`), - ); - fallbackPathArgs = file - ? paths.filter(isFallbackPath).flatMap((value) => [ - `:(top,literal)${value}`, - // A literal trailing-slash exclusion also hides the gitlink; glob matches descendants only. - ...(fallbackPaths.includes(value) - ? [`:(top,exclude,glob)${value.replace(/[\\*?[\]]/g, "\\$&")}/**`] - : []), - ]) - : fallbackPaths.map((value) => `:(top,literal)${value}`); - if (fallbackPaths.length > 0) { - if ( - ownStats.some( - (entry) => - (isFallbackPath(entry.path) && !fallbackPaths.includes(entry.path)) || - (entry.previousPath !== null && - (isFallbackPath(entry.path) || isFallbackPath(entry.previousPath))), - ) - ) { - const own = yield* readStat(ownPathArgs); - if (own.exitCode !== 0) - return yield* new GitCommandError({ - operation: "GitVcsDriver.review.partitionedStat", - command: "git diff --numstat", - cwd, - detail: "Could not read diff statistics outside submodule paths.", - exitCode: own.exitCode, - }); - ownStats = parseReviewNumstat(own.stdout); - } - const fallback = yield* readStat(fallbackPathArgs, ["--ignore-submodules=none"]); - if (fallback.exitCode !== 0) - return yield* new GitCommandError({ - operation: "GitVcsDriver.review.submoduleStat", - command: "git diff --numstat", - cwd, - detail: "Could not read submodule pointer statistics.", - exitCode: fallback.exitCode, - }); - ownStats = [ - ...ownStats.filter((entry) => !fallbackPaths.includes(entry.path)), - ...parseReviewNumstat(fallback.stdout), - ]; - } - } - const ownFiles = ownStats - .filter((entry) => !expanded.has(entry.path)) - .map((entry) => ({ - ...entry, - path: `${prefix}${entry.path}`, - previousPath: entry.previousPath === null ? null : `${prefix}${entry.previousPath}`, - })); - const limit = file ? REVIEW_DIFF_FILE_MAX_OUTPUT_BYTES : REVIEW_DIFF_PATCH_MAX_OUTPUT_BYTES; - const readPatch = ( - selectedPaths: ReadonlyArray, - extraArgs: ReadonlyArray = [], - ) => - executeGit( - headRef - ? "GitVcsDriver.getReviewDiffPreview.commitPatch" - : "GitVcsDriver.getReviewDiffPreview.patch", - cwd, - [ - ...args, - ...extraArgs, - "--patch", - baseRef, - ...(headRef ? [headRef] : []), - "--", - ...selectedPaths, - ], - { maxOutputBytes: limit, appendTruncationMarker: true, env: untracked?.env }, - ); - const patches = - metadataOnly || ownFiles.length === 0 - ? [] - : yield* Effect.all( - [ - readPatch(ownPathArgs), - ...(fallbackPaths.length > 0 - ? [readPatch(fallbackPathArgs, ["--ignore-submodules=none"])] - : []), - ], - { concurrency: 2 }, - ); - const children = Array.from(expanded.values()); - const patchText = patches.map((patch) => patch.stdout).join(""); - // Git leaves extended rename/copy headers relative to the child despite src/dst prefixes. - const ownPatch = prefix - ? patchText.replace( - /^(rename (?:from|to)|copy (?:from|to)) (.+)$/gm, - (_, kind, value) => `${kind} ${quoteGitPatchPath(prefix + unquoteGitPatchPath(value))}`, - ) - : patchText; - const bytes = new TextEncoder().encode( - [ownPatch, ...children.map((child) => child.stdout)].join(""), - ); - const truncated = - untracked === null || - submodulesTruncated || - patches.some((patch) => patch.stdoutTruncated) || - children.some((child) => child.stdoutTruncated) || - bytes.length > limit; - return { - stdout: - new TextDecoder().decode(bytes.subarray(0, limit)) + - (bytes.length > limit ? OUTPUT_TRUNCATED_MARKER : ""), - stdoutTruncated: truncated, - files: - untracked === null - ? undefined - : [...ownFiles, ...children.flatMap((child) => child.files ?? [])], - }; - }); - const getReviewDiffPreview = Effect.fn("getReviewDiffPreview")(function* ( input: ReviewDiffPreviewInput, ) { + const pathArgs = input.file + ? [input.file.path, ...(input.file.previousPath ? [input.file.previousPath] : [])].map( + (path) => `:(top,literal)${path}`, + ) + : []; + const patchLimit = input.file + ? REVIEW_DIFF_FILE_MAX_OUTPUT_BYTES + : REVIEW_DIFF_PATCH_MAX_OUTPUT_BYTES; const sourceKind = input.file?.sourceKind ?? input.sourceKind; const needsCurrentBranch = input.commitSha === undefined && @@ -3872,8 +3561,6 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* } const cwd = repository.worktreeRoot; - const indexes = new Map>(); - const statuses = new Map>(); const hashDiff = (diff: string, files: ReadonlyArray) => digest.digest(Digest.sha256, new TextEncoder().encode(JSON.stringify([diff, files]))).pipe( Effect.map(Hex.encode), @@ -3894,17 +3581,26 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* } const commit = yield* resolveReviewCommit(cwd, input.commitSha); const { subject } = yield* readReviewCommitMessage(cwd, commit.sha); - const patch = yield* readReviewComparison({ - indexes, - statuses, + const diffArgs = [ + ...REVIEW_DIFF_ARGS, + ...(input.ignoreWhitespace ? ["--ignore-all-space"] : []), + ]; + const stat = yield* executeGit( + "GitVcsDriver.getReviewDiffPreview.commitStat", cwd, - baseRef: commit.parent, - headRef: commit.sha, - file: input.file, - metadataOnly: input.metadataOnly === true && !input.file, - ignoreWhitespace: input.ignoreWhitespace, - }); - const files = patch.files ?? []; + [...diffArgs, "--numstat", "-z", commit.parent, commit.sha, "--", ...pathArgs], + { maxOutputBytes: REVIEW_METADATA_MAX_OUTPUT_BYTES }, + ); + const files = parseReviewNumstat(stat.stdout); + const patch = + input.metadataOnly && !input.file + ? { stdout: "", stdoutTruncated: false } + : yield* executeGit( + "GitVcsDriver.getReviewDiffPreview.commitPatch", + cwd, + [...diffArgs, "--patch", commit.parent, commit.sha, "--", ...pathArgs], + { maxOutputBytes: patchLimit, appendTruncationMarker: true }, + ); const diffHash = yield* hashDiff(patch.stdout, files); return { cwd: input.cwd, @@ -3933,29 +3629,81 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* ? { baseRef: input.baseRef ?? null, mergeBase: null } : yield* resolveReviewMergeBase(cwd, repository.currentBranch, input.baseRef); - const readTrackedDiff = (ref: string | null) => - ref === null - ? Effect.succeed({ - stdout: "", - stdoutTruncated: false, - files: [] as ReadonlyArray, - }) - : readReviewComparison({ - indexes, - statuses, - cwd, - baseRef: ref, - file: input.file, - metadataOnly: input.metadataOnly === true && !input.file, - ignoreWhitespace: input.ignoreWhitespace, - trackedOnly: input.trackedOnly, - }); - const [dirtyTrackedResult, baseResult] = - review.mergeBase === dirtyRef - ? yield* readTrackedDiff(dirtyRef).pipe(Effect.map((result) => [result, result] as const)) - : yield* Effect.all([readTrackedDiff(dirtyRef), readTrackedDiff(review.mergeBase)], { - concurrency: 2, - }); + const diffArgs = [ + ...REVIEW_DIFF_ARGS, + "--minimal", + ...(input.ignoreWhitespace ? ["--ignore-all-space"] : []), + ]; + const readStats = Effect.fn("GitVcsDriver.getReviewDiffPreview.stat")(function* ( + ref: string, + env?: NodeJS.ProcessEnv, + ) { + const args = [...diffArgs, "--numstat", "-z"]; + const result = yield* executeGit( + "GitVcsDriver.getReviewDiffPreview.stat", + cwd, + [...args, ref, "--", ...pathArgs], + { allowNonZeroExit: true, maxOutputBytes: REVIEW_METADATA_MAX_OUTPUT_BYTES, env }, + ); + if (result.exitCode === 0) return { ref, files: parseReviewNumstat(result.stdout) }; + if (ref === "HEAD" && isUnbornHeadStderr(result.stderr)) { + const emptyTree = yield* readEmptyTreeHash(cwd); + const stdout = yield* runGitStdoutWithOptions( + "GitVcsDriver.getReviewDiffPreview.unbornStat", + cwd, + [...args, emptyTree, "--", ...pathArgs], + { maxOutputBytes: REVIEW_METADATA_MAX_OUTPUT_BYTES, env }, + ); + return { ref: emptyTree, files: parseReviewNumstat(stdout) }; + } + return yield* new GitCommandError({ + operation: "GitVcsDriver.getReviewDiffPreview.stat", + cwd, + command: "git diff --numstat", + detail: "Could not read complete diff statistics.", + exitCode: result.exitCode, + }); + }); + const readTrackedDiff = Effect.fn("GitVcsDriver.getReviewDiffPreview.tracked")(function* ( + ref: string | null, + env?: NodeJS.ProcessEnv, + metadataOnly = false, + ) { + if (ref === null) return { stdout: "", stdoutTruncated: false, files: [] }; + const stat = yield* readStats(ref, env); + if (metadataOnly || stat.files.length === 0) { + return { stdout: "", stdoutTruncated: false, files: stat.files }; + } + const patch = yield* executeGit( + "GitVcsDriver.getReviewDiffPreview.patch", + cwd, + [...diffArgs, "--patch", stat.ref, "--", ...pathArgs], + { maxOutputBytes: patchLimit, appendTruncationMarker: true, env }, + ); + return { ...patch, files: stat.files }; + }); + const [dirtyTrackedResult, baseResult] = yield* Effect.gen(function* () { + const untracked = input.trackedOnly + ? { env: undefined } + : yield* prepareUntrackedReviewIndex(cwd, pathArgs, input.file?.path); + const metadataOnly = input.metadataOnly === true && !input.file && untracked !== null; + const [dirty, base] = + review.mergeBase === dirtyRef + ? yield* readTrackedDiff(dirtyRef, untracked?.env, metadataOnly).pipe( + Effect.map((result) => [result, result] as const), + ) + : yield* Effect.all( + [ + readTrackedDiff(dirtyRef, untracked?.env, metadataOnly), + readTrackedDiff(review.mergeBase, untracked?.env, metadataOnly), + ], + { concurrency: 2 }, + ); + if (untracked !== null) return [dirty, base] as const; + const incomplete = (ref: string | null, result: typeof dirty) => + ref === null ? result : { ...result, files: undefined, stdoutTruncated: true }; + return [incomplete(dirtyRef, dirty), incomplete(review.mergeBase, base)] as const; + }).pipe(Effect.scoped); const dirtyFiles = dirtyTrackedResult.files; const baseFiles = baseResult.files; const dirtyDiff = dirtyTrackedResult.stdout; @@ -4004,7 +3752,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* : sources, ...(input.trackedOnly ? { incomplete: true } : {}), }; - }, Effect.scoped); + }); const reviewDiffFileError = ( input: ReviewDiffFileContentsInput, @@ -4031,21 +3779,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* input: ReviewDiffFileContentsInput, revision: string, relativePath: string, - ): Effect.fn.Return { - const parts = relativePath.split("/"); - const ancestors = parts.slice(0, -1).map((_, index) => parts.slice(0, index + 1).join("/")); - const links = yield* readReviewGitlinks(input.cwd, revision, ancestors); - for (const [modulePath, sha] of links) { - if (!relativePath.startsWith(`${modulePath}/`)) continue; - const moduleCwd = yield* resolveReviewSubmodule(input.cwd, modulePath); - if (!moduleCwd) - return yield* reviewDiffFileError(input, "Submodule is not available locally."); - return yield* readReviewFileAtRevision( - { ...input, cwd: moduleCwd }, - sha, - relativePath.slice(modulePath.length + 1), - ); - } + ) { const result = yield* executeGit( "GitVcsDriver.getReviewDiffFileContents.revision", input.cwd, @@ -4137,7 +3871,6 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* if (repositoryRoot.length === 0) { return yield* reviewDiffFileError(input, "Could not resolve the Git repository root."); } - const revisionInput = { ...input, cwd: repositoryRoot }; if (input.sourceKind === "commit") { if (!input.headRef) { return yield* reviewDiffFileError(input, "Selected commit is no longer available."); @@ -4153,10 +3886,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* [ input.changeType === "new" ? Effect.succeed("") - : readReviewFileAtRevision(revisionInput, commit.parent, input.oldPath), + : readReviewFileAtRevision(input, commit.parent, input.oldPath), input.changeType === "deleted" ? Effect.succeed("") - : readReviewFileAtRevision(revisionInput, commit.sha, input.newPath), + : readReviewFileAtRevision(input, commit.sha, input.newPath), ], { concurrency: 2 }, ); @@ -4170,7 +3903,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* [ input.changeType === "new" ? Effect.succeed("") - : readReviewFileAtRevision(revisionInput, oldRevision, input.oldPath), + : readReviewFileAtRevision(input, oldRevision, input.oldPath), input.changeType === "deleted" ? Effect.succeed("") : readWorkingTreeReviewFile(input, repositoryRoot), @@ -5493,6 +5226,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* return yield* withListRefsInvalidation(cwd, pullCurrentBranch(cwd)); }), readRangeContext, + getReviewRepositories, getReviewBranchCommits, getReviewDiffPreview, getReviewDiffFileContents, diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts index 8a8c4e1be719c..de18b17d2f67e 100644 --- a/apps/server/src/ws.ts +++ b/apps/server/src/ws.ts @@ -3217,6 +3217,7 @@ export const layerWsRpc = ( gitWorkflow.switchRef(input).pipe(Effect.tap(() => refreshGitStatus(input.cwd))), [WS_METHODS.vcsInit]: (input) => vcsProvisioning.initRepository(input).pipe(Effect.tap(() => refreshGitStatus(input.cwd))), + [WS_METHODS.reviewGetRepositories]: (input) => review.getRepositories(input), [WS_METHODS.reviewGetBranchCommits]: (input) => review.getBranchCommits(input), [WS_METHODS.reviewGetDiffPreview]: (input) => review.getDiffPreview(input), [WS_METHODS.reviewGetDiffFileContents]: (input) => review.getDiffFileContents(input), diff --git a/apps/web/src/components/DiffPanel.browser.test.tsx b/apps/web/src/components/DiffPanel.browser.test.tsx index 874118daf306f..4282a0fb0ef97 100644 --- a/apps/web/src/components/DiffPanel.browser.test.tsx +++ b/apps/web/src/components/DiffPanel.browser.test.tsx @@ -7,6 +7,7 @@ import { ThreadId, type ClientSettings, type ReviewBranchCommitsResult, + type ReviewRepositoriesResult, type ReviewDiffPreviewResult, type ReviewDiffPreviewSource, type ScopedThreadRef, @@ -34,6 +35,7 @@ const state = vi.hoisted(() => ({ status: null as unknown as ResultAtom, localRefs: null as unknown as ResultAtom, remoteRefs: null as unknown as ResultAtom, + repositories: null as unknown as ResultAtom, commits: null as unknown as ResultAtom, })); @@ -44,6 +46,7 @@ vi.mock("~/state/review", () => ({ reviewEnvironment: { diffPreview: ({ input }: { input: { baseRef?: string } }) => state.previews.get(input.baseRef ?? null)!, + repositories: () => state.repositories, branchCommits: () => state.commits, diffFilePatch: () => state.patch, diffFileContents: {}, @@ -121,6 +124,7 @@ function setup(failedBaseRef: string | null) { useDiffPanelStore.setState({ byThreadKey: {}, branchBaseRefByThreadKey: {}, + repositoryCwdByThreadKey: {}, keepSelectionOnOpenThreadKey: null, }); useDiffPanelSessionStore.setState({ collapsedFileKeysByScopeKey: {} }); @@ -146,6 +150,7 @@ function setup(failedBaseRef: string | null) { state.previews.set(null, Atom.make(AsyncResult.success(preview()))); state.previews.set("main", Atom.make(AsyncResult.success(preview()))); state.previews.set(failedBaseRef, Atom.make(AsyncResult.fail(new Error(errorMessage)))); + state.repositories = Atom.make(AsyncResult.success({ repositories: [], truncated: false })); state.config = Atom.make(null); state.status = Atom.make( AsyncResult.success({ diff --git a/apps/web/src/components/DiffPanel.tsx b/apps/web/src/components/DiffPanel.tsx index e7e6d614ef77f..7f30d7cf1b0d7 100644 --- a/apps/web/src/components/DiffPanel.tsx +++ b/apps/web/src/components/DiffPanel.tsx @@ -1,3 +1,4 @@ +import { scopedThreadKey } from "@t3tools/client-runtime/environment"; import { RefreshIcon } from "~/components/ui/refresh-icon"; import { useAtomValue } from "@effect/atom-react"; import type { FileDiffContentsLoader, FileDiffMetadata } from "@pierre/diffs"; @@ -256,10 +257,56 @@ export default function DiffPanel({ } : null, ); - const activeCwd = activeThread?.worktreePath ?? activeProject?.workspaceRoot; - const activeRepositoryRoot = activeThread?.worktreePath - ? undefined - : activeProject?.repositoryIdentity?.rootPath; + const storedDiffSelection = useDiffPanelStore((state) => + selectThreadDiffPanelSelection(state.byThreadKey, routeThreadRef), + ); + const workspaceCwd = activeThread?.worktreePath ?? activeProject?.workspaceRoot; + const [repositoryQuery, setRepositoryQuery] = useState(""); + const requestedRepositoryCwd = useDiffPanelStore((state) => + routeThreadRef ? state.repositoryCwdByThreadKey[scopedThreadKey(routeThreadRef)] : undefined, + ); + const repositoriesQuery = useEnvironmentQuery( + canReadFiles && activeThread && workspaceCwd + ? reviewEnvironment.repositories({ + environmentId: activeThread.environmentId, + input: { cwd: workspaceCwd }, + }) + : null, + ); + const repositories = repositoriesQuery.data?.repositories ?? []; + const selectedRepository = + repositories.find((repo) => repo.cwd === requestedRepositoryCwd) ?? repositories[0]; + const isRepositoryDiff = + storedDiffSelection.kind === "branch" || + storedDiffSelection.kind === "unstaged" || + storedDiffSelection.kind === "commit"; + const repositoryMissing = + !!requestedRepositoryCwd && + repositoriesQuery.data !== null && + !repositories.some((repo) => repo.cwd === requestedRepositoryCwd); + // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- clears a persisted child repository and its refs after that repository disappears + useEffect(() => { + if (repositoryMissing && routeThreadRef && repositories[0]) + useDiffPanelStore.getState().selectRepository(routeThreadRef, repositories[0].cwd); + }, [repositoryMissing, routeThreadRef, repositories]); + const diffSelection = + repositoryMissing && isRepositoryDiff + ? { kind: "branch" as const, baseRef: null } + : storedDiffSelection; + const activeCwd = isRepositoryDiff + ? requestedRepositoryCwd && repositoriesQuery.isPending && !repositoriesQuery.data + ? undefined + : (selectedRepository?.cwd ?? workspaceCwd) + : workspaceCwd; + const isChildRepository = isRepositoryDiff && !!selectedRepository?.path; + const repositoryLabel = selectedRepository + ? selectedRepository.name + ": " + (selectedRepository.branch ?? "HEAD") + : "Repository"; + const activeRepositoryRoot = isChildRepository + ? activeCwd + : activeThread?.worktreePath + ? undefined + : activeProject?.repositoryIdentity?.rootPath; const serverConfig = useAtomValue( serverEnvironment.configValueAtom(activeThread?.environmentId ?? null), ); @@ -277,9 +324,7 @@ export default function DiffPanel({ }) : null, ); - const diffSelection = useDiffPanelStore((state) => - selectThreadDiffPanelSelection(state.byThreadKey, routeThreadRef), - ); + const rememberedBaseRef = useDiffPanelStore((state) => selectThreadBranchBaseRef(state, routeThreadRef), ); @@ -344,7 +389,7 @@ export default function DiffPanel({ ? `commit:${selectedCommitSha}` : selectedGitScope; const collapseScopeKey = routeThreadRef - ? `${routeThreadRef.environmentId}:${routeThreadRef.threadId}:${reviewSectionId}` + ? `${routeThreadRef.environmentId}:${routeThreadRef.threadId}:${isRepositoryDiff ? activeCwd : ""}:${reviewSectionId}` : null; const codeViewMountKey = `${collapseScopeKey ?? reviewSectionId}:${codeViewRevision}`; const selectedCheckpointRange = @@ -404,7 +449,7 @@ export default function DiffPanel({ environmentId: activeThread.environmentId, input: { cwd: activeCwd, - ...(rememberedBaseRef ? { baseRef: rememberedBaseRef } : {}), + ...(!repositoryMissing && rememberedBaseRef ? { baseRef: rememberedBaseRef } : {}), }, }) : null, @@ -452,7 +497,10 @@ export default function DiffPanel({ const headerCommitSha = selectedRunId === null ? selectedCommitSha : null; const selectedCommitUrl = headerCommitSha !== null - ? projectCommitUrl(activeProject?.repositoryIdentity, headerCommitSha) + ? projectCommitUrl( + isChildRepository ? null : activeProject?.repositoryIdentity, + headerCommitSha, + ) : null; const openLink = useOpenLink(routeThreadRef); // A SHA link goes to the code host; it must not route back into this pane's commit selection. @@ -816,7 +864,71 @@ export default function DiffPanel({ const headerRow = ( <> -
+
+ {isGitSelection && repositories.length > 1 && ( + repo.cwd)} + filteredItems={repositories + .filter((repo) => + (repo.name + " " + repo.path + " " + (repo.branch ?? "HEAD")) + .toLowerCase() + .includes(repositoryQuery.toLowerCase()), + ) + .map((repo) => repo.cwd)} + value={selectedRepository?.cwd ?? null} + onOpenChange={(open) => { + if (open) repositoriesQuery.refresh(); + else setRepositoryQuery(""); + }} + onValueChange={(cwd) => { + if (!routeThreadRef || !cwd || !repositories.some((repo) => repo.cwd === cwd)) return; + useDiffPanelStore.getState().selectRepository(routeThreadRef, cwd); + setOpenCommitMenu(null); + setBaseRefQuery(""); + }} + > + } + aria-label={"Repository: " + repositoryLabel} + > + {repositoryLabel} + + + + setRepositoryQuery(event.target.value)} + /> + No matching repository. + + {(cwd) => { + const repo = repositories.find((repo) => repo.cwd === cwd)!; + return ( + + + + {repo.name}: {repo.branch ?? "HEAD"} + + + {repo.path || "Workspace repository"} + + + + ); + }} + + {repositoriesQuery.data?.truncated && ( +

+ Repository discovery reached its limit. +

+ )} + {repositoriesQuery.error && ( +

{repositoriesQuery.error}

+ )} +
+
+ )} } @@ -903,7 +1015,7 @@ export default function DiffPanel({ > {branchCommits?.commits.map((commit) => { const commitUrl = projectCommitUrl( - activeProject?.repositoryIdentity, + isChildRepository ? null : activeProject?.repositoryIdentity, commit.sha, ); return ( @@ -1433,7 +1545,12 @@ export default function DiffPanel({ files={codeViewFiles} pendingFiles={pendingFiles} renderCodeViewFooter={renderLoadingBoundary} - sectionId={reviewSectionId} + filePathPrefix={isChildRepository ? selectedRepository?.path : undefined} + sectionId={ + isChildRepository + ? selectedRepository?.path + ":" + reviewSectionId + : reviewSectionId + } sectionTitle={reviewSectionTitle} composerDraftTarget={composerDraftTarget} renderHeaderFilenameSuffix={(fileDiff) => { diff --git a/apps/web/src/components/DiffPanelShell.tsx b/apps/web/src/components/DiffPanelShell.tsx index 456f70516b23b..cc548f05cf8a9 100644 --- a/apps/web/src/components/DiffPanelShell.tsx +++ b/apps/web/src/components/DiffPanelShell.tsx @@ -10,7 +10,7 @@ export type DiffPanelMode = "inline" | "sheet" | "sidebar" | "embedded"; function getDiffPanelHeaderRowClassName(mode: DiffPanelMode) { const shouldUseDragRegion = isElectron && mode !== "sheet" && mode !== "embedded"; return cn( - "flex items-center justify-between gap-2", + "flex items-center justify-between gap-2 @max-[42rem]/diff-panel:flex-wrap @max-[42rem]/diff-panel:h-auto @max-[42rem]/diff-panel:py-1", mode === "embedded" ? "px-2" : "px-4", shouldUseDragRegion ? "drag-region h-[var(--workspace-topbar-height)] border-b border-border wco:pr-(--workspace-native-controls-inset)" @@ -28,7 +28,7 @@ export function DiffPanelShell(props: { return (
[], codeViewKey = "initial", pendingFiles: FileDiffMetadata[] = [], + filePathPrefix = "", ) { return ( { - it("keeps the text and selected line through refresh and a provisional lazy patch", async () => { - using _drafts = setup(); - const original = file("shared.ts", 1, "rename-pure", "old.ts"); - await using screen = await render(view([original]), { container }); - await click("Comment on shared.ts"); - await typeComment("Keep this draft"); - const refreshed = file("shared.ts", 2, "rename-pure", "old.ts"); - await screen.rerender(view([refreshed], "refreshed")); - expect(editor().value).toBe("Keep this draft"); - expect(container.querySelector("mark")?.textContent).toBe("Line 1"); + it.each(["", "packages/auth"])( + "keeps a comment through lazy refresh with repository prefix %j", + async (prefix) => { + using _drafts = setup(); + const original = file("shared.ts", 1, "rename-pure", "old.ts"); + await using screen = await render(view([original], "initial", [], prefix), { container }); + await click("Comment on shared.ts"); + await typeComment("Keep this draft"); + const refreshed = file("shared.ts", 2, "rename-pure", "old.ts"); + await screen.rerender(view([refreshed], "refreshed", [], prefix)); + expect(editor().value).toBe("Keep this draft"); + expect(container.querySelector("mark")?.textContent).toBe("Line 1"); - await screen.rerender( - view([], "loading", [ - { ...refreshed.fileDiff, type: "rename-changed", hunks: [], cacheKey: "shared:pending" }, - ]), - ); - expect(container.querySelector("textarea")).toBeNull(); - await screen.rerender(view([refreshed], "loaded")); - expect(editor().value).toBe("Keep this draft"); - expect(container.querySelector("mark")?.textContent).toBe("Line 1"); - await click("Comment"); - expect(useComposerDraftStore.getState().getComposerDraft(target)?.reviewComments).toEqual([ - expect.objectContaining({ - filePath: "shared.ts", - text: "Keep this draft", - selection: { ...range, endSide: "additions" }, - }), - ]); - expect(container.querySelector("p")?.textContent).toBe("Keep this draft"); - }); + await screen.rerender( + view( + [], + "loading", + [ + { + ...refreshed.fileDiff, + type: "rename-changed", + hunks: [], + cacheKey: "shared:pending", + }, + ], + prefix, + ), + ); + expect(container.querySelector("textarea")).toBeNull(); + await screen.rerender(view([refreshed], "loaded", [], prefix)); + expect(editor().value).toBe("Keep this draft"); + expect(container.querySelector("mark")?.textContent).toBe("Line 1"); + await click("Comment"); + expect(useComposerDraftStore.getState().getComposerDraft(target)?.reviewComments).toEqual([ + expect.objectContaining({ + filePath: prefix ? prefix + "/shared.ts" : "shared.ts", + text: "Keep this draft", + selection: { ...range, endSide: "additions" }, + }), + ]); + expect(container.querySelector("p")?.textContent).toBe("Keep this draft"); + }, + ); }); diff --git a/apps/web/src/components/diffs/AnnotatableCodeView.tsx b/apps/web/src/components/diffs/AnnotatableCodeView.tsx index b7965428478fb..896ae0c5f8e9d 100644 --- a/apps/web/src/components/diffs/AnnotatableCodeView.tsx +++ b/apps/web/src/components/diffs/AnnotatableCodeView.tsx @@ -83,6 +83,7 @@ interface AnnotatableCodeViewProps { collapsed: boolean; }>; pendingFiles: ReadonlyArray; + filePathPrefix?: string | undefined; sectionId: string; sectionTitle: string; composerDraftTarget: ScopedThreadRef | DraftId; @@ -109,6 +110,7 @@ export function AnnotatableCodeView({ environmentId, projectId, codeViewKey, + filePathPrefix, files, pendingFiles, sectionId, @@ -141,6 +143,8 @@ export function AnnotatableCodeView({ } | null>(null); const [draftText, setDraftText] = useState(""); + const commentPath = (filePath: string) => + filePathPrefix ? filePathPrefix + "/" + filePath : filePath; const filesByKey = new Map(files.map((file) => [file.fileKey, file])); if ( draft !== null && @@ -162,7 +166,7 @@ export function AnnotatableCodeView({ .filter( (comment) => comment.sectionId === sectionId && - comment.filePath === filePath && + comment.filePath === commentPath(filePath) && (comment.fenceLanguage ?? "diff") === "diff", ) .reduce((annotations, comment) => { @@ -214,7 +218,7 @@ export function AnnotatableCodeView({ id: entry.id, sectionId, sectionTitle, - filePath: file.filePath, + filePath: commentPath(file.filePath), fileDiff: file.fileDiff, range: entry.range, text, @@ -235,7 +239,7 @@ export function AnnotatableCodeView({ id, sectionId, sectionTitle, - filePath, + filePath: commentPath(filePath), fileDiff: item.fileDiff, range, text: "", diff --git a/apps/web/src/diffPanelStore.test.ts b/apps/web/src/diffPanelStore.test.ts index 82065c86fe3aa..b6565075f9125 100644 --- a/apps/web/src/diffPanelStore.test.ts +++ b/apps/web/src/diffPanelStore.test.ts @@ -10,10 +10,43 @@ import { } from "./diffPanelStore"; describe("diffPanelStore", () => { + it("resets repository-specific refs and commits when changing repositories", () => { + const ref = { environmentId: EnvironmentId.make("env"), threadId: ThreadId.make("thread") }; + const other = { ...ref, environmentId: EnvironmentId.make("remote") }; + const key = scopedThreadKey(ref); + const store = useDiffPanelStore.getState(); + store.selectBranchBaseRef(ref, "parent-only-branch"); + store.selectCommit(ref, "parent-commit"); + store.selectRepository(ref, "/project/auth"); + expect(useDiffPanelStore.getState().byThreadKey[key]).toEqual({ + kind: "branch", + baseRef: null, + }); + expect(useDiffPanelStore.getState().repositoryCwdByThreadKey[key]).toBe("/project/auth"); + expect( + useDiffPanelStore.getState().repositoryCwdByThreadKey[scopedThreadKey(other)], + ).toBeUndefined(); + store.selectGitScope(ref, "unstaged"); + store.selectRepository(ref, "/project/ui"); + expect(useDiffPanelStore.getState().byThreadKey[key]).toEqual({ kind: "unstaged" }); + store.selectBranchBaseRef(ref, "child-only-branch"); + store.selectRepository(ref, null); + store.selectCommit(ref, "linked-parent-commit", true); + expect(useDiffPanelStore.getState().repositoryCwdByThreadKey[key]).toBeUndefined(); + expect(useDiffPanelStore.getState().byThreadKey[key]).toEqual({ + kind: "commit", + sha: "linked-parent-commit", + baseRef: null, + }); + store.selectRepository(ref, "/project/auth"); + store.removeThread(ref); + expect(useDiffPanelStore.getState().repositoryCwdByThreadKey[key]).toBeUndefined(); + }); beforeEach(() => { useDiffPanelStore.setState({ byThreadKey: {}, branchBaseRefByThreadKey: {}, + repositoryCwdByThreadKey: {}, }); useDiffPanelSessionStore.setState({ collapsedFileKeysByScopeKey: {} }); }); diff --git a/apps/web/src/diffPanelStore.ts b/apps/web/src/diffPanelStore.ts index 6bd7eb0e01bdd..8bb38c5c85d7e 100644 --- a/apps/web/src/diffPanelStore.ts +++ b/apps/web/src/diffPanelStore.ts @@ -25,6 +25,8 @@ const DEFAULT_SELECTION: DiffPanelSelection = { kind: "branch", baseRef: null }; interface DiffPanelStoreState { byThreadKey: Record; branchBaseRefByThreadKey: Record; + repositoryCwdByThreadKey: Record; + selectRepository: (ref: ScopedThreadRef, cwd: string | null) => void; /** A link that selected a commit before opening the closed pane; opening keeps that selection. */ keepSelectionOnOpenThreadKey: string | null; selectGitScope: (ref: ScopedThreadRef, scope: "branch" | "unstaged") => void; @@ -57,6 +59,30 @@ export const useDiffPanelStore = create()( (set, get) => ({ byThreadKey: {}, branchBaseRefByThreadKey: {}, + repositoryCwdByThreadKey: {}, + selectRepository: (ref, cwd) => + set((state) => { + const key = scopedThreadKey(ref); + return { + repositoryCwdByThreadKey: + cwd === null + ? Object.fromEntries( + Object.entries(state.repositoryCwdByThreadKey).filter( + ([entry]) => entry !== key, + ), + ) + : { ...state.repositoryCwdByThreadKey, [key]: cwd }, + byThreadKey: { + ...state.byThreadKey, + [key]: + state.byThreadKey[key]?.kind === "unstaged" + ? { kind: "unstaged" } + : { kind: "branch", baseRef: null }, + }, + branchBaseRefByThreadKey: { ...state.branchBaseRefByThreadKey, [key]: null }, + keepSelectionOnOpenThreadKey: null, + }; + }), keepSelectionOnOpenThreadKey: null, selectGitScope: (ref, scope) => set((state) => { @@ -157,13 +183,19 @@ export const useDiffPanelStore = create()( removeThread: (ref) => set((state) => { const threadKey = scopedThreadKey(ref); - if (!(threadKey in state.byThreadKey) && !(threadKey in state.branchBaseRefByThreadKey)) { + if ( + !(threadKey in state.byThreadKey) && + !(threadKey in state.branchBaseRefByThreadKey) && + !(threadKey in state.repositoryCwdByThreadKey) + ) { return state; } const { [threadKey]: _removed, ...byThreadKey } = state.byThreadKey; const { [threadKey]: _removedBaseRef, ...branchBaseRefByThreadKey } = state.branchBaseRefByThreadKey; - return { byThreadKey, branchBaseRefByThreadKey }; + const { [threadKey]: _removedRepository, ...repositoryCwdByThreadKey } = + state.repositoryCwdByThreadKey; + return { byThreadKey, branchBaseRefByThreadKey, repositoryCwdByThreadKey }; }), }), { @@ -175,6 +207,7 @@ export const useDiffPanelStore = create()( partialize: (state) => ({ byThreadKey: state.byThreadKey, branchBaseRefByThreadKey: state.branchBaseRefByThreadKey, + repositoryCwdByThreadKey: state.repositoryCwdByThreadKey, }), }, ), diff --git a/apps/web/src/lib/openPullRequestLink.browser.test.tsx b/apps/web/src/lib/openPullRequestLink.browser.test.tsx index a93f32a8902f7..b5fea8a104050 100644 --- a/apps/web/src/lib/openPullRequestLink.browser.test.tsx +++ b/apps/web/src/lib/openPullRequestLink.browser.test.tsx @@ -138,23 +138,32 @@ it.each(["system", "app"] as const)( }, ); -it("opens an available commit in the closed Diff pane without the pane resetting it", async () => { - setup(); - const sha = "a".repeat(40); - state.preview.mockResolvedValue( - AsyncResult.success({ sources: [{ kind: "commit", headRef: sha }] }), - ); - await using screen = await render( - - - , - ); - await screen.getByRole("link", { name: "Commit" }).click(); - await vi.waitFor(() => - expect(useDiffPanelStore.getState().byThreadKey).toEqual({ - [scopedThreadKey(threadRef)]: { kind: "commit", sha, baseRef: null }, - }), - ); - // ChatView resets a pane opened without an explicit request to Changes unless this is set. - expect(useDiffPanelStore.getState().consumeKeepSelectionOnOpen(threadRef)).toBe(true); -}); +it.each([false, true])( + "opens a linked parent commit from a child repository with diffOpen=%j", + async (diffOpen) => { + setup(); + useDiffPanelStore.getState().selectRepository(threadRef, "/repo/auth"); + useDiffPanelStore.getState().selectBranchBaseRef(threadRef, "child-only-branch"); + if (diffOpen) useRightPanelStore.getState().open(threadRef, "diff"); + const sha = "a".repeat(40); + state.preview.mockResolvedValue( + AsyncResult.success({ sources: [{ kind: "commit", headRef: sha }] }), + ); + await using screen = await render( + + + , + ); + await screen.getByRole("link", { name: "Commit" }).click(); + await vi.waitFor(() => + expect(useDiffPanelStore.getState().byThreadKey).toEqual({ + [scopedThreadKey(threadRef)]: { kind: "commit", sha, baseRef: null }, + }), + ); + // ChatView resets a pane opened without an explicit request to Changes unless this is set. + expect(useDiffPanelStore.getState().consumeKeepSelectionOnOpen(threadRef)).toBe(!diffOpen); + expect( + useDiffPanelStore.getState().repositoryCwdByThreadKey[scopedThreadKey(threadRef)], + ).toBeUndefined(); + }, +); diff --git a/apps/web/src/lib/openPullRequestLink.ts b/apps/web/src/lib/openPullRequestLink.ts index 31c04b6263e5f..f0fbd5b7b696a 100644 --- a/apps/web/src/lib/openPullRequestLink.ts +++ b/apps/web/src/lib/openPullRequestLink.ts @@ -188,6 +188,7 @@ export function useOpenChangeRequestLink( useRightPanelStore.getState().byThreadKey, resolvedThreadRef, ) === "diff"; + useDiffPanelStore.getState().selectRepository(resolvedThreadRef, null); useDiffPanelStore.getState().selectCommit(resolvedThreadRef, commit.sha, !diffOpen); useRightPanelStore.getState().open(resolvedThreadRef, "diff"); return; diff --git a/docs/user/source-control.md b/docs/user/source-control.md index ed06d900de310..437f82c5863cd 100644 --- a/docs/user/source-control.md +++ b/docs/user/source-control.md @@ -20,22 +20,17 @@ commit links follow your **Open links in** setting; on web, when that opens a ne shows an **Open link** button. Command/Ctrl+Click always opens the link in your browser. Commit links follow the **Open pull requests in T3 Code** setting in **Settings → Diffs**. -## Review submodule changes - -Changes and Uncommitted include files inside initialized submodules, including nested submodules. -Paths include the submodule directory, so files with the same name in different repositories stay -distinct. Comparisons use the submodule commit recorded by the parent repository at the selected -base. Commit reviews compare the recorded submodule commits without including local edits. -Review includes declared submodules even when Git's submodule ignore settings hide them from summaries. -This override applies to direct submodules. If a nested submodule is hidden by its parent's own ignore -settings, unset those settings in that parent submodule to review it. - -Initialize submodules and fetch their recorded commits to review their file contents. When those -are unavailable, or a comparison adds or removes a submodule, it shows the submodule commit pointer. -If a submodule has too many untracked files to list, the review shows its pointer instead of its files. -Files in the parent and other submodules remain available. Turn diffs, checkpoint restore, -staging, and committing still operate on the thread's repository. Commit submodule changes inside each -submodule before recording the updated pointer in the parent. +## Review a submodule + +The Diff pane starts with the thread's repository. Its changes include submodule commit +pointers, as in Git. Use the repository picker to review an initialized submodule, including +nested submodules, without opening another project. Each entry shows its repository name and +branch. Detached checkouts show HEAD; repository paths distinguish matching names. + +Changes, Uncommitted, commit lists and comparison branches use the selected repository. +Switching repositories resets the comparison branch to Automatic. File paths are relative to +the selected repository. Turn diffs still show the thread's checkpoints and do not capture +uncommitted contents inside submodules. Initialize a submodule with Git before selecting it. ## Connect an account diff --git a/packages/client-runtime/src/state/review.ts b/packages/client-runtime/src/state/review.ts index a374e51b1df89..baddcf6afc248 100644 --- a/packages/client-runtime/src/state/review.ts +++ b/packages/client-runtime/src/state/review.ts @@ -74,6 +74,11 @@ export function createReviewEnvironmentAtoms( }); }); return { + repositories: createEnvironmentRpcQueryAtomFamily(runtime, { + label: "environment-data:review:repositories", + tag: WS_METHODS.reviewGetRepositories, + staleTimeMs: 30_000, + }), branchCommits: createEnvironmentRpcQueryAtomFamily(runtime, { label: "environment-data:review:branch-commits", tag: WS_METHODS.reviewGetBranchCommits, diff --git a/packages/contracts/src/review.ts b/packages/contracts/src/review.ts index 2608e635ba906..dbb426e5793d1 100644 --- a/packages/contracts/src/review.ts +++ b/packages/contracts/src/review.ts @@ -3,6 +3,21 @@ import { TrimmedNonEmptyString } from "./baseSchemas.ts"; import { GitCommandError } from "./git.ts"; import { VcsError } from "./vcs.ts"; +export const ReviewRepositoriesInput = Schema.Struct({ cwd: TrimmedNonEmptyString }); +export type ReviewRepositoriesInput = typeof ReviewRepositoriesInput.Type; +export const ReviewRepositoriesResult = Schema.Struct({ + repositories: Schema.Array( + Schema.Struct({ + cwd: TrimmedNonEmptyString, + path: Schema.String, + name: Schema.NonEmptyString, + branch: Schema.NullOr(TrimmedNonEmptyString), + }), + ), + truncated: Schema.Boolean, +}); +export type ReviewRepositoriesResult = typeof ReviewRepositoriesResult.Type; + export const ReviewDiffPreviewInput = Schema.Struct({ cwd: TrimmedNonEmptyString, baseRef: Schema.optional(TrimmedNonEmptyString), diff --git a/packages/contracts/src/rpc.ts b/packages/contracts/src/rpc.ts index 0f6af19188e18..02e34b48ea74b 100644 --- a/packages/contracts/src/rpc.ts +++ b/packages/contracts/src/rpc.ts @@ -151,6 +151,8 @@ import { VcsStatusStreamEvent, } from "./git.ts"; import { + ReviewRepositoriesInput, + ReviewRepositoriesResult, ReviewBranchCommitsInput, ReviewBranchCommitsResult, ReviewDiffFileContentsInput, @@ -470,6 +472,7 @@ export const WS_METHODS = { gitResolvePullRequest: "git.resolvePullRequest", gitPreparePullRequestThread: "git.preparePullRequestThread", + reviewGetRepositories: "review.getRepositories", reviewGetBranchCommits: "review.getBranchCommits", reviewGetDiffPreview: "review.getDiffPreview", reviewGetDiffFileContents: "review.getDiffFileContents", @@ -1581,6 +1584,12 @@ const WsVcsInitRpc = Rpc.make(WS_METHODS.vcsInit, { error: Schema.Union([VcsError, EnvironmentAuthorizationError]), }); +const WsReviewGetRepositoriesRpc = Rpc.make(WS_METHODS.reviewGetRepositories, { + payload: ReviewRepositoriesInput, + success: ReviewRepositoriesResult, + error: Schema.Union([ReviewDiffPreviewError, EnvironmentAuthorizationError]), +}); + const WsReviewGetBranchCommitsRpc = Rpc.make(WS_METHODS.reviewGetBranchCommits, { payload: ReviewBranchCommitsInput, success: ReviewBranchCommitsResult, @@ -2223,6 +2232,7 @@ export const WsRpcGroup = RpcGroup.make( WsVcsCreateRefRpc, WsVcsSwitchRefRpc, WsVcsInitRpc, + WsReviewGetRepositoriesRpc, WsReviewGetBranchCommitsRpc, WsReviewGetDiffPreviewRpc, WsReviewGetDiffFileContentsRpc, From 4a9b933f11184f7629daa18017673190d71ec2b7 Mon Sep 17 00:00:00 2001 From: Neil Mazumdar <84065477+NeilMazumdar@users.noreply.github.com> Date: Sun, 8 Nov 2026 10:01:09 +1030 Subject: [PATCH 06/14] fix(git): keep repository review paths and scopes consistent --- .../src/features/review/ReviewSheet.tsx | 4 +- .../src/features/review/useReviewSections.ts | 8 ++- apps/server/src/vcs/GitVcsDriverCore.test.ts | 29 +++++++++- apps/server/src/vcs/GitVcsDriverCore.ts | 26 +++++++-- apps/web/src/components/DiffPanel.tsx | 32 +++++++---- apps/web/src/diffFileActions.test.ts | 56 ++++++++++++++----- apps/web/src/diffFileActions.ts | 4 +- apps/web/src/diffPanelStore.test.ts | 18 +++++- apps/web/src/diffPanelStore.ts | 32 ++++++++--- .../lib/openPullRequestLink.browser.test.tsx | 37 +++++++++--- apps/web/src/lib/openPullRequestLink.ts | 14 ++++- 11 files changed, 201 insertions(+), 59 deletions(-) diff --git a/apps/mobile/src/features/review/ReviewSheet.tsx b/apps/mobile/src/features/review/ReviewSheet.tsx index a328a5c6a5ec5..e3690850b3fdd 100644 --- a/apps/mobile/src/features/review/ReviewSheet.tsx +++ b/apps/mobile/src/features/review/ReviewSheet.tsx @@ -580,7 +580,7 @@ export function ReviewSheet(props: ReviewSheetProps) { }); }; const androidHeaderSubtitle = [ - selectedRepository + repositories.length > 1 && selectedSection?.kind !== "turn" && selectedRepository ? selectedRepository.name + ": " + (selectedRepository.branch ?? "HEAD") : null, selectedSection?.title, @@ -626,7 +626,7 @@ export function ReviewSheet(props: ReviewSheetProps) { return <>{children}; })(); const headerSubtitle = [ - selectedRepository + repositories.length > 1 && selectedSection?.kind !== "turn" && selectedRepository ? selectedRepository.name + ": " + (selectedRepository.branch ?? "HEAD") : null, headerDiffSummary.additions, diff --git a/apps/mobile/src/features/review/useReviewSections.ts b/apps/mobile/src/features/review/useReviewSections.ts index f24d98610aa37..2c99b8631a1ce 100644 --- a/apps/mobile/src/features/review/useReviewSections.ts +++ b/apps/mobile/src/features/review/useReviewSections.ts @@ -1,3 +1,4 @@ +import { useThreadSelection } from "../../state/use-thread-selection"; import { useState } from "react"; import { useAtomValue } from "@effect/atom-react"; import { resolveFilesystemReadAccess } from "@t3tools/client-runtime/state/filesystem"; @@ -63,13 +64,16 @@ export function useReviewSections(input: { ? reviewEnvironment.repositories({ environmentId, input: { cwd: selectedThreadCwd } }) : null, ); - const repositories = repositoriesQuery.data?.repositories ?? []; + const { selectedThreadProject } = useThreadSelection(); + const repositories = (repositoriesQuery.data?.repositories ?? []).map((repo) => + repo.path ? repo : { ...repo, name: selectedThreadProject?.title || repo.name }, + ); const selectedRepository = repositories.find( (repo) => repositoryChoice?.threadKey === reviewCache.threadKey && repo.cwd === repositoryChoice.cwd, ) ?? repositories[0]; - const reviewCwd = selectedRepository?.cwd ?? selectedThreadCwd; + const reviewCwd = selectedRepository?.path ? selectedRepository.cwd : selectedThreadCwd; const repositoryPath = selectedRepository?.path ?? ""; const diffPreview = useEnvironmentQuery( canReadFiles && enabled && environmentId !== undefined && reviewCwd !== null diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts index ca7fe2931783a..a08123ca6642e 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.test.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts @@ -2252,6 +2252,18 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { ], ); assert.isFalse(repos.truncated); + const subdirectory = NodePath.join(cwd, "packages"); + const scoped = yield* driver.getReviewRepositories({ cwd: subdirectory }); + assert.equal(scoped.repositories[0]!.cwd, subdirectory); + assert.deepStrictEqual( + scoped.repositories.map((repo) => repo.path), + ["", "auth service", "auth service/vendor/schema"], + ); + yield* git(cwd, ["config", "-f", ".gitmodules", "submodule.file.path", "README.md"]); + assert.deepStrictEqual( + (yield* driver.getReviewRepositories({ cwd })).repositories, + repos.repositories, + ); const commits = yield* driver.getReviewBranchCommits({ cwd: child, baseRef: "HEAD~1" }); assert.equal(commits.commits.length, 1); assert.equal(commits.commits[0]!.subject, "Child change"); @@ -2282,11 +2294,26 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { makeReviewDiffFileContentsInput(child), ); assert.equal(file.newContents, "# changed auth\n"); + const fs = yield* FileSystem.FileSystem; + if ((yield* HostProcess.Platform) !== "win32") { + const locked = NodePath.join(cwd, "locked"); + yield* fs.makeDirectory(locked); + yield* git(cwd, ["config", "-f", ".gitmodules", "submodule.locked.path", "locked"]); + yield* fs.chmod(locked, 0); + const result = yield* driver + .getReviewRepositories({ cwd }) + .pipe(Effect.ensuring(fs.chmod(locked, 0o755).pipe(Effect.orDie))); + assert.deepStrictEqual(result.repositories, repos.repositories); + } // A hostile manifest must not turn discovery into an arbitrary filesystem scan. yield* git(cwd, ["config", "-f", ".gitmodules", "submodule.escape.path", upstream]); yield* git(cwd, ["config", "-f", ".gitmodules", "submodule.parent.path", ".."]); if (symlinksSupported) { - const fs = yield* FileSystem.FileSystem; + const alias = NodePath.join(yield* makeTmpDir(), "alias"); + yield* fs.symlink(subdirectory, alias); + const throughAlias = yield* driver.getReviewRepositories({ cwd: alias }); + assert.equal(throughAlias.repositories[0]!.cwd, alias); + assert.equal(throughAlias.repositories[1]!.cwd, NodePath.join(alias, "auth service")); yield* fs.symlink(upstream, NodePath.join(cwd, "escape-link")); yield* git(cwd, ["config", "-f", ".gitmodules", "submodule.link.path", "escape-link"]); } diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts index 6589b3c83eaf7..854c69be40fda 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.ts @@ -3375,8 +3375,11 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* const canonicalRoot = yield* fileSystem .realPath(root.worktreeRoot) .pipe(Effect.orElseSucceed(() => root.worktreeRoot!)); + const canonicalWorkspace = yield* fileSystem + .realPath(input.cwd) + .pipe(Effect.orElseSucceed(() => path.resolve(input.cwd))); const withinRoot = (candidate: string) => { - const relative = path.relative(canonicalRoot, candidate); + const relative = path.relative(canonicalWorkspace, candidate); return ( relative === "" || (relative !== ".." && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative)) @@ -3396,7 +3399,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* const real = yield* fileSystem .realPath(candidate) .pipe(Effect.orElseSucceed(() => null)); - if (!real || !withinRoot(real) || seen.has(real)) return null; + if (!real || (real !== canonicalRoot && !withinRoot(real)) || seen.has(real)) + return null; + const stat = yield* fileSystem.stat(real).pipe(Effect.orElseSucceed(() => null)); + if (stat?.type !== "Directory") return null; seen.add(real); const repo = yield* resolveRepositoryPathsUncached(real); if (!repo?.worktreeRoot) return null; @@ -3430,14 +3436,24 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* : []; return { repository: { - cwd: real, - path: path.relative(canonicalRoot, real).split(path.sep).join("/"), + cwd: + real === canonicalRoot + ? input.cwd + : path.resolve(input.cwd, path.relative(canonicalWorkspace, real)), + path: + real === canonicalRoot + ? "" + : path.relative(canonicalWorkspace, real).split(path.sep).join("/"), name: path.basename(real) || "Repository", branch: repo.currentBranch, }, children, }; - }), + }).pipe( + Effect.catchCause((cause) => + candidate === canonicalRoot ? Effect.failCause(cause) : Effect.succeed(null), + ), + ), { concurrency: 8 }, ); for (const item of found) { diff --git a/apps/web/src/components/DiffPanel.tsx b/apps/web/src/components/DiffPanel.tsx index 7f30d7cf1b0d7..8daa1a50951c2 100644 --- a/apps/web/src/components/DiffPanel.tsx +++ b/apps/web/src/components/DiffPanel.tsx @@ -273,7 +273,9 @@ export default function DiffPanel({ }) : null, ); - const repositories = repositoriesQuery.data?.repositories ?? []; + const repositories = (repositoriesQuery.data?.repositories ?? []).map((repo) => + repo.path ? repo : { ...repo, name: activeProject?.title || repo.name }, + ); const selectedRepository = repositories.find((repo) => repo.cwd === requestedRepositoryCwd) ?? repositories[0]; const isRepositoryDiff = @@ -287,17 +289,19 @@ export default function DiffPanel({ // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- clears a persisted child repository and its refs after that repository disappears useEffect(() => { if (repositoryMissing && routeThreadRef && repositories[0]) - useDiffPanelStore.getState().selectRepository(routeThreadRef, repositories[0].cwd); + useDiffPanelStore.getState().clearRepository(routeThreadRef); }, [repositoryMissing, routeThreadRef, repositories]); const diffSelection = repositoryMissing && isRepositoryDiff ? { kind: "branch" as const, baseRef: null } : storedDiffSelection; - const activeCwd = isRepositoryDiff - ? requestedRepositoryCwd && repositoriesQuery.isPending && !repositoriesQuery.data + const repositoryCwd = + requestedRepositoryCwd && repositoriesQuery.isPending && !repositoriesQuery.data ? undefined - : (selectedRepository?.cwd ?? workspaceCwd) - : workspaceCwd; + : selectedRepository?.path + ? selectedRepository.cwd + : workspaceCwd; + const activeCwd = isRepositoryDiff ? repositoryCwd : workspaceCwd; const isChildRepository = isRepositoryDiff && !!selectedRepository?.path; const repositoryLabel = selectedRepository ? selectedRepository.name + ": " + (selectedRepository.branch ?? "HEAD") @@ -320,7 +324,7 @@ export default function DiffPanel({ activeThread !== null && activeThread !== undefined && activeCwd != null ? vcsEnvironment.status({ environmentId: activeThread.environmentId, - input: { cwd: activeCwd }, + input: { cwd: activeCwd, ...(isChildRepository ? { includeRemote: false } : {}) }, }) : null, ); @@ -444,11 +448,11 @@ export default function DiffPanel({ : null, ); const branchCommitsQuery = useEnvironmentQuery( - visible && isGitRepo && canReadFiles && activeThread && activeCwd + visible && isGitRepo && canReadFiles && activeThread && repositoryCwd ? reviewEnvironment.branchCommits({ environmentId: activeThread.environmentId, input: { - cwd: activeCwd, + cwd: repositoryCwd, ...(!repositoryMissing && rememberedBaseRef ? { baseRef: rememberedBaseRef } : {}), }, }) @@ -774,8 +778,11 @@ export default function DiffPanel({ openDiffFilePrimaryAction({ threadRef: routeThreadRef, filePath, - activeCwd, - repositoryRoot: activeRepositoryRoot, + activeCwd: workspaceCwd, + fileCwd: isChildRepository ? selectedRepository?.cwd : undefined, + repositoryRoot: activeThread?.worktreePath + ? undefined + : activeProject?.repositoryIdentity?.rootPath, openInEditor: (targetPath) => { void (async () => { const result = await openInPreferredEditor(targetPath); @@ -865,7 +872,7 @@ export default function DiffPanel({ const headerRow = ( <>
- {isGitSelection && repositories.length > 1 && ( + {repositories.length > 1 && ( repo.cwd)} filteredItems={repositories @@ -890,6 +897,7 @@ export default function DiffPanel({ } aria-label={"Repository: " + repositoryLabel} + title="Repository for Changes, Uncommitted and Commits. Turn diffs use the thread checkpoints." > {repositoryLabel} diff --git a/apps/web/src/diffFileActions.test.ts b/apps/web/src/diffFileActions.test.ts index f7faf72ef8bd3..ad44aed62b76d 100644 --- a/apps/web/src/diffFileActions.test.ts +++ b/apps/web/src/diffFileActions.test.ts @@ -15,25 +15,51 @@ describe("openDiffFilePrimaryAction", () => { useRightPanelStore.setState({ byThreadKey: {}, threadPanelVisibilityByThreadKey: {} }); }); - it("opens repository-relative diff files from a nested project", () => { - const openInEditor = vi.fn(); - - openDiffFilePrimaryAction({ - threadRef: THREAD_REF, + it.each([ + { filePath: "frontend/Dockerfile", activeCwd: "/repo/frontend", repositoryRoot: "/repo", - openInEditor, - }); + fileCwd: undefined, + expected: "Dockerfile", + }, + { + filePath: "src/policy.ts", + activeCwd: "/repo", + repositoryRoot: "/repo", + fileCwd: "/repo/packages/auth", + expected: "packages/auth/src/policy.ts", + }, + { + filePath: "src/policy.ts", + activeCwd: "/repo/packages", + repositoryRoot: "/repo", + fileCwd: "/repo/packages/auth", + expected: "auth/src/policy.ts", + }, + ])( + "opens a diff in its workspace at $expected", + ({ filePath, activeCwd, repositoryRoot, fileCwd, expected }) => { + const openInEditor = vi.fn(); - expect( - selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, THREAD_REF), - ).toMatchObject({ - isOpen: true, - activeSurfaceId: "file:Dockerfile", - }); - expect(openInEditor).not.toHaveBeenCalled(); - }); + openDiffFilePrimaryAction({ + threadRef: THREAD_REF, + filePath, + activeCwd, + repositoryRoot, + fileCwd, + openInEditor, + }); + + expect( + selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, THREAD_REF), + ).toMatchObject({ + isOpen: true, + activeSurfaceId: `file:${expected}`, + }); + expect(openInEditor).not.toHaveBeenCalled(); + }, + ); it("preserves repository-relative paths in a separate worktree", () => { expect( diff --git a/apps/web/src/diffFileActions.ts b/apps/web/src/diffFileActions.ts index c6a3fa3d436fc..b02b2dca3799d 100644 --- a/apps/web/src/diffFileActions.ts +++ b/apps/web/src/diffFileActions.ts @@ -13,6 +13,7 @@ interface OpenDiffFilePrimaryActionInput { readonly threadRef: ScopedThreadRef | null; readonly filePath: string; readonly activeCwd: string | undefined; + readonly fileCwd?: string | undefined; readonly repositoryRoot?: string | undefined; readonly openInEditor: (targetPath: string) => void; } @@ -93,11 +94,12 @@ export function openDiffFilePrimaryAction({ threadRef, filePath, activeCwd, + fileCwd, repositoryRoot, openInEditor, }: OpenDiffFilePrimaryActionInput): void { const workspaceFilePath = resolveDiffPathForWorkspace({ - filePath, + filePath: fileCwd ? resolvePathLinkTarget(filePath, fileCwd) : filePath, workspaceRoot: activeCwd, repositoryRoot, }); diff --git a/apps/web/src/diffPanelStore.test.ts b/apps/web/src/diffPanelStore.test.ts index b6565075f9125..2fc966b57fe9a 100644 --- a/apps/web/src/diffPanelStore.test.ts +++ b/apps/web/src/diffPanelStore.test.ts @@ -1,4 +1,4 @@ -import { EnvironmentId, ThreadId, TurnItemId } from "@t3tools/contracts"; +import { EnvironmentId, ThreadId, TurnItemId, RunId } from "@t3tools/contracts"; import * as DateTime from "effect/DateTime"; import { scopedThreadKey } from "@t3tools/client-runtime/environment"; import { beforeEach, describe, expect, it } from "vite-plus/test"; @@ -30,7 +30,7 @@ describe("diffPanelStore", () => { store.selectRepository(ref, "/project/ui"); expect(useDiffPanelStore.getState().byThreadKey[key]).toEqual({ kind: "unstaged" }); store.selectBranchBaseRef(ref, "child-only-branch"); - store.selectRepository(ref, null); + store.clearRepository(ref); store.selectCommit(ref, "linked-parent-commit", true); expect(useDiffPanelStore.getState().repositoryCwdByThreadKey[key]).toBeUndefined(); expect(useDiffPanelStore.getState().byThreadKey[key]).toEqual({ @@ -39,6 +39,20 @@ describe("diffPanelStore", () => { baseRef: null, }); store.selectRepository(ref, "/project/auth"); + store.selectTurn(ref, RunId.make("turn"), "file.ts"); + const turn = useDiffPanelStore.getState().byThreadKey[key]; + store.clearRepository(ref); + expect(useDiffPanelStore.getState().byThreadKey[key]).toEqual(turn); + store.selectRepository(ref, "/project/auth"); + store.selectToolCall(ref, { + threadId: ThreadId.make("child"), + itemId: TurnItemId.make("edit"), + revision: "1", + }); + const tool = useDiffPanelStore.getState().byThreadKey[key]; + store.clearRepository(ref); + expect(useDiffPanelStore.getState().byThreadKey[key]).toEqual(tool); + store.selectRepository(ref, "/project/auth"); store.removeThread(ref); expect(useDiffPanelStore.getState().repositoryCwdByThreadKey[key]).toBeUndefined(); }); diff --git a/apps/web/src/diffPanelStore.ts b/apps/web/src/diffPanelStore.ts index 8bb38c5c85d7e..664884d967d89 100644 --- a/apps/web/src/diffPanelStore.ts +++ b/apps/web/src/diffPanelStore.ts @@ -26,7 +26,8 @@ interface DiffPanelStoreState { byThreadKey: Record; branchBaseRefByThreadKey: Record; repositoryCwdByThreadKey: Record; - selectRepository: (ref: ScopedThreadRef, cwd: string | null) => void; + selectRepository: (ref: ScopedThreadRef, cwd: string) => void; + clearRepository: (ref: ScopedThreadRef) => void; /** A link that selected a commit before opening the closed pane; opening keeps that selection. */ keepSelectionOnOpenThreadKey: string | null; selectGitScope: (ref: ScopedThreadRef, scope: "branch" | "unstaged") => void; @@ -64,14 +65,7 @@ export const useDiffPanelStore = create()( set((state) => { const key = scopedThreadKey(ref); return { - repositoryCwdByThreadKey: - cwd === null - ? Object.fromEntries( - Object.entries(state.repositoryCwdByThreadKey).filter( - ([entry]) => entry !== key, - ), - ) - : { ...state.repositoryCwdByThreadKey, [key]: cwd }, + repositoryCwdByThreadKey: { ...state.repositoryCwdByThreadKey, [key]: cwd }, byThreadKey: { ...state.byThreadKey, [key]: @@ -83,6 +77,26 @@ export const useDiffPanelStore = create()( keepSelectionOnOpenThreadKey: null, }; }), + clearRepository: (ref) => + set((state) => { + const key = scopedThreadKey(ref); + const { [key]: _removedRepository, ...repositoryCwdByThreadKey } = + state.repositoryCwdByThreadKey; + const previous = state.byThreadKey[key]; + return { + repositoryCwdByThreadKey, + branchBaseRefByThreadKey: { ...state.branchBaseRefByThreadKey, [key]: null }, + byThreadKey: { + ...state.byThreadKey, + [key]: + previous?.kind === "turn" || + previous?.kind === "tool-call" || + previous?.kind === "unstaged" + ? previous + : { kind: "branch", baseRef: null }, + }, + }; + }), keepSelectionOnOpenThreadKey: null, selectGitScope: (ref, scope) => set((state) => { diff --git a/apps/web/src/lib/openPullRequestLink.browser.test.tsx b/apps/web/src/lib/openPullRequestLink.browser.test.tsx index b5fea8a104050..efb0039821497 100644 --- a/apps/web/src/lib/openPullRequestLink.browser.test.tsx +++ b/apps/web/src/lib/openPullRequestLink.browser.test.tsx @@ -75,6 +75,7 @@ function setup() { }); useDiffPanelStore.setState({ byThreadKey: {}, + repositoryCwdByThreadKey: {}, branchBaseRefByThreadKey: {}, keepSelectionOnOpenThreadKey: null, }); @@ -138,12 +139,25 @@ it.each(["system", "app"] as const)( }, ); -it.each([false, true])( - "opens a linked parent commit from a child repository with diffOpen=%j", - async (diffOpen) => { +it.each( + [false, true].flatMap((diffOpen) => + ["child", "parent", "default"].map((repository) => ({ diffOpen, repository })), + ), +)( + "opens a linked parent commit with $repository selected and diffOpen=$diffOpen", + async ({ diffOpen, repository }) => { setup(); - useDiffPanelStore.getState().selectRepository(threadRef, "/repo/auth"); - useDiffPanelStore.getState().selectBranchBaseRef(threadRef, "child-only-branch"); + if (repository !== "default") + useDiffPanelStore + .getState() + .selectRepository(threadRef, repository === "child" ? "/repo/auth" : "/repo"); + const expectedBase = repository === "child" ? null : "origin/develop"; + useDiffPanelStore + .getState() + .selectBranchBaseRef( + threadRef, + repository === "child" ? "child-only-branch" : "origin/develop", + ); if (diffOpen) useRightPanelStore.getState().open(threadRef, "diff"); const sha = "a".repeat(40); state.preview.mockResolvedValue( @@ -157,13 +171,18 @@ it.each([false, true])( await screen.getByRole("link", { name: "Commit" }).click(); await vi.waitFor(() => expect(useDiffPanelStore.getState().byThreadKey).toEqual({ - [scopedThreadKey(threadRef)]: { kind: "commit", sha, baseRef: null }, + [scopedThreadKey(threadRef)]: { kind: "commit", sha, baseRef: expectedBase }, }), ); // ChatView resets a pane opened without an explicit request to Changes unless this is set. expect(useDiffPanelStore.getState().consumeKeepSelectionOnOpen(threadRef)).toBe(!diffOpen); - expect( - useDiffPanelStore.getState().repositoryCwdByThreadKey[scopedThreadKey(threadRef)], - ).toBeUndefined(); + expect(useDiffPanelStore.getState().repositoryCwdByThreadKey[scopedThreadKey(threadRef)]).toBe( + repository === "parent" ? "/repo" : undefined, + ); + useDiffPanelStore.getState().selectGitScope(threadRef, "branch"); + expect(useDiffPanelStore.getState().byThreadKey[scopedThreadKey(threadRef)]).toEqual({ + kind: "branch", + baseRef: expectedBase, + }); }, ); diff --git a/apps/web/src/lib/openPullRequestLink.ts b/apps/web/src/lib/openPullRequestLink.ts index f0fbd5b7b696a..6f3e3a0abeb02 100644 --- a/apps/web/src/lib/openPullRequestLink.ts +++ b/apps/web/src/lib/openPullRequestLink.ts @@ -1,3 +1,5 @@ +import { scopedThreadKey } from "@t3tools/client-runtime/environment"; +import { normalizeProjectPathForComparison } from "@t3tools/shared/path"; import type { EnvironmentId, ScopedThreadRef } from "@t3tools/contracts"; import { useAtomValue } from "@effect/atom-react"; import { useNavigate } from "@tanstack/react-router"; @@ -188,7 +190,17 @@ export function useOpenChangeRequestLink( useRightPanelStore.getState().byThreadKey, resolvedThreadRef, ) === "diff"; - useDiffPanelStore.getState().selectRepository(resolvedThreadRef, null); + const selectedCwd = + useDiffPanelStore.getState().repositoryCwdByThreadKey[ + scopedThreadKey(resolvedThreadRef) + ]; + const workspaceCwd = commitThread.worktreePath ?? commitProject.workspaceRoot; + if ( + selectedCwd && + normalizeProjectPathForComparison(selectedCwd) !== + normalizeProjectPathForComparison(workspaceCwd) + ) + useDiffPanelStore.getState().clearRepository(resolvedThreadRef); useDiffPanelStore.getState().selectCommit(resolvedThreadRef, commit.sha, !diffOpen); useRightPanelStore.getState().open(resolvedThreadRef, "diff"); return; From f1f44f8bf00e2244ac8df2d54450a4540d71b1ea Mon Sep 17 00:00:00 2001 From: Neil Mazumdar <84065477+NeilMazumdar@users.noreply.github.com> Date: Sun, 8 Nov 2026 10:34:45 +1030 Subject: [PATCH 07/14] fix(git): refresh and deduplicate repository choices --- apps/server/src/vcs/GitVcsDriverCore.test.ts | 18 ++++++++++++++++++ apps/server/src/vcs/GitVcsDriverCore.ts | 4 ++-- apps/web/src/components/DiffPanel.tsx | 9 ++++++--- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts index a08123ca6642e..3493414fc2603 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.test.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts @@ -2252,6 +2252,24 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { ], ); assert.isFalse(repos.truncated); + yield* git(cwd, [ + "config", + "-f", + ".gitmodules", + "submodule.duplicate.path", + "./packages/auth service", + ]); + yield* git(cwd, [ + "config", + "-f", + ".gitmodules", + "submodule.trailing.path", + "packages/auth service/", + ]); + assert.deepStrictEqual( + (yield* driver.getReviewRepositories({ cwd })).repositories, + repos.repositories, + ); const subdirectory = NodePath.join(cwd, "packages"); const scoped = yield* driver.getReviewRepositories({ cwd: subdirectory }); assert.equal(scoped.repositories[0]!.cwd, subdirectory); diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts index 854c69be40fda..cb6ce7d6b2410 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.ts @@ -3401,9 +3401,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* .pipe(Effect.orElseSucceed(() => null)); if (!real || (real !== canonicalRoot && !withinRoot(real)) || seen.has(real)) return null; + seen.add(real); const stat = yield* fileSystem.stat(real).pipe(Effect.orElseSucceed(() => null)); if (stat?.type !== "Directory") return null; - seen.add(real); const repo = yield* resolveRepositoryPathsUncached(real); if (!repo?.worktreeRoot) return null; const actualRoot = yield* fileSystem @@ -3459,7 +3459,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* for (const item of found) { if (!item) continue; repositories.push(item.repository); - pending.push(...item.children.filter((child) => !seen.has(child))); + pending.push(...new Set(item.children.filter((child) => !seen.has(child)))); } if (seen.size + pending.length > 256) { pending = pending.slice(0, Math.max(0, 256 - seen.size)); diff --git a/apps/web/src/components/DiffPanel.tsx b/apps/web/src/components/DiffPanel.tsx index 8daa1a50951c2..fa65f734ca990 100644 --- a/apps/web/src/components/DiffPanel.tsx +++ b/apps/web/src/components/DiffPanel.tsx @@ -516,7 +516,10 @@ export default function DiffPanel({ void openLink(url, { event }); }; const refreshPreviewQuery = branchDiffPreview.refresh; - const refreshDiffFromUserAction = refreshPreviewQuery; + const refreshDiffFromUserAction = () => { + repositoriesQuery.refresh(); + refreshPreviewQuery(); + }; const preview = branchDiffPreview.data; const gitDiff = selectedGitSource?.diff; @@ -877,9 +880,9 @@ export default function DiffPanel({ items={repositories.map((repo) => repo.cwd)} filteredItems={repositories .filter((repo) => - (repo.name + " " + repo.path + " " + (repo.branch ?? "HEAD")) + (repo.name + ": " + (repo.branch ?? "HEAD") + " " + repo.path) .toLowerCase() - .includes(repositoryQuery.toLowerCase()), + .includes(repositoryQuery.trim().toLowerCase()), ) .map((repo) => repo.cwd)} value={selectedRepository?.cwd ?? null} From c73f7b8f0d98e491828011cd3bd3d29d54ee50c9 Mon Sep 17 00:00:00 2001 From: Neil Mazumdar <84065477+NeilMazumdar@users.noreply.github.com> Date: Wed, 11 Nov 2026 09:38:02 +1030 Subject: [PATCH 08/14] fix(web): keep literal child paths and the loading state during repository discovery --- .../src/components/DiffPanel.browser.test.tsx | 12 ++++++++++ apps/web/src/components/DiffPanel.tsx | 22 +++++++++++-------- apps/web/src/diffFileActions.test.ts | 7 ++++++ apps/web/src/diffFileActions.ts | 7 ++++-- docs/user/source-control.md | 3 +-- 5 files changed, 38 insertions(+), 13 deletions(-) diff --git a/apps/web/src/components/DiffPanel.browser.test.tsx b/apps/web/src/components/DiffPanel.browser.test.tsx index 4282a0fb0ef97..cd4a912ddd904 100644 --- a/apps/web/src/components/DiffPanel.browser.test.tsx +++ b/apps/web/src/components/DiffPanel.browser.test.tsx @@ -273,3 +273,15 @@ it.each([false, true])( await expect.element(toggle).toHaveAttribute("aria-expanded", "false"); }, ); + +it("keeps the loading state while a saved repository is still being discovered", async () => { + using context = setup(null); + state.repositories = Atom.make(AsyncResult.initial(true)); + useDiffPanelStore.getState().selectRepository(context.threadRef, `${cwd}/packages/auth`); + await using screen = await render( + + + , + ); + await expect.element(screen.getByRole("status", { name: "Loading changes..." })).toBeVisible(); +}); diff --git a/apps/web/src/components/DiffPanel.tsx b/apps/web/src/components/DiffPanel.tsx index fa65f734ca990..cc49753e71869 100644 --- a/apps/web/src/components/DiffPanel.tsx +++ b/apps/web/src/components/DiffPanel.tsx @@ -295,12 +295,13 @@ export default function DiffPanel({ repositoryMissing && isRepositoryDiff ? { kind: "branch" as const, baseRef: null } : storedDiffSelection; - const repositoryCwd = - requestedRepositoryCwd && repositoriesQuery.isPending && !repositoriesQuery.data - ? undefined - : selectedRepository?.path - ? selectedRepository.cwd - : workspaceCwd; + const repositoryPending = + !!requestedRepositoryCwd && repositoriesQuery.isPending && !repositoriesQuery.data; + const repositoryCwd = repositoryPending + ? undefined + : selectedRepository?.path + ? selectedRepository.cwd + : workspaceCwd; const activeCwd = isRepositoryDiff ? repositoryCwd : workspaceCwd; const isChildRepository = isRepositoryDiff && !!selectedRepository?.path; const repositoryLabel = selectedRepository @@ -540,9 +541,12 @@ export default function DiffPanel({ ? activeCheckpointDiff : branchDiffPreview; const isLoadingSelectedPatch = - selectedPatchQuery.isPending && - (selectedPatchQuery.data === null || - (isGitSelection && preview?.incomplete === true && selectedGitSource?.files?.length === 0)); + (isGitSelection && repositoryPending) || + (selectedPatchQuery.isPending && + (selectedPatchQuery.data === null || + (isGitSelection && + preview?.incomplete === true && + selectedGitSource?.files?.length === 0))); const selectedPatchError = toolCallSelection ? toolCallDetail.error : selectedTurn diff --git a/apps/web/src/diffFileActions.test.ts b/apps/web/src/diffFileActions.test.ts index ad44aed62b76d..8aa447527faa2 100644 --- a/apps/web/src/diffFileActions.test.ts +++ b/apps/web/src/diffFileActions.test.ts @@ -37,6 +37,13 @@ describe("openDiffFilePrimaryAction", () => { fileCwd: "/repo/packages/auth", expected: "auth/src/policy.ts", }, + { + filePath: "notes:1-2", + activeCwd: "/repo", + repositoryRoot: "/repo", + fileCwd: "/repo/packages/auth", + expected: "packages/auth/notes:1-2", + }, ])( "opens a diff in its workspace at $expected", ({ filePath, activeCwd, repositoryRoot, fileCwd, expected }) => { diff --git a/apps/web/src/diffFileActions.ts b/apps/web/src/diffFileActions.ts index b02b2dca3799d..06b96afe22df4 100644 --- a/apps/web/src/diffFileActions.ts +++ b/apps/web/src/diffFileActions.ts @@ -7,7 +7,7 @@ import { } from "@t3tools/shared/path"; import { useRightPanelStore } from "./rightPanelStore"; -import { resolvePathLinkTarget } from "@t3tools/shared/fileLinks"; +import { joinPath, resolvePathLinkTarget } from "@t3tools/shared/fileLinks"; interface OpenDiffFilePrimaryActionInput { readonly threadRef: ScopedThreadRef | null; @@ -99,7 +99,10 @@ export function openDiffFilePrimaryAction({ openInEditor, }: OpenDiffFilePrimaryActionInput): void { const workspaceFilePath = resolveDiffPathForWorkspace({ - filePath: fileCwd ? resolvePathLinkTarget(filePath, fileCwd) : filePath, + // Git paths are literal: a trailing ":12" is part of the name, not a line position. + filePath: fileCwd + ? joinPath(fileCwd, filePath, fileCwd.startsWith("/") ? "/" : "\\") + : filePath, workspaceRoot: activeCwd, repositoryRoot, }); diff --git a/docs/user/source-control.md b/docs/user/source-control.md index 437f82c5863cd..8efb594492bfa 100644 --- a/docs/user/source-control.md +++ b/docs/user/source-control.md @@ -24,8 +24,7 @@ setting in **Settings → Diffs**. The Diff pane starts with the thread's repository. Its changes include submodule commit pointers, as in Git. Use the repository picker to review an initialized submodule, including -nested submodules, without opening another project. Each entry shows its repository name and -branch. Detached checkouts show HEAD; repository paths distinguish matching names. +nested submodules, without opening another project. Detached checkouts appear as HEAD. Changes, Uncommitted, commit lists and comparison branches use the selected repository. Switching repositories resets the comparison branch to Automatic. File paths are relative to From 4429ed7b310648aa14a06776d5d2014a7ef21441 Mon Sep 17 00:00:00 2001 From: Neil Mazumdar <84065477+NeilMazumdar@users.noreply.github.com> Date: Wed, 11 Nov 2026 10:50:40 +1030 Subject: [PATCH 09/14] fix(mobile): reselecting the active repository keeps the selected section --- apps/mobile/src/features/review/reviewModel.ts | 6 ++++-- apps/mobile/src/features/review/useReviewSections.ts | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/apps/mobile/src/features/review/reviewModel.ts b/apps/mobile/src/features/review/reviewModel.ts index ffb48412c6056..890be2116060a 100644 --- a/apps/mobile/src/features/review/reviewModel.ts +++ b/apps/mobile/src/features/review/reviewModel.ts @@ -535,13 +535,15 @@ export function buildReviewSectionItems(input: { }, ); + const repositorySectionId = (id: string) => + input.repositoryPath ? `${id}:${input.repositoryPath}` : id; const gitItems = input.gitSections .filter( (section): section is typeof section & { kind: ReviewSectionKind } => section.kind !== "commit", ) .map((section) => ({ - id: `git:${section.kind}${input.repositoryPath ? ":" + input.repositoryPath : ""}`, + id: repositorySectionId(`git:${section.kind}`), kind: section.kind, title: section.title, subtitle: gitSubtitle(section), @@ -556,7 +558,7 @@ export function buildReviewSectionItems(input: { input.loadingGitSections && !hasChangesItem ? [ { - id: CHANGES_SECTION_ID + (input.repositoryPath ? ":" + input.repositoryPath : ""), + id: repositorySectionId(CHANGES_SECTION_ID), kind: "branch-range", title: CHANGES_TITLE, subtitle: null, diff --git a/apps/mobile/src/features/review/useReviewSections.ts b/apps/mobile/src/features/review/useReviewSections.ts index 2c99b8631a1ce..9ea711d0b80f6 100644 --- a/apps/mobile/src/features/review/useReviewSections.ts +++ b/apps/mobile/src/features/review/useReviewSections.ts @@ -167,6 +167,7 @@ export function useReviewSections(input: { const selectRepository = (cwd: string) => { if (!repositories.some((repo) => repo.cwd === cwd)) return; + if (selectedRepository?.cwd === cwd) return; setRepositoryChoice({ threadKey: reviewCache.threadKey, cwd }); if (reviewCache.threadKey) setReviewSelectedSectionId(reviewCache.threadKey, null); }; From b9b72b985442d49e728b9bf54a30e449089bf941 Mon Sep 17 00:00:00 2001 From: Neil Mazumdar <84065477+NeilMazumdar@users.noreply.github.com> Date: Wed, 11 Nov 2026 10:59:31 +1030 Subject: [PATCH 10/14] test(server): give the submodule clone a Git identity before committing --- apps/server/src/vcs/GitVcsDriverCore.test.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts index 3493414fc2603..d00ca036c2b6e 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.test.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts @@ -2235,6 +2235,8 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { yield* git(cwd, ["submodule", "update", "--init", "--recursive"]); yield* git(cwd, ["submodule", "deinit", "-f", "--", "uninitialized"]); const child = NodePath.join(cwd, "packages/auth service"); + yield* git(child, ["config", "user.email", "test@test.com"]); + yield* git(child, ["config", "user.name", "Test"]); yield* writeTextFile(child, "committed.txt", "child commit\n"); yield* git(child, ["add", "committed.txt"]); yield* git(child, ["commit", "-m", "Child change"]); From 85434cea7ce3b33806fa4b7589ac4f17e573f174 Mon Sep 17 00:00:00 2001 From: Neil Mazumdar <84065477+NeilMazumdar@users.noreply.github.com> Date: Wed, 11 Nov 2026 11:24:50 +1030 Subject: [PATCH 11/14] fix(web,mobile): show a failed repository discovery instead of the parent diff --- .../src/features/review/useReviewSections.ts | 12 ++++++++++- .../src/components/DiffPanel.browser.test.tsx | 19 +++++++++++++++++ apps/web/src/components/DiffPanel.tsx | 21 +++++++++++++++---- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/apps/mobile/src/features/review/useReviewSections.ts b/apps/mobile/src/features/review/useReviewSections.ts index 9ea711d0b80f6..264b160f60566 100644 --- a/apps/mobile/src/features/review/useReviewSections.ts +++ b/apps/mobile/src/features/review/useReviewSections.ts @@ -73,7 +73,16 @@ export function useReviewSections(input: { (repo) => repositoryChoice?.threadKey === reviewCache.threadKey && repo.cwd === repositoryChoice.cwd, ) ?? repositories[0]; - const reviewCwd = selectedRepository?.path ? selectedRepository.cwd : selectedThreadCwd; + // A chosen child stays unresolved while discovery has no answer, so its review never shows the parent. + const repositoryUnresolved = + repositoryChoice?.threadKey === reviewCache.threadKey && + repositoryChoice.cwd !== selectedThreadCwd && + !repositoriesQuery.data; + const reviewCwd = repositoryUnresolved + ? null + : selectedRepository?.path + ? selectedRepository.cwd + : selectedThreadCwd; const repositoryPath = selectedRepository?.path ?? ""; const diffPreview = useEnvironmentQuery( canReadFiles && enabled && environmentId !== undefined && reviewCwd !== null @@ -179,6 +188,7 @@ export function useReviewSections(input: { reviewCwd: selectedSection?.kind === "turn" ? selectedThreadCwd : reviewCwd, repositoryPath: selectedSection?.kind === "turn" ? "" : repositoryPath, error: + (repositoryUnresolved && selectedSection?.kind !== "turn" ? repositoriesQuery.error : null) ?? diffPreview.error ?? activeTurnDiff.error ?? (selectedSection === null && !fileAccess.isPending && !canReadFiles diff --git a/apps/web/src/components/DiffPanel.browser.test.tsx b/apps/web/src/components/DiffPanel.browser.test.tsx index cd4a912ddd904..78589ad57712f 100644 --- a/apps/web/src/components/DiffPanel.browser.test.tsx +++ b/apps/web/src/components/DiffPanel.browser.test.tsx @@ -285,3 +285,22 @@ it("keeps the loading state while a saved repository is still being discovered", ); await expect.element(screen.getByRole("status", { name: "Loading changes..." })).toBeVisible(); }); + +it("shows a failed repository discovery instead of the workspace diff", async () => { + using context = setup(null); + state.repositories = Atom.make(AsyncResult.fail(new Error("discovery failed"))); + useDiffPanelStore.getState().selectRepository(context.threadRef, `${cwd}/packages/auth`); + await using screen = await render( + + + , + ); + await expect.element(screen.getByText("discovery failed", { exact: true })).toBeVisible(); + await userEvent.click(screen.getByRole("button", { name: "Review the thread's repository" })); + await expect + .element(screen.getByText("discovery failed", { exact: true })) + .not.toBeInTheDocument(); + expect( + useDiffPanelStore.getState().repositoryCwdByThreadKey[scopedThreadKey(context.threadRef)], + ).toBeUndefined(); +}); diff --git a/apps/web/src/components/DiffPanel.tsx b/apps/web/src/components/DiffPanel.tsx index cc49753e71869..e3ae3bb843978 100644 --- a/apps/web/src/components/DiffPanel.tsx +++ b/apps/web/src/components/DiffPanel.tsx @@ -295,9 +295,13 @@ export default function DiffPanel({ repositoryMissing && isRepositoryDiff ? { kind: "branch" as const, baseRef: null } : storedDiffSelection; - const repositoryPending = - !!requestedRepositoryCwd && repositoriesQuery.isPending && !repositoriesQuery.data; - const repositoryCwd = repositoryPending + // A saved child stays unresolved until discovery answers; the thread's repository needs no answer. + const repositoryUnresolved = + !!requestedRepositoryCwd && requestedRepositoryCwd !== workspaceCwd && !repositoriesQuery.data; + const repositoryPending = repositoryUnresolved && repositoriesQuery.isPending; + const repositoryError = + repositoryUnresolved && !repositoriesQuery.isPending ? repositoriesQuery.error : null; + const repositoryCwd = repositoryUnresolved ? undefined : selectedRepository?.path ? selectedRepository.cwd @@ -551,7 +555,7 @@ export default function DiffPanel({ ? toolCallDetail.error : selectedTurn ? activeCheckpointDiff.error - : branchDiffPreview.error; + : (repositoryError ?? branchDiffPreview.error); const hasResolvedPatch = typeof selectedPatch === "string"; const hasNoNetChanges = hasResolvedPatch && @@ -1452,6 +1456,15 @@ export default function DiffPanel({ {selectedPatchError && !renderablePatch && (

{selectedPatchError}

+ {isGitSelection && repositoryError && routeThreadRef && ( + + )}
)} {!renderablePatch && toolCallPreview && toolCallPreview.content.length > 0 ? ( From c33562fb2c7d34774a775bc15c691a904c24b6ce Mon Sep 17 00:00:00 2001 From: Neil Mazumdar <84065477+NeilMazumdar@users.noreply.github.com> Date: Wed, 11 Nov 2026 11:36:56 +1030 Subject: [PATCH 12/14] fix(web,mobile): retry a failed repository discovery without losing the selection --- .../src/features/review/useReviewSections.ts | 9 +++++++-- apps/web/src/components/DiffPanel.tsx | 19 ++++++++++++------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/apps/mobile/src/features/review/useReviewSections.ts b/apps/mobile/src/features/review/useReviewSections.ts index 264b160f60566..d85562d9bfd37 100644 --- a/apps/mobile/src/features/review/useReviewSections.ts +++ b/apps/mobile/src/features/review/useReviewSections.ts @@ -156,13 +156,18 @@ export function useReviewSections(input: { const selectedSection = reviewSections.find((section) => section.id === selectedSectionId) ?? null; + // With a child unresolved there are no Git sections, so a turn can be selected only as a fallback. + const repositoryFailureVisible = + repositoryUnresolved && + !(selectedSection?.kind === "turn" && reviewCache.selectedSectionId === selectedSection.id); + const refreshSelectedSection = async () => { if (!enabled) { return; } if (selectedSection?.kind === "turn") { activeTurnDiff.refresh(); - return; + if (!repositoryFailureVisible) return; } diffPreview.refresh(); repositoriesQuery.refresh(); @@ -188,7 +193,7 @@ export function useReviewSections(input: { reviewCwd: selectedSection?.kind === "turn" ? selectedThreadCwd : reviewCwd, repositoryPath: selectedSection?.kind === "turn" ? "" : repositoryPath, error: - (repositoryUnresolved && selectedSection?.kind !== "turn" ? repositoriesQuery.error : null) ?? + (repositoryFailureVisible ? repositoriesQuery.error : null) ?? diffPreview.error ?? activeTurnDiff.error ?? (selectedSection === null && !fileAccess.isPending && !canReadFiles diff --git a/apps/web/src/components/DiffPanel.tsx b/apps/web/src/components/DiffPanel.tsx index e3ae3bb843978..09bbb228b3955 100644 --- a/apps/web/src/components/DiffPanel.tsx +++ b/apps/web/src/components/DiffPanel.tsx @@ -1457,13 +1457,18 @@ export default function DiffPanel({

{selectedPatchError}

{isGitSelection && repositoryError && routeThreadRef && ( - +
+ + +
)}
)} From 5c1110dada225cd6d86a834599a352838fb01623 Mon Sep 17 00:00:00 2001 From: maria-rcks Date: Wed, 11 Nov 2026 11:46:24 +0000 Subject: [PATCH 13/14] perf(web): only discover review repositories while the Diff panel is visible --- apps/web/src/components/DiffPanel.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/web/src/components/DiffPanel.tsx b/apps/web/src/components/DiffPanel.tsx index e8f49d189f898..3ff5a73523ded 100644 --- a/apps/web/src/components/DiffPanel.tsx +++ b/apps/web/src/components/DiffPanel.tsx @@ -269,7 +269,7 @@ export default function DiffPanel({ routeThreadRef ? state.repositoryCwdByThreadKey[scopedThreadKey(routeThreadRef)] : undefined, ); const repositoriesQuery = useEnvironmentQuery( - canReadFiles && activeThread && workspaceCwd + visible && canReadFiles && activeThread && workspaceCwd ? reviewEnvironment.repositories({ environmentId: activeThread.environmentId, input: { cwd: workspaceCwd }, From a4237c6a2700a31303859302cc39e2fd45b8b9b5 Mon Sep 17 00:00:00 2001 From: maria-rcks Date: Wed, 11 Nov 2026 11:52:56 +0000 Subject: [PATCH 14/14] test(web): drop the removed keep-on-open argument after merging main --- apps/web/src/diffPanelStore.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/web/src/diffPanelStore.test.ts b/apps/web/src/diffPanelStore.test.ts index 2fc966b57fe9a..8675910ffe9ff 100644 --- a/apps/web/src/diffPanelStore.test.ts +++ b/apps/web/src/diffPanelStore.test.ts @@ -31,7 +31,7 @@ describe("diffPanelStore", () => { expect(useDiffPanelStore.getState().byThreadKey[key]).toEqual({ kind: "unstaged" }); store.selectBranchBaseRef(ref, "child-only-branch"); store.clearRepository(ref); - store.selectCommit(ref, "linked-parent-commit", true); + store.selectCommit(ref, "linked-parent-commit"); expect(useDiffPanelStore.getState().repositoryCwdByThreadKey[key]).toBeUndefined(); expect(useDiffPanelStore.getState().byThreadKey[key]).toEqual({ kind: "commit",