@devries-cameron20 / t3code-1 / commits / 1b8ac6d77e
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(gitlab): large merge request files show counts and load their diff (#37898)

Julius Marminge committed Nov 16, 2026 at 21:09 UTC 1b8ac6d77ee35ab8ce27d875352805db01bf791f
5 files changed +201 -16
packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.ts
+84
@@ -1129,6 +1129,90 @@ layer("GitLabPullRequestCli.layer", (it) => {
1129 }),
1130 );
1131
1132 + it.effect("counts a file GitLab withheld as too large and offers to read it alone", () =>
1133 + Effect.gen(function* () {
1134 + mockedExecute
1135 + .mockReturnValueOnce(
1136 + Effect.succeed(
1137 + output(
1138 + JSON.stringify([
1139 + { old_path: "src/a.ts", new_path: "src/a.ts", diff: "@@ -1 +1 @@\n-a\n+b\n" },
1140 + { old_path: "src/huge.txt", new_path: "src/huge.txt", diff: "", too_large: true },
1141 + ]),
1142 + ),
1143 + ),
1144 + )
1145 + .mockReturnValueOnce(
1146 + Effect.succeed(
1147 + output(
1148 + JSON.stringify({
1149 + data: {
1150 + project: {
1151 + mergeRequest: {
1152 + diffStatsSummary: { fileCount: 2 },
1153 + diffStats: [
1154 + { path: "src/a.ts", additions: 1, deletions: 1 },
1155 + { path: "src/huge.txt", additions: 20000, deletions: 0 },
1156 + ],
1157 + },
1158 + },
1159 + },
1160 + }),
1161 + ),
1162 + ),
1163 + );
1164 + const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1165 +
1166 + const diff = yield* cli.getMergeRequestDiff({ cwd: "/w", repository: "acme/web", number: 7 });
1167 +
1168 + expect(diff.omittedFileStats).toEqual([
1169 + { path: "src/huge.txt", additions: 20000, deletions: 0 },
1170 + ]);
1171 + assert.isTrue(diff.withheldFilesReadable);
1172 + }),
1173 + );
1174 +
1175 + it.effect("reads one withheld file's section from the merge request's raw diff", () =>
1176 + Effect.gen(function* () {
1177 + mockedExecute.mockReturnValueOnce(
1178 + Effect.succeed(
1179 + output(
1180 + [
1181 + "diff --git a/src/a.ts b/src/a.ts",
1182 + "--- a/src/a.ts",
1183 + "+++ b/src/a.ts",
1184 + "@@ -1 +1 @@",
1185 + "-a",
1186 + "+b",
1187 + "diff --git a/src/huge.txt b/src/huge.txt",
1188 + "new file mode 100644",
1189 + "--- /dev/null",
1190 + "+++ b/src/huge.txt",
1191 + "@@ -0,0 +1,2 @@",
1192 + "+one",
1193 + "+two",
1194 + "",
1195 + ].join("\n"),
1196 + ),
1197 + ),
1198 + );
1199 + const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1200 +
1201 + const diff = yield* cli.getMergeRequestDiff({
1202 + cwd: "/w",
1203 + repository: "acme/web",
1204 + number: 7,
1205 + path: "src/huge.txt",
1206 + });
1207 +
1208 + expect(argsOfCall(0)[1]).toBe("projects/acme%2Fweb/merge_requests/7/raw_diffs");
1209 + assert.strictEqual(
1210 + diff.patch,
1211 + "diff --git a/src/huge.txt b/src/huge.txt\nnew file mode 100644\n--- /dev/null\n+++ b/src/huge.txt\n@@ -0,0 +1,2 @@\n+one\n+two\n",
1212 + );
1213 + }),
1214 + );
1215 +
1216 it.effect(
1217 "pages a merge request diff 30 files at a time, which GitLab 15.7 to 17.10 accept",
1218 () =>
packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts
+108 -14
@@ -1,5 +1,9 @@
1 import { removeAgentCredits } from "@t3tools/source-control-core/server/mergeMessage";
2 import { isCommitSha } from "@t3tools/source-control-core/server/pullRequestUtils";
3 +import {
4 + diffFileSection,
5 + truncateLongDiffLines,
6 +} from "@t3tools/source-control-core/server/diffFileRevisions";
7 import * as Context from "effect/Context";
8 import * as Digest from "effect/crypto/Digest";
9 import * as DateTime from "effect/DateTime";
@@ -25,6 +29,7 @@ import type {
29 PullRequestListState,
30 PullRequestMergeCapabilities,
31 PullRequestMergeMethod,
32 + PullRequestOmittedFileStat,
33 PullRequestReaction,
34 PullRequestReactionContent,
35 PullRequestReviewCommentDraft,
@@ -344,6 +349,9 @@ const diffPageSize = (commit: string | undefined) =>
349 const CONVERSATION_PAGES = 10;
350 const DIFF_MAX_OUTPUT_BYTES = 8 * 1024 * 1024;
351 const DIFF_TIMEOUT_MS = 60_000;
352 +const WITHHELD_FILE_DIFF_MAX_OUTPUT_BYTES = 32 * 1024 * 1024;
353 +// Withheld files are often generated or minified, where one line can be megabytes.
354 +const WITHHELD_FILE_MAX_LINE_LENGTH = 1_000;
355 const isVcsProcessExitError = Schema.is(VcsProcessExitError);
356 const DIFF_FILE_MAX_OUTPUT_BYTES = 1024 * 1024;
357 // REST list rows carry full descriptions, so a page can pass the 1 MB default output cap.
@@ -370,6 +378,8 @@ export interface GitLabMergeRequestDiffSlice {
378 readonly patch: string;
379 readonly truncated: boolean;
380 readonly nextCursor: string | null;
381 + readonly omittedFileStats?: ReadonlyArray<PullRequestOmittedFileStat>;
382 + readonly withheldFilesReadable?: boolean;
383 }
384
385 export class GitLabPullRequestCli extends Context.Service<
@@ -482,6 +492,8 @@ export class GitLabPullRequestCli extends Context.Service<
492 readonly number: number;
493 readonly cursor?: string | undefined;
494 readonly commit?: string | undefined;
495 + /** Reads one file a page withheld as too large; ignores `cursor`. */
496 + readonly path?: string | undefined;
497 }) => Effect.Effect<GitLabMergeRequestDiffSlice, GitLabPullRequestCliError>;
498
499 readonly getMergeRequestDiffFileContents: (input: {
@@ -1174,7 +1186,13 @@ export const make = Effect.gen(function* () {
1186 readonly number: number;
1187 readonly page: number;
1188 readonly commit?: string | undefined;
1177 - }): Effect.Effect<GitLabMergeRequestDiffSlice, GitLabPullRequestCliError> =>
1189 + }): Effect.Effect<
1190 + GitLabMergeRequestDiffSlice & {
1191 + readonly withheldPaths: ReadonlyArray<string>;
1192 + readonly legacy: boolean;
1193 + },
1194 + GitLabPullRequestCliError
1195 + > =>
1196 api({
1197 cwd: input.cwd,
1198 host: input.host,
@@ -1246,6 +1264,8 @@ export const make = Effect.gen(function* () {
1264 patch: patch.length === 0 ? patch : patch.replace(/\n?$/, "\n"),
1265 truncated: decoded.success.truncated,
1266 nextCursor: morePages ? String(input.page + 1) : null,
1267 + withheldPaths: decoded.success.withheldPaths,
1268 + legacy,
1269 });
1270 }),
1271 );
@@ -1847,6 +1867,67 @@ export const make = Effect.gen(function* () {
1867 ): Effect.Effect<A, GitLabPullRequestCliError> =>
1868 read.pipe(Effect.catchIf(isOptionalReadFailure, () => Effect.succeed(fallback)));
1869
1870 + const mergeRequestFileStats = (input: {
1871 + readonly cwd: string;
1872 + readonly host?: string | undefined;
1873 + readonly repository: string;
1874 + readonly number: number;
1875 + }) =>
1876 + graphqlRead({
1877 + cwd: input.cwd,
1878 + host: input.host,
1879 + operation: "getMergeRequestFileStats",
1880 + query: MERGE_REQUEST_FILE_STATS_GRAPHQL_QUERY,
1881 + variables: { fullPath: input.repository, iid: String(input.number) },
1882 + decode: decodeMergeRequestFileStatsJson,
1883 + });
1884 +
1885 + // GitLab has no per-file diff, so a withheld file is cut from the whole raw diff here and
1886 + // only its section reaches the client. `/raw_diffs` needs GitLab 17.9 or newer.
1887 + const withheldFileDiff = (input: {
1888 + readonly cwd: string;
1889 + readonly host?: string | undefined;
1890 + readonly repository: string;
1891 + readonly number: number;
1892 + readonly commit?: string | undefined;
1893 + readonly path: string;
1894 + }): Effect.Effect<GitLabMergeRequestDiffSlice, GitLabPullRequestCliError> => {
1895 + const readError = (message: string) =>
1896 + new GitLabMergeRequestReadError({
1897 + command: "glab",
1898 + cwd: input.cwd,
1899 + operation: "getMergeRequestDiff",
1900 + cause: new Error(message),
1901 + });
1902 + if (input.commit !== undefined) {
1903 + return Effect.fail(readError("A commit's withheld files cannot be read one at a time."));
1904 + }
1905 + return api({
1906 + cwd: input.cwd,
1907 + host: input.host,
1908 + path: `projects/${projectPath(input.repository)}/merge_requests/${input.number}/raw_diffs`,
1909 + maxOutputBytes: WITHHELD_FILE_DIFF_MAX_OUTPUT_BYTES,
1910 + timeoutMs: DIFF_TIMEOUT_MS,
1911 + }).pipe(
1912 + Effect.flatMap((result) => {
1913 + const section = result.stdoutTruncated ? null : diffFileSection(result.stdout, input.path);
1914 + return section === null
1915 + ? Effect.fail(
1916 + readError(
1917 + result.stdoutTruncated
1918 + ? `The diff for ${input.path} is too large to load.`
1919 + : `GitLab's diff does not include ${input.path}.`,
1920 + ),
1921 + )
1922 + : Effect.succeed({
1923 + patch: truncateLongDiffLines(section, WITHHELD_FILE_MAX_LINE_LENGTH),
1924 + truncated: false,
1925 + nextCursor: null,
1926 + });
1927 + }),
1928 + );
1929 + };
1930 +
1931 const graphqlMutation = (input: {
1932 readonly cwd: string;
1933 readonly host?: string | undefined;
@@ -2305,15 +2386,8 @@ export const make = Effect.gen(function* () {
2386
2387 listMergeRequestStats,
2388
2308 - getMergeRequestFileStats: Effect.fn("GitLabPullRequestCli.getMergeRequestFileStats")((input) =>
2309 - graphqlRead({
2310 - cwd: input.cwd,
2311 - host: input.host,
2312 - operation: "getMergeRequestFileStats",
2313 - query: MERGE_REQUEST_FILE_STATS_GRAPHQL_QUERY,
2314 - variables: { fullPath: input.repository, iid: String(input.number) },
2315 - decode: decodeMergeRequestFileStatsJson,
2316 - }),
2389 + getMergeRequestFileStats: Effect.fn("GitLabPullRequestCli.getMergeRequestFileStats")(
2390 + mergeRequestFileStats,
2391 ),
2392
2393 getMergeRequestWatchFingerprint: Effect.fn(
@@ -2426,13 +2500,33 @@ export const make = Effect.gen(function* () {
2500 number: input.number,
2501 ...(input.commit === undefined ? {} : { commit: input.commit }),
2502 };
2429 - if (input.cursor === undefined) {
2430 - return yield* diffPage({ ...target, page: 1 });
2503 + if (input.path !== undefined) {
2504 + return yield* withheldFileDiff({ ...target, path: input.path });
2505 }
2432 - const page = diffCursorPage(input.cursor);
2506 + const page = input.cursor === undefined ? 1 : diffCursorPage(input.cursor);
2507 if (page === null)
2508 return yield* new GitLabDiffCursorError({ command: "glab", cwd: input.cwd });
2435 - return yield* diffPage({ ...target, page });
2509 + const { withheldPaths, legacy, ...slice } = yield* diffPage({ ...target, page });
2510 + // A commit's diff has no per-file counts or raw diff to read a withheld file from.
2511 + if (input.commit !== undefined) return slice;
2512 + const readable = page === 1 && !legacy ? { withheldFilesReadable: true } : {};
2513 + if (withheldPaths.length === 0) return { ...slice, ...readable };
2514 + const stats = yield* orWhenUnanswered(mergeRequestFileStats(target), {
2515 + files: [],
2516 + truncated: true,
2517 + });
2518 + const statsByPath = new Map(stats.files.map((file) => [file.path, file]));
2519 + const omittedFileStats = withheldPaths.flatMap((path) => {
2520 + const stat = statsByPath.get(path);
2521 + return stat === undefined
2522 + ? []
2523 + : [{ path, additions: stat.additions, deletions: stat.deletions }];
2524 + });
2525 + return {
2526 + ...slice,
2527 + ...readable,
2528 + ...(omittedFileStats.length === 0 ? {} : { omittedFileStats }),
2529 + };
2530 }),
2531
2532 getMergeRequestDiffFileContents: Effect.fn(
packages/source-control-gitlab/src/server/GitLabPullRequestProvider.ts
+1
@@ -47,6 +47,7 @@ const CAPABILITIES: PullRequestCapabilities = {
47 reviewers: { request: true, listCandidates: true, searchCandidates: true },
48 labels: true,
49 edit: { changeRequest: true, comment: true, maintainerEdits: true },
50 + withheldFileDiff: true,
51 };
52
53 const UNAVAILABLE_REQUIREMENT = {
packages/source-control-gitlab/src/server/gitLabMergeRequestJson.test.ts
+1
@@ -681,6 +681,7 @@ describe("decodeMergeRequestDiffsJson", () => {
681
682 expect(result.truncated).toBe(true);
683 expect(result.patch).toContain("diff --git a/big.bin b/big.bin");
684 + expect(result.withheldPaths).toEqual(["big.bin"]);
685 });
686
687 it("reports how many files GitLab returned, so the caller can page", () => {
packages/source-control-gitlab/src/server/gitLabMergeRequestJson.ts
+7 -2
@@ -1014,6 +1014,8 @@ export interface GitLabMergeRequestPatch {
1014 readonly patch: string;
1015 readonly truncated: boolean;
1016 readonly rawCount: number;
1017 + /** Files GitLab sent without their diff because they are too large or collapsed. */
1018 + readonly withheldPaths: ReadonlyArray<string>;
1019 }
1020
1021 export function decodeMergeRequestDiffsJson(
@@ -1044,6 +1046,7 @@ export function decodeMergeRequestChangesJson(
1046
1047 function mergeRequestPatch(entries: ReadonlyArray<unknown>): GitLabMergeRequestPatch {
1048 const sections: string[] = [];
1049 + const withheldPaths: string[] = [];
1050 let truncated = false;
1051 for (const entry of entries) {
1052 const file = decodeDiffEntry(entry);
@@ -1053,8 +1056,9 @@ function mergeRequestPatch(entries: ReadonlyArray<unknown>): GitLabMergeRequestP
1056 }
1057 const value = file.value;
1058 const hunks = value.diff ?? "";
1056 - if (hunks.length === 0) {
1057 - truncated = truncated || value.too_large === true || value.collapsed === true;
1059 + if (hunks.length === 0 && (value.too_large === true || value.collapsed === true)) {
1060 + truncated = true;
1061 + withheldPaths.push(value.new_path);
1062 }
1063 const { from, to } = diffHeaderPaths(value);
1064 const header = [
@@ -1078,6 +1082,7 @@ function mergeRequestPatch(entries: ReadonlyArray<unknown>): GitLabMergeRequestP
1082 patch: sections.join("\n"),
1083 truncated,
1084 rawCount: entries.length,
1085 + withheldPaths,
1086 };
1087 }
1088