type-git 0.2.0 → 0.3.0-alpha.1

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-BFRtEriX.cjs';
1
+ import { G as GitErrorCategory, e as ExecOpts, f as RawResult, L as LfsMode, g as GitProgress, h as GitOpenOptions, i as EnvInheritance, A as AuditConfig, R as RuntimeAdapters, j as ExecutionContext, k 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-Bpuvmw9y.cjs';
2
2
 
3
3
  /**
4
4
  * stdout parsing utilities for Git output
@@ -3940,12 +3940,24 @@ type CliRunnerOptions = {
3940
3940
  gitBinary?: string;
3941
3941
  /** Additional environment variables */
3942
3942
  env?: Record<string, string>;
3943
+ /**
3944
+ * Controls how the parent process environment is inherited by spawned Git commands.
3945
+ *
3946
+ * By default, only a curated allowlist of variables that Git needs to operate is
3947
+ * inherited (environment variable traversal prevention); the full parent environment is
3948
+ * NOT forwarded. See {@link EnvInheritance} for all options.
3949
+ *
3950
+ * @default undefined (inherit the default allowlist)
3951
+ */
3952
+ inheritEnv?: EnvInheritance;
3943
3953
  /** Directories to prepend to PATH */
3944
3954
  pathPrefix?: string[];
3945
3955
  /** Custom HOME directory for git config isolation */
3946
3956
  home?: string;
3947
3957
  /** Credential helper configuration */
3948
3958
  credential?: CredentialHelperConfig;
3959
+ /** Audit configuration for command tracking and tracing */
3960
+ audit?: AuditConfig;
3949
3961
  };
3950
3962
  /**
3951
3963
  * CLI Runner for executing Git commands
@@ -3960,9 +3972,11 @@ declare class CliRunner {
3960
3972
  private readonly exec;
3961
3973
  private readonly gitBinary;
3962
3974
  private readonly baseEnv;
3975
+ private readonly inheritEnv;
3963
3976
  private readonly pathPrefix;
3964
3977
  private readonly home;
3965
3978
  private readonly credential;
3979
+ private readonly audit;
3966
3980
  constructor(adapters: RuntimeAdapters, options?: CliRunnerOptions);
3967
3981
  /**
3968
3982
  * Create a new CliRunner with additional options merged
@@ -3977,7 +3991,10 @@ declare class CliRunner {
3977
3991
  /**
3978
3992
  * Build the effective environment for command execution
3979
3993
  *
3980
- * Applies home directory and PATH prefix overrides
3994
+ * Seeds the environment from the inherited (allowlisted) parent variables, then layers
3995
+ * the configured `env`, home directory, and PATH prefix overrides on top. The parent
3996
+ * process environment is never forwarded wholesale unless `inheritEnv: true` is set
3997
+ * (environment variable traversal prevention).
3981
3998
  */
3982
3999
  private buildEnv;
3983
4000
  /**
@@ -3988,6 +4005,10 @@ declare class CliRunner {
3988
4005
  * Run a Git command
3989
4006
  */
3990
4007
  run(context: ExecutionContext, args: string[], opts?: ExecOpts): Promise<RawResult>;
4008
+ /**
4009
+ * Process a single line from stderr for progress and trace callbacks
4010
+ */
4011
+ private processStderrLine;
3991
4012
  /**
3992
4013
  * Map Git result to GitError if needed
3993
4014
  */
@@ -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-BFRtEriX.js';
1
+ import { G as GitErrorCategory, e as ExecOpts, f as RawResult, L as LfsMode, g as GitProgress, h as GitOpenOptions, i as EnvInheritance, A as AuditConfig, R as RuntimeAdapters, j as ExecutionContext, k 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-Bpuvmw9y.js';
2
2
 
3
3
  /**
4
4
  * stdout parsing utilities for Git output
@@ -3940,12 +3940,24 @@ type CliRunnerOptions = {
3940
3940
  gitBinary?: string;
3941
3941
  /** Additional environment variables */
3942
3942
  env?: Record<string, string>;
3943
+ /**
3944
+ * Controls how the parent process environment is inherited by spawned Git commands.
3945
+ *
3946
+ * By default, only a curated allowlist of variables that Git needs to operate is
3947
+ * inherited (environment variable traversal prevention); the full parent environment is
3948
+ * NOT forwarded. See {@link EnvInheritance} for all options.
3949
+ *
3950
+ * @default undefined (inherit the default allowlist)
3951
+ */
3952
+ inheritEnv?: EnvInheritance;
3943
3953
  /** Directories to prepend to PATH */
3944
3954
  pathPrefix?: string[];
3945
3955
  /** Custom HOME directory for git config isolation */
3946
3956
  home?: string;
3947
3957
  /** Credential helper configuration */
3948
3958
  credential?: CredentialHelperConfig;
3959
+ /** Audit configuration for command tracking and tracing */
3960
+ audit?: AuditConfig;
3949
3961
  };
3950
3962
  /**
3951
3963
  * CLI Runner for executing Git commands
@@ -3960,9 +3972,11 @@ declare class CliRunner {
3960
3972
  private readonly exec;
3961
3973
  private readonly gitBinary;
3962
3974
  private readonly baseEnv;
3975
+ private readonly inheritEnv;
3963
3976
  private readonly pathPrefix;
3964
3977
  private readonly home;
3965
3978
  private readonly credential;
3979
+ private readonly audit;
3966
3980
  constructor(adapters: RuntimeAdapters, options?: CliRunnerOptions);
3967
3981
  /**
3968
3982
  * Create a new CliRunner with additional options merged
@@ -3977,7 +3991,10 @@ declare class CliRunner {
3977
3991
  /**
3978
3992
  * Build the effective environment for command execution
3979
3993
  *
3980
- * Applies home directory and PATH prefix overrides
3994
+ * Seeds the environment from the inherited (allowlisted) parent variables, then layers
3995
+ * the configured `env`, home directory, and PATH prefix overrides on top. The parent
3996
+ * process environment is never forwarded wholesale unless `inheritEnv: true` is set
3997
+ * (environment variable traversal prevention).
3981
3998
  */
3982
3999
  private buildEnv;
3983
4000
  /**
@@ -3988,6 +4005,10 @@ declare class CliRunner {
3988
4005
  * Run a Git command
3989
4006
  */
3990
4007
  run(context: ExecutionContext, args: string[], opts?: ExecOpts): Promise<RawResult>;
4008
+ /**
4009
+ * Process a single line from stderr for progress and trace callbacks
4010
+ */
4011
+ private processStderrLine;
3991
4012
  /**
3992
4013
  * Map Git result to GitError if needed
3993
4014
  */
@@ -417,9 +417,103 @@ function detectErrorCategory(stderr) {
417
417
 
418
418
  // src/runner/cli-runner.ts
419
419
  import process2 from "process";
420
+
421
+ // src/core/env.ts
422
+ var DEFAULT_ENV_ALLOWLIST = [
423
+ // Executable resolution
424
+ "PATH",
425
+ "PATHEXT",
426
+ // User home / identity
427
+ "HOME",
428
+ "USER",
429
+ "LOGNAME",
430
+ // Locale and time
431
+ "LANG",
432
+ "LANGUAGE",
433
+ "LC_ALL",
434
+ "LC_CTYPE",
435
+ "LC_MESSAGES",
436
+ "LC_NUMERIC",
437
+ "LC_TIME",
438
+ "LC_COLLATE",
439
+ "LC_MONETARY",
440
+ "TZ",
441
+ // Temporary directories
442
+ "TMPDIR",
443
+ "TMP",
444
+ "TEMP",
445
+ // Terminal
446
+ "TERM",
447
+ "COLORTERM",
448
+ // SSH agent (required for Git-over-SSH authentication)
449
+ "SSH_AUTH_SOCK",
450
+ "SSH_AGENT_PID",
451
+ // HTTP(S) proxy configuration (required for Git network operations behind a proxy)
452
+ "HTTP_PROXY",
453
+ "HTTPS_PROXY",
454
+ "http_proxy",
455
+ "https_proxy",
456
+ "ALL_PROXY",
457
+ "all_proxy",
458
+ "NO_PROXY",
459
+ "no_proxy",
460
+ // Windows essentials
461
+ "USERPROFILE",
462
+ "HOMEDRIVE",
463
+ "HOMEPATH",
464
+ "APPDATA",
465
+ "LOCALAPPDATA",
466
+ "PROGRAMDATA",
467
+ "ALLUSERSPROFILE",
468
+ "PROGRAMFILES",
469
+ "PROGRAMFILES(X86)",
470
+ "PROGRAMW6432",
471
+ "PUBLIC",
472
+ "SYSTEMROOT",
473
+ "SYSTEMDRIVE",
474
+ "WINDIR",
475
+ "COMSPEC",
476
+ "COMPUTERNAME",
477
+ "USERDOMAIN",
478
+ "NUMBER_OF_PROCESSORS",
479
+ "PROCESSOR_ARCHITECTURE"
480
+ ];
481
+ function resolveInheritedEnv(parentEnv, inheritEnv, platform = "") {
482
+ if (inheritEnv === true) {
483
+ const all = {};
484
+ for (const [key, value] of Object.entries(parentEnv)) {
485
+ if (value !== void 0) {
486
+ all[key] = value;
487
+ }
488
+ }
489
+ return all;
490
+ }
491
+ const allow = new Set(inheritEnv === false ? [] : DEFAULT_ENV_ALLOWLIST);
492
+ if (Array.isArray(inheritEnv)) {
493
+ for (const name of inheritEnv) {
494
+ allow.add(name);
495
+ }
496
+ }
497
+ const isWindows = platform === "win32";
498
+ const allowLower = isWindows ? new Set([...allow].map((name) => name.toLowerCase())) : null;
499
+ const result = {};
500
+ for (const [key, value] of Object.entries(parentEnv)) {
501
+ if (value === void 0) {
502
+ continue;
503
+ }
504
+ const allowed = allowLower ? allowLower.has(key.toLowerCase()) : allow.has(key);
505
+ if (allowed) {
506
+ result[key] = value;
507
+ }
508
+ }
509
+ return result;
510
+ }
511
+
512
+ // src/runner/cli-runner.ts
420
513
  var FATAL_ERROR_PATTERN = /fatal:\s*(.+)/i;
421
514
  var ERROR_PATTERN = /error:\s*(.+)/i;
422
515
  var LINE_SEPARATOR_PATTERN = /\r|\n/;
516
+ var GIT_TRACE_PATTERN = /^\d{2}:\d{2}:\d{2}\.\d+\s+(?:trace:|[A-Za-z0-9_.-]+\.c:)/;
423
517
  function toLfsProgress(info) {
424
518
  if (!info) {
425
519
  return null;
@@ -469,16 +563,20 @@ var CliRunner = class _CliRunner {
469
563
  exec;
470
564
  gitBinary;
471
565
  baseEnv;
566
+ inheritEnv;
472
567
  pathPrefix;
473
568
  home;
474
569
  credential;
570
+ audit;
475
571
  constructor(adapters, options) {
476
572
  this.exec = adapters.exec;
477
573
  this.gitBinary = options?.gitBinary ?? "git";
478
574
  this.baseEnv = options?.env ?? {};
575
+ this.inheritEnv = options?.inheritEnv;
479
576
  this.pathPrefix = options?.pathPrefix ?? [];
480
577
  this.home = options?.home;
481
578
  this.credential = options?.credential;
579
+ this.audit = options?.audit;
482
580
  }
483
581
  /**
484
582
  * Create a new CliRunner with additional options merged
@@ -488,8 +586,10 @@ var CliRunner = class _CliRunner {
488
586
  withOptions(options) {
489
587
  const mergedEnv = { ...this.baseEnv, ...options.env };
490
588
  const mergedPathPrefix = [...this.pathPrefix, ...options.pathPrefix ?? []];
589
+ const inheritEnv = options.inheritEnv ?? this.inheritEnv;
491
590
  const home = options.home ?? this.home;
492
591
  const credential = options.credential ?? this.credential;
592
+ const audit = options.audit ?? this.audit;
493
593
  return new _CliRunner(
494
594
  {
495
595
  exec: this.exec,
@@ -504,9 +604,11 @@ var CliRunner = class _CliRunner {
504
604
  {
505
605
  gitBinary: options.gitBinary ?? this.gitBinary,
506
606
  env: mergedEnv,
607
+ inheritEnv,
507
608
  pathPrefix: mergedPathPrefix,
508
609
  home,
509
- credential
610
+ credential,
611
+ audit
510
612
  }
511
613
  );
512
614
  }
@@ -534,10 +636,16 @@ var CliRunner = class _CliRunner {
534
636
  /**
535
637
  * Build the effective environment for command execution
536
638
  *
537
- * Applies home directory and PATH prefix overrides
639
+ * Seeds the environment from the inherited (allowlisted) parent variables, then layers
640
+ * the configured `env`, home directory, and PATH prefix overrides on top. The parent
641
+ * process environment is never forwarded wholesale unless `inheritEnv: true` is set
642
+ * (environment variable traversal prevention).
538
643
  */
539
644
  buildEnv() {
540
- const env = { ...this.baseEnv };
645
+ const env = {
646
+ ...resolveInheritedEnv(process2.env, this.inheritEnv, process2.platform),
647
+ ...this.baseEnv
648
+ };
541
649
  if (this.home) {
542
650
  env.HOME = this.home;
543
651
  env.USERPROFILE = this.home;
@@ -550,9 +658,20 @@ var CliRunner = class _CliRunner {
550
658
  }
551
659
  }
552
660
  if (allPathPrefixes.length > 0) {
553
- const separator = process2.platform === "win32" ? ";" : ":";
554
- const currentPath = process2.env.PATH ?? "";
555
- env.PATH = [...allPathPrefixes, currentPath].join(separator);
661
+ const isWindows = process2.platform === "win32";
662
+ const separator = isWindows ? ";" : ":";
663
+ const pathKey = isWindows ? Object.keys(env).find((key) => key.toLowerCase() === "path") ?? "PATH" : "PATH";
664
+ const currentPath = env[pathKey] ?? "";
665
+ env[pathKey] = [...allPathPrefixes, currentPath].join(separator);
666
+ }
667
+ if (this.audit?.onTrace) {
668
+ if (env.GIT_TRACE === void 0) {
669
+ env.GIT_TRACE = "1";
670
+ } else if (!env.GIT_TRACE || env.GIT_TRACE === "0") {
671
+ console.warn(
672
+ "type-git: onTrace callback provided but GIT_TRACE is disabled. Trace events will not be emitted."
673
+ );
674
+ }
556
675
  }
557
676
  return env;
558
677
  }
@@ -577,27 +696,73 @@ var CliRunner = class _CliRunner {
577
696
  if (onLfsProgress) {
578
697
  env.GIT_LFS_FORCE_PROGRESS = "1";
579
698
  }
580
- let stderrBuffer = "";
581
- const result = await this.exec.spawn(
582
- {
699
+ const startTimestamp = Date.now();
700
+ if (this.audit?.onAudit) {
701
+ this.audit.onAudit({
702
+ type: "start",
703
+ timestamp: startTimestamp,
583
704
  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);
705
+ context
706
+ });
707
+ }
708
+ let stderrBuffer = "";
709
+ const needsStderrStreaming = onProgress || onLfsProgress || this.audit?.onTrace;
710
+ let result;
711
+ try {
712
+ result = await this.exec.spawn(
713
+ {
714
+ argv,
715
+ env,
716
+ // The env above is already fully resolved (including any inherited variables),
717
+ // so the adapter must not merge the parent environment on top of it.
718
+ inheritEnv: false,
719
+ cwd: context.type === "worktree" ? context.workdir : void 0,
720
+ signal
721
+ },
722
+ needsStderrStreaming ? {
723
+ onStderr: (chunk) => {
724
+ stderrBuffer += chunk;
725
+ const lines = stderrBuffer.split(LINE_SEPARATOR_PATTERN);
726
+ stderrBuffer = lines.pop() ?? "";
727
+ for (const line of lines) {
728
+ this.processStderrLine(line, onProgress, onLfsProgress);
729
+ }
595
730
  }
596
- }
597
- } : void 0
598
- );
599
- if (stderrBuffer.trim() && (onProgress || onLfsProgress)) {
600
- processProgressLine(stderrBuffer, onProgress, onLfsProgress);
731
+ } : void 0
732
+ );
733
+ if (stderrBuffer.trim() && needsStderrStreaming) {
734
+ this.processStderrLine(stderrBuffer, onProgress, onLfsProgress);
735
+ }
736
+ } catch (error) {
737
+ if (this.audit?.onAudit) {
738
+ const endTimestamp = Date.now();
739
+ this.audit.onAudit({
740
+ type: "end",
741
+ timestamp: endTimestamp,
742
+ argv,
743
+ context,
744
+ stdout: "",
745
+ stderr: error instanceof Error ? error.message : String(error),
746
+ exitCode: -1,
747
+ aborted: signal?.aborted ?? false,
748
+ duration: endTimestamp - startTimestamp
749
+ });
750
+ }
751
+ throw error;
752
+ }
753
+ if (this.audit?.onAudit) {
754
+ const endTimestamp = Date.now();
755
+ this.audit.onAudit({
756
+ type: "end",
757
+ timestamp: endTimestamp,
758
+ argv,
759
+ context,
760
+ stdout: result.stdout,
761
+ stderr: result.stderr,
762
+ exitCode: result.exitCode,
763
+ aborted: result.aborted,
764
+ duration: endTimestamp - startTimestamp
765
+ });
601
766
  }
602
767
  return {
603
768
  stdout: result.stdout,
@@ -606,6 +771,23 @@ var CliRunner = class _CliRunner {
606
771
  aborted: result.aborted
607
772
  };
608
773
  }
774
+ /**
775
+ * Process a single line from stderr for progress and trace callbacks
776
+ */
777
+ processStderrLine(line, onProgress, onLfsProgress) {
778
+ const trimmed = line.trim();
779
+ if (!trimmed) {
780
+ return;
781
+ }
782
+ if (this.audit?.onTrace && GIT_TRACE_PATTERN.test(trimmed)) {
783
+ this.audit.onTrace({
784
+ timestamp: Date.now(),
785
+ line: trimmed
786
+ });
787
+ return;
788
+ }
789
+ processProgressLine(line, onProgress, onLfsProgress);
790
+ }
609
791
  /**
610
792
  * Map Git result to GitError if needed
611
793
  */
@@ -1099,7 +1281,8 @@ var BareRepoImpl = class {
1099
1281
  }
1100
1282
  await this.runner.runOrThrow(this.context, args, {
1101
1283
  signal: opts?.signal,
1102
- onProgress: opts?.onProgress
1284
+ onProgress: opts?.onProgress,
1285
+ onLfsProgress: opts?.onLfsProgress
1103
1286
  });
1104
1287
  }
1105
1288
  // ==========================================================================
@@ -2070,7 +2253,8 @@ var WorktreeRepoImpl = class _WorktreeRepoImpl {
2070
2253
  }
2071
2254
  await this.runner.runOrThrow(this.context, args, {
2072
2255
  signal: opts?.signal,
2073
- onProgress: opts?.onProgress
2256
+ onProgress: opts?.onProgress,
2257
+ onLfsProgress: opts?.onLfsProgress
2074
2258
  });
2075
2259
  }
2076
2260
  /**
@@ -5306,6 +5490,7 @@ function toCliRunnerOptions(opts) {
5306
5490
  }
5307
5491
  return {
5308
5492
  env: opts.env,
5493
+ inheritEnv: opts.inheritEnv,
5309
5494
  pathPrefix: opts.pathPrefix,
5310
5495
  home: opts.home,
5311
5496
  credential: opts.credential ? {
@@ -5414,7 +5599,8 @@ var GitImpl = class {
5414
5599
  try {
5415
5600
  await this.runner.runOrThrow({ type: "global" }, args, {
5416
5601
  signal: opts?.signal,
5417
- onProgress: opts?.onProgress
5602
+ onProgress: opts?.onProgress,
5603
+ onLfsProgress: opts?.onLfsProgress
5418
5604
  });
5419
5605
  } catch (error) {
5420
5606
  if (error instanceof GitError && error.kind === "Aborted" && opts?.cleanupOnAbort !== false) {
@@ -5748,6 +5934,12 @@ async function createGit(options) {
5748
5934
 
5749
5935
  // src/adapters/node/exec.ts
5750
5936
  import { spawn } from "child_process";
5937
+ function resolveChildEnv(env, inheritEnv) {
5938
+ if (inheritEnv === false) {
5939
+ return env ?? {};
5940
+ }
5941
+ return env ? { ...process.env, ...env } : process.env;
5942
+ }
5751
5943
  async function* streamToAsyncIterable(stream) {
5752
5944
  let buffer = "";
5753
5945
  for await (const chunk of stream) {
@@ -5774,7 +5966,7 @@ var NodeExecAdapter = class {
5774
5966
  };
5775
5967
  }
5776
5968
  spawn(options, handlers) {
5777
- const { argv, env, cwd, signal } = options;
5969
+ const { argv, env, inheritEnv, cwd, signal } = options;
5778
5970
  const command = argv[0];
5779
5971
  if (command === void 0) {
5780
5972
  throw new Error("argv must not be empty");
@@ -5783,7 +5975,7 @@ var NodeExecAdapter = class {
5783
5975
  return new Promise((resolve, reject) => {
5784
5976
  const child = spawn(command, args, {
5785
5977
  cwd,
5786
- env: env ? { ...process.env, ...env } : process.env,
5978
+ env: resolveChildEnv(env, inheritEnv),
5787
5979
  stdio: ["ignore", "pipe", "pipe"]
5788
5980
  });
5789
5981
  let stdout = "";
@@ -5831,7 +6023,7 @@ var NodeExecAdapter = class {
5831
6023
  });
5832
6024
  }
5833
6025
  spawnStreaming(options) {
5834
- const { argv, env, cwd, signal } = options;
6026
+ const { argv, env, inheritEnv, cwd, signal } = options;
5835
6027
  const command = argv[0];
5836
6028
  if (command === void 0) {
5837
6029
  throw new Error("argv must not be empty");
@@ -5839,7 +6031,7 @@ var NodeExecAdapter = class {
5839
6031
  const args = argv.slice(1);
5840
6032
  const child = spawn(command, args, {
5841
6033
  cwd,
5842
- env: env ? { ...process.env, ...env } : process.env,
6034
+ env: resolveChildEnv(env, inheritEnv),
5843
6035
  stdio: ["ignore", "pipe", "pipe"]
5844
6036
  });
5845
6037
  let aborted = false;