type-git 0.3.0-alpha.2 → 0.3.0-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -623,6 +623,8 @@ type AuditConfig = {
623
623
  type SpawnOptions = {
624
624
  /** Command and arguments (first element is the command) */
625
625
  argv: string[];
626
+ /** UTF-8 text to write to standard input, then close it. */
627
+ stdin?: string;
626
628
  /** Additional environment variables */
627
629
  env?: Record<string, string>;
628
630
  /**
@@ -623,6 +623,8 @@ type AuditConfig = {
623
623
  type SpawnOptions = {
624
624
  /** Command and arguments (first element is the command) */
625
625
  argv: string[];
626
+ /** UTF-8 text to write to standard input, then close it. */
627
+ stdin?: string;
626
628
  /** Additional environment variables */
627
629
  env?: Record<string, string>;
628
630
  /**
package/dist/index.cjs CHANGED
@@ -896,6 +896,7 @@ var CliRunner = class _CliRunner {
896
896
  result = await this.exec.spawn(
897
897
  {
898
898
  argv,
899
+ stdin: opts?.stdin,
899
900
  env,
900
901
  // The env above is already fully resolved (including any inherited variables),
901
902
  // so the adapter must not merge the parent environment on top of it.
@@ -1751,7 +1752,53 @@ var BareRepoImpl = class {
1751
1752
  }
1752
1753
  };
1753
1754
 
1755
+ // src/internal/concurrency.ts
1756
+ async function runWithConcurrency(items, run, options) {
1757
+ const { concurrency, warmupCount = 0 } = options;
1758
+ if (!Number.isInteger(concurrency) || concurrency < 1) {
1759
+ throw new RangeError(`concurrency must be a positive integer, got: ${concurrency}`);
1760
+ }
1761
+ if (!Number.isInteger(warmupCount) || warmupCount < 0) {
1762
+ throw new RangeError(`warmupCount must be a non-negative integer, got: ${warmupCount}`);
1763
+ }
1764
+ const results = new Array(items.length);
1765
+ let nextIndex = 0;
1766
+ let failed = false;
1767
+ let firstError;
1768
+ const runOne = async (index) => {
1769
+ try {
1770
+ const item = items[index];
1771
+ results[index] = await run(item, index);
1772
+ } catch (error) {
1773
+ if (!failed) {
1774
+ failed = true;
1775
+ firstError = error;
1776
+ }
1777
+ }
1778
+ };
1779
+ const warmupEnd = Math.min(warmupCount, items.length);
1780
+ while (nextIndex < warmupEnd && !failed) {
1781
+ await runOne(nextIndex++);
1782
+ }
1783
+ if (!failed && nextIndex < items.length) {
1784
+ const worker = async () => {
1785
+ while (!failed && nextIndex < items.length) {
1786
+ await runOne(nextIndex++);
1787
+ }
1788
+ };
1789
+ const workerCount = Math.min(concurrency, items.length - nextIndex);
1790
+ await Promise.all(Array.from({ length: workerCount }, () => worker()));
1791
+ }
1792
+ if (failed) {
1793
+ throw firstError;
1794
+ }
1795
+ return results;
1796
+ }
1797
+
1754
1798
  // src/impl/worktree-repo-impl.ts
1799
+ var PRE_UPLOAD_DEFAULT_BATCH_SIZE = 200;
1800
+ var PRE_UPLOAD_DEFAULT_CONCURRENCY = 4;
1801
+ var PRE_UPLOAD_WARMUP_BATCH_COUNT = 1;
1755
1802
  var WorktreeRepoImpl = class _WorktreeRepoImpl {
1756
1803
  workdir;
1757
1804
  runner;
@@ -2500,27 +2547,30 @@ var WorktreeRepoImpl = class _WorktreeRepoImpl {
2500
2547
  if (this._lfsMode === "disabled") {
2501
2548
  return;
2502
2549
  }
2550
+ if (opts?.stdin && opts.objectId === void 0 && !opts.ref) {
2551
+ throw new TypeError("lfs.push with stdin requires objectId or ref");
2552
+ }
2503
2553
  const args = ["lfs", "push"];
2504
2554
  if (opts?.dryRun) {
2505
2555
  args.push("--dry-run");
2506
2556
  }
2507
- if (opts?.objectId) {
2508
- const oids = Array.isArray(opts.objectId) ? opts.objectId : [opts.objectId];
2509
- for (const oid of oids) {
2510
- args.push("--object-id", oid);
2511
- }
2512
- } else {
2513
- args.push("--all");
2557
+ const oids = opts?.objectId !== void 0 ? Array.isArray(opts.objectId) ? opts.objectId : [opts.objectId] : void 0;
2558
+ if (oids?.length === 0 && !opts?.stdin) {
2559
+ throw new TypeError(
2560
+ "lfs.push with objectId requires at least one OID unless stdin is enabled"
2561
+ );
2514
2562
  }
2515
- if (opts?.remote) {
2516
- args.push(opts.remote);
2563
+ args.push(oids ? "--object-id" : "--all");
2564
+ args.push(opts?.remote ?? "origin");
2565
+ const values = oids ?? (opts?.ref ? [opts.ref] : []);
2566
+ if (opts?.stdin) {
2567
+ args.push("--stdin");
2517
2568
  } else {
2518
- args.push("origin");
2519
- }
2520
- if (opts?.ref) {
2521
- args.push(opts.ref);
2569
+ args.push(...values);
2522
2570
  }
2523
2571
  await this.runner.runOrThrow(this.context, args, {
2572
+ stdin: opts?.stdin ? values.length > 0 ? `${values.join("\n")}
2573
+ ` : "" : void 0,
2524
2574
  signal: opts?.signal,
2525
2575
  onProgress: opts?.onProgress,
2526
2576
  onLfsProgress: opts?.onLfsProgress
@@ -2559,12 +2609,19 @@ var WorktreeRepoImpl = class _WorktreeRepoImpl {
2559
2609
  * Pre-upload LFS objects before commit/push (2-phase commit pattern)
2560
2610
  *
2561
2611
  * This allows uploading LFS objects to the remote before creating a commit,
2562
- * ensuring reliable large file handling. Objects are batched to handle
2563
- * Windows command line length limits (8KB).
2612
+ * ensuring reliable large file handling. Objects are batched to stay within
2613
+ * command-line length limits, and batches run with bounded concurrency to
2614
+ * overlap per-invocation fixed costs with other batches' transfers.
2615
+ *
2616
+ * Failure handling: once a batch fails, no new batch is started (in-flight
2617
+ * batches are awaited), and the objects of failed and unstarted batches are
2618
+ * reported in `skippedCount`. The first batch always runs alone before the
2619
+ * remaining batches widen to the concurrency limit, so a failure common to
2620
+ * all batches costs a single round-trip.
2564
2621
  *
2565
2622
  * Usage pattern:
2566
2623
  * 1. Stage changes: git add
2567
- * 2. Pre-upload LFS: await repo.lfs.preUpload()
2624
+ * 2. Pre-upload LFS: await repo.lfsExtra.preUpload()
2568
2625
  * 3. Commit: git commit
2569
2626
  * 4. Push refs: git push (only refs, objects already uploaded)
2570
2627
  */
@@ -2573,7 +2630,14 @@ var WorktreeRepoImpl = class _WorktreeRepoImpl {
2573
2630
  return { uploadedCount: 0, uploadedBytes: 0, skippedCount: 0 };
2574
2631
  }
2575
2632
  const remote = opts?.remote ?? "origin";
2576
- const batchSize = opts?.batchSize ?? 50;
2633
+ const batchSize = opts?.batchSize ?? PRE_UPLOAD_DEFAULT_BATCH_SIZE;
2634
+ const concurrency = opts?.concurrency ?? PRE_UPLOAD_DEFAULT_CONCURRENCY;
2635
+ if (!Number.isInteger(batchSize) || batchSize < 1) {
2636
+ throw new RangeError(`batchSize must be a positive integer, got: ${batchSize}`);
2637
+ }
2638
+ if (!Number.isInteger(concurrency) || concurrency < 1) {
2639
+ throw new RangeError(`concurrency must be a positive integer, got: ${concurrency}`);
2640
+ }
2577
2641
  let oids;
2578
2642
  if (opts?.oids && opts.oids.length > 0) {
2579
2643
  oids = opts.oids;
@@ -2598,18 +2662,24 @@ var WorktreeRepoImpl = class _WorktreeRepoImpl {
2598
2662
  if (oids.length === 0) {
2599
2663
  return { uploadedCount: 0, uploadedBytes: 0, skippedCount: 0 };
2600
2664
  }
2665
+ const batches = [];
2666
+ for (let i = 0; i < oids.length; i += batchSize) {
2667
+ batches.push(oids.slice(i, i + batchSize));
2668
+ }
2601
2669
  let uploadedCount = 0;
2602
2670
  let uploadedBytes = 0;
2603
2671
  let skippedCount = 0;
2604
- for (let i = 0; i < oids.length; i += batchSize) {
2605
- const batch = oids.slice(i, i + batchSize);
2672
+ let attemptedCount = 0;
2673
+ const batchFailedStop = new Error("lfs pre-upload batch failed");
2674
+ const pushBatch = async (batch) => {
2606
2675
  const args = ["lfs", "push", remote, "--object-id", ...batch];
2607
2676
  const result = await this.runner.run(this.context, args, {
2608
2677
  signal: opts?.signal,
2609
2678
  onProgress: opts?.onProgress,
2610
2679
  onLfsProgress: opts?.onLfsProgress
2611
2680
  });
2612
- if (result.exitCode === 0) {
2681
+ attemptedCount += batch.length;
2682
+ if (result.exitCode === 0 && !result.aborted) {
2613
2683
  uploadedCount += batch.length;
2614
2684
  const bytesMatch = result.stderr.match(
2615
2685
  /Uploading LFS objects:.*?\((\d+(?:\.\d+)?)\s*([KMG]?B)/i
@@ -2629,7 +2699,19 @@ var WorktreeRepoImpl = class _WorktreeRepoImpl {
2629
2699
  }
2630
2700
  } else {
2631
2701
  skippedCount += batch.length;
2702
+ throw batchFailedStop;
2632
2703
  }
2704
+ };
2705
+ try {
2706
+ await runWithConcurrency(batches, pushBatch, {
2707
+ concurrency,
2708
+ warmupCount: PRE_UPLOAD_WARMUP_BATCH_COUNT
2709
+ });
2710
+ } catch (error) {
2711
+ if (error !== batchFailedStop) {
2712
+ throw error;
2713
+ }
2714
+ skippedCount += oids.length - attemptedCount;
2633
2715
  }
2634
2716
  return { uploadedCount, uploadedBytes, skippedCount };
2635
2717
  }
@@ -2641,7 +2723,7 @@ var WorktreeRepoImpl = class _WorktreeRepoImpl {
2641
2723
  *
2642
2724
  * Usage pattern:
2643
2725
  * 1. Fetch refs: git fetch
2644
- * 2. Pre-download LFS: await repo.lfs.preDownload({ ref: 'origin/feature' })
2726
+ * 2. Pre-download LFS: await repo.lfsExtra.preDownload({ ref: 'origin/feature' })
2645
2727
  * 3. Checkout: git checkout feature
2646
2728
  */
2647
2729
  async lfsPreDownload(opts) {
@@ -2650,6 +2732,9 @@ var WorktreeRepoImpl = class _WorktreeRepoImpl {
2650
2732
  }
2651
2733
  const remote = opts?.remote ?? "origin";
2652
2734
  const batchSize = opts?.batchSize ?? 50;
2735
+ if (!Number.isInteger(batchSize) || batchSize < 1) {
2736
+ throw new RangeError(`batchSize must be a positive integer, got: ${batchSize}`);
2737
+ }
2653
2738
  let oids;
2654
2739
  if (opts?.oids && opts.oids.length > 0) {
2655
2740
  oids = opts.oids;
@@ -6196,7 +6281,7 @@ var NodeExecAdapter = class {
6196
6281
  const child = (0, import_node_child_process.spawn)(command, args, {
6197
6282
  cwd,
6198
6283
  env: resolveChildEnv(env, inheritEnv),
6199
- stdio: ["ignore", "pipe", "pipe"]
6284
+ stdio: [options.stdin === void 0 ? "ignore" : "pipe", "pipe", "pipe"]
6200
6285
  });
6201
6286
  let stdout = "";
6202
6287
  let stderr = "";
@@ -6224,6 +6309,12 @@ var NodeExecAdapter = class {
6224
6309
  stderr += text;
6225
6310
  handlers?.onStderr?.(text);
6226
6311
  });
6312
+ child.stdin?.on("error", (error) => {
6313
+ if (error.code !== "EPIPE" && error.code !== "EOF") {
6314
+ reject(error);
6315
+ }
6316
+ });
6317
+ child.stdin?.end(options.stdin);
6227
6318
  child.on("error", (error) => {
6228
6319
  signal?.removeEventListener("abort", abortHandler);
6229
6320
  reject(error);
@@ -6252,7 +6343,7 @@ var NodeExecAdapter = class {
6252
6343
  const child = (0, import_node_child_process.spawn)(command, args, {
6253
6344
  cwd,
6254
6345
  env: resolveChildEnv(env, inheritEnv),
6255
- stdio: ["ignore", "pipe", "pipe"]
6346
+ stdio: [options.stdin === void 0 ? "ignore" : "pipe", "pipe", "pipe"]
6256
6347
  });
6257
6348
  let aborted = false;
6258
6349
  let exitSignal;
@@ -6277,6 +6368,12 @@ var NodeExecAdapter = class {
6277
6368
  child.stderr?.on("data", (chunk) => {
6278
6369
  stderr += chunk.toString("utf8");
6279
6370
  });
6371
+ child.stdin?.on("error", (error) => {
6372
+ if (error.code !== "EPIPE" && error.code !== "EOF") {
6373
+ reject(error);
6374
+ }
6375
+ });
6376
+ child.stdin?.end(options.stdin);
6280
6377
  child.on("error", (error) => {
6281
6378
  signal?.removeEventListener("abort", abortHandler);
6282
6379
  reject(error);