({
adapter = workspaceIsomorphicGitClient,
}: CreateGitClientOptions = {})
| 359 | * or CLI) reuses the resolved modules. |
| 360 | */ |
| 361 | export function createGitClient({ |
| 362 | adapter = workspaceIsomorphicGitClient, |
| 363 | }: CreateGitClientOptions = {}): GitClientFactory { |
| 364 | return function createWorkspaceGitClient({ |
| 365 | ws, |
| 366 | defaultIdentity, |
| 367 | }: WorkspaceGitClientOptions): GitClient { |
| 368 | let fsPromise: Promise<IsomorphicGitFSClient> | undefined; |
| 369 | const fs = () => { |
| 370 | if (!fsPromise) fsPromise = adapter(ws.provider()); |
| 371 | return fsPromise; |
| 372 | }; |
| 373 | const cache: Record<string, unknown> = {}; |
| 374 | |
| 375 | // Memoised module loaders. Each holds the promise returned by |
| 376 | // the dynamic import so concurrent first-use callers share one |
| 377 | // import pass, and subsequent callers reuse the resolved module |
| 378 | // synchronously through the cached promise. |
| 379 | let gitPromise: Promise<unknown> | undefined; |
| 380 | let httpPromise: Promise<object> | undefined; |
| 381 | let createPatchPromise: Promise<CreatePatchFn> | undefined; |
| 382 | const loadGit = <T>(): Promise<T> => { |
| 383 | if (!gitPromise) gitPromise = loadIsomorphicGit(); |
| 384 | return gitPromise as Promise<T>; |
| 385 | }; |
| 386 | const loadHttp = (): Promise<object> => { |
| 387 | if (!httpPromise) httpPromise = loadDefaultHTTP(); |
| 388 | return httpPromise; |
| 389 | }; |
| 390 | const loadDiffPatch = (): Promise<CreatePatchFn> => { |
| 391 | if (!createPatchPromise) createPatchPromise = loadCreatePatch(); |
| 392 | return createPatchPromise; |
| 393 | }; |
| 394 | |
| 395 | const client: GitClient = { |
| 396 | async clone(options) { |
| 397 | await cloneWith({ |
| 398 | ...options, |
| 399 | fs: await fs(), |
| 400 | git: await loadGit<IsomorphicGitClient>(), |
| 401 | http: await loadHttp(), |
| 402 | cache, |
| 403 | }); |
| 404 | }, |
| 405 | async diff(options = {}) { |
| 406 | const f = await fs(); |
| 407 | return diffWith({ |
| 408 | ...options, |
| 409 | fs: f, |
| 410 | git: await loadGit<IsomorphicGitDiffClient>(), |
| 411 | createPatch: await loadDiffPatch(), |
| 412 | readFile: readFileFrom(f), |
| 413 | cache, |
| 414 | }); |
| 415 | }, |
| 416 | async diffSummary(options = {}) { |
| 417 | const f = await fs(); |
| 418 | return diffSummaryWith({ |
no outgoing calls