Skip to content

Commit a2685e6

Browse files
fix(web): SCP-style Git remotes no longer become terminal file links (#35404)
1 parent f4cf69f commit a2685e6

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

‎apps/web/src/terminal-links.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ describe("extractTerminalLinks", () => {
6464
expect(extractTerminalLinks(`see ${quote}${text}${quote}.`)).toEqual([
6565
{ kind: "path", text, start: 5, end: 5 + text.length },
6666
]);
67+
const atText = "src/my [email protected]:12:3";
68+
expect(extractTerminalLinks(`see ${quote}${atText}${quote}.`)).toEqual([
69+
{ kind: "path", text: atText, start: 5, end: 5 + atText.length },
70+
]);
6771
});
6872

6973
it.each([
@@ -127,6 +131,11 @@ describe("extractTerminalLinks", () => {
127131
"src/𐐀.ts:12",
128132
"src/app/[id]/page.tsx:3:10",
129133
"src/routes/(app)/+page.svelte:12:4",
134+
"src/routes/(app)/[email protected]:12:3",
135+
"src/[email protected]:12:3",
136+
String.raw`src\[email protected]:12:3`,
137+
"src/é[email protected]:12:3",
138+
"src/[email protected]:12:3",
130139
"node_modules/.pnpm/[email protected]/node_modules/react/index.js:12:5",
131140
"app/routes/posts.$id.tsx:5",
132141
])("keeps the complete relative path %s", (text) => {
@@ -198,6 +207,8 @@ describe("extractTerminalLinks", () => {
198207
" at main (file:///Users/jane/app/src/index.mjs:3:7)",
199208
"+redis://localhost:6379/0",
200209
"-redis://localhost:6379/0",
210+
"git clone [email protected]:acme/widgets.git",
211+
"remote: [email protected]:group/sub/project.git",
201212
])("does not turn part of a non-web URL into an editor link: %s", (line) => {
202213
expect(extractTerminalLinks(line)).toEqual([]);
203214
});

‎apps/web/src/terminal-links.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ const REQUEST_ROUTE_PATTERN =
4646
// The tail of `redis://host/0` or `file:///a.mjs` is not a path. One-letter schemes stay drives,
4747
// a diff's `+` or `-` may lead, and the lookbehind keeps runs from being rescanned at every suffix.
4848
const SCHEME_URL_PATTERN = /(?<![A-Za-z\d+.-])[+-]?[A-Za-z][A-Za-z\d+.-]+:\/\/[^\s"'`<>]+/gu;
49+
// Nor is the tail of an SCP-style Git remote such as `[email protected]:acme/widgets.git`. Only a
50+
// path wholly inside the tail is dropped, so `src/[email protected]:12:3` keeps its link.
51+
const SCP_REMOTE_PATTERN = /(?<![\w.@-])[\w.-]+@[\w.-]+:[^\s"'`<>]+/gu;
4952

5053
function trimClosingDelimiters(value: string, kind: TerminalLinkKind): string {
5154
let output = value.replace(
@@ -119,6 +122,10 @@ export function extractTerminalLinks(line: string): TerminalLinkMatch[] {
119122
[...line.matchAll(REQUEST_ROUTE_PATTERN), ...line.matchAll(SCHEME_URL_PATTERN)],
120123
(match) => ({ start: match.index, end: match.index + match[0].length }),
121124
);
125+
const remoteTails = Array.from(line.matchAll(SCP_REMOTE_PATTERN), (match) => ({
126+
start: match.index + match[0].indexOf(":") + 1,
127+
end: match.index + match[0].length,
128+
}));
122129
const urlMatches = collectMatches(line, "url", URL_PATTERN, promptMatches);
123130
const quotedMatches = collectMatches(line, "path", QUOTED_FILE_PATH_PATTERN, [
124131
...promptMatches,
@@ -131,7 +138,10 @@ export function extractTerminalLinks(line: string): TerminalLinkMatch[] {
131138
]);
132139
return [...promptMatches, ...urlMatches, ...quotedMatches, ...pathMatches]
133140
.filter(
134-
(match) => match.kind === "url" || !nonPathRanges.some((range) => overlaps(match, range)),
141+
(match) =>
142+
match.kind === "url" ||
143+
(!nonPathRanges.some((range) => overlaps(match, range)) &&
144+
!remoteTails.some((tail) => match.start >= tail.start && match.end <= tail.end)),
135145
)
136146
.toSorted((a, b) => a.start - b.start);
137147
}

0 commit comments

Comments
 (0)