{
  "version": 3,
  "sources": ["../../../../src/packages/git-fetcher/index.ts"],
  "sourcesContent": ["import assert from 'node:assert';\nimport path from 'node:path';\nimport util from 'node:util';\nimport type { GitFetcher } from '../fetcher-base/index.ts';\nimport { packlist } from '../fs.packlist/index.ts';\nimport { globalWarn } from '../logger/index.ts';\nimport { preparePackage } from '../prepare-package/index.ts';\nimport { addFilesFromDir, type AddFilesResult } from '../worker/index.ts';\nimport rimraf from '@zkochan/rimraf';\nimport { execa } from 'execa';\nimport { URL } from 'node:url';\nimport type { Cafs } from '../cafs-types/index.ts';\nimport type { GitResolution } from '../resolver-base/index.ts';\n\nexport type CreateGitFetcherOptions = {\n  gitShallowHosts?: string[] | undefined;\n  rawConfig: Record<string, unknown>;\n  unsafePerm?: boolean | undefined;\n  ignoreScripts?: boolean | undefined;\n};\n\nexport function createGitFetcher(createOpts: CreateGitFetcherOptions): {\n  git: GitFetcher;\n} {\n  const allowedHosts = new Set(createOpts.gitShallowHosts ?? []);\n\n  const ignoreScripts = createOpts.ignoreScripts ?? false;\n\n  const preparePkg = preparePackage.bind(null, {\n    ignoreScripts: createOpts.ignoreScripts,\n    rawConfig: createOpts.rawConfig,\n    unsafePerm: createOpts.unsafePerm,\n  });\n\n  const gitFetcher: GitFetcher = async (\n    cafs: Cafs,\n    resolution: GitResolution,\n    opts\n  ): Promise<AddFilesResult> => {\n    const tempLocation = await cafs.tempDir();\n\n    if (\n      typeof resolution.repo !== 'string' ||\n      typeof resolution.commit !== 'string'\n    ) {\n      throw new Error('Invalid git resolution');\n    }\n\n    if (\n      allowedHosts.size > 0 &&\n      shouldUseShallow(resolution.repo, allowedHosts)\n    ) {\n      await execGit(['init'], { cwd: tempLocation });\n      await execGit(['remote', 'add', 'origin', resolution.repo], {\n        cwd: tempLocation,\n      });\n      await execGit(['fetch', '--depth', '1', 'origin', resolution.commit], {\n        cwd: tempLocation,\n      });\n    } else {\n      await execGit(['clone', resolution.repo, tempLocation]);\n    }\n\n    await execGit(['checkout', resolution.commit], { cwd: tempLocation });\n\n    let pkgDir: string;\n\n    try {\n      const prepareResult = await preparePkg(tempLocation, resolution.path);\n\n      pkgDir = prepareResult.pkgDir;\n\n      if (ignoreScripts && prepareResult.shouldBeBuilt) {\n        globalWarn(\n          `The git-hosted package fetched from \"${resolution.repo}\" has to be built but the build scripts were ignored.`\n        );\n      }\n    } catch (err: unknown) {\n      assert(util.types.isNativeError(err));\n\n      err.message = `Failed to prepare git-hosted package fetched from \"${resolution.repo}\": ${err.message}`;\n\n      throw err;\n    }\n\n    // removing /.git to make directory integrity calculation faster\n    await rimraf(path.join(tempLocation, '.git'));\n\n    const files = await packlist(pkgDir);\n\n    // Important! We cannot remove the temp location at this stage.\n    // Even though we have the index of the package,\n    // the linking of files to the store is in progress.\n    return addFilesFromDir({\n      storeDir: cafs.storeDir,\n      dir: pkgDir,\n      files,\n      filesIndexFile: opts.filesIndexFile,\n      readManifest: opts.readManifest,\n      pkg: opts.pkg,\n    });\n  };\n\n  return {\n    git: gitFetcher,\n  };\n}\n\nfunction shouldUseShallow(repoUrl: string, allowedHosts: Set<string>): boolean {\n  try {\n    const { host } = new URL(repoUrl);\n\n    if (allowedHosts.has(host)) {\n      return true;\n    }\n  } catch {\n    // URL might be malformed\n  }\n\n  return false;\n}\n\nfunction prefixGitArgs(): string[] {\n  return process.platform === 'win32' ? ['-c', 'core.longpaths=true'] : [];\n}\n\nasync function execGit(\n  args: string[],\n  opts?: object | undefined\n): Promise<void> {\n  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, @typescript-eslint/strict-boolean-expressions\n  const fullArgs = prefixGitArgs().concat(args || []);\n  await execa('git', fullArgs, opts);\n}\n"],
  "mappings": "AAAA,OAAO,YAAY;AACnB,OAAO,UAAU;AACjB,OAAO,UAAU;AAEjB,SAAS,gBAAgB;AACzB,SAAS,kBAAkB;AAC3B,SAAS,sBAAsB;AAC/B,SAAS,uBAA4C;AACrD,OAAO,YAAY;AACnB,SAAS,aAAa;AACtB,SAAS,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