@@ -10,6 +10,8 @@ import * as Effect from "effect/Effect";
1010import * as Exit from "effect/Exit" ;
1111import * as Layer from "effect/Layer" ;
1212
13+ import { parseSshResolveOutput } from "@t3tools/ssh/command" ;
14+
1315import * as ProcessRunner from "../processRunner.ts" ;
1416
1517const DEFAULT_REPOSITORY_IDENTITY_CACHE_CAPACITY = 512 ;
@@ -90,9 +92,12 @@ function buildRepositoryIdentity(input: {
9092 readonly remoteUrl : string ;
9193 readonly originUrl : string | undefined ;
9294 readonly rootPath : string ;
95+ readonly resolvedRemoteUrl : string ;
9396} ) : RepositoryIdentity {
94- const canonicalKey = normalizeGitRemoteUrl ( input . remoteUrl ) ;
95- const sourceControlProvider = detectSourceControlProviderFromGitRemoteUrl ( input . remoteUrl ) ;
97+ const canonicalKey = normalizeGitRemoteUrl ( input . resolvedRemoteUrl ) ;
98+ const sourceControlProvider = detectSourceControlProviderFromGitRemoteUrl (
99+ input . resolvedRemoteUrl ,
100+ ) ;
96101 const repositoryPath = repositoryPathOf ( canonicalKey ) ;
97102 const repositoryPathSegments = repositoryPath . split ( "/" ) . filter ( ( segment ) => segment . length > 0 ) ;
98103 const [ owner ] = repositoryPathSegments ;
@@ -137,6 +142,43 @@ const resolveRepositoryIdentityCacheKey = Effect.fn("RepositoryIdentityResolver.
137142 } ,
138143) ;
139144
145+ const SSH_HOST_PATTERN = / ^ [ a - z A - Z 0 - 9 ] [ a - z A - Z 0 - 9 . _ - ] * $ / ;
146+
147+ function parseSshRemote (
148+ remoteUrl : string ,
149+ ) : { readonly host : string ; readonly path : string } | null {
150+ if ( remoteUrl . startsWith ( "ssh://" ) ) {
151+ const url = URL . parse ( remoteUrl ) ;
152+ return url === null ? null : { host : url . hostname , path : url . pathname . replace ( / ^ \/ + / , "" ) } ;
153+ }
154+ const scp = / ^ [ ^ @ / : ] + @ ( [ ^ / : ] + ) : ( [ ^ / ] .* ) $ / . exec ( remoteUrl ) ;
155+ return scp === null ? null : { host : scp [ 1 ] ! , path : scp [ 2 ] ! } ;
156+ }
157+
158+ /**
159+ * An SSH remote such as `git@github-work:team/repo` names a `Host` alias from the user's SSH
160+ * config. Its configured `HostName` is what the provider is known by, so the remote is rewritten
161+ * onto that host. Anything that is not a resolvable alias comes back unchanged.
162+ */
163+ const resolveSshAliasRemoteUrl = Effect . fn ( "RepositoryIdentityResolver.resolveSshAliasRemoteUrl" ) (
164+ function * ( remoteUrl : string ) : Effect . fn . Return < string , never , ProcessRunner . ProcessRunner > {
165+ const remote = parseSshRemote ( remoteUrl ) ;
166+ if ( remote === null || ! SSH_HOST_PATTERN . test ( remote . host ) ) return remoteUrl ;
167+ const processRunner = yield * ProcessRunner . ProcessRunner ;
168+ const result = yield * processRunner
169+ . run ( {
170+ command : "ssh" ,
171+ args : [ "-G" , "-o" , "CanonicalizeHostname=no" , "--" , remote . host ] ,
172+ timeout : Duration . seconds ( 5 ) ,
173+ timeoutBehavior : "timedOutResult" ,
174+ } )
175+ . pipe ( Effect . option ) ;
176+ if ( result . _tag === "None" || result . value . code !== 0 ) return remoteUrl ;
177+ const { hostname } = parseSshResolveOutput ( remote . host , result . value . stdout ) ;
178+ return SSH_HOST_PATTERN . test ( hostname ) ? `https://${ hostname } /${ remote . path } ` : remoteUrl ;
179+ } ,
180+ ) ;
181+
140182const resolveRepositoryIdentityFromCacheKey = Effect . fn (
141183 "RepositoryIdentityResolver.resolveFromCacheKey" ,
142184) ( function * (
@@ -156,9 +198,18 @@ const resolveRepositoryIdentityFromCacheKey = Effect.fn(
156198
157199 const remotes = parseRemoteFetchUrls ( remoteResult . value . stdout ) ;
158200 const remote = pickPrimaryRemote ( remotes ) ;
159- return remote
160- ? buildRepositoryIdentity ( { ...remote , originUrl : remotes . get ( "origin" ) , rootPath : cacheKey } )
161- : null ;
201+ if ( remote === null ) return null ;
202+ // Git keeps the alias in the locator; provider detection and API routing use its real host.
203+ const resolvedRemoteUrl =
204+ detectSourceControlProviderFromGitRemoteUrl ( remote . remoteUrl ) ?. kind === "unknown"
205+ ? yield * resolveSshAliasRemoteUrl ( remote . remoteUrl )
206+ : remote . remoteUrl ;
207+ return buildRepositoryIdentity ( {
208+ ...remote ,
209+ originUrl : remote . remoteName === "origin" ? resolvedRemoteUrl : remotes . get ( "origin" ) ,
210+ rootPath : cacheKey ,
211+ resolvedRemoteUrl,
212+ } ) ;
162213} ) ;
163214
164215export const make = Effect . fn ( "RepositoryIdentityResolver.make" ) ( function * (
0 commit comments