@@ -104,14 +104,16 @@ it.layer(NodeServices.layer)("RepositoryIdentityResolverLive", (it) => {
104104 expect ( calls ) . toEqual ( [
105105 [ "-C" , "/repo/packages/web" , "rev-parse" , "--show-toplevel" ] ,
106106 [ "-C" , "/repo" , "remote" , "-v" ] ,
107+ [ "-G" , "-l" , "git" , "github.com" ] ,
107108 ] ) ;
108109
109110 const refreshed = yield * resolver . resolve ( "/repo/packages/web" , { refresh : true } ) ;
110111 expect ( refreshed ?. rootPath ) . toBe ( "/repo/packages/web" ) ;
111112 expect ( yield * resolver . resolve ( "/repo/packages/web" ) ) . toEqual ( refreshed ) ;
112- expect ( calls . slice ( 2 ) ) . toEqual ( [
113+ expect ( calls . slice ( 3 ) ) . toEqual ( [
113114 [ "-C" , "/repo/packages/web" , "rev-parse" , "--show-toplevel" ] ,
114115 [ "-C" , "/repo/packages/web" , "remote" , "-v" ] ,
116+ [ "-G" , "-l" , "git" , "github.com" ] ,
115117 ] ) ;
116118 remoteUrl = "[email protected] :team/repo.git" ; 117119 const forgejo = yield * resolver . resolve ( rootPath , { refresh : true } ) ;
@@ -170,10 +172,155 @@ it.layer(NodeServices.layer)("RepositoryIdentityResolverLive", (it) => {
170172 [ "-C" , "/repo/packages/web" , "rev-parse" , "--show-toplevel" ] ,
171173 [ "-C" , "/repo/packages/web" , "rev-parse" , "--show-toplevel" ] ,
172174 [ "-C" , "/repo" , "remote" , "-v" ] ,
175+ [ "-G" , "-l" , "git" , "github.com" ] ,
173176 ] ) ;
174177 } ) . pipe ( Effect . provide ( Layer . merge ( TestClock . layer ( ) , resolverLayer ) ) ) ;
175178 } ) ;
176179
180+ it . effect ( "keys a remote through its SSH host alias" , ( ) => {
181+ const sshHosts : Array < string > = [ ] ;
182+ let remoteUrl = "gh:DarkyEg/spinup" ;
183+ let sshFails = false ;
184+ // `Host gh` / `HostName github.com`, `Host gh443` for the port-443 endpoint,
185+ // and `admin` / `codex`, two logins on one server.
186+ const sshConfigs : Record < string , string > = {
187+ gh : "user git\nhostname github.com" ,
188+ gh443 : "user git\nhostname ssh.github.com" ,
189+ admin : "user darky\nhostname 192.168.8.105" ,
190+ codex : "user codex-remote\nhostname 192.168.8.105" ,
191+ nouser : "user usfkh\nhostname github.com" ,
192+ // `Match originalhost review user git` picks GitHub; otherwise GitLab.
193+ "git@review" : "user git\nhostname github.com" ,
194+ review : "user me\nhostname gitlab.com" ,
195+ } ;
196+ const processRunner = Layer . succeed ( ProcessRunner . ProcessRunner , {
197+ run : ( input ) =>
198+ Effect . sync ( ( ) => {
199+ const sshArgs = input . args . slice ( 1 ) ;
200+ const host = sshArgs . at ( - 1 ) ?? "" ;
201+ const user = sshArgs . includes ( "-l" ) ? sshArgs [ sshArgs . indexOf ( "-l" ) + 1 ] : undefined ;
202+ if ( input . command === "ssh" ) sshHosts . push ( sshArgs . join ( " " ) ) ;
203+ const config = sshConfigs [ `${ user } @${ host } ` ] ?? sshConfigs [ host ] ;
204+ const stdout =
205+ input . command === "ssh"
206+ ? `${ config ?? `user git\nhostname ${ host } ` } \nport 22\n`
207+ : input . args . includes ( "rev-parse" )
208+ ? "/repo\n"
209+ : `origin\t${ remoteUrl } (fetch)\n` ;
210+ const failed = input . command === "ssh" && sshFails ;
211+ return {
212+ stdout : failed ? "" : stdout ,
213+ stderr : "" ,
214+ code : ChildProcessSpawner . ExitCode ( failed ? 255 : 0 ) ,
215+ timedOut : false ,
216+ stdoutTruncated : false ,
217+ stderrTruncated : false ,
218+ stdoutInvalidUtf8 : false ,
219+ stderrInvalidUtf8 : false ,
220+ } ;
221+ } ) ,
222+ } ) ;
223+ const resolverLayer = Layer . effect (
224+ RepositoryIdentityResolver . RepositoryIdentityResolver ,
225+ RepositoryIdentityResolver . make ( ) ,
226+ ) . pipe ( Layer . provide ( processRunner ) ) ;
227+
228+ return Effect . gen ( function * ( ) {
229+ const resolver = yield * RepositoryIdentityResolver . RepositoryIdentityResolver ;
230+ const resolveKey = ( url : string ) => {
231+ remoteUrl = url ;
232+ return resolver . resolve ( "/repo" , { refresh : true } ) ;
233+ } ;
234+
235+ const aliased = yield * resolveKey ( "gh:DarkyEg/spinup" ) ;
236+ expect ( aliased ?. canonicalKey ) . toBe ( "github.com/darkyeg/spinup" ) ;
237+ expect ( aliased ?. provider ) . toBe ( "github" ) ;
238+ expect ( aliased ?. owner ) . toBe ( "darkyeg" ) ;
239+ // Git still reaches the repository through the alias.
240+ expect ( aliased ?. locator . remoteUrl ) . toBe ( "gh:DarkyEg/spinup" ) ;
241+
242+ expect ( ( yield * resolveKey ( "me@gh:DarkyEg/spinup.git" ) ) ?. canonicalKey ) . toBe (
243+ "github.com/darkyeg/spinup" ,
244+ ) ;
245+ expect ( ( yield * resolveKey ( "ssh://gh/DarkyEg/spinup.git" ) ) ?. canonicalKey ) . toBe (
246+ "github.com/darkyeg/spinup" ,
247+ ) ;
248+ expect ( ( yield * resolveKey ( "git+ssh://git@gh:2222/DarkyEg/spinup" ) ) ?. canonicalKey ) . toBe (
249+ "github.com/darkyeg/spinup" ,
250+ ) ;
251+ expect ( ( yield * resolveKey ( "gh443:DarkyEg/spinup" ) ) ?. canonicalKey ) . toBe (
252+ "github.com/darkyeg/spinup" ,
253+ ) ;
254+ // A server alias with an absolute path keys like the same path by address.
255+ const byAlias = yield * resolveKey ( "admin:/srv/git/app.git" ) ;
256+ const byAddress = yield * resolveKey ( "ssh://[email protected] /srv/git/app.git" ) ; 257+ expect ( byAlias ?. canonicalKey ) . toBe ( "192.168.8.105/srv/git/app" ) ;
258+ expect ( byAddress ?. canonicalKey ) . toBe ( byAlias ?. canonicalKey ) ;
259+ // The remote's user and port reach ssh, which `Match` rules can depend on.
260+ expect ( sshHosts ) . toEqual ( [
261+ "gh" ,
262+ "-l me gh" ,
263+ "gh" ,
264+ "-l git -p 2222 gh" ,
265+ "gh443" ,
266+ "admin" ,
267+ "-l deploy 192.168.8.105" ,
268+ ] ) ;
269+ expect ( ( yield * resolveKey ( "git@review:Team/App" ) ) ?. canonicalKey ) . toBe ( "github.com/team/app" ) ;
270+ expect ( ( yield * resolveKey ( "review:Team/App" ) ) ?. canonicalKey ) . toBe ( "gitlab.com/team/app" ) ;
271+
272+ // A relative path on a server is in the login's home: two logins, two repositories.
273+ const darkyHome = "192.168.8.105/~darky/app" ;
274+ expect ( ( yield * resolveKey ( "admin:app.git" ) ) ?. canonicalKey ) . toBe ( darkyHome ) ;
275+ expect ( ( yield * resolveKey ( "codex:app.git" ) ) ?. canonicalKey ) . toBe (
276+ "192.168.8.105/~codex-remote/app" ,
277+ ) ;
278+ expect ( ( yield * resolveKey ( "[email protected] :~/app.git" ) ) ?. canonicalKey ) . toBe ( darkyHome ) ; 279+ expect ( ( yield * resolveKey ( "ssh://[email protected] /~/app.git" ) ) ?. canonicalKey ) . toBe ( 280+ darkyHome ,
281+ ) ;
282+ expect ( ( yield * resolveKey ( "codex:~darky/app.git" ) ) ?. canonicalKey ) . toBe ( darkyHome ) ;
283+ // A forge reads the path as the repository, whatever the login.
284+ expect ( ( yield * resolveKey ( "[email protected] :team/app.git" ) ) ?. canonicalKey ) . toBe ( 285+ "gitlab.example.com/team/app" ,
286+ ) ;
287+
288+ // Every spelling of one repository keys the same, on every kind of host.
289+ const codeCommit = "git-codecommit.us-east-1.amazonaws.com/v1/repos/app" ;
290+ for ( const [ remote , key ] of [
291+ [ "[email protected] :Team/App.git" , "bitbucket.org/team/app" ] , 292+ [ "https://[email protected] /Team/App.git" , "bitbucket.org/team/app" ] , 293+ [ "[email protected] :~darky/app" , "git.sr.ht/~darky/app" ] , 294+ [ "https://git.sr.ht/~darky/app" , "git.sr.ht/~darky/app" ] ,
295+ // A self-hosted forge on a plain name still serves everyone as `git`.
296+ [ "[email protected] :team/app.git" , "git.corp.example/team/app" ] , 297+ [ "https://git.corp.example/team/app" , "git.corp.example/team/app" ] ,
298+ // Per-user logins with absolute paths: CodeCommit's key id, Gerrit's account.
299+ [ "ssh://[email protected] /v1/repos/App" , codeCommit ] , 300+ [ `https://${ codeCommit } ` , codeCommit ] ,
301+ [ "ssh://[email protected] :29418/platform/app" , "review.corp.example/platform/app" ] , 302+ [ "https://review.corp.example/platform/app" , "review.corp.example/platform/app" ] ,
303+ // An alias without `User` logs in as the local account; GitHub ignores it.
304+ [ "nouser:Team/App" , "github.com/team/app" ] ,
305+ ] as const ) {
306+ expect ( ( yield * resolveKey ( remote ) ) ?. canonicalKey , remote ) . toBe ( key ) ;
307+ }
308+ const sshCalls = sshHosts . length ;
309+
310+ // Neither HTTPS nor a host ssh would read as an option reaches ssh.
311+ expect ( ( yield * resolveKey ( "https://github.com/DarkyEg/spinup" ) ) ?. canonicalKey ) . toBe (
312+ "github.com/darkyeg/spinup" ,
313+ ) ;
314+ yield * resolveKey ( "-oProxyCommand=calc:DarkyEg/spinup" ) ;
315+ expect ( sshHosts ) . toHaveLength ( sshCalls ) ;
316+
317+ // Without ssh, the alias keys the repository as before.
318+ sshFails = true ;
319+ const unresolved = yield * resolveKey ( "gh:DarkyEg/spinup" ) ;
320+ expect ( unresolved ?. canonicalKey ) . toBe ( "gh/darkyeg/spinup" ) ;
321+ } ) . pipe ( Effect . provide ( resolverLayer ) ) ;
322+ } ) ;
323+
177324 it . effect ( "normalizes equivalent GitHub remotes into a stable repository identity" , ( ) =>
178325 Effect . gen ( function * ( ) {
179326 const fileSystem = yield * FileSystem . FileSystem ;
0 commit comments