Open History to see the last commit
1import { isWindowsDrivePath } from "./path.ts";
2
3export type CloneProtocol = "ssh" | "https";
4
5const SCP_LIKE_URL = /^(?:[^@/\s]+@)?(\[[^\]@/\s]+\]|[^:/\s[\]]+):(?!\/)(.+)$/;
6// Userinfo may contain `@`; the last one before the first slash separates the host.
7const SSH_URL = /^ssh:\/\/(?:[^/\s]*@)?(\[[^\]@/\s]+\]|[^@:/\s]+)(:\d+)?\/(.+)$/;
8const HTTPS_URL = /^https?:\/\/(?:[^/\s]*@)?(\[[^\]@/\s]+\]|[^@:/\s]+)(:\d+)?\/(.+)$/;
9
10function parseCloneUrl(
11 url: string,
12): { protocol: CloneProtocol; host: string; path: string; customPort: boolean } | null {
13 const trimmed = url.trim();
14 const ssh = SSH_URL.exec(trimmed);
15 if (ssh) return { protocol: "ssh", host: ssh[1]!, path: ssh[3]!, customPort: !!ssh[2] };
16 const https = HTTPS_URL.exec(trimmed);
17 if (https) return { protocol: "https", host: https[1]!, path: https[3]!, customPort: !!https[2] };
18 const scp = SCP_LIKE_URL.exec(trimmed);
19 if (scp) return { protocol: "ssh", host: scp[1]!, path: scp[2]!, customPort: false };
20 return null;
21}
22
23export function cloneUrlProtocol(url: string): CloneProtocol | null {
24 return parseCloneUrl(url)?.protocol ?? null;
25}
26
27/**
28 * Rewrites a clone URL to the other protocol, because the target machine may
29 * only have SSH keys or only an HTTPS credential helper. Returns null when the
30 * URL cannot be rewritten safely, such as a custom port.
31 */
32export function cloneUrlForProtocol(url: string, protocol: CloneProtocol): string | null {
33 const parsed = parseCloneUrl(url);
34 if (!parsed) return null;
35 if (parsed.protocol === protocol) return url.trim();
36 if (parsed.customPort) return null;
37 // Azure's transports use different hosts and put the repository marker in
38 // different positions; changing only the scheme produces an invalid clone URL.
39 if (parsed.protocol === "ssh" && parsed.host.toLowerCase() === "ssh.dev.azure.com") {
40 const azure = /^v3\/([^/?#]+)\/([^/?#]+)\/([^/?#]+)$/.exec(parsed.path);
41 return azure ? `https://dev.azure.com/${azure[1]}/${azure[2]}/_git/${azure[3]}` : null;
42 }
43 if (parsed.protocol === "https" && parsed.host.toLowerCase() === "dev.azure.com") {
44 const azure = /^([^/?#]+)\/([^/?#]+)\/_git\/([^/?#]+)$/.exec(parsed.path);
45 return azure ? `git@ssh.dev.azure.com:v3/${azure[1]}/${azure[2]}/${azure[3]}` : null;
46 }
47 return protocol === "ssh"
48 ? `git@${parsed.host}:${parsed.path}`
49 : `https://${parsed.host}/${parsed.path}`;
50}
51
52const UNSAFE_SEGMENT = /^\.{1,2}$|[\\:*?"<>|\p{Cc}]|[ .]$/u;
53const WINDOWS_DEVICE_NAME =
54 /^(?:con|prn|aux|nul|conin\$|conout\$|com[0-9¹²³]|lpt[0-9¹²³])(?: *\.| *$)/i;
55
56/**
57 * The `<host>/<owner>/<repo>` folders ghq clones a URL into, or null for local paths and
58 * anything with a folder that could leave the root.
59 */
60export function cloneUrlRepositoryPath(url: string): string | null {
61 if (isWindowsDrivePath(url.trim())) return null;
62 const parsed = parseCloneUrl(url);
63 if (!parsed) return null;
64 const segments = [parsed.host, ...parsed.path.split(/[?#]/)[0]!.split("/").filter(Boolean)];
65 const last = segments.at(-1)!;
66 if (last.endsWith(".git")) segments[segments.length - 1] = last.slice(0, -".git".length);
67 if (
68 segments.length < 2 ||
69 segments.some((s) => s === "" || UNSAFE_SEGMENT.test(s) || WINDOWS_DEVICE_NAME.test(s))
70 )
71 return null;
72 return segments.join("/");
73}