Open History to see the last commit
1import { isSshRemoteUrl, rewriteGitRemoteUrlHost } from "@t3tools/shared/sourceControl";
2import { resolveSshTarget } from "@t3tools/ssh/command";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Path from "effect/Path";
6import { ChildProcessSpawner } from "effect/unstable/process";
7
8const SCP_SSH_HOST_PATTERN = /^[a-zA-Z0-9._-]+@([^:/]+):/;
9
10const KNOWN_PUBLIC_SSH_HOSTS = new Set([
11 "github.com",
12 "gitlab.com",
13 "bitbucket.org",
14 "dev.azure.com",
15]);
16
17export type ResolveGitRemoteServices =
18 | ChildProcessSpawner.ChildProcessSpawner
19 | FileSystem.FileSystem
20 | Path.Path;
21
22export type ResolveSshHostname<R = never> = (
23 alias: string,
24) => Effect.Effect<string | null, never, R>;
25
26function parseSshRemoteHostname(remoteUrl: string): string | null {
27 const trimmed = remoteUrl.trim();
28 if (trimmed.length === 0 || !isSshRemoteUrl(trimmed)) {
29 return null;
30 }
31
32 const scpMatch = SCP_SSH_HOST_PATTERN.exec(trimmed);
33 if (scpMatch?.[1]) {
34 return scpMatch[1].toLowerCase();
35 }
36
37 if (trimmed.toLowerCase().startsWith("ssh://")) {
38 try {
39 const hostname = new URL(trimmed).hostname.toLowerCase();
40 return hostname.length > 0 ? hostname : null;
41 } catch {
42 return null;
43 }
44 }
45
46 return null;
47}
48
49/**
50 * SSH aliases such as `github-personal` have no dot and are not public hosts.
51 * Those are the only remotes worth asking `ssh -G` about.
52 */
53export function gitRemoteSshAliasToResolve(remoteUrl: string): string | null {
54 const hostname = parseSshRemoteHostname(remoteUrl);
55 if (hostname === null || hostname.includes(".") || KNOWN_PUBLIC_SSH_HOSTS.has(hostname)) {
56 return null;
57 }
58 return hostname;
59}
60
61/** Same budget as `RepositoryIdentityResolver`'s `ssh -G` probe. */
62export const SSH_CONFIG_RESOLVE_TIMEOUT_MS = 5_000;
63
64export const resolveSshHostnameFromConfig: ResolveSshHostname<ResolveGitRemoteServices> = (alias) =>
65 resolveSshTarget(alias, {
66 timeoutMs: SSH_CONFIG_RESOLVE_TIMEOUT_MS,
67 fallbackOnError: false,
68 }).pipe(
69 Effect.map((target) => {
70 const hostname = target.hostname.trim();
71 return hostname.length > 0 ? hostname : null;
72 }),
73 Effect.orElseSucceed(() => null),
74 );
75
76/**
77 * Rewrites `git@alias:path` to the HostName from `ssh -G` so provider
78 * detection and the PR pane talk to the real API host. The original remote
79 * is left unchanged when resolve fails or the host is already a real name.
80 */
81export function resolveGitRemoteForSourceControl(
82 remoteUrl: string,
83 resolveHostname: ResolveSshHostname,
84): Effect.Effect<string>;
85export function resolveGitRemoteForSourceControl(
86 remoteUrl: string,
87): Effect.Effect<string, never, ResolveGitRemoteServices>;
88export function resolveGitRemoteForSourceControl(
89 remoteUrl: string,
90 resolveHostname: ResolveSshHostname<ResolveGitRemoteServices> = resolveSshHostnameFromConfig,
91): Effect.Effect<string, never, ResolveGitRemoteServices> {
92 return Effect.gen(function* () {
93 const alias = gitRemoteSshAliasToResolve(remoteUrl);
94 if (alias === null) {
95 return remoteUrl;
96 }
97
98 const resolved = yield* resolveHostname(alias);
99 const hostname = resolved?.trim() ?? "";
100 if (hostname.length === 0) {
101 return remoteUrl;
102 }
103
104 return rewriteGitRemoteUrlHost(remoteUrl, hostname);
105 });
106}