The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { isWindowsDrivePath } from "./path.ts"; |
| 2 | |
| 3 | export type CloneProtocol = "ssh" | "https"; |
| 4 | |
| 5 | const SCP_LIKE_URL = /^(?:[^@/\s]+@)?(\[[^\]@/\s]+\]|[^:/\s[\]]+):(?!\/)(.+)$/; |
| 6 | // Userinfo may contain `@`; the last one before the first slash separates the host. |
| 7 | const SSH_URL = /^ssh:\/\/(?:[^/\s]*@)?(\[[^\]@/\s]+\]|[^@:/\s]+)(:\d+)?\/(.+)$/; |
| 8 | const HTTPS_URL = /^https?:\/\/(?:[^/\s]*@)?(\[[^\]@/\s]+\]|[^@:/\s]+)(:\d+)?\/(.+)$/; |
| 9 | |
| 10 | function parseCloneUrl(url: string): { |
| 11 | protocol: CloneProtocol; |
| 12 | host: string; |
| 13 | path: string; |
| 14 | customPort: boolean; |
| 15 | scp: boolean; |
| 16 | } | null { |
| 17 | const trimmed = url.trim(); |
| 18 | const ssh = SSH_URL.exec(trimmed); |
| 19 | if (ssh) |
| 20 | return { protocol: "ssh", host: ssh[1]!, path: ssh[3]!, customPort: !!ssh[2], scp: false }; |
| 21 | const https = HTTPS_URL.exec(trimmed); |
| 22 | if (https) |
| 23 | return { |
| 24 | protocol: "https", |
| 25 | host: https[1]!, |
| 26 | path: https[3]!, |
| 27 | customPort: !!https[2], |
| 28 | scp: false, |
| 29 | }; |
| 30 | const scp = SCP_LIKE_URL.exec(trimmed); |
| 31 | if (scp) return { protocol: "ssh", host: scp[1]!, path: scp[2]!, customPort: false, scp: true }; |
| 32 | return null; |
| 33 | } |
| 34 | |
| 35 | export function cloneUrlProtocol(url: string): CloneProtocol | null { |
| 36 | return parseCloneUrl(url)?.protocol ?? null; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Rewrites a clone URL to the other protocol, because the target machine may |
| 41 | * only have SSH keys or only an HTTPS credential helper. Returns null when the |
| 42 | * URL cannot be rewritten safely, such as a custom port. |
| 43 | */ |
| 44 | export function cloneUrlForProtocol(url: string, protocol: CloneProtocol): string | null { |
| 45 | const parsed = parseCloneUrl(url); |
| 46 | if (!parsed) return null; |
| 47 | if (parsed.protocol === protocol) return url.trim(); |
| 48 | if (parsed.customPort) return null; |
| 49 | // Azure's transports use different hosts and put the repository marker in |
| 50 | // different positions; changing only the scheme produces an invalid clone URL. |
| 51 | if (parsed.protocol === "ssh" && parsed.host.toLowerCase() === "ssh.dev.azure.com") { |
| 52 | const azure = /^v3\/([^/?#]+)\/([^/?#]+)\/([^/?#]+)$/.exec(parsed.path); |
| 53 | return azure ? `https://dev.azure.com/${azure[1]}/${azure[2]}/_git/${azure[3]}` : null; |
| 54 | } |
| 55 | if (parsed.protocol === "https" && parsed.host.toLowerCase() === "dev.azure.com") { |
| 56 | const azure = /^([^/?#]+)\/([^/?#]+)\/_git\/([^/?#]+)$/.exec(parsed.path); |
| 57 | return azure ? `git@ssh.dev.azure.com:v3/${azure[1]}/${azure[2]}/${azure[3]}` : null; |
| 58 | } |
| 59 | // Bitbucket Cloud serves SSH only from `ssh.bitbucket.org` and HTTPS only from `bitbucket.org`. |
| 60 | const host = /^(?:(?:alt)?ssh\.)?bitbucket\.org$/i.test(parsed.host) |
| 61 | ? protocol === "ssh" |
| 62 | ? "ssh.bitbucket.org" |
| 63 | : "bitbucket.org" |
| 64 | : parsed.host; |
| 65 | // SCP paths are literal; URI paths use percent escapes. |
| 66 | try { |
| 67 | if (protocol === "ssh") return `git@${host}:${decodeURIComponent(parsed.path)}`; |
| 68 | const path = parsed.scp |
| 69 | ? parsed.path.split("/").map(encodeURIComponent).join("/") |
| 70 | : parsed.path; |
| 71 | return `https://${host}/${path}`; |
| 72 | } catch { |
| 73 | return null; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | const UNSAFE_SEGMENT = /^\.{1,2}$|[\\:*?"<>|\p{Cc}]|[ .]$/u; |
| 78 | const WINDOWS_DEVICE_NAME = |
| 79 | /^(?:con|prn|aux|nul|conin\$|conout\$|com[0-9¹²³]|lpt[0-9¹²³])(?: *\.| *$)/i; |
| 80 | |
| 81 | /** |
| 82 | * The `<host>/<owner>/<repo>` folders ghq clones a URL into, or null for local paths and |
| 83 | * anything with a folder that could leave the root. |
| 84 | */ |
| 85 | export function cloneUrlRepositoryPath(url: string): string | null { |
| 86 | if (isWindowsDrivePath(url.trim())) return null; |
| 87 | const parsed = parseCloneUrl(url); |
| 88 | if (!parsed) return null; |
| 89 | let repositoryPath = parsed.path.split(/[?#]/)[0]!; |
| 90 | try { |
| 91 | if (!parsed.scp) repositoryPath = decodeURIComponent(repositoryPath); |
| 92 | } catch { |
| 93 | return null; |
| 94 | } |
| 95 | const segments = [parsed.host, ...repositoryPath.split("/").filter(Boolean)]; |
| 96 | const last = segments.at(-1)!; |
| 97 | if (last.endsWith(".git")) segments[segments.length - 1] = last.slice(0, -".git".length); |
| 98 | if ( |
| 99 | segments.length < 2 || |
| 100 | segments.some((s) => s === "" || UNSAFE_SEGMENT.test(s) || WINDOWS_DEVICE_NAME.test(s)) |
| 101 | ) |
| 102 | return null; |
| 103 | return segments.join("/"); |
| 104 | } |