@devries-cameron20 / t3code Mirror · read-only / commits / 62a51e26a4
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 13:30 UTC

fix(web): more GitLab merge request links open their file, line or commit (#34662)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Antony committed Nov 14, 2026 at 22:13 UTC 62a51e26a4a8f25c8dfe854dca11b3628fea2101
4 files changed +153 -40
apps/mobile/src/features/pull-requests/PullRequestRouteScreen.tsx
+10 -1
@@ -3,6 +3,7 @@ import {
3 pullRequestAnchorMatchesUrl,
4 pullRequestDiffPathMatches,
5 pullRequestLinkCommit,
6 + resolvePullRequestAnchorRange,
7 resolvePullRequestReviewAnchor,
8 } from "@t3tools/shared/pullRequestAnchor";
9 import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator";
@@ -1072,7 +1073,15 @@ function PullRequestFileRouteContent({ route }: PullRequestFileRouteProps) {
1073 : !jumpPath || requestedFile
1074 ? data.files[0]
1075 : undefined);
1075 - const range = pickedPath === null && linkAnchor?.kind === "diff" ? linkAnchor.range : null;
1076 + const linkRange = pickedPath === null && linkAnchor?.kind === "diff" ? linkAnchor.range : null;
1077 + const range =
1078 + linkRange &&
1079 + resolvePullRequestAnchorRange(linkRange, (line) =>
1080 + data.rows.some(
1081 + (row) =>
1082 + row.fileId === targetFile?.id && row.change === "delete" && row.oldLineNumber === line,
1083 + ),
1084 + );
1085 const focus =
1086 route.params.focus ??
1087 (range
apps/web/src/components/pullRequest/PullRequestCodeTab.tsx
+19 -1
@@ -2,6 +2,7 @@ import {
2 parsePullRequestAnchor,
3 pullRequestDiffPathMatches,
4 pullRequestLinkCommit,
5 + resolvePullRequestAnchorRange,
6 } from "@t3tools/shared/pullRequestAnchor";
7 import {
8 capitalizeTerm,
@@ -349,6 +350,18 @@ export interface PullRequestAgentSelectionInput {
350 readonly request: string;
351 }
352
353 +function isRemovedLine(fileDiff: FileDiffMetadata, oldLine: number): boolean {
354 + return fileDiff.hunks.some((hunk) => {
355 + let next = hunk.deletionStart;
356 + return hunk.hunkContent.some((content) => {
357 + const count = content.type === "context" ? content.lines : content.deletions;
358 + const removed = content.type === "change" && oldLine >= next && oldLine < next + count;
359 + next += count;
360 + return removed;
361 + });
362 + });
363 +}
364 +
365 function PullRequestCodeTab({
366 scrollerRef,
367 environmentId,
@@ -1556,7 +1569,12 @@ function PullRequestCodeTab({
1569 answeredLink.current = key;
1570 setRevealedLink({ scope: scopeKey, path: pendingLinkPath });
1571 onRevealSettled?.(revealUrl);
1559 - const range = linkAnchor.range;
1572 + const range =
1573 + linkAnchor.range &&
1574 + resolvePullRequestAnchorRange(
1575 + linkAnchor.range,
1576 + (line) => linkFile !== undefined && isRemovedLine(linkFile, line),
1577 + );
1578 revealFile(
1579 pendingLinkPath,
1580 range
packages/shared/src/pullRequestAnchor.test.ts
+57 -22
@@ -4,9 +4,12 @@ import {
4 pullRequestAnchorMatchesUrl,
5 pullRequestDiffPathMatches,
6 pullRequestLinkCommit,
7 + resolvePullRequestAnchorRange,
8 resolvePullRequestReviewAnchor,
9 } from "./pullRequestAnchor.ts";
10
11 +const gitlab = "https://gitlab.com/group/repo/-/merge_requests/7";
12 +
13 const url = "https://github.com/owner/repo/pull/7";
14 it.each(["discussion_r123", "r123", "discussion-diff-123R5-L9", "discussion-diff-123"])(
15 "resolves review comment alias %s",
@@ -53,19 +56,22 @@ it.each([
56 "src/hello world.ts",
57 "bcea8deb9d67b305e28c4a0b0d021615",
58 "066802cf2cf056706e4e4951cb7dff2717a2ae32fd9797c1f1b41062d57db929",
59 + "f3f78c2026bd34f4ad36bf36e9c557dd9df528fb",
60 ],
61 [
62 "src/日本語.ts",
63 "5c66d5d565666e46924cf80d7bbfdab3",
64 "191b8ebf2241225db999eaf8b6ff44b07934d577f347d0ff684591eecad34944",
65 + "2ec8ce696c4c6df9419ad640fbc0d6ef4d79ebad",
66 ],
67 [
68 "old/path.ts",
69 "f5d455478d496859b654884654393a61",
70 "aa38d67014e2c59d51ea456a155e039f4e32f636a4c450714c3b517bb8bde7e7",
71 + "ca0d69af01140f25bafd6ab7aec204ce52a9bbb3",
72 ],
67 -])("matches GitHub path digests for %s", (path, legacy, current) => {
68 - for (const digest of [legacy, current]) {
73 +])("matches GitHub and GitLab path digests for %s", (path, legacy, current, gitLab) => {
74 + for (const digest of [legacy, current, gitLab, gitLab.slice(0, 9)]) {
75 expect(pullRequestDiffPathMatches(digest, path)).toBe(true);
76 expect(pullRequestDiffPathMatches(digest, "other.ts")).toBe(false);
77 }
@@ -77,6 +83,19 @@ it("handles encoded hashes and rejects invalid line numbers and URLs", () => {
83 });
84 expect(parsePullRequestAnchor(`${url}#diff-abcR0`)).toBeNull();
85 expect(parsePullRequestAnchor(`${url}#diff-abcR9999999999999999999`)).toBeNull();
86 + const sha = "c".repeat(40);
87 + for (const anchor of [
88 + `${sha}_0_0`,
89 + `${sha}_1_9999999999999999999`,
90 + `line_${sha.slice(0, 9)}_0`,
91 + ]) {
92 + expect(parsePullRequestAnchor(`${gitlab}/diffs#${anchor}`)).toMatchObject({ range: null });
93 + }
94 + expect(parsePullRequestAnchor(`${gitlab}/diffs#${sha}_0_5`)).toEqual({
95 + kind: "diff",
96 + digest: sha,
97 + range: { start: 5, side: "right", end: 5, endSide: "right" },
98 + });
99 expect(parsePullRequestAnchor("invalid")).toBeNull();
100 expect(parsePullRequestAnchor(url)).toBeNull();
101 expect(parsePullRequestAnchor(`${url}#unknown`)).toEqual({ kind: "summary", anchor: "unknown" });
@@ -89,9 +108,44 @@ it("keeps the commit scope of a PR diff permalink", () => {
108 const sha = "a".repeat(40);
109 expect(pullRequestLinkCommit(`${url}/commits/${sha}#diff-abcR5`)).toBe(sha);
110 expect(pullRequestLinkCommit(`${url}/files#diff-abcR5`)).toBeNull();
111 + expect(pullRequestLinkCommit(`${gitlab}/diffs?commit_id=${sha}#${"b".repeat(40)}`)).toBe(sha);
112 + expect(pullRequestLinkCommit(`${gitlab}/diffs?commit_id=main`)).toBeNull();
113 + expect(pullRequestLinkCommit(`${gitlab}?commit_id=${sha}`)).toBeNull();
114 +});
115 +
116 +it("reads GitLab file and line anchors, letting a removed old line win", () => {
117 + const digest = "c".repeat(40);
118 + const diff = (range: object | null, at = digest) => ({ kind: "diff", digest: at, range });
119 + const line = (start: number, side: "left" | "right") => ({
120 + start,
121 + side,
122 + end: start,
123 + endSide: side,
124 + });
125 + expect(parsePullRequestAnchor(`${gitlab}/diffs#${digest}`)).toEqual(diff(null));
126 + expect(parsePullRequestAnchor(`${gitlab}/diffs#diff-content-${digest}`)).toEqual(diff(null));
127 + expect(parsePullRequestAnchor(`${gitlab}/diffs?file=${digest}`)).toEqual(diff(null));
128 + expect(parsePullRequestAnchor(`${gitlab}/diffs?file=${digest}#note_5`)).toEqual({
129 + kind: "timeline",
130 + anchor: "note_5",
131 + });
132 + const code = parsePullRequestAnchor(`${gitlab}/diffs#${digest}_248_253`);
133 + expect(code).toEqual(diff({ ...line(253, "right"), oldLine: 248 }));
134 + const range = code?.kind === "diff" ? code.range : null;
135 + expect(resolvePullRequestAnchorRange(range!, () => false)).toBe(range);
136 + expect(resolvePullRequestAnchorRange(range!, (old) => old === 248)).toEqual(line(248, "left"));
137 + expect(parsePullRequestAnchor(`${gitlab}/diffs#${digest}_12_0`)).toEqual(diff(line(12, "left")));
138 + expect(parsePullRequestAnchor(`${gitlab}/diffs#line_${digest.slice(0, 9)}_A12`)).toEqual(
139 + diff(line(12, "right"), digest.slice(0, 9)),
140 + );
141 + expect(parsePullRequestAnchor(`${gitlab}/diffs#line_${digest.slice(0, 9)}_12`)).toEqual(
142 + diff(line(12, "left"), digest.slice(0, 9)),
143 + );
144 + expect(parsePullRequestAnchor(`${gitlab}#${digest}_248_253`)).toEqual(code);
145 + expect(parsePullRequestAnchor(`${gitlab}#${digest}`)).toEqual(diff(null));
146 + expect(parsePullRequestAnchor(`${gitlab}?file=${digest}`)).toBeNull();
147 });
148
94 -const gitlab = "https://gitlab.com/group/repo/-/merge_requests/7";
149 it.each([
150 [`${url}/files`, "code"],
151 [`${url}/files/`, "code"],
@@ -133,22 +187,3 @@ it("finds a review with no text of its own through the earliest comment it poste
187 );
188 expect(resolvePullRequestReviewAnchor("issuecomment-9", [], threads)).toBe("issuecomment-9");
189 });
136 -
137 -it("reads GitLab merge request file and line anchors", () => {
138 - const path = "internal/commands/issuable/view/issuable_view_test.go";
139 - const digest = "0cd3702f060909d3161d1095b8347d4d07671427";
140 - expect(pullRequestDiffPathMatches(digest, path)).toBe(true);
141 - expect(parsePullRequestAnchor(`${gitlab}/diffs#${digest}`)).toEqual({
142 - kind: "diff",
143 - digest,
144 - range: null,
145 - });
146 - expect(parsePullRequestAnchor(`${gitlab}/diffs#${digest}_248_253`)).toEqual({
147 - kind: "diff",
148 - digest,
149 - range: { start: 253, side: "right", end: 253, endSide: "right" },
150 - });
151 - expect(parsePullRequestAnchor(`${gitlab}/diffs#${digest}_12_0`)).toMatchObject({
152 - range: { start: 12, side: "left" },
153 - });
154 -});
packages/shared/src/pullRequestAnchor.ts
+67 -16
@@ -7,6 +7,8 @@ export interface PullRequestAnchorRange {
7 readonly side: "left" | "right";
8 readonly end: number;
9 readonly endSide: "left" | "right";
10 + /** A GitLab line code's old-side counter: the line it names when that old line was removed. */
11 + readonly oldLine?: number;
12 }
13
14 export type PullRequestAnchor =
@@ -57,6 +59,55 @@ function pullRequestPathTab(pathname: string): PullRequestAnchor | null {
59 }
60 }
61
62 +const isLine = (line: number) => Number.isSafeInteger(line) && line >= 1;
63 +
64 +function gitLabLine(
65 + start: number,
66 + side: "left" | "right",
67 + oldLine = 0,
68 +): PullRequestAnchorRange | null {
69 + if (!isLine(start)) return null;
70 + return { start, side, end: start, endSide: side, ...(isLine(oldLine) ? { oldLine } : {}) };
71 +}
72 +
73 +// GitLab names a file by its path's SHA-1 and a line by `<sha1>_<old>_<new>`, which carries both
74 +// sides' counters. Rapid Diffs writes `line_<sha1 prefix>_<old>` or `_A<new>`.
75 +function gitLabDiffAnchor(anchor: string, file: string | null): PullRequestAnchor | null {
76 + const rapid = /^line_([a-f0-9]{9})_(A?)(\d+)$/i.exec(anchor);
77 + if (rapid) {
78 + const line = Number(rapid[3]);
79 + return {
80 + kind: "diff",
81 + digest: (rapid[1] ?? "").toLowerCase(),
82 + range: gitLabLine(line, rapid[2] ? "right" : "left"),
83 + };
84 + }
85 + const code = /^(?:diff-content-)?([a-f0-9]{40})(?:_(\d+)_(\d+))?$/i.exec(anchor);
86 + // A comment anchor beside `?file=` names the comment.
87 + const digest =
88 + code?.[1] ?? (!anchor && file !== null && /^[a-f0-9]{40}$/i.test(file) ? file : null);
89 + if (digest === null) return null;
90 + const [oldLine, newLine] = [Number(code?.[2]), Number(code?.[3])];
91 + // A deleted file has no new side, so its new counter is 0.
92 + const range =
93 + code?.[2] === undefined
94 + ? null
95 + : newLine === 0
96 + ? gitLabLine(oldLine, "left")
97 + : gitLabLine(newLine, "right", oldLine);
98 + return { kind: "diff", digest: digest.toLowerCase(), range };
99 +}
100 +
101 +/** The range to select, once the diff shows whether a GitLab line code's old line was removed. */
102 +export function resolvePullRequestAnchorRange(
103 + range: PullRequestAnchorRange,
104 + isRemovedLine: (oldLine: number) => boolean,
105 +): PullRequestAnchorRange {
106 + return range.oldLine !== undefined && isRemovedLine(range.oldLine)
107 + ? (gitLabLine(range.oldLine, "left") ?? range)
108 + : range;
109 +}
110 +
111 export function parsePullRequestAnchor(url: string | null | undefined): PullRequestAnchor | null {
112 let parsed: URL;
113 try {
@@ -66,6 +117,13 @@ export function parsePullRequestAnchor(url: string | null | undefined): PullRequ
117 }
118 const fallback = pullRequestPathTab(parsed.pathname);
119 const anchor = pullRequestUrlHash(url);
120 + const gitLab = gitLabDiffAnchor(
121 + anchor,
122 + /\/-\/merge_requests\/\d+\/diffs\/?$/i.test(parsed.pathname)
123 + ? parsed.searchParams.get("file")
124 + : null,
125 + );
126 + if (gitLab) return gitLab;
127 if (!anchor) return fallback;
128 // GitHub line ranges may cross diff sides; file digests were MD5 before SHA-256.
129 const diff = /^diff-([a-f0-9]+)(?:([LR])(\d+)(?:-([LR])(\d+))?)?$/i.exec(anchor);
@@ -90,19 +148,6 @@ export function parsePullRequestAnchor(url: string | null | undefined): PullRequ
148 : null,
149 };
150 }
93 - // GitLab: SHA-1 of the path, then old and new line counters. A removed line still carries a
94 - // new counter, so the new side wins unless the file has none (a 0 for a deleted file).
95 - const gitlab = /^([a-f0-9]{40})(?:_(\d+)_(\d+))?$/i.exec(anchor);
96 - if (gitlab) {
97 - const digest = (gitlab[1] ?? "").toLowerCase();
98 - if (!gitlab[2]) return { kind: "diff", digest, range: null };
99 - const newLine = Number(gitlab[3]);
100 - const oldLine = Number(gitlab[2]);
101 - const side = newLine >= 1 ? "right" : "left";
102 - const line = side === "right" ? newLine : oldLine;
103 - if (!Number.isSafeInteger(line) || line < 1) return fallback;
104 - return { kind: "diff", digest, range: { start: line, side, end: line, endSide: side } };
105 - }
151 if (
152 /^(?:(?:issuecomment-|pullrequestreview-|discussion_r|r|commitcomment-|event-|note_|comment-)\d+|discussion-diff-\d+(?:[LR]\d+(?:-[LR]\d+)?)?)$/i.test(
153 anchor,
@@ -149,14 +194,20 @@ export function resolvePullRequestReviewAnchor(
194
195 export function pullRequestDiffPathMatches(digest: string, path: string): boolean {
196 const bytes = utf8ToBytes(path);
152 - const hash = digest.length === 32 ? md5 : digest.length === 40 ? sha1 : sha256;
153 - return digest === bytesToHex(hash(bytes));
197 + // GitLab uses SHA-1, abbreviated to 9 characters in Rapid Diffs line anchors.
198 + if (digest.length === 40 || digest.length === 9)
199 + return bytesToHex(sha1(bytes)).startsWith(digest);
200 + return digest === bytesToHex(digest.length === 32 ? md5(bytes) : sha256(bytes));
201 }
202
203 export function pullRequestLinkCommit(url: string | null | undefined): string | null {
204 if (!url) return null;
205 try {
159 - return /\/pull\/\d+\/commits\/([a-f0-9]{40})\/?$/i.exec(new URL(url).pathname)?.[1] ?? null;
206 + const { pathname, searchParams } = new URL(url);
207 + const commit = /\/-\/merge_requests\/\d+\/diffs\/?$/i.test(pathname)
208 + ? searchParams.get("commit_id")
209 + : /\/pull\/\d+\/commits\/([a-f0-9]{40})\/?$/i.exec(pathname)?.[1];
210 + return commit && /^[a-f0-9]{40}$/i.test(commit) ? commit : null;
211 } catch {
212 return null;
213 }