type-git 0.2.0 → 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.
package/README.md CHANGED
@@ -12,6 +12,7 @@ Type-safe Git wrapper library with LFS support, progress tracking, and abort con
12
12
  - **Repository-context aware**: Distinguishes between repository-agnostic and repository-specific operations
13
13
  - **Git LFS support**: Built-in support for Git LFS with progress tracking
14
14
  - **Progress tracking**: Real-time progress events for clone, fetch, push, and LFS operations
15
+ - **Audit mode**: Track all Git command executions with start/end events and optional GIT_TRACE output
15
16
  - **Abort control**: Cancel operations using AbortController
16
17
  - **Cross-runtime**: Works with Node.js, Deno, and Bun
17
18
  - **No cwd dependency**: Uses `git -C` for clean repository context management
@@ -131,6 +132,43 @@ const lfsStatus = await repo.lfs.status();
131
132
  const result = await repo.raw(['rev-parse', 'HEAD']);
132
133
  ```
133
134
 
135
+ ### Audit Mode
136
+
137
+ Track all Git command executions for logging, debugging, or monitoring:
138
+
139
+ ```typescript
140
+ import { createGit } from 'type-git';
141
+ import { createNodeAdapters } from 'type-git/node';
142
+
143
+ const git = await createGit({
144
+ adapters: createNodeAdapters(),
145
+ audit: {
146
+ // Called before and after every Git command
147
+ onAudit: (event) => {
148
+ if (event.type === 'start') {
149
+ console.log(`[START] ${event.argv.join(' ')}`);
150
+ } else {
151
+ console.log(`[END] exit ${event.exitCode}, ${event.duration}ms`);
152
+ // event.stdout, event.stderr also available
153
+ }
154
+ },
155
+ // Optional: Capture GIT_TRACE output (automatically sets GIT_TRACE=1)
156
+ onTrace: (trace) => {
157
+ console.log(`[TRACE] ${trace.line}`);
158
+ },
159
+ },
160
+ });
161
+
162
+ // All operations will now emit audit events
163
+ const repo = await git.open('/path/to/repo');
164
+ await repo.status();
165
+ ```
166
+
167
+ **Audit Event Types:**
168
+ - `AuditEventStart`: Emitted before command execution with `argv`, `context`, `timestamp`
169
+ - `AuditEventEnd`: Emitted after completion with `stdout`, `stderr`, `exitCode`, `duration`, `aborted`
170
+ - `TraceEvent`: GIT_TRACE output lines with `timestamp` and `line`
171
+
134
172
  ## Architecture
135
173
 
136
174
  ```
@@ -1,4 +1,4 @@
1
- import { 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, R as RuntimeAdapters } from '../../adapters-BFRtEriX.cjs';
1
+ import { 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, R as RuntimeAdapters } from '../../adapters-BEKkCViX.cjs';
2
2
 
3
3
  /**
4
4
  * Bun ExecAdapter implementation
@@ -1,4 +1,4 @@
1
- import { 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, R as RuntimeAdapters } from '../../adapters-BFRtEriX.js';
1
+ import { 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, R as RuntimeAdapters } from '../../adapters-BEKkCViX.js';
2
2
 
3
3
  /**
4
4
  * Bun ExecAdapter implementation
@@ -1,4 +1,4 @@
1
- import { 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, R as RuntimeAdapters } from '../../adapters-BFRtEriX.cjs';
1
+ import { 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, R as RuntimeAdapters } from '../../adapters-BEKkCViX.cjs';
2
2
 
3
3
  /**
4
4
  * Deno ExecAdapter implementation
@@ -1,4 +1,4 @@
1
- import { 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, R as RuntimeAdapters } from '../../adapters-BFRtEriX.js';
1
+ import { 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, R as RuntimeAdapters } from '../../adapters-BEKkCViX.js';
2
2
 
3
3
  /**
4
4
  * Deno ExecAdapter implementation
@@ -459,6 +459,7 @@ var import_node_process = __toESM(require("process"), 1);
459
459
  var FATAL_ERROR_PATTERN = /fatal:\s*(.+)/i;
460
460
  var ERROR_PATTERN = /error:\s*(.+)/i;
461
461
  var LINE_SEPARATOR_PATTERN = /\r|\n/;
462
+ var GIT_TRACE_PATTERN = /^\d{2}:\d{2}:\d{2}\.\d+\s+(?:trace:|[A-Za-z0-9_.-]+\.c:)/;
462
463
  function toLfsProgress(info) {
463
464
  if (!info) {
464
465
  return null;
@@ -511,6 +512,7 @@ var CliRunner = class _CliRunner {
511
512
  pathPrefix;
512
513
  home;
513
514
  credential;
515
+ audit;
514
516
  constructor(adapters, options) {
515
517
  this.exec = adapters.exec;
516
518
  this.gitBinary = options?.gitBinary ?? "git";
@@ -518,6 +520,7 @@ var CliRunner = class _CliRunner {
518
520
  this.pathPrefix = options?.pathPrefix ?? [];
519
521
  this.home = options?.home;
520
522
  this.credential = options?.credential;
523
+ this.audit = options?.audit;
521
524
  }
522
525
  /**
523
526
  * Create a new CliRunner with additional options merged
@@ -529,6 +532,7 @@ var CliRunner = class _CliRunner {
529
532
  const mergedPathPrefix = [...this.pathPrefix, ...options.pathPrefix ?? []];
530
533
  const home = options.home ?? this.home;
531
534
  const credential = options.credential ?? this.credential;
535
+ const audit = options.audit ?? this.audit;
532
536
  return new _CliRunner(
533
537
  {
534
538
  exec: this.exec,
@@ -545,7 +549,8 @@ var CliRunner = class _CliRunner {
545
549
  env: mergedEnv,
546
550
  pathPrefix: mergedPathPrefix,
547
551
  home,
548
- credential
552
+ credential,
553
+ audit
549
554
  }
550
555
  );
551
556
  }
@@ -593,6 +598,15 @@ var CliRunner = class _CliRunner {
593
598
  const currentPath = import_node_process.default.env.PATH ?? "";
594
599
  env.PATH = [...allPathPrefixes, currentPath].join(separator);
595
600
  }
601
+ if (this.audit?.onTrace) {
602
+ if (env.GIT_TRACE === void 0) {
603
+ env.GIT_TRACE = "1";
604
+ } else if (!env.GIT_TRACE || env.GIT_TRACE === "0") {
605
+ console.warn(
606
+ "type-git: onTrace callback provided but GIT_TRACE is disabled. Trace events will not be emitted."
607
+ );
608
+ }
609
+ }
596
610
  return env;
597
611
  }
598
612
  /**
@@ -616,27 +630,70 @@ var CliRunner = class _CliRunner {
616
630
  if (onLfsProgress) {
617
631
  env.GIT_LFS_FORCE_PROGRESS = "1";
618
632
  }
619
- let stderrBuffer = "";
620
- const result = await this.exec.spawn(
621
- {
633
+ const startTimestamp = Date.now();
634
+ if (this.audit?.onAudit) {
635
+ this.audit.onAudit({
636
+ type: "start",
637
+ timestamp: startTimestamp,
622
638
  argv,
623
- env,
624
- cwd: context.type === "worktree" ? context.workdir : void 0,
625
- signal
626
- },
627
- onProgress || onLfsProgress ? {
628
- onStderr: (chunk) => {
629
- stderrBuffer += chunk;
630
- const lines = stderrBuffer.split(LINE_SEPARATOR_PATTERN);
631
- stderrBuffer = lines.pop() ?? "";
632
- for (const line of lines) {
633
- processProgressLine(line, onProgress, onLfsProgress);
639
+ context
640
+ });
641
+ }
642
+ let stderrBuffer = "";
643
+ const needsStderrStreaming = onProgress || onLfsProgress || this.audit?.onTrace;
644
+ let result;
645
+ try {
646
+ result = await this.exec.spawn(
647
+ {
648
+ argv,
649
+ env,
650
+ cwd: context.type === "worktree" ? context.workdir : void 0,
651
+ signal
652
+ },
653
+ needsStderrStreaming ? {
654
+ onStderr: (chunk) => {
655
+ stderrBuffer += chunk;
656
+ const lines = stderrBuffer.split(LINE_SEPARATOR_PATTERN);
657
+ stderrBuffer = lines.pop() ?? "";
658
+ for (const line of lines) {
659
+ this.processStderrLine(line, onProgress, onLfsProgress);
660
+ }
634
661
  }
635
- }
636
- } : void 0
637
- );
638
- if (stderrBuffer.trim() && (onProgress || onLfsProgress)) {
639
- processProgressLine(stderrBuffer, onProgress, onLfsProgress);
662
+ } : void 0
663
+ );
664
+ if (stderrBuffer.trim() && needsStderrStreaming) {
665
+ this.processStderrLine(stderrBuffer, onProgress, onLfsProgress);
666
+ }
667
+ } catch (error) {
668
+ if (this.audit?.onAudit) {
669
+ const endTimestamp = Date.now();
670
+ this.audit.onAudit({
671
+ type: "end",
672
+ timestamp: endTimestamp,
673
+ argv,
674
+ context,
675
+ stdout: "",
676
+ stderr: error instanceof Error ? error.message : String(error),
677
+ exitCode: -1,
678
+ aborted: signal?.aborted ?? false,
679
+ duration: endTimestamp - startTimestamp
680
+ });
681
+ }
682
+ throw error;
683
+ }
684
+ if (this.audit?.onAudit) {
685
+ const endTimestamp = Date.now();
686
+ this.audit.onAudit({
687
+ type: "end",
688
+ timestamp: endTimestamp,
689
+ argv,
690
+ context,
691
+ stdout: result.stdout,
692
+ stderr: result.stderr,
693
+ exitCode: result.exitCode,
694
+ aborted: result.aborted,
695
+ duration: endTimestamp - startTimestamp
696
+ });
640
697
  }
641
698
  return {
642
699
  stdout: result.stdout,
@@ -645,6 +702,23 @@ var CliRunner = class _CliRunner {
645
702
  aborted: result.aborted
646
703
  };
647
704
  }
705
+ /**
706
+ * Process a single line from stderr for progress and trace callbacks
707
+ */
708
+ processStderrLine(line, onProgress, onLfsProgress) {
709
+ const trimmed = line.trim();
710
+ if (!trimmed) {
711
+ return;
712
+ }
713
+ if (this.audit?.onTrace && GIT_TRACE_PATTERN.test(trimmed)) {
714
+ this.audit.onTrace({
715
+ timestamp: Date.now(),
716
+ line: trimmed
717
+ });
718
+ return;
719
+ }
720
+ processProgressLine(line, onProgress, onLfsProgress);
721
+ }
648
722
  /**
649
723
  * Map Git result to GitError if needed
650
724
  */
@@ -1138,7 +1212,8 @@ var BareRepoImpl = class {
1138
1212
  }
1139
1213
  await this.runner.runOrThrow(this.context, args, {
1140
1214
  signal: opts?.signal,
1141
- onProgress: opts?.onProgress
1215
+ onProgress: opts?.onProgress,
1216
+ onLfsProgress: opts?.onLfsProgress
1142
1217
  });
1143
1218
  }
1144
1219
  // ==========================================================================
@@ -2109,7 +2184,8 @@ var WorktreeRepoImpl = class _WorktreeRepoImpl {
2109
2184
  }
2110
2185
  await this.runner.runOrThrow(this.context, args, {
2111
2186
  signal: opts?.signal,
2112
- onProgress: opts?.onProgress
2187
+ onProgress: opts?.onProgress,
2188
+ onLfsProgress: opts?.onLfsProgress
2113
2189
  });
2114
2190
  }
2115
2191
  /**
@@ -5453,7 +5529,8 @@ var GitImpl = class {
5453
5529
  try {
5454
5530
  await this.runner.runOrThrow({ type: "global" }, args, {
5455
5531
  signal: opts?.signal,
5456
- onProgress: opts?.onProgress
5532
+ onProgress: opts?.onProgress,
5533
+ onLfsProgress: opts?.onLfsProgress
5457
5534
  });
5458
5535
  } catch (error) {
5459
5536
  if (error instanceof GitError && error.kind === "Aborted" && opts?.cleanupOnAbort !== false) {