( gitUrl: string, targetPath: string, ref?: string, sha?: string, )
| 533 | * @param sha - Optional specific commit SHA to checkout |
| 534 | */ |
| 535 | export async function gitClone( |
| 536 | gitUrl: string, |
| 537 | targetPath: string, |
| 538 | ref?: string, |
| 539 | sha?: string, |
| 540 | ): Promise<void> { |
| 541 | // Use --recurse-submodules to initialize submodules |
| 542 | // Always start with shallow clone for efficiency |
| 543 | const args = [ |
| 544 | 'clone', |
| 545 | '--depth', |
| 546 | '1', |
| 547 | '--recurse-submodules', |
| 548 | '--shallow-submodules', |
| 549 | ] |
| 550 | |
| 551 | // Add --branch flag for specific ref (works for both branches and tags) |
| 552 | if (ref) { |
| 553 | args.push('--branch', ref) |
| 554 | } |
| 555 | |
| 556 | // If sha is specified, use --no-checkout since we'll checkout the SHA separately |
| 557 | if (sha) { |
| 558 | args.push('--no-checkout') |
| 559 | } |
| 560 | |
| 561 | args.push(gitUrl, targetPath) |
| 562 | |
| 563 | const cloneStarted = performance.now() |
| 564 | const cloneResult = await execFileNoThrow(gitExe(), args) |
| 565 | |
| 566 | if (cloneResult.code !== 0) { |
| 567 | logPluginFetch( |
| 568 | 'plugin_clone', |
| 569 | gitUrl, |
| 570 | 'failure', |
| 571 | performance.now() - cloneStarted, |
| 572 | classifyFetchError(cloneResult.stderr), |
| 573 | ) |
| 574 | throw new Error(`Failed to clone repository: ${cloneResult.stderr}`) |
| 575 | } |
| 576 | |
| 577 | // If sha is specified, fetch and checkout that specific commit |
| 578 | if (sha) { |
| 579 | // Try shallow fetch of the specific SHA first (most efficient) |
| 580 | const shallowFetchResult = await execFileNoThrowWithCwd( |
| 581 | gitExe(), |
| 582 | ['fetch', '--depth', '1', 'origin', sha], |
| 583 | { cwd: targetPath }, |
| 584 | ) |
| 585 | |
| 586 | if (shallowFetchResult.code !== 0) { |
| 587 | // Some servers don't support fetching arbitrary SHAs |
| 588 | // Fall back to unshallow fetch to get full history |
| 589 | logForDebugging( |
| 590 | `Shallow fetch of SHA ${sha} failed, falling back to unshallow fetch`, |
| 591 | ) |
| 592 | const unshallowResult = await execFileNoThrowWithCwd( |
no test coverage detected