@@ -7,6 +7,7 @@ import * as Schedule from "effect/Schedule";
77import * as Schema from "effect/Schema" ;
88
99import {
10+ GitCommandError ,
1011 SourceControlProviderError ,
1112 SourceControlRepositoryError ,
1213 type GitCommandFailureReason ,
@@ -31,8 +32,10 @@ import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
3132import * as BitbucketApi from "@t3tools/source-control-bitbucket/server/BitbucketApi" ;
3233import * as SourceControlProviderRegistry from "./SourceControlProviderRegistry.ts" ;
3334import * as HostProcess from "@t3tools/shared/HostProcess" ;
35+ import { cloneUrlForProtocol , cloneUrlProtocol } from "@t3tools/shared/cloneUrl" ;
3436const isSourceControlRepositoryError = Schema . is ( SourceControlRepositoryError ) ;
3537const isSourceControlProviderError = Schema . is ( SourceControlProviderError ) ;
38+ const isGitCommandError = Schema . is ( GitCommandError ) ;
3639const isBitbucketRepositoryLocatorError = Schema . is ( BitbucketApi . BitbucketRepositoryLocatorError ) ;
3740
3841export class SourceControlRepositoryService extends Context . Service <
@@ -169,6 +172,27 @@ function publishPushFailureDetail(input: {
169172 }
170173}
171174
175+ /**
176+ * Adding a project clones over SSH for most hosts, and a machine with no SSH
177+ * key there would fail every time, while the host's HTTPS credential helper
178+ * may be set up. A clone whose SSH login was refused is retried once over
179+ * HTTPS, unless the caller asked for SSH. A looked-up repository has the
180+ * provider's own HTTPS URL; any other URL is rewritten, which is refused
181+ * where it would have to guess.
182+ */
183+ function httpsFallbackUrl (
184+ input : SourceControlCloneRepositoryInput ,
185+ prepared : SourceControlPreparedClone ,
186+ ) : string | null {
187+ if ( input . protocol === "ssh" || cloneUrlProtocol ( prepared . cloneUrl ) !== "ssh" ) return null ;
188+ if ( prepared . repository ?. sshUrl === prepared . cloneUrl ) return prepared . repository . url ;
189+ return cloneUrlForProtocol ( prepared . cloneUrl , "https" ) ;
190+ }
191+
192+ const isSshRejection = ( error : SourceControlRepositoryError ) =>
193+ isGitCommandError ( error . cause ) &&
194+ ( error . cause . reason === "authentication_failed" || error . cause . reason === "host_key_unverified" ) ;
195+
172196function selectRemoteUrl (
173197 urls : SourceControlRepositoryCloneUrls ,
174198 protocol : SourceControlCloneProtocol | undefined ,
@@ -312,11 +336,12 @@ export const make = Effect.gen(function* () {
312336 } satisfies SourceControlPreparedClone ;
313337 } ) ;
314338
315- const cloneRepository = Effect . fn ( "SourceControlRepositoryService.cloneRepository " ) ( function * (
339+ const runClone = Effect . fn ( "SourceControlRepositoryService.runClone " ) ( function * (
316340 input : SourceControlCloneRepositoryInput ,
341+ destinationPath : string ,
342+ cloneUrl : string ,
317343 options ?: SourceControlCloneOptions ,
318344 ) {
319- const prepared = yield * prepareClone ( input ) ;
320345 const onProgress = options ?. onProgress ;
321346 // Git interleaves progress redraws with its real messages on stderr. The
322347 // last non-progress lines are what explain a failure ("Repository not
@@ -336,14 +361,8 @@ export const make = Effect.gen(function* () {
336361 yield * git
337362 . execute ( {
338363 operation : "SourceControlRepositoryService.cloneRepository" ,
339- cwd : path . dirname ( prepared . destinationPath ) ,
340- args : [
341- "clone" ,
342- "--progress" ,
343- "--" ,
344- prepared . cloneUrl ,
345- path . basename ( prepared . destinationPath ) ,
346- ] ,
364+ cwd : path . dirname ( destinationPath ) ,
365+ args : [ "clone" , "--progress" , "--" , cloneUrl , path . basename ( destinationPath ) ] ,
347366 timeoutMs : options ?. timeoutMs === undefined ? CLONE_TIMEOUT_MS : options . timeoutMs ,
348367 // Progress redraws add up on a slow multi-GB clone. The buffered copy
349368 // is never read (the tail is kept by hand above), so keep it small
@@ -368,10 +387,42 @@ export const make = Effect.gen(function* () {
368387 } ) ,
369388 ) ,
370389 ) ;
390+ } ) ;
391+
392+ const cloneRepository = Effect . fn ( "SourceControlRepositoryService.cloneRepository" ) ( function * (
393+ input : SourceControlCloneRepositoryInput ,
394+ options ?: SourceControlCloneOptions ,
395+ ) {
396+ const prepared = yield * prepareClone ( input ) ;
397+ const httpsUrl = httpsFallbackUrl ( input , prepared ) ;
398+ const remoteUrl = yield * runClone (
399+ input ,
400+ prepared . destinationPath ,
401+ prepared . cloneUrl ,
402+ options ,
403+ ) . pipe (
404+ Effect . as ( prepared . remoteUrl ) ,
405+ Effect . catch ( ( sshError ) =>
406+ httpsUrl === null || ! isSshRejection ( sshError )
407+ ? Effect . fail ( sshError )
408+ : runClone ( input , prepared . destinationPath , httpsUrl , options ) . pipe (
409+ Effect . as ( redactRemoteUrl ( httpsUrl ) ) ,
410+ Effect . mapError (
411+ ( httpsError ) =>
412+ new SourceControlRepositoryError ( {
413+ operation : "cloneRepository" ,
414+ provider : sshError . provider ,
415+ detail : `${ sshError . detail } Cloning over HTTPS instead also failed: ${ httpsError . detail } ` ,
416+ cause : httpsError . cause ,
417+ } ) ,
418+ ) ,
419+ ) ,
420+ ) ,
421+ ) ;
371422
372423 return {
373424 cwd : prepared . destinationPath ,
374- remoteUrl : prepared . remoteUrl ,
425+ remoteUrl,
375426 repository : prepared . repository ,
376427 } ;
377428 } ) ;
0 commit comments