type-git 0.1.1 → 0.3.0-alpha.0

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.
@@ -1,4 +1,4 @@
1
- import { G as GitErrorCategory, e as ExecOpts, f as RawResult, L as LfsMode, g as GitProgress, h as GitOpenOptions, R as RuntimeAdapters, i as ExecutionContext, j as GitError, E as ExecAdapter, C as Capabilities, S as SpawnOptions, a as StreamHandler, b as SpawnResult, c as SpawnHandle, F as FsAdapter, T as TailOptions, d as TailHandle } from '../../adapters-DehG3CsV.cjs';
1
+ import { G as GitErrorCategory, e as ExecOpts, f as RawResult, L as LfsMode, g as GitProgress, h as GitOpenOptions, A as AuditConfig, R as RuntimeAdapters, i as ExecutionContext, j as GitError, E as ExecAdapter, C as Capabilities, S as SpawnOptions, a as StreamHandler, b as SpawnResult, c as SpawnHandle, F as FsAdapter, T as TailOptions, d as TailHandle } from '../../adapters-BEKkCViX.cjs';
2
2
 
3
3
  /**
4
4
  * stdout parsing utilities for Git output
@@ -3946,6 +3946,8 @@ type CliRunnerOptions = {
3946
3946
  home?: string;
3947
3947
  /** Credential helper configuration */
3948
3948
  credential?: CredentialHelperConfig;
3949
+ /** Audit configuration for command tracking and tracing */
3950
+ audit?: AuditConfig;
3949
3951
  };
3950
3952
  /**
3951
3953
  * CLI Runner for executing Git commands
@@ -3963,6 +3965,7 @@ declare class CliRunner {
3963
3965
  private readonly pathPrefix;
3964
3966
  private readonly home;
3965
3967
  private readonly credential;
3968
+ private readonly audit;
3966
3969
  constructor(adapters: RuntimeAdapters, options?: CliRunnerOptions);
3967
3970
  /**
3968
3971
  * Create a new CliRunner with additional options merged
@@ -3988,6 +3991,10 @@ declare class CliRunner {
3988
3991
  * Run a Git command
3989
3992
  */
3990
3993
  run(context: ExecutionContext, args: string[], opts?: ExecOpts): Promise<RawResult>;
3994
+ /**
3995
+ * Process a single line from stderr for progress and trace callbacks
3996
+ */
3997
+ private processStderrLine;
3991
3998
  /**
3992
3999
  * Map Git result to GitError if needed
3993
4000
  */
@@ -1,4 +1,4 @@
1
- import { G as GitErrorCategory, e as ExecOpts, f as RawResult, L as LfsMode, g as GitProgress, h as GitOpenOptions, R as RuntimeAdapters, i as ExecutionContext, j as GitError, E as ExecAdapter, C as Capabilities, S as SpawnOptions, a as StreamHandler, b as SpawnResult, c as SpawnHandle, F as FsAdapter, T as TailOptions, d as TailHandle } from '../../adapters-DehG3CsV.js';
1
+ import { G as GitErrorCategory, e as ExecOpts, f as RawResult, L as LfsMode, g as GitProgress, h as GitOpenOptions, A as AuditConfig, R as RuntimeAdapters, i as ExecutionContext, j as GitError, E as ExecAdapter, C as Capabilities, S as SpawnOptions, a as StreamHandler, b as SpawnResult, c as SpawnHandle, F as FsAdapter, T as TailOptions, d as TailHandle } from '../../adapters-BEKkCViX.js';
2
2
 
3
3
  /**
4
4
  * stdout parsing utilities for Git output
@@ -3946,6 +3946,8 @@ type CliRunnerOptions = {
3946
3946
  home?: string;
3947
3947
  /** Credential helper configuration */
3948
3948
  credential?: CredentialHelperConfig;
3949
+ /** Audit configuration for command tracking and tracing */
3950
+ audit?: AuditConfig;
3949
3951
  };
3950
3952
  /**
3951
3953
  * CLI Runner for executing Git commands
@@ -3963,6 +3965,7 @@ declare class CliRunner {
3963
3965
  private readonly pathPrefix;
3964
3966
  private readonly home;
3965
3967
  private readonly credential;
3968
+ private readonly audit;
3966
3969
  constructor(adapters: RuntimeAdapters, options?: CliRunnerOptions);
3967
3970
  /**
3968
3971
  * Create a new CliRunner with additional options merged
@@ -3988,6 +3991,10 @@ declare class CliRunner {
3988
3991
  * Run a Git command
3989
3992
  */
3990
3993
  run(context: ExecutionContext, args: string[], opts?: ExecOpts): Promise<RawResult>;
3994
+ /**
3995
+ * Process a single line from stderr for progress and trace callbacks
3996
+ */
3997
+ private processStderrLine;
3991
3998
  /**
3992
3999
  * Map Git result to GitError if needed
3993
4000
  */
@@ -420,6 +420,7 @@ import process2 from "process";
420
420
  var FATAL_ERROR_PATTERN = /fatal:\s*(.+)/i;
421
421
  var ERROR_PATTERN = /error:\s*(.+)/i;
422
422
  var LINE_SEPARATOR_PATTERN = /\r|\n/;
423
+ var GIT_TRACE_PATTERN = /^\d{2}:\d{2}:\d{2}\.\d+\s+(?:trace:|[A-Za-z0-9_.-]+\.c:)/;
423
424
  function toLfsProgress(info) {
424
425
  if (!info) {
425
426
  return null;
@@ -472,6 +473,7 @@ var CliRunner = class _CliRunner {
472
473
  pathPrefix;
473
474
  home;
474
475
  credential;
476
+ audit;
475
477
  constructor(adapters, options) {
476
478
  this.exec = adapters.exec;
477
479
  this.gitBinary = options?.gitBinary ?? "git";
@@ -479,6 +481,7 @@ var CliRunner = class _CliRunner {
479
481
  this.pathPrefix = options?.pathPrefix ?? [];
480
482
  this.home = options?.home;
481
483
  this.credential = options?.credential;
484
+ this.audit = options?.audit;
482
485
  }
483
486
  /**
484
487
  * Create a new CliRunner with additional options merged
@@ -490,6 +493,7 @@ var CliRunner = class _CliRunner {
490
493
  const mergedPathPrefix = [...this.pathPrefix, ...options.pathPrefix ?? []];
491
494
  const home = options.home ?? this.home;
492
495
  const credential = options.credential ?? this.credential;
496
+ const audit = options.audit ?? this.audit;
493
497
  return new _CliRunner(
494
498
  {
495
499
  exec: this.exec,
@@ -506,7 +510,8 @@ var CliRunner = class _CliRunner {
506
510
  env: mergedEnv,
507
511
  pathPrefix: mergedPathPrefix,
508
512
  home,
509
- credential
513
+ credential,
514
+ audit
510
515
  }
511
516
  );
512
517
  }
@@ -554,6 +559,15 @@ var CliRunner = class _CliRunner {
554
559
  const currentPath = process2.env.PATH ?? "";
555
560
  env.PATH = [...allPathPrefixes, currentPath].join(separator);
556
561
  }
562
+ if (this.audit?.onTrace) {
563
+ if (env.GIT_TRACE === void 0) {
564
+ env.GIT_TRACE = "1";
565
+ } else if (!env.GIT_TRACE || env.GIT_TRACE === "0") {
566
+ console.warn(
567
+ "type-git: onTrace callback provided but GIT_TRACE is disabled. Trace events will not be emitted."
568
+ );
569
+ }
570
+ }
557
571
  return env;
558
572
  }
559
573
  /**
@@ -577,27 +591,70 @@ var CliRunner = class _CliRunner {
577
591
  if (onLfsProgress) {
578
592
  env.GIT_LFS_FORCE_PROGRESS = "1";
579
593
  }
580
- let stderrBuffer = "";
581
- const result = await this.exec.spawn(
582
- {
594
+ const startTimestamp = Date.now();
595
+ if (this.audit?.onAudit) {
596
+ this.audit.onAudit({
597
+ type: "start",
598
+ timestamp: startTimestamp,
583
599
  argv,
584
- env,
585
- cwd: context.type === "worktree" ? context.workdir : void 0,
586
- signal
587
- },
588
- onProgress || onLfsProgress ? {
589
- onStderr: (chunk) => {
590
- stderrBuffer += chunk;
591
- const lines = stderrBuffer.split(LINE_SEPARATOR_PATTERN);
592
- stderrBuffer = lines.pop() ?? "";
593
- for (const line of lines) {
594
- processProgressLine(line, onProgress, onLfsProgress);
600
+ context
601
+ });
602
+ }
603
+ let stderrBuffer = "";
604
+ const needsStderrStreaming = onProgress || onLfsProgress || this.audit?.onTrace;
605
+ let result;
606
+ try {
607
+ result = await this.exec.spawn(
608
+ {
609
+ argv,
610
+ env,
611
+ cwd: context.type === "worktree" ? context.workdir : void 0,
612
+ signal
613
+ },
614
+ needsStderrStreaming ? {
615
+ onStderr: (chunk) => {
616
+ stderrBuffer += chunk;
617
+ const lines = stderrBuffer.split(LINE_SEPARATOR_PATTERN);
618
+ stderrBuffer = lines.pop() ?? "";
619
+ for (const line of lines) {
620
+ this.processStderrLine(line, onProgress, onLfsProgress);
621
+ }
595
622
  }
596
- }
597
- } : void 0
598
- );
599
- if (stderrBuffer.trim() && (onProgress || onLfsProgress)) {
600
- processProgressLine(stderrBuffer, onProgress, onLfsProgress);
623
+ } : void 0
624
+ );
625
+ if (stderrBuffer.trim() && needsStderrStreaming) {
626
+ this.processStderrLine(stderrBuffer, onProgress, onLfsProgress);
627
+ }
628
+ } catch (error) {
629
+ if (this.audit?.onAudit) {
630
+ const endTimestamp = Date.now();
631
+ this.audit.onAudit({
632
+ type: "end",
633
+ timestamp: endTimestamp,
634
+ argv,
635
+ context,
636
+ stdout: "",
637
+ stderr: error instanceof Error ? error.message : String(error),
638
+ exitCode: -1,
639
+ aborted: signal?.aborted ?? false,
640
+ duration: endTimestamp - startTimestamp
641
+ });
642
+ }
643
+ throw error;
644
+ }
645
+ if (this.audit?.onAudit) {
646
+ const endTimestamp = Date.now();
647
+ this.audit.onAudit({
648
+ type: "end",
649
+ timestamp: endTimestamp,
650
+ argv,
651
+ context,
652
+ stdout: result.stdout,
653
+ stderr: result.stderr,
654
+ exitCode: result.exitCode,
655
+ aborted: result.aborted,
656
+ duration: endTimestamp - startTimestamp
657
+ });
601
658
  }
602
659
  return {
603
660
  stdout: result.stdout,
@@ -606,6 +663,23 @@ var CliRunner = class _CliRunner {
606
663
  aborted: result.aborted
607
664
  };
608
665
  }
666
+ /**
667
+ * Process a single line from stderr for progress and trace callbacks
668
+ */
669
+ processStderrLine(line, onProgress, onLfsProgress) {
670
+ const trimmed = line.trim();
671
+ if (!trimmed) {
672
+ return;
673
+ }
674
+ if (this.audit?.onTrace && GIT_TRACE_PATTERN.test(trimmed)) {
675
+ this.audit.onTrace({
676
+ timestamp: Date.now(),
677
+ line: trimmed
678
+ });
679
+ return;
680
+ }
681
+ processProgressLine(line, onProgress, onLfsProgress);
682
+ }
609
683
  /**
610
684
  * Map Git result to GitError if needed
611
685
  */
@@ -1099,7 +1173,8 @@ var BareRepoImpl = class {
1099
1173
  }
1100
1174
  await this.runner.runOrThrow(this.context, args, {
1101
1175
  signal: opts?.signal,
1102
- onProgress: opts?.onProgress
1176
+ onProgress: opts?.onProgress,
1177
+ onLfsProgress: opts?.onLfsProgress
1103
1178
  });
1104
1179
  }
1105
1180
  // ==========================================================================
@@ -2070,7 +2145,8 @@ var WorktreeRepoImpl = class _WorktreeRepoImpl {
2070
2145
  }
2071
2146
  await this.runner.runOrThrow(this.context, args, {
2072
2147
  signal: opts?.signal,
2073
- onProgress: opts?.onProgress
2148
+ onProgress: opts?.onProgress,
2149
+ onLfsProgress: opts?.onLfsProgress
2074
2150
  });
2075
2151
  }
2076
2152
  /**
@@ -5414,7 +5490,8 @@ var GitImpl = class {
5414
5490
  try {
5415
5491
  await this.runner.runOrThrow({ type: "global" }, args, {
5416
5492
  signal: opts?.signal,
5417
- onProgress: opts?.onProgress
5493
+ onProgress: opts?.onProgress,
5494
+ onLfsProgress: opts?.onLfsProgress
5418
5495
  });
5419
5496
  } catch (error) {
5420
5497
  if (error instanceof GitError && error.kind === "Aborted" && opts?.cleanupOnAbort !== false) {
@@ -5894,6 +5971,13 @@ var NodeExecAdapter = class {
5894
5971
  wait: () => waitPromise,
5895
5972
  kill: (sig) => {
5896
5973
  child.kill(sig ?? "SIGTERM");
5974
+ },
5975
+ [Symbol.asyncDispose]: async () => {
5976
+ if (child.exitCode === null) {
5977
+ child.kill("SIGTERM");
5978
+ }
5979
+ await waitPromise.catch(() => {
5980
+ });
5897
5981
  }
5898
5982
  };
5899
5983
  }
@@ -5941,7 +6025,8 @@ function createTailPolling(options) {
5941
6025
  state.resolveNext = null;
5942
6026
  }
5943
6027
  };
5944
- startPollingLoop(adapter, state, signal, pollInterval, stop).catch(() => {
6028
+ const pollingPromise = startPollingLoop(adapter, state, signal, pollInterval, stop);
6029
+ pollingPromise.catch(() => {
5945
6030
  });
5946
6031
  const lines = {
5947
6032
  [Symbol.asyncIterator]() {
@@ -5963,7 +6048,15 @@ function createTailPolling(options) {
5963
6048
  };
5964
6049
  }
5965
6050
  };
5966
- return { lines, stop };
6051
+ return {
6052
+ lines,
6053
+ stop,
6054
+ [Symbol.asyncDispose]: async () => {
6055
+ stop();
6056
+ await pollingPromise.catch(() => {
6057
+ });
6058
+ }
6059
+ };
5967
6060
  }
5968
6061
  async function startPollingLoop(adapter, state, signal, pollInterval, stop) {
5969
6062
  let position = 0;
@@ -5987,6 +6080,9 @@ async function startPollingLoop(adapter, state, signal, pollInterval, stop) {
5987
6080
  }
5988
6081
  }
5989
6082
  }
6083
+ if (state.stopped || signal?.aborted) {
6084
+ break;
6085
+ }
5990
6086
  await adapter.sleep(pollInterval);
5991
6087
  }
5992
6088
  stop();
@@ -6075,16 +6171,26 @@ var NodeFsAdapter = class {
6075
6171
  sleep: (ms) => new Promise((resolve) => setTimeout(resolve, ms))
6076
6172
  };
6077
6173
  const polling = createTailPolling({ adapter, signal, pollInterval });
6078
- const originalStop = polling.stop;
6079
- const enhancedStop = () => {
6080
- originalStop();
6174
+ const closeFileHandle = async () => {
6081
6175
  if (state.file) {
6082
- state.file.close().catch(() => {
6176
+ await state.file.close().catch(() => {
6083
6177
  });
6084
6178
  state.file = null;
6085
6179
  }
6086
6180
  };
6087
- return { lines: polling.lines, stop: enhancedStop };
6181
+ const originalStop = polling.stop;
6182
+ const enhancedStop = () => {
6183
+ originalStop();
6184
+ void closeFileHandle();
6185
+ };
6186
+ return {
6187
+ lines: polling.lines,
6188
+ stop: enhancedStop,
6189
+ [Symbol.asyncDispose]: async () => {
6190
+ await polling[Symbol.asyncDispose]();
6191
+ await closeFileHandle();
6192
+ }
6193
+ };
6088
6194
  }
6089
6195
  };
6090
6196