Skip to content
Source
Sign inSign up

sourceControl.ts

18 lines765 Bytes

1const SCP_SSH_REMOTE_PATTERN = /^[a-zA-Z0-9._-]+@([^:/]+):/;
2// Git reads `host:path` as SSH too, leaving the user to ssh config. A single letter is a Windows
3// drive and `::` names a remote helper.
4const USERLESS_SCP_SSH_REMOTE_PATTERN = /^([^@:/\\\s]{2,}):(?!:)\/?[^/\s]+(?:\/[^/\s]+)+\/?$/u;
5​
6/** Null unless the remote is `host:path`, with no user. */
7export function userlessScpRemoteHost(remoteUrl: string): string | null {
8 return USERLESS_SCP_SSH_REMOTE_PATTERN.exec(remoteUrl.trim())?.[1] ?? null;
9}
10​
11export function isSshRemoteUrl(remoteUrl: string): boolean {
12 const trimmed = remoteUrl.trim();
13 return (
14 SCP_SSH_REMOTE_PATTERN.test(trimmed) ||
15 trimmed.toLowerCase().startsWith("ssh://") ||
16 userlessScpRemoteHost(trimmed) !== null
17 );