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.
package/dist/index.cjs CHANGED
@@ -31,6 +31,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
31
31
  var src_exports = {};
32
32
  __export(src_exports, {
33
33
  CliRunner: () => CliRunner,
34
+ DEFAULT_ENV_ALLOWLIST: () => DEFAULT_ENV_ALLOWLIST,
34
35
  GIT_LOG_FORMAT: () => GIT_LOG_FORMAT,
35
36
  GitArgumentError: () => GitArgumentError,
36
37
  GitError: () => GitError,
@@ -55,7 +56,8 @@ __export(src_exports, {
55
56
  parseLsTree: () => parseLsTree,
56
57
  parsePorcelainV2: () => parsePorcelainV2,
57
58
  parseRecords: () => parseRecords,
58
- parseWorktreeList: () => parseWorktreeList
59
+ parseWorktreeList: () => parseWorktreeList,
60
+ resolveInheritedEnv: () => resolveInheritedEnv
59
61
  });
60
62
  module.exports = __toCommonJS(src_exports);
61
63
 
@@ -563,9 +565,103 @@ function detectErrorCategory(stderr) {
563
565
 
564
566
  // src/runner/cli-runner.ts
565
567
  var import_node_process = __toESM(require("process"), 1);
568
+
569
+ // src/core/env.ts
570
+ var DEFAULT_ENV_ALLOWLIST = [
571
+ // Executable resolution
572
+ "PATH",
573
+ "PATHEXT",
574
+ // User home / identity
575
+ "HOME",
576
+ "USER",
577
+ "LOGNAME",
578
+ // Locale and time
579
+ "LANG",
580
+ "LANGUAGE",
581
+ "LC_ALL",
582
+ "LC_CTYPE",
583
+ "LC_MESSAGES",
584
+ "LC_NUMERIC",
585
+ "LC_TIME",
586
+ "LC_COLLATE",
587
+ "LC_MONETARY",
588
+ "TZ",
589
+ // Temporary directories
590
+ "TMPDIR",
591
+ "TMP",
592
+ "TEMP",
593
+ // Terminal
594
+ "TERM",
595
+ "COLORTERM",
596
+ // SSH agent (required for Git-over-SSH authentication)
597
+ "SSH_AUTH_SOCK",
598
+ "SSH_AGENT_PID",
599
+ // HTTP(S) proxy configuration (required for Git network operations behind a proxy)
600
+ "HTTP_PROXY",
601
+ "HTTPS_PROXY",
602
+ "http_proxy",
603
+ "https_proxy",
604
+ "ALL_PROXY",
605
+ "all_proxy",
606
+ "NO_PROXY",
607
+ "no_proxy",
608
+ // Windows essentials
609
+ "USERPROFILE",
610
+ "HOMEDRIVE",
611
+ "HOMEPATH",
612
+ "APPDATA",
613
+ "LOCALAPPDATA",
614
+ "PROGRAMDATA",
615
+ "ALLUSERSPROFILE",
616
+ "PROGRAMFILES",
617
+ "PROGRAMFILES(X86)",
618
+ "PROGRAMW6432",
619
+ "PUBLIC",
620
+ "SYSTEMROOT",
621
+ "SYSTEMDRIVE",
622
+ "WINDIR",
623
+ "COMSPEC",
624
+ "COMPUTERNAME",
625
+ "USERDOMAIN",
626
+ "NUMBER_OF_PROCESSORS",
627
+ "PROCESSOR_ARCHITECTURE"
628
+ ];
629
+ function resolveInheritedEnv(parentEnv, inheritEnv, platform = "") {
630
+ if (inheritEnv === true) {
631
+ const all = {};
632
+ for (const [key, value] of Object.entries(parentEnv)) {
633
+ if (value !== void 0) {
634
+ all[key] = value;
635
+ }
636
+ }
637
+ return all;
638
+ }
639
+ const allow = new Set(inheritEnv === false ? [] : DEFAULT_ENV_ALLOWLIST);
640
+ if (Array.isArray(inheritEnv)) {
641
+ for (const name of inheritEnv) {
642
+ allow.add(name);
643
+ }
644
+ }
645
+ const isWindows = platform === "win32";
646
+ const allowLower = isWindows ? new Set([...allow].map((name) => name.toLowerCase())) : null;
647
+ const result = {};
648
+ for (const [key, value] of Object.entries(parentEnv)) {
649
+ if (value === void 0) {
650
+ continue;
651
+ }
652
+ const allowed = allowLower ? allowLower.has(key.toLowerCase()) : allow.has(key);
653
+ if (allowed) {
654
+ result[key] = value;
655
+ }
656
+ }
657
+ return result;
658
+ }
659
+
660
+ // src/runner/cli-runner.ts
566
661
  var FATAL_ERROR_PATTERN = /fatal:\s*(.+)/i;
567
662
  var ERROR_PATTERN = /error:\s*(.+)/i;
568
663
  var LINE_SEPARATOR_PATTERN = /\r|\n/;
664
+ var GIT_TRACE_PATTERN = /^\d{2}:\d{2}:\d{2}\.\d+\s+(?:trace:|[A-Za-z0-9_.-]+\.c:)/;
569
665
  function toLfsProgress(info) {
570
666
  if (!info) {
571
667
  return null;
@@ -615,16 +711,20 @@ var CliRunner = class _CliRunner {
615
711
  exec;
616
712
  gitBinary;
617
713
  baseEnv;
714
+ inheritEnv;
618
715
  pathPrefix;
619
716
  home;
620
717
  credential;
718
+ audit;
621
719
  constructor(adapters, options) {
622
720
  this.exec = adapters.exec;
623
721
  this.gitBinary = options?.gitBinary ?? "git";
624
722
  this.baseEnv = options?.env ?? {};
723
+ this.inheritEnv = options?.inheritEnv;
625
724
  this.pathPrefix = options?.pathPrefix ?? [];
626
725
  this.home = options?.home;
627
726
  this.credential = options?.credential;
727
+ this.audit = options?.audit;
628
728
  }
629
729
  /**
630
730
  * Create a new CliRunner with additional options merged
@@ -634,8 +734,10 @@ var CliRunner = class _CliRunner {
634
734
  withOptions(options) {
635
735
  const mergedEnv = { ...this.baseEnv, ...options.env };
636
736
  const mergedPathPrefix = [...this.pathPrefix, ...options.pathPrefix ?? []];
737
+ const inheritEnv = options.inheritEnv ?? this.inheritEnv;
637
738
  const home = options.home ?? this.home;
638
739
  const credential = options.credential ?? this.credential;
740
+ const audit = options.audit ?? this.audit;
639
741
  return new _CliRunner(
640
742
  {
641
743
  exec: this.exec,
@@ -650,9 +752,11 @@ var CliRunner = class _CliRunner {
650
752
  {
651
753
  gitBinary: options.gitBinary ?? this.gitBinary,
652
754
  env: mergedEnv,
755
+ inheritEnv,
653
756
  pathPrefix: mergedPathPrefix,
654
757
  home,
655
- credential
758
+ credential,
759
+ audit
656
760
  }
657
761
  );
658
762
  }
@@ -680,10 +784,16 @@ var CliRunner = class _CliRunner {
680
784
  /**
681
785
  * Build the effective environment for command execution
682
786
  *
683
- * Applies home directory and PATH prefix overrides
787
+ * Seeds the environment from the inherited (allowlisted) parent variables, then layers
788
+ * the configured `env`, home directory, and PATH prefix overrides on top. The parent
789
+ * process environment is never forwarded wholesale unless `inheritEnv: true` is set
790
+ * (environment variable traversal prevention).
684
791
  */
685
792
  buildEnv() {
686
- const env = { ...this.baseEnv };
793
+ const env = {
794
+ ...resolveInheritedEnv(import_node_process.default.env, this.inheritEnv, import_node_process.default.platform),
795
+ ...this.baseEnv
796
+ };
687
797
  if (this.home) {
688
798
  env.HOME = this.home;
689
799
  env.USERPROFILE = this.home;
@@ -696,9 +806,20 @@ var CliRunner = class _CliRunner {
696
806
  }
697
807
  }
698
808
  if (allPathPrefixes.length > 0) {
699
- const separator = import_node_process.default.platform === "win32" ? ";" : ":";
700
- const currentPath = import_node_process.default.env.PATH ?? "";
701
- env.PATH = [...allPathPrefixes, currentPath].join(separator);
809
+ const isWindows = import_node_process.default.platform === "win32";
810
+ const separator = isWindows ? ";" : ":";
811
+ const pathKey = isWindows ? Object.keys(env).find((key) => key.toLowerCase() === "path") ?? "PATH" : "PATH";
812
+ const currentPath = env[pathKey] ?? "";
813
+ env[pathKey] = [...allPathPrefixes, currentPath].join(separator);
814
+ }
815
+ if (this.audit?.onTrace) {
816
+ if (env.GIT_TRACE === void 0) {
817
+ env.GIT_TRACE = "1";
818
+ } else if (!env.GIT_TRACE || env.GIT_TRACE === "0") {
819
+ console.warn(
820
+ "type-git: onTrace callback provided but GIT_TRACE is disabled. Trace events will not be emitted."
821
+ );
822
+ }
702
823
  }
703
824
  return env;
704
825
  }
@@ -723,27 +844,73 @@ var CliRunner = class _CliRunner {
723
844
  if (onLfsProgress) {
724
845
  env.GIT_LFS_FORCE_PROGRESS = "1";
725
846
  }
726
- let stderrBuffer = "";
727
- const result = await this.exec.spawn(
728
- {
847
+ const startTimestamp = Date.now();
848
+ if (this.audit?.onAudit) {
849
+ this.audit.onAudit({
850
+ type: "start",
851
+ timestamp: startTimestamp,
729
852
  argv,
730
- env,
731
- cwd: context.type === "worktree" ? context.workdir : void 0,
732
- signal
733
- },
734
- onProgress || onLfsProgress ? {
735
- onStderr: (chunk) => {
736
- stderrBuffer += chunk;
737
- const lines = stderrBuffer.split(LINE_SEPARATOR_PATTERN);
738
- stderrBuffer = lines.pop() ?? "";
739
- for (const line of lines) {
740
- processProgressLine(line, onProgress, onLfsProgress);
853
+ context
854
+ });
855
+ }
856
+ let stderrBuffer = "";
857
+ const needsStderrStreaming = onProgress || onLfsProgress || this.audit?.onTrace;
858
+ let result;
859
+ try {
860
+ result = await this.exec.spawn(
861
+ {
862
+ argv,
863
+ env,
864
+ // The env above is already fully resolved (including any inherited variables),
865
+ // so the adapter must not merge the parent environment on top of it.
866
+ inheritEnv: false,
867
+ cwd: context.type === "worktree" ? context.workdir : void 0,
868
+ signal
869
+ },
870
+ needsStderrStreaming ? {
871
+ onStderr: (chunk) => {
872
+ stderrBuffer += chunk;
873
+ const lines = stderrBuffer.split(LINE_SEPARATOR_PATTERN);
874
+ stderrBuffer = lines.pop() ?? "";
875
+ for (const line of lines) {
876
+ this.processStderrLine(line, onProgress, onLfsProgress);
877
+ }
741
878
  }
742
- }
743
- } : void 0
744
- );
745
- if (stderrBuffer.trim() && (onProgress || onLfsProgress)) {
746
- processProgressLine(stderrBuffer, onProgress, onLfsProgress);
879
+ } : void 0
880
+ );
881
+ if (stderrBuffer.trim() && needsStderrStreaming) {
882
+ this.processStderrLine(stderrBuffer, onProgress, onLfsProgress);
883
+ }
884
+ } catch (error) {
885
+ if (this.audit?.onAudit) {
886
+ const endTimestamp = Date.now();
887
+ this.audit.onAudit({
888
+ type: "end",
889
+ timestamp: endTimestamp,
890
+ argv,
891
+ context,
892
+ stdout: "",
893
+ stderr: error instanceof Error ? error.message : String(error),
894
+ exitCode: -1,
895
+ aborted: signal?.aborted ?? false,
896
+ duration: endTimestamp - startTimestamp
897
+ });
898
+ }
899
+ throw error;
900
+ }
901
+ if (this.audit?.onAudit) {
902
+ const endTimestamp = Date.now();
903
+ this.audit.onAudit({
904
+ type: "end",
905
+ timestamp: endTimestamp,
906
+ argv,
907
+ context,
908
+ stdout: result.stdout,
909
+ stderr: result.stderr,
910
+ exitCode: result.exitCode,
911
+ aborted: result.aborted,
912
+ duration: endTimestamp - startTimestamp
913
+ });
747
914
  }
748
915
  return {
749
916
  stdout: result.stdout,
@@ -752,6 +919,23 @@ var CliRunner = class _CliRunner {
752
919
  aborted: result.aborted
753
920
  };
754
921
  }
922
+ /**
923
+ * Process a single line from stderr for progress and trace callbacks
924
+ */
925
+ processStderrLine(line, onProgress, onLfsProgress) {
926
+ const trimmed = line.trim();
927
+ if (!trimmed) {
928
+ return;
929
+ }
930
+ if (this.audit?.onTrace && GIT_TRACE_PATTERN.test(trimmed)) {
931
+ this.audit.onTrace({
932
+ timestamp: Date.now(),
933
+ line: trimmed
934
+ });
935
+ return;
936
+ }
937
+ processProgressLine(line, onProgress, onLfsProgress);
938
+ }
755
939
  /**
756
940
  * Map Git result to GitError if needed
757
941
  */
@@ -1245,7 +1429,8 @@ var BareRepoImpl = class {
1245
1429
  }
1246
1430
  await this.runner.runOrThrow(this.context, args, {
1247
1431
  signal: opts?.signal,
1248
- onProgress: opts?.onProgress
1432
+ onProgress: opts?.onProgress,
1433
+ onLfsProgress: opts?.onLfsProgress
1249
1434
  });
1250
1435
  }
1251
1436
  // ==========================================================================
@@ -2216,7 +2401,8 @@ var WorktreeRepoImpl = class _WorktreeRepoImpl {
2216
2401
  }
2217
2402
  await this.runner.runOrThrow(this.context, args, {
2218
2403
  signal: opts?.signal,
2219
- onProgress: opts?.onProgress
2404
+ onProgress: opts?.onProgress,
2405
+ onLfsProgress: opts?.onLfsProgress
2220
2406
  });
2221
2407
  }
2222
2408
  /**
@@ -5452,6 +5638,7 @@ function toCliRunnerOptions(opts) {
5452
5638
  }
5453
5639
  return {
5454
5640
  env: opts.env,
5641
+ inheritEnv: opts.inheritEnv,
5455
5642
  pathPrefix: opts.pathPrefix,
5456
5643
  home: opts.home,
5457
5644
  credential: opts.credential ? {
@@ -5560,7 +5747,8 @@ var GitImpl = class {
5560
5747
  try {
5561
5748
  await this.runner.runOrThrow({ type: "global" }, args, {
5562
5749
  signal: opts?.signal,
5563
- onProgress: opts?.onProgress
5750
+ onProgress: opts?.onProgress,
5751
+ onLfsProgress: opts?.onLfsProgress
5564
5752
  });
5565
5753
  } catch (error) {
5566
5754
  if (error instanceof GitError && error.kind === "Aborted" && opts?.cleanupOnAbort !== false) {
@@ -5894,6 +6082,12 @@ async function createGit(options) {
5894
6082
 
5895
6083
  // src/adapters/node/exec.ts
5896
6084
  var import_node_child_process = require("child_process");
6085
+ function resolveChildEnv(env, inheritEnv) {
6086
+ if (inheritEnv === false) {
6087
+ return env ?? {};
6088
+ }
6089
+ return env ? { ...process.env, ...env } : process.env;
6090
+ }
5897
6091
  async function* streamToAsyncIterable(stream) {
5898
6092
  let buffer = "";
5899
6093
  for await (const chunk of stream) {
@@ -5920,7 +6114,7 @@ var NodeExecAdapter = class {
5920
6114
  };
5921
6115
  }
5922
6116
  spawn(options, handlers) {
5923
- const { argv, env, cwd, signal } = options;
6117
+ const { argv, env, inheritEnv, cwd, signal } = options;
5924
6118
  const command = argv[0];
5925
6119
  if (command === void 0) {
5926
6120
  throw new Error("argv must not be empty");
@@ -5929,7 +6123,7 @@ var NodeExecAdapter = class {
5929
6123
  return new Promise((resolve, reject) => {
5930
6124
  const child = (0, import_node_child_process.spawn)(command, args, {
5931
6125
  cwd,
5932
- env: env ? { ...process.env, ...env } : process.env,
6126
+ env: resolveChildEnv(env, inheritEnv),
5933
6127
  stdio: ["ignore", "pipe", "pipe"]
5934
6128
  });
5935
6129
  let stdout = "";
@@ -5977,7 +6171,7 @@ var NodeExecAdapter = class {
5977
6171
  });
5978
6172
  }
5979
6173
  spawnStreaming(options) {
5980
- const { argv, env, cwd, signal } = options;
6174
+ const { argv, env, inheritEnv, cwd, signal } = options;
5981
6175
  const command = argv[0];
5982
6176
  if (command === void 0) {
5983
6177
  throw new Error("argv must not be empty");
@@ -5985,7 +6179,7 @@ var NodeExecAdapter = class {
5985
6179
  const args = argv.slice(1);
5986
6180
  const child = (0, import_node_child_process.spawn)(command, args, {
5987
6181
  cwd,
5988
- env: env ? { ...process.env, ...env } : process.env,
6182
+ env: resolveChildEnv(env, inheritEnv),
5989
6183
  stdio: ["ignore", "pipe", "pipe"]
5990
6184
  });
5991
6185
  let aborted = false;
@@ -6383,6 +6577,7 @@ var TypeGit = class _TypeGit {
6383
6577
  // Annotate the CommonJS export names for ESM import in node:
6384
6578
  0 && (module.exports = {
6385
6579
  CliRunner,
6580
+ DEFAULT_ENV_ALLOWLIST,
6386
6581
  GIT_LOG_FORMAT,
6387
6582
  GitArgumentError,
6388
6583
  GitError,
@@ -6407,6 +6602,7 @@ var TypeGit = class _TypeGit {
6407
6602
  parseLsTree,
6408
6603
  parsePorcelainV2,
6409
6604
  parseRecords,
6410
- parseWorktreeList
6605
+ parseWorktreeList,
6606
+ resolveInheritedEnv
6411
6607
  });
6412
6608
  //# sourceMappingURL=index.cjs.map