@procedures/git 0.66.0 → 0.68.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.
@@ -89,14 +89,23 @@ export function remoteUrlInGitRoot(url: string): string {
89
89
  * configuration file. Git gives these options to the git commands that it starts.
90
90
  */
91
91
  export function safeConfig(hooks: string): string[] {
92
+ return noProgramConfig(hooks, "never");
93
+ }
94
+
95
+ /**
96
+ * The `-c` options that stop git from starting a program of the repository: no hook, no
97
+ * `core.fsmonitor`, no `ext::`, no remote helper and no git in a submodule. `file` is the rule of
98
+ * the transport of a local path. A root gives `never`, because the path can be outside the root.
99
+ */
100
+ export function noProgramConfig(hooks: string, file: "always" | "never"): string[] {
92
101
  return [
93
102
  // No hook runs: the folder is empty
94
103
  `core.hooksPath=${hooks}`,
95
104
  "core.fsmonitor=false",
96
- // Only the network transports: no local path, no file://, no ext:: and no remote helper. The
97
- // rule of a transport overrides protocol.allow, so file and ext get their own rule.
105
+ // Only the transports of git: no ext:: and no remote helper. The rule of a transport overrides
106
+ // protocol.allow, so file and ext get their own rule.
98
107
  "protocol.allow=never",
99
- "protocol.file.allow=never",
108
+ `protocol.file.allow=${file}`,
100
109
  "protocol.ext.allow=never",
101
110
  "protocol.https.allow=always",
102
111
  "protocol.http.allow=always",
@@ -224,9 +233,8 @@ async function makeSetup(): Promise<GitRootSetup> {
224
233
  // The configuration of a repository
225
234
  // =============================================================================
226
235
 
227
- /** The keys (section.name, or section.subsection.name as section.*.name) that start a program or name a file outside the repository. */
228
- const REFUSED_KEYS: ReadonlySet<string> = new Set([
229
- // Programs
236
+ /** The keys (section.name, or section.subsection.name as section.*.name) that start a program. */
237
+ const PROGRAM_KEYS: ReadonlySet<string> = new Set([
230
238
  "core.sshcommand",
231
239
  "core.gitproxy",
232
240
  "core.askpass",
@@ -260,7 +268,12 @@ const REFUSED_KEYS: ReadonlySet<string> = new Set([
260
268
  "uploadpack.packobjectshook",
261
269
  "trailer.*.command",
262
270
  "trailer.*.cmd",
263
- // Files outside the repository
271
+ // A transport that the -c options do not name, for example the remote helper of foo://
272
+ "protocol.*.allow",
273
+ ]);
274
+
275
+ /** The keys that name a file outside the repository. */
276
+ const OUTSIDE_KEYS: ReadonlySet<string> = new Set([
264
277
  "core.worktree",
265
278
  "core.excludesfile",
266
279
  "core.attributesfile",
@@ -281,12 +294,11 @@ const REFUSED_KEYS: ReadonlySet<string> = new Set([
281
294
  ]),
282
295
  ]);
283
296
 
284
- /**
285
- * The sections that a root refuses as a whole: `pager.<command>` and the hooks of the
286
- * configuration start a program, `protocol.*` allows a transport again, and a bundle URI can be
287
- * a local file.
288
- */
289
- const REFUSED_SECTIONS: ReadonlySet<string> = new Set(["pager", "hook", "protocol", "bundle"]);
297
+ /** The sections that start a program: `pager.<command>` and the hooks of the configuration. */
298
+ const PROGRAM_SECTIONS: ReadonlySet<string> = new Set(["pager", "hook"]);
299
+
300
+ /** The sections that a root also refuses as a whole: `protocol.*` allows a transport again, and a bundle URI can be a local file. */
301
+ const ROOT_SECTIONS: ReadonlySet<string> = new Set(["protocol", "bundle"]);
290
302
 
291
303
  /** The keys that start the signing program when they are not false. */
292
304
  const SIGNING_KEYS: ReadonlySet<string> = new Set(["commit.gpgsign", "tag.gpgsign", "tag.forcesignannotated", "push.gpgsign"]);
@@ -306,15 +318,27 @@ function partsOf(key: string): { section: string; subsection: string | undefined
306
318
  };
307
319
  }
308
320
 
309
- /** True when a key of the configuration of a repository starts a program or names a file outside it. */
321
+ /** True when a set of keys has the key of these parts. */
322
+ function hasKey(keys: ReadonlySet<string>, section: string, subsection: string | undefined, name: string): boolean {
323
+ if (keys.has(subsection === undefined ? `${section}.${name}` : `${section}.*.${name}`)) return true;
324
+ // gpg.ssh.defaultKeyCommand and gpg.ssh.allowedSignersFile have a fixed subsection
325
+ return subsection !== undefined && keys.has(`${section}.${subsection.toLowerCase()}.${name}`);
326
+ }
327
+
328
+ /** True when a key of the configuration of a repository starts a program or names a file outside it. A root refuses these keys. */
310
329
  export function isRefusedConfigKey(key: string, value: string | undefined): boolean {
311
330
  const { section, subsection, name } = partsOf(key);
312
- if (REFUSED_SECTIONS.has(section)) return true;
313
- const pattern = subsection === undefined ? `${section}.${name}` : `${section}.*.${name}`;
314
- if (REFUSED_KEYS.has(pattern)) return true;
315
- // gpg.ssh.defaultKeyCommand and gpg.ssh.allowedSignersFile have a fixed subsection
316
- if (subsection !== undefined && REFUSED_KEYS.has(`${section}.${subsection.toLowerCase()}.${name}`)) return true;
317
- if (SIGNING_KEYS.has(pattern)) return !FALSE_VALUES.has((value ?? "true").trim().toLowerCase());
331
+ return ROOT_SECTIONS.has(section) || hasKey(OUTSIDE_KEYS, section, subsection, name) || startsProgram(key, value);
332
+ }
333
+
334
+ /**
335
+ * True when a key of the configuration of a repository starts a program. The key counts also when
336
+ * the next git command does not start that program: the examination does not know the next commands.
337
+ */
338
+ export function startsProgram(key: string, value: string | undefined): boolean {
339
+ const { section, subsection, name } = partsOf(key);
340
+ if (PROGRAM_SECTIONS.has(section) || hasKey(PROGRAM_KEYS, section, subsection, name)) return true;
341
+ if (subsection === undefined && SIGNING_KEYS.has(`${section}.${name}`)) return !FALSE_VALUES.has((value ?? "true").trim().toLowerCase());
318
342
  // A remote helper: `<transport>::<address>`, also as the base of url.<base>.insteadOf
319
343
  if (section === "remote" && (name === "url" || name === "pushurl")) return (value ?? "").includes("::");
320
344
  if (section === "url" && (subsection ?? "").includes("::")) return true;
@@ -330,6 +354,12 @@ export interface ConfigEntry {
330
354
  readonly value: string | undefined;
331
355
  }
332
356
 
357
+ /**
358
+ * The scopes of the configuration of the host: its programs are programs of the host, not of the
359
+ * project. A root has no system configuration, and its global configuration is a file of this process.
360
+ */
361
+ const HOST_SCOPES: ReadonlySet<string> = new Set(["system", "global", "command"]);
362
+
333
363
  /** This function reads the output of `git config --list --show-scope -z`: the scope, then the key and the value. */
334
364
  export function parseConfigList(output: string): ConfigEntry[] {
335
365
  const fields = output.split("\0");
@@ -344,15 +374,15 @@ export function parseConfigList(output: string): ConfigEntry[] {
344
374
  }
345
375
 
346
376
  /**
347
- * This function gives the keys of the configuration of a repository that a root refuses. It
348
- * examines the files of the repository (the scopes `local` and `worktree`), not the global
349
- * configuration of this process and not the `-c` options.
377
+ * This function gives the keys of the configuration of a repository that `refuses` refuses. The
378
+ * default is the rule of a root, `isRefusedConfigKey()`. It examines the files of the repository
379
+ * (the scopes `local` and `worktree`), not the configuration of the host (`HOST_SCOPES`).
350
380
  */
351
- export function refusedConfigKeys(entries: readonly ConfigEntry[]): string[] {
381
+ export function refusedConfigKeys(entries: readonly ConfigEntry[], refuses: (key: string, value: string | undefined) => boolean = isRefusedConfigKey): string[] {
352
382
  const refused = new Set<string>();
353
383
  for (const entry of entries) {
354
- if (entry.scope === "global" || entry.scope === "command") continue;
355
- if (isRefusedConfigKey(entry.key, entry.value)) refused.add(entry.key);
384
+ if (HOST_SCOPES.has(entry.scope)) continue;
385
+ if (refuses(entry.key, entry.value)) refused.add(entry.key);
356
386
  }
357
387
  return [...refused];
358
388
  }
@@ -372,7 +402,7 @@ async function isInRoot(root: ResolvedRoot, path: string): Promise<boolean> {
372
402
  }
373
403
 
374
404
  /** True when a file or a folder exists. */
375
- async function exists(path: string): Promise<boolean> {
405
+ export async function exists(path: string): Promise<boolean> {
376
406
  return access(path).then(
377
407
  () => true,
378
408
  () => false,
@@ -8,6 +8,7 @@
8
8
 
9
9
  import { runCommand } from "@procedures/shell/command";
10
10
  import { GIT_MAX_BUFFER } from "./args.js";
11
+ import { currentGitLimits, limitedGitCommand } from "./limits.js";
11
12
  import { confinedGitCommand, currentGitRoot } from "./root.js";
12
13
 
13
14
  /** The part of the procedure context that the git procedures read. */
@@ -48,11 +49,14 @@ export class GitError extends Error {
48
49
  /**
49
50
  * This function runs `git <args>` and gives its stdout. It throws a `GitError` when git fails,
50
51
  * and an `AbortError` when the signal aborts. In the handler of a git procedure under a root
51
- * (`underGitRoot()`), git stays in the root: `confinedGitCommand()` of `root.ts`.
52
+ * (`underGitRoot()`), git stays in the root: `confinedGitCommand()` of `root.ts`. Under a capability
53
+ * without `process` (`underGitLimits()`), git starts no program of the repository: `limits.ts`.
52
54
  */
53
55
  export async function git(args: string[], options: GitRunOptions = {}): Promise<string> {
54
56
  const root = currentGitRoot();
55
- const command = root === undefined ? { args, cwd: options.cwd, env: options.env } : await confinedGitCommand(root, args, options.cwd, options.env);
57
+ const limits = currentGitLimits();
58
+ const confined = root === undefined ? { args, cwd: options.cwd, env: options.env } : await confinedGitCommand(root, args, options.cwd, options.env);
59
+ const command = limits === undefined ? confined : await limitedGitCommand(limits, confined, root !== undefined);
56
60
  const result = await runCommand("git", {
57
61
  args: command.args,
58
62
  cwd: command.cwd,