@statewalker/vcs-commands 0.13.0 → 0.14.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.
Files changed (102) hide show
  1. package/dist/commands/archive/archive-command.d.ts +6 -2
  2. package/dist/commands/archive/archive-command.d.ts.map +1 -1
  3. package/dist/commands/archive/remote-archive-command.d.ts +23 -1
  4. package/dist/commands/archive/remote-archive-command.d.ts.map +1 -1
  5. package/dist/commands/archive/upload-archive-writer.d.ts +11 -5
  6. package/dist/commands/archive/upload-archive-writer.d.ts.map +1 -1
  7. package/dist/commands/bundle/bundle-format.d.ts +6 -1
  8. package/dist/commands/bundle/bundle-format.d.ts.map +1 -1
  9. package/dist/commands/bundle/bundle-uri-host.d.ts +12 -1
  10. package/dist/commands/bundle/bundle-uri-host.d.ts.map +1 -1
  11. package/dist/commands/bundle/bundle-uri.d.ts +7 -1
  12. package/dist/commands/bundle/bundle-uri.d.ts.map +1 -1
  13. package/dist/commands/bundle/bundle-verify.d.ts +4 -1
  14. package/dist/commands/bundle/bundle-verify.d.ts.map +1 -1
  15. package/dist/commands/cat-file/cat-file-command.d.ts.map +1 -1
  16. package/dist/commands/check-ignore-command.d.ts.map +1 -1
  17. package/dist/commands/clone-command.d.ts +15 -0
  18. package/dist/commands/clone-command.d.ts.map +1 -1
  19. package/dist/commands/commit-command.d.ts +10 -1
  20. package/dist/commands/commit-command.d.ts.map +1 -1
  21. package/dist/commands/default-branch.d.ts +17 -3
  22. package/dist/commands/default-branch.d.ts.map +1 -1
  23. package/dist/commands/fetch-command.d.ts +2 -1
  24. package/dist/commands/fetch-command.d.ts.map +1 -1
  25. package/dist/commands/fsck/fsck-object-checks.d.ts.map +1 -1
  26. package/dist/commands/git-hooks.d.ts +3 -3
  27. package/dist/commands/git-hooks.d.ts.map +1 -1
  28. package/dist/commands/index.js +6 -6
  29. package/dist/commands/init-command.d.ts.map +1 -1
  30. package/dist/commands/local-transport.d.ts +11 -1
  31. package/dist/commands/local-transport.d.ts.map +1 -1
  32. package/dist/commands/ls-tree-command.d.ts +10 -0
  33. package/dist/commands/ls-tree-command.d.ts.map +1 -1
  34. package/dist/commands/merge-command.d.ts +11 -1
  35. package/dist/commands/merge-command.d.ts.map +1 -1
  36. package/dist/commands/merge-support.d.ts +19 -2
  37. package/dist/commands/merge-support.d.ts.map +1 -1
  38. package/dist/commands/plumbing-input.d.ts.map +1 -1
  39. package/dist/commands/promisor-remote-reply.d.ts +31 -4
  40. package/dist/commands/promisor-remote-reply.d.ts.map +1 -1
  41. package/dist/commands/promisor-remote.d.ts +8 -1
  42. package/dist/commands/promisor-remote.d.ts.map +1 -1
  43. package/dist/commands/push-command.d.ts.map +1 -1
  44. package/dist/commands/ref-filter/index.d.ts +1 -0
  45. package/dist/commands/ref-filter/index.d.ts.map +1 -1
  46. package/dist/commands/ref-filter/peel-limit.d.ts +8 -0
  47. package/dist/commands/ref-filter/peel-limit.d.ts.map +1 -0
  48. package/dist/commands/ref-filter/ref-filter.d.ts.map +1 -1
  49. package/dist/commands/ref-filter/ref-format.d.ts.map +1 -1
  50. package/dist/commands/serve-repository.d.ts +4 -3
  51. package/dist/commands/serve-repository.d.ts.map +1 -1
  52. package/dist/commands/signing.d.ts +29 -2
  53. package/dist/commands/signing.d.ts.map +1 -1
  54. package/dist/commands/split-cmdline.d.ts +6 -0
  55. package/dist/commands/split-cmdline.d.ts.map +1 -0
  56. package/dist/{commands-CPceUCP2.js → commands-C9M78Chn.js} +1186 -1036
  57. package/dist/errors/commit-errors.d.ts +17 -1
  58. package/dist/errors/commit-errors.d.ts.map +1 -1
  59. package/dist/errors/index.js +18 -3
  60. package/dist/{fsck-tbrPZX2q.js → fsck-C4US3l0B.js} +2 -2
  61. package/dist/index.js +7 -7
  62. package/dist/{maintenance-run-command-UiSIaeP8.js → maintenance-run-command-CoJFKr2p.js} +84 -32
  63. package/dist/{remote-transport-C0OSJtor.js → remote-transport-C43eykfK.js} +51 -16
  64. package/dist/results/index.js +1 -1
  65. package/dist/{results-CkS1p1CH.js → results-B0CWLCAp.js} +371 -53
  66. package/dist/transport-command.d.ts +3 -0
  67. package/dist/transport-command.d.ts.map +1 -1
  68. package/package.json +21 -13
  69. package/src/commands/archive/archive-command.ts +7 -3
  70. package/src/commands/archive/remote-archive-command.ts +37 -3
  71. package/src/commands/archive/upload-archive-writer.ts +34 -22
  72. package/src/commands/bundle/bundle-format.ts +10 -2
  73. package/src/commands/bundle/bundle-uri-host.ts +71 -11
  74. package/src/commands/bundle/bundle-uri.ts +11 -4
  75. package/src/commands/bundle/bundle-verify.ts +4 -1
  76. package/src/commands/cat-file/cat-file-command.ts +1 -33
  77. package/src/commands/check-ignore-command.ts +60 -15
  78. package/src/commands/clone-command.ts +89 -11
  79. package/src/commands/commit-command.ts +42 -7
  80. package/src/commands/default-branch.ts +19 -5
  81. package/src/commands/fetch-command.ts +13 -2
  82. package/src/commands/fsck/fsck-object-checks.ts +3 -1
  83. package/src/commands/git-hooks.ts +11 -5
  84. package/src/commands/init-command.ts +24 -4
  85. package/src/commands/local-transport.ts +53 -9
  86. package/src/commands/ls-tree-command.ts +20 -12
  87. package/src/commands/merge-command.ts +32 -8
  88. package/src/commands/merge-support.ts +42 -4
  89. package/src/commands/plumbing-input.ts +3 -1
  90. package/src/commands/promisor-remote-reply.ts +338 -56
  91. package/src/commands/promisor-remote.ts +25 -3
  92. package/src/commands/push-command.ts +5 -1
  93. package/src/commands/read-tree-command.ts +2 -2
  94. package/src/commands/ref-filter/index.ts +1 -0
  95. package/src/commands/ref-filter/peel-limit.ts +7 -0
  96. package/src/commands/ref-filter/ref-filter.ts +9 -3
  97. package/src/commands/ref-filter/ref-format.ts +2 -1
  98. package/src/commands/serve-repository.ts +8 -3
  99. package/src/commands/signing.ts +93 -10
  100. package/src/commands/split-cmdline.ts +32 -0
  101. package/src/errors/commit-errors.ts +24 -2
  102. package/src/transport-command.ts +31 -1
@@ -46,6 +46,7 @@ import {
46
46
  import { GitApiError, InvalidConfigError } from "../errors/index.js";
47
47
  import { type ConfigValueWhere, RemoteConfigStore } from "../remote-config/index.js";
48
48
  import { hostEnvironmentOf } from "./git-ident.js";
49
+ import { splitCmdline } from "./split-cmdline.js";
49
50
 
50
51
  /**
51
52
  * A request to sign: the key to sign with (`""`: the default key, git's `-S` without a key
@@ -492,11 +493,83 @@ function sshPublicKeyBlob(text: string): Uint8Array | undefined {
492
493
  return field ? base64Bytes(field) : undefined;
493
494
  }
494
495
 
496
+ /**
497
+ * Where {@link signingKeyIdOf} runs `gpg.ssh.defaultKeyCommand` and reads a relative key path
498
+ * (a command's {@link HookSession}).
499
+ */
500
+ export interface SigningKeyHost {
501
+ /** Runs a program with its arguments in the directory git runs in (the worktree top) */
502
+ runShellCommand(
503
+ name: string,
504
+ command: string,
505
+ args: string[],
506
+ ): Promise<{ exitCode: number; stdout: string; stderr: string } | undefined>;
507
+ /** That directory on the host, where the repository's files have one */
508
+ hostWorkingDirectory(): Promise<string | undefined>;
509
+ }
510
+
511
+ /** How {@link signingKeyIdOf} finds the key. */
512
+ export interface SigningKeyIdOptions {
513
+ /** Runs `gpg.ssh.defaultKeyCommand`, resolves a relative key path; without it neither is */
514
+ host?: SigningKeyHost;
515
+ /**
516
+ * The git version followed: 2.43 (up to 2.51) keeps the space after an ssh fingerprint
517
+ * (`strbuf_split_max`), 2.52+ cuts the fingerprint at it. Default 2.56
518
+ */
519
+ compatibility?: "2.43" | "2.56";
520
+ }
521
+
522
+ /** gpg-interface.c's `is_literal_ssh_key`: the key itself rather than a file naming it. */
523
+ function isLiteralSshKey(key: string): boolean {
524
+ return key.startsWith("key::") || key.startsWith("ssh-");
525
+ }
526
+
527
+ /**
528
+ * gpg-interface.c's `get_default_ssh_signing_key` (as `get_signing_key_id` runs it): the
529
+ * first line of `command`'s output, newline kept, when it is a literal key. git goes on with
530
+ * a NULL key when it is not and crashes in `get_ssh_key_fingerprint` (SIGSEGV) after its
531
+ * warning: that is thrown with exit status 139.
532
+ */
533
+ async function defaultSshSigningKey(
534
+ command: string,
535
+ sign: string,
536
+ host: SigningKeyHost | undefined,
537
+ ): Promise<string> {
538
+ const argv = splitCmdline(command);
539
+ if (typeof argv === "string") {
540
+ throw new SigningFailedError(
541
+ `fatal: malformed build-time gpg.ssh.defaultKeyCommand: ${argv}`,
542
+ sign,
543
+ 128,
544
+ );
545
+ }
546
+ const [program = "", ...args] = argv;
547
+ const messages: string[] = [];
548
+ // pipe_command of an empty program: execvp("") fails, start_command names no program
549
+ let ran: { exitCode: number; stdout: string; stderr: string } | undefined;
550
+ if (program === "") messages.push("error: cannot run : No such file or directory");
551
+ else ran = await host?.runShellCommand("gpg.ssh.defaultKeyCommand", program, args);
552
+ const stdout = ran?.stdout ?? "";
553
+ const stderr = ran?.stderr ?? "";
554
+ if (ran?.exitCode === 0) {
555
+ const nl = stdout.indexOf("\n");
556
+ const first = nl < 0 ? stdout : stdout.slice(0, nl + 1);
557
+ if (first !== "" && isLiteralSshKey(first)) return first;
558
+ messages.push(
559
+ `warning: gpg.ssh.defaultKeyCommand succeeded but returned no keys: ${stderr} ${stdout}`,
560
+ );
561
+ } else messages.push(`warning: gpg.ssh.defaultKeyCommand failed: ${stderr} ${stdout}`);
562
+ throw new SigningFailedError(messages.join("\n"), sign, 139);
563
+ }
564
+
495
565
  /**
496
566
  * gpg-interface.c's `get_signing_key_id()` for `sign` (`""`: the default key): the key of an
497
567
  * openpgp or x509 signature (`user.signingKey`, else the committer as `Name <email>`), the
498
568
  * SHA-256 fingerprint of an ssh key (`ssh-keygen -lf`'s `SHA256:...`, for a literal key, a
499
- * public key file or an OpenSSH private key file).
569
+ * public key file or an OpenSSH private key file). Without a key, ssh's comes from
570
+ * `gpg.ssh.defaultKeyCommand` (`get_default_ssh_signing_key`), run through `options.host`. A
571
+ * key file is read as `ssh-keygen -lf <key>` reads it in the directory git runs in: a relative
572
+ * path from the worktree top, `~/` not expanded (git passes the key as configured).
500
573
  *
501
574
  * @throws SigningFailedError as git dies when it cannot fingerprint an ssh key (`fatal: failed
502
575
  * to get the ssh fingerprint for key '<key>'`), or has none
@@ -505,34 +578,44 @@ export async function signingKeyIdOf(
505
578
  workingCopy: WorkingCopy,
506
579
  sign: string,
507
580
  committer: Pick<PersonIdent, "name" | "email">,
581
+ options: SigningKeyIdOptions = {},
508
582
  ): Promise<string> {
509
583
  const config = signingConfigOf(workingCopy);
510
- const key = sign !== "" ? sign : config.signingKey;
584
+ let key = sign !== "" ? sign : config.signingKey;
511
585
  if (config.format !== "ssh") return key ?? `${committer.name} <${committer.email}>`;
512
586
  if (key === undefined) {
513
- throw new SigningFailedError(
514
- "fatal: either user.signingkey or gpg.ssh.defaultKeyCommand needs to be configured",
515
- sign,
516
- 128,
517
- );
587
+ if (config.defaultKeyCommand === undefined) {
588
+ throw new SigningFailedError(
589
+ "fatal: either user.signingkey or gpg.ssh.defaultKeyCommand needs to be configured",
590
+ sign,
591
+ 128,
592
+ );
593
+ }
594
+ key = await defaultSshSigningKey(config.defaultKeyCommand, sign, options.host);
518
595
  }
519
596
  let text: string | undefined;
520
597
  if (key.startsWith("key::")) text = key.slice(5);
521
598
  else if (key.startsWith("ssh-")) text = key;
522
599
  else {
523
600
  const environment = hostEnvironmentOf(workingCopy);
524
- const path = expandUserPath(key, environment) ?? key;
601
+ let path = key;
602
+ if (!path.startsWith("/")) {
603
+ const cwd = await options.host?.hostWorkingDirectory().catch(() => undefined);
604
+ if (cwd !== undefined) path = `${cwd.replace(/\/+$/, "")}/${path}`;
605
+ }
525
606
  const bytes = await environment?.readFile(path).catch(() => undefined);
526
607
  text = bytes ? new TextDecoder().decode(bytes) : undefined;
527
608
  }
528
609
  const blob = text === undefined ? undefined : sshPublicKeyBlob(text);
529
610
  if (!blob) {
611
+ // die_errno after ssh-keygen failed: errno is still 0
530
612
  throw new SigningFailedError(
531
- `fatal: failed to get the ssh fingerprint for key '${key}'`,
613
+ `fatal: failed to get the ssh fingerprint for key '${key}': Success`,
532
614
  sign,
533
615
  128,
534
616
  );
535
617
  }
536
618
  const digest = createSha256().update(blob).digest();
537
- return `SHA256:${btoa(String.fromCharCode(...digest)).replace(/=+$/, "")}`;
619
+ const fingerprint = `SHA256:${btoa(String.fromCharCode(...digest)).replace(/=+$/, "")}`;
620
+ return options.compatibility === "2.43" ? `${fingerprint} ` : fingerprint;
538
621
  }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * alias.c's split_cmdline: words split on whitespace, quotes removed, a backslash escaping
3
+ * the next character outside single quotes; split_cmdline_strerror's text on an error.
4
+ */
5
+ export function splitCmdline(line: string): string[] | string {
6
+ // one word to begin with; every whitespace run ends one and starts the next (a trailing
7
+ // run leaves an empty last word, as git's)
8
+ const args: string[] = [];
9
+ let current = "";
10
+ let quoted = "";
11
+ for (let i = 0; i < line.length; i++) {
12
+ const c = line[i] as string;
13
+ if (!quoted && /[ \t\n\r]/.test(c)) {
14
+ args.push(current);
15
+ current = "";
16
+ while (i + 1 < line.length && /[ \t\n\r]/.test(line[i + 1] as string)) i++;
17
+ } else if (!quoted && (c === "'" || c === '"')) {
18
+ quoted = c;
19
+ } else if (c === quoted) {
20
+ quoted = "";
21
+ } else {
22
+ if (c === "\\" && quoted !== "'") {
23
+ i++;
24
+ if (i >= line.length) return "cmdline ends with \\";
25
+ }
26
+ current += line[i];
27
+ }
28
+ }
29
+ if (quoted) return "unclosed quote";
30
+ args.push(current);
31
+ return args;
32
+ }
@@ -12,15 +12,37 @@ export class NoMessageError extends GitApiError {
12
12
  }
13
13
  }
14
14
 
15
+ /**
16
+ * builtin/commit.c's `empty_amend_advice`: what `git commit --amend` prints on stderr when
17
+ * the amended commit would have its first parent's tree (the same text in 2.43 and 2.5x).
18
+ */
19
+ export const EMPTY_AMEND_ADVICE =
20
+ "You asked to amend the most recent commit, but doing so would make\n" +
21
+ "it empty. You can repeat your command with --allow-empty, or you can\n" +
22
+ 'remove the commit entirely with "git reset HEAD^".\n';
23
+
15
24
  /**
16
25
  * Thrown when trying to create an empty commit without allow-empty.
17
26
  *
27
+ * git's prepare_to_commit refuses it with exit status 1, printing the status of what would
28
+ * be committed on stdout ({@link stdout}, `git commit --dry-run`'s text) and, when amending,
29
+ * {@link EMPTY_AMEND_ADVICE} on stderr ({@link stderr}, also the message then).
30
+ *
18
31
  * Based on JGit's EmptyCommitException.
19
32
  */
20
33
  export class EmptyCommitError extends GitApiError {
21
- constructor(message?: string) {
22
- super(message ?? "Nothing to commit", { exitCode: 1 });
34
+ /** git's stdout: the status run_status prints before the refusal (when computed) */
35
+ readonly stdout?: string;
36
+ /** git's stderr: the amend advice, or nothing */
37
+ readonly stderr: string;
38
+
39
+ constructor(message?: string, output?: { stdout?: string; stderr?: string }) {
40
+ super(message ?? (output?.stderr ? output.stderr.replace(/\n$/, "") : "Nothing to commit"), {
41
+ exitCode: 1,
42
+ });
23
43
  this.name = "EmptyCommitError";
44
+ this.stdout = output?.stdout;
45
+ this.stderr = output?.stderr ?? "";
24
46
  }
25
47
  }
26
48
 
@@ -6,6 +6,7 @@ import {
6
6
  type HttpCredentialHelper,
7
7
  type ProgressInfo,
8
8
  } from "@statewalker/vcs-transport";
9
+ import type { ObjectFormat } from "@statewalker/vcs-utils/hash";
9
10
  import type { WorkingCopy } from "@statewalker/vcs-working-tree";
10
11
  import {
11
12
  type BundleUriHost,
@@ -85,13 +86,15 @@ export abstract class TransportCommand<T> extends GitCommand<T> {
85
86
  constructor(workingCopy: WorkingCopy) {
86
87
  super(workingCopy);
87
88
  // a partial clone's lazy fetch during the call (a clone's checkout, a pull's merge) reaches
88
- // the promisor remote as this command does: its transports, credentials, headers, timeout;
89
+ // the promisor remote as this command does: its transports, credentials, headers, timeout,
90
+ // credential helper and warning sink;
89
91
  // the store's previous fetcher comes back when the call ends
90
92
  this.aroundEntryPoints(() => {
91
93
  if (
92
94
  !this.transportRegistry &&
93
95
  !this.credentials &&
94
96
  !this.headers &&
97
+ !this.credentialHelper &&
95
98
  this.timeout === undefined
96
99
  ) {
97
100
  return undefined;
@@ -102,6 +105,10 @@ export abstract class TransportCommand<T> extends GitCommand<T> {
102
105
  const fetcher = createPromisorFetcher(this._workingCopy, {
103
106
  ...this.promisorFetcherAuth(),
104
107
  ...(this.timeout !== undefined ? { timeout: this.timeout } : {}),
108
+ // git's child fetch reads credential.helper: the helper is asked per URL, so the
109
+ // guarded commands hand it over too
110
+ ...(this.credentialHelper ? { credentialHelper: this.credentialHelper } : {}),
111
+ ...(this.warningCallback ? { warningCallback: this.warningCallback } : {}),
105
112
  });
106
113
  promisor.setFetcher(fetcher);
107
114
  return () => {
@@ -398,10 +405,33 @@ export abstract class TransportCommand<T> extends GitCommand<T> {
398
405
  message(line: string): void;
399
406
  setConfigValue?(key: string, value: string): Promise<void>;
400
407
  tempFileName?(): string;
408
+ /** The hash bundle headers are read in (default the repository's) */
409
+ objectFormat?: ObjectFormat;
401
410
  }): BundleUriHost {
402
411
  const store = RemoteConfigStore.from(this._workingCopy);
403
412
  const worktree = repositoryPlaceOf(this._workingCopy);
413
+ const environment = hostEnvironmentOf(this._workingCopy);
404
414
  return createBundleUriHost({
415
+ ...(options.objectFormat ? { objectFormat: options.objectFormat } : {}),
416
+ // git 2.46+'s unbundle_from_file: VERIFY_BUNDLE_FSCK under fetch_pack_fsck_objects, an
417
+ // index-pack --fsck-objects given no message types (fetch.fsck.* is not read)
418
+ ...(options.compatibility !== "2.43"
419
+ ? {
420
+ fsckObjects: async () => {
421
+ const { fetchFsckOf } = await import("./commands/fsck/index.js");
422
+ return fetchFsckOf({
423
+ value: (key) => (/^fetch\.fsck\./i.test(key) ? undefined : store.value(key)),
424
+ compatibility: options.compatibility,
425
+ ...(environment
426
+ ? {
427
+ readFile: (path: string) => environment.readFile(path),
428
+ home: environment.home,
429
+ }
430
+ : {}),
431
+ });
432
+ },
433
+ }
434
+ : {}),
405
435
  workingCopy: this._workingCopy,
406
436
  setRef: (name, id, log) => this.refsStore.set(name, id, log),
407
437
  getRef: async (name) => (await this.refsStore.resolve(name).catch(() => undefined))?.objectId,