@osdlc/typed-git 0.5.0 → 0.5.2

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/verbs.js CHANGED
@@ -9,6 +9,8 @@
9
9
  *
10
10
  * {@link gate} is the single place a git result becomes either a value or a
11
11
  * `CommandFailed`. Both clients call it, so they cannot disagree.
12
+ *
13
+ * `GitVerb` and `gate` are internal: the package entries do not export them.
12
14
  */
13
15
  import { CommandFailed } from '@osdlc/command-seam';
14
16
  /**
@@ -31,9 +33,15 @@ import { CommandFailed } from '@osdlc/command-seam';
31
33
  * this table (`configGet`, `resolveRef`, `mergeBase`, `checkIgnore`,
32
34
  * `isAncestor`, `grep`, `revParse`). Read as an absence it would answer "no
33
35
  * such key / ref / match" for a git that never ran.
36
+ *
37
+ * A result flagged `outputIncomplete` is a failure too. The child exited, but
38
+ * the runner stopped reading at the output cap (`maxOutputBytes`) or at a
39
+ * timeout, so a parse would read cut-short output as the whole answer.
34
40
  */
35
41
  export function gate(verb, result, argv) {
36
- if (result.neverExited || (result.exitCode !== 0 && !verb.absent.includes(result.exitCode))) {
42
+ if (result.neverExited ||
43
+ result.outputIncomplete ||
44
+ (result.exitCode !== 0 && !verb.absent.includes(result.exitCode))) {
37
45
  throw new CommandFailed(argv ?? verb.argv, result);
38
46
  }
39
47
  return verb.parse(result, argv ?? verb.argv);
@@ -52,6 +60,17 @@ export function mergeEnv(base, call) {
52
60
  return base;
53
61
  return { ...base, ...call };
54
62
  }
63
+ /**
64
+ * Split a variadic `revParse` call into its string args and a trailing
65
+ * options object, when there is one. Shared by `Git` and `AsyncGit`.
66
+ */
67
+ export function splitTrailingOptions(args) {
68
+ const last = args[args.length - 1];
69
+ if (last !== undefined && typeof last !== 'string') {
70
+ return [args.slice(0, -1), last];
71
+ }
72
+ return [args, {}];
73
+ }
55
74
  /**
56
75
  * The `--git-dir=` / `--work-tree=` flags that bind an invocation to one
57
76
  * repository, or `[]` when neither is set. Shared by `Git` and `cliBackend`.
@@ -128,6 +147,52 @@ function nameStatusEntries(r) {
128
147
  function nothing() { }
129
148
  /** No exit code is an answer here — the gate throws on any non-zero exit. */
130
149
  const NONE = [];
150
+ /**
151
+ * Refuse a ref, remote, URL, path, pattern or config-key positional that
152
+ * starts with `-`.
153
+ *
154
+ * git reads such a value as an option. `--upload-pack=<cmd>` given as a remote
155
+ * runs `<cmd>`. The check throws a `TypeError` before anything spawns, so
156
+ * `try` and `soft` let it through. Write `./-name` for a path that starts with
157
+ * `-`, and `@{-1}` for the previous branch.
158
+ */
159
+ function positional(verb, param, value) {
160
+ if (value.startsWith('-')) {
161
+ throw new TypeError(`typed-git: ${verb}: ${param} must not start with "-" (got ${JSON.stringify(value)})`);
162
+ }
163
+ return value;
164
+ }
165
+ /**
166
+ * A config key for the `config` reads: {@link positional}, plus a section and
167
+ * a name.
168
+ *
169
+ * git exits 1 for a key with no `.`, or one that starts or ends with `.`. That
170
+ * is the code the reads take as "unset", so a typo would read as no value. The
171
+ * check throws a `TypeError` instead, before anything spawns.
172
+ */
173
+ function configKey(verb, key) {
174
+ positional(verb, 'key', key);
175
+ if (!key.includes('.') || key.startsWith('.') || key.endsWith('.')) {
176
+ throw new TypeError(`typed-git: ${verb}: key must be <section>.<name> (got ${JSON.stringify(key)})`);
177
+ }
178
+ return key;
179
+ }
180
+ /**
181
+ * A `--depth` value, refused with a `TypeError` unless it is a positive safe
182
+ * integer. git rejects the others too, but only after it runs.
183
+ */
184
+ function depthArg(verb, depth) {
185
+ if (!Number.isSafeInteger(depth) || depth < 1) {
186
+ throw new TypeError(`typed-git: ${verb}: depth must be a positive integer (got ${JSON.stringify(depth)})`);
187
+ }
188
+ return `--depth=${depth}`;
189
+ }
190
+ /** {@link positional}, for an optional value. */
191
+ function optionalPositional(verb, param, value) {
192
+ return value === undefined ? undefined : positional(verb, param, value);
193
+ }
194
+ /** The child env that makes git print English, whatever the caller's locale. */
195
+ const C_LOCALE = Object.freeze({ LC_ALL: 'C' });
131
196
  // ── queries ─────────────────────────────────────────────────────────────────
132
197
  /**
133
198
  * A file's content at a rev (`git show <rev>:<relPath>`), null if absent.
@@ -138,7 +203,7 @@ const NONE = [];
138
203
  */
139
204
  export function showAtRev(rev, relPath) {
140
205
  return {
141
- argv: ['show', `${rev}:${relPath}`],
206
+ argv: ['show', `${positional('showAtRev', 'rev', rev)}:${relPath}`],
142
207
  absent: [128],
143
208
  // NOT trimmedOrNull: file content is returned verbatim, trailing newline
144
209
  // included, because a caller round-tripping a blob must get its bytes.
@@ -173,7 +238,7 @@ export function revParse(...args) {
173
238
  */
174
239
  export function resolveRef(ref) {
175
240
  return {
176
- argv: ['rev-parse', '--verify', '--quiet', ref],
241
+ argv: ['rev-parse', '--verify', '--quiet', positional('resolveRef', 'ref', ref)],
177
242
  absent: [1],
178
243
  parse: trimmedOrNull,
179
244
  };
@@ -223,6 +288,19 @@ export function gitCommonDir() {
223
288
  parse: trimmedOrNull,
224
289
  };
225
290
  }
291
+ /**
292
+ * This worktree's own `.git` dir as an absolute path, or null outside a repo
293
+ * (`rev-parse --absolute-git-dir`).
294
+ *
295
+ * In a linked worktree it is `<common>/worktrees/<name>`, not the shared dir
296
+ * {@link gitCommonDir} gives. Per-worktree state lives here: a rebase in
297
+ * progress is a `rebase-merge` or `rebase-apply` directory in it.
298
+ *
299
+ * `absent: [128]`, as for {@link gitCommonDir}: "not a repository" is an answer.
300
+ */
301
+ export function gitDir() {
302
+ return { argv: ['rev-parse', '--absolute-git-dir'], absent: [128], parse: trimmedOrNull };
303
+ }
226
304
  /**
227
305
  * The full refname a symbolic ref points at, or null when it is not one.
228
306
  *
@@ -236,7 +314,11 @@ export function gitCommonDir() {
236
314
  * rather than taking the last segment: the name may contain slashes.
237
315
  */
238
316
  export function symbolicRef(ref) {
239
- return { argv: ['symbolic-ref', ref], absent: [128], parse: trimmedOrNull };
317
+ return {
318
+ argv: ['symbolic-ref', positional('symbolicRef', 'ref', ref)],
319
+ absent: [128],
320
+ parse: trimmedOrNull,
321
+ };
240
322
  }
241
323
  /**
242
324
  * Current branch shortname, or null when detached.
@@ -249,7 +331,13 @@ export function symbolicRef(ref) {
249
331
  export function currentBranch() {
250
332
  return { argv: ['branch', '--show-current'], absent: NONE, parse: trimmedOrNull };
251
333
  }
252
- /** `git status --porcelain` lines. See {@link StatusPorcelainOptions}. */
334
+ /**
335
+ * `git status --porcelain` lines; empty array on a clean tree, and a throw
336
+ * outside a repository (exit 128).
337
+ *
338
+ * Verbatim: the first two columns are the status, so ` M f.ts` (unstaged
339
+ * modification) must not be trimmed down to `M f.ts` (staged one).
340
+ */
253
341
  export function statusPorcelain(opts = {}) {
254
342
  const argv = ['status', '--porcelain'];
255
343
  if (opts.paths !== undefined && opts.paths.length > 0)
@@ -292,7 +380,7 @@ export function statusEntries() {
292
380
  export function branches(pattern) {
293
381
  const argv = ['branch', '--list', '--format=%(refname:short)'];
294
382
  if (pattern !== undefined)
295
- argv.push(pattern);
383
+ argv.push(positional('branches', 'pattern', pattern));
296
384
  return { argv, absent: NONE, parse: trimmedLines };
297
385
  }
298
386
  /** `git worktree list --porcelain`, parsed. Throws outside a repository. */
@@ -348,7 +436,11 @@ export function worktrees() {
348
436
  * (exit 1). A rev that does not resolve exits 128 and throws.
349
437
  */
350
438
  export function mergeBase(a, b) {
351
- return { argv: ['merge-base', a, b], absent: [1], parse: trimmedOrNull };
439
+ return {
440
+ argv: ['merge-base', positional('mergeBase', 'a', a), positional('mergeBase', 'b', b)],
441
+ absent: [1],
442
+ parse: trimmedOrNull,
443
+ };
352
444
  }
353
445
  /**
354
446
  * Subject line of a ref's HEAD commit (`log -1 --format=%s`), or null.
@@ -359,7 +451,7 @@ export function mergeBase(a, b) {
359
451
  */
360
452
  export function logSubject(ref) {
361
453
  return {
362
- argv: ['log', '-1', '--format=%s', ref],
454
+ argv: ['log', '-1', '--format=%s', positional('logSubject', 'ref', ref)],
363
455
  absent: [128],
364
456
  parse(r) {
365
457
  if (r.exitCode !== 0)
@@ -370,9 +462,63 @@ export function logSubject(ref) {
370
462
  },
371
463
  };
372
464
  }
465
+ /** The `config` read argv: the scope flag when one is given, the flags, the checked key. */
466
+ function configRead(flags, key, opts) {
467
+ const argv = ['config'];
468
+ if (opts.scope !== undefined)
469
+ argv.push(`--${opts.scope}`);
470
+ argv.push(...flags, key);
471
+ return argv;
472
+ }
373
473
  /** `git config --get <key>`, or null when unset (exit 1). */
374
- export function configGet(key) {
375
- return { argv: ['config', '--get', key], absent: [1], parse: trimmedOrNull };
474
+ export function configGet(key, opts = {}) {
475
+ return {
476
+ argv: configRead(['--get'], configKey('configGet', key), opts),
477
+ absent: [1],
478
+ parse: trimmedOrNull,
479
+ };
480
+ }
481
+ /**
482
+ * Every value of a multi-valued key (`git config --get-all -z <key>`), in the
483
+ * order git reads them. `[]` when the key is unset (exit 1).
484
+ *
485
+ * `-z` ends each value with NUL, so a value holding a newline stays whole and
486
+ * an empty value is kept as `""`. Values are verbatim, not trimmed.
487
+ */
488
+ export function configGetAll(key, opts = {}) {
489
+ return {
490
+ argv: configRead(['--get-all', '-z'], configKey('configGetAll', key), opts),
491
+ absent: [1],
492
+ parse(r) {
493
+ if (r.exitCode !== 0)
494
+ return [];
495
+ // Each value ends with NUL, so the last field is always the empty tail.
496
+ return r.stdout.split('\x00').slice(0, -1);
497
+ },
498
+ };
499
+ }
500
+ /**
501
+ * A key read as a boolean (`git config --type=bool --get <key>`), or null when
502
+ * unset (exit 1).
503
+ *
504
+ * git does the reading: `yes`, `on`, `1` and a bare key are `true`. A value
505
+ * git cannot read as a boolean exits 128 and throws.
506
+ */
507
+ export function configGetBool(key, opts = {}) {
508
+ return {
509
+ argv: configRead(['--type=bool', '--get'], configKey('configGetBool', key), opts),
510
+ absent: [1],
511
+ parse(r, ranArgv) {
512
+ if (r.exitCode !== 0)
513
+ return null;
514
+ const out = r.stdout.trim();
515
+ if (out === 'true')
516
+ return true;
517
+ if (out === 'false')
518
+ return false;
519
+ throw new CommandFailed(ranArgv, r);
520
+ },
521
+ };
376
522
  }
377
523
  /**
378
524
  * True iff `rev` resolves to an object (`cat-file -e`).
@@ -391,21 +537,26 @@ export function configGet(key) {
391
537
  */
392
538
  export function objectExists(rev) {
393
539
  return {
394
- argv: ['cat-file', '-e', rev],
540
+ argv: ['cat-file', '-e', positional('objectExists', 'rev', rev)],
395
541
  absent: [1, 128],
396
542
  parse: (r) => r.exitCode === 0,
397
543
  };
398
544
  }
399
- /** Refs on a remote, optionally pattern-filtered. See {@link LsRemoteOptions}. */
545
+ /**
546
+ * `ls-remote` rows for a remote (optionally pattern- and kind-filtered).
547
+ *
548
+ * `absent: []` — a remote with no matching refs answers with exit 0 and no
549
+ * output, so `[]` means "no refs". An unreachable remote exits 128 and throws.
550
+ */
400
551
  export function lsRemote(remote, pattern, opts = {}) {
401
552
  const argv = ['ls-remote'];
402
553
  if (opts.heads)
403
554
  argv.push('--heads');
404
555
  if (opts.tags)
405
556
  argv.push('--tags');
406
- argv.push(remote);
557
+ argv.push(positional('lsRemote', 'remote', remote));
407
558
  if (pattern !== undefined)
408
- argv.push(pattern);
559
+ argv.push(positional('lsRemote', 'pattern', pattern));
409
560
  return {
410
561
  argv,
411
562
  absent: NONE,
@@ -529,7 +680,9 @@ export function forEachRef(pattern, opts = {}) {
529
680
  argv.push(`--sort=${opts.sort}`);
530
681
  if (opts.count !== undefined)
531
682
  argv.push(`--count=${opts.count}`);
532
- argv.push(...(typeof pattern === 'string' ? [pattern] : pattern));
683
+ for (const p of typeof pattern === 'string' ? [pattern] : pattern) {
684
+ argv.push(positional('forEachRef', 'pattern', p));
685
+ }
533
686
  return { argv, absent: NONE, parse: lines };
534
687
  }
535
688
  /**
@@ -562,8 +715,8 @@ export function diffNameOnly(opts = {}) {
562
715
  argv.push('--cached');
563
716
  if (opts.diffFilter !== undefined)
564
717
  argv.push(`--diff-filter=${opts.diffFilter}`);
565
- if (opts.rangeArgs !== undefined)
566
- argv.push(...opts.rangeArgs);
718
+ for (const token of opts.rangeArgs ?? [])
719
+ argv.push(positional('diffNameOnly', 'rangeArgs', token));
567
720
  if (opts.paths !== undefined && opts.paths.length > 0)
568
721
  argv.push('--', ...opts.paths);
569
722
  return { argv, absent: NONE, parse: nulFields };
@@ -574,8 +727,8 @@ export function diffNameOnly(opts = {}) {
574
727
  */
575
728
  export function diffStat(opts = {}) {
576
729
  const argv = ['diff', '--stat'];
577
- if (opts.rangeArgs !== undefined)
578
- argv.push(...opts.rangeArgs);
730
+ for (const token of opts.rangeArgs ?? [])
731
+ argv.push(positional('diffStat', 'rangeArgs', token));
579
732
  if (opts.paths !== undefined && opts.paths.length > 0)
580
733
  argv.push('--', ...opts.paths);
581
734
  return { argv, absent: NONE, parse: (r) => r.stdout };
@@ -611,7 +764,7 @@ export function diffNameStatus(opts = {}) {
611
764
  if (opts.noRenames)
612
765
  argv.push('--no-renames');
613
766
  if (opts.range !== undefined)
614
- argv.push(opts.range);
767
+ argv.push(positional('diffNameStatus', 'range', opts.range));
615
768
  if (opts.paths !== undefined && opts.paths.length > 0)
616
769
  argv.push('--', ...opts.paths);
617
770
  return { argv, absent: NONE, parse: nameStatusEntries };
@@ -652,12 +805,12 @@ export function log(opts = {}) {
652
805
  if (opts.relative)
653
806
  argv.push('--relative');
654
807
  if (opts.rev !== undefined)
655
- argv.push(opts.rev);
808
+ argv.push(positional('log', 'rev', opts.rev));
656
809
  if (opts.range !== undefined)
657
- argv.push(opts.range);
810
+ argv.push(positional('log', 'range', opts.range));
658
811
  // `--not` negates every rev after it, so the positive revs must precede it.
659
812
  if (opts.notRefs !== undefined && opts.notRefs.length > 0) {
660
- argv.push('--not', ...opts.notRefs);
813
+ argv.push('--not', ...opts.notRefs.map((ref) => positional('log', 'notRefs', ref)));
661
814
  }
662
815
  if (opts.paths !== undefined && opts.paths.length > 0)
663
816
  argv.push('--', ...opts.paths);
@@ -678,7 +831,7 @@ export function lsTree(rev, opts = {}) {
678
831
  argv.push('-r');
679
832
  if (opts.long)
680
833
  argv.push('--long');
681
- argv.push(rev);
834
+ argv.push(positional('lsTree', 'rev', rev));
682
835
  if (opts.paths !== undefined && opts.paths.length > 0)
683
836
  argv.push('--', ...opts.paths);
684
837
  return {
@@ -714,43 +867,47 @@ export function lsTree(rev, opts = {}) {
714
867
  };
715
868
  }
716
869
  /**
717
- * The unified diff text itself, for the same rev/staged shape
718
- * {@link diffNameOnly} selects paths for. Empty string on failure or when
870
+ * The unified diff text (`git diff [--cached] [rev] [-- paths]`), for the same
871
+ * rev and staged shape {@link diffNameOnly} selects paths for. Empty when
719
872
  * nothing differs.
873
+ *
874
+ * `absent: []`. A rev that does not resolve exits 128, and outside a
875
+ * repository `git diff` exits 129. Both throw, so "git failed" never reads as
876
+ * "no diff". `git.try.diffText` reports them as `{ ok: false }`.
720
877
  */
721
878
  export function diffText(rev, opts = {}) {
722
879
  const argv = ['diff'];
723
880
  if (opts.staged === true)
724
881
  argv.push('--cached');
725
- if (rev !== undefined)
726
- argv.push(rev);
882
+ const from = optionalPositional('diffText', 'rev', rev);
883
+ if (from !== undefined)
884
+ argv.push(from);
727
885
  if (opts.paths !== undefined && opts.paths.length > 0)
728
886
  argv.push('--', ...opts.paths);
729
- // Any non-zero exit is reported as "no diff" rather than thrown: a bad rev
730
- // (128) and being outside a repository (129) both mean there is nothing to
731
- // judge, which is what the caller acts on either way.
732
- return { argv, absent: [1, 128, 129], parse: (r) => (r.exitCode === 0 ? r.stdout : '') };
887
+ return { argv, absent: NONE, parse: (r) => r.stdout };
733
888
  }
734
889
  /**
735
890
  * One untracked file rendered as an all-added diff
736
891
  * (`diff --no-index -- /dev/null <path>`), so a new file reaches a consumer in
737
- * the same shape as a modified one. Empty string when unreadable.
892
+ * the same shape as a modified one. An empty existing file still gives a diff
893
+ * header.
738
894
  *
739
895
  * `--no-index` exits 1 whenever the inputs differ, which for a real file is
740
- * always — so a non-zero exit here is the success case, and only empty output
741
- * means failure.
896
+ * always, so exit 1 is the success case. git also exits 1, with empty stdout,
897
+ * for a path it cannot read; that throws `CommandFailed`. Exit 2 (a usage or
898
+ * read error) throws too.
742
899
  */
743
900
  export function diffAddedFile(relPath) {
744
901
  return {
745
902
  argv: ['diff', '--no-index', '--', '/dev/null', relPath],
746
- // 1 is the SUCCESS case here — `--no-index` returns it whenever the inputs
747
- // differ, which for a real file is always. 2 is its usage/read error, and
748
- // yields the empty stdout the parser already treats as failure.
749
- absent: [1, 2],
750
- parse: (r) => r.stdout,
903
+ absent: [1],
904
+ parse(r, ranArgv) {
905
+ if (r.exitCode !== 0 && r.stdout === '')
906
+ throw new CommandFailed(ranArgv, r);
907
+ return r.stdout;
908
+ },
751
909
  };
752
910
  }
753
- // ── mutations (checked — the caller throws CommandFailed on non-zero) ────────
754
911
  /**
755
912
  * The paths and statuses one commit changed (`git diff-tree --name-status -z`).
756
913
  *
@@ -772,7 +929,7 @@ export function diffTree(rev, opts = {}) {
772
929
  argv.push('--no-renames');
773
930
  if (opts.findRenames)
774
931
  argv.push('-M');
775
- argv.push('--name-status', '-z', rev);
932
+ argv.push('--name-status', '-z', positional('diffTree', 'rev', rev));
776
933
  if (opts.paths !== undefined && opts.paths.length > 0)
777
934
  argv.push('--', ...opts.paths);
778
935
  return { argv, absent: NONE, parse: nameStatusEntries };
@@ -791,7 +948,7 @@ export function diffTreeNumstat(rev, opts = {}) {
791
948
  argv.push('--root');
792
949
  if (opts.noRenames)
793
950
  argv.push('--no-renames');
794
- argv.push('--numstat', '-z', rev);
951
+ argv.push('--numstat', '-z', positional('diffTreeNumstat', 'rev', rev));
795
952
  if (opts.paths !== undefined && opts.paths.length > 0)
796
953
  argv.push('--', ...opts.paths);
797
954
  return {
@@ -843,7 +1000,8 @@ export function grep(pattern, opts = {}) {
843
1000
  argv.push('-E');
844
1001
  if (opts.noColor)
845
1002
  argv.push('--no-color');
846
- argv.push(pattern);
1003
+ // `-e` marks the pattern as a pattern, so one that starts with `-` is searched for.
1004
+ argv.push('-e', pattern);
847
1005
  if (opts.paths !== undefined && opts.paths.length > 0)
848
1006
  argv.push('--', ...opts.paths);
849
1007
  return { argv, absent: [1], parse: lines };
@@ -859,7 +1017,12 @@ export function grep(pattern, opts = {}) {
859
1017
  */
860
1018
  export function isAncestor(a, b) {
861
1019
  return {
862
- argv: ['merge-base', '--is-ancestor', a, b],
1020
+ argv: [
1021
+ 'merge-base',
1022
+ '--is-ancestor',
1023
+ positional('isAncestor', 'a', a),
1024
+ positional('isAncestor', 'b', b),
1025
+ ],
863
1026
  absent: [1],
864
1027
  parse: (r) => r.exitCode === 0,
865
1028
  };
@@ -875,7 +1038,13 @@ export function isAncestor(a, b) {
875
1038
  * rev that does not resolve exits 128 and throws.
876
1039
  */
877
1040
  export function aheadBehind(base, tip) {
878
- const argv = ['rev-list', '--left-right', '--count', `${base}...${tip}`];
1041
+ positional('aheadBehind', 'tip', tip);
1042
+ const argv = [
1043
+ 'rev-list',
1044
+ '--left-right',
1045
+ '--count',
1046
+ `${positional('aheadBehind', 'base', base)}...${tip}`,
1047
+ ];
879
1048
  return {
880
1049
  argv,
881
1050
  absent: NONE,
@@ -891,7 +1060,7 @@ export function aheadBehind(base, tip) {
891
1060
  };
892
1061
  }
893
1062
  /**
894
- * Configured remote names (`git remote`).
1063
+ * Configured remote names (`git remote`). The clients call it as `remotes()`.
895
1064
  *
896
1065
  * `absent: []` — a repository with no remotes is exit 0 with empty stdout.
897
1066
  * Outside a repository git exits 128 and this throws.
@@ -901,13 +1070,60 @@ export function remote() {
901
1070
  }
902
1071
  /**
903
1072
  * A remote's fetch URL (`git remote get-url <name>`), or null when no remote
904
- * by that name is configured.
1073
+ * by that name is configured. With `push`, its push URL. When a remote has
1074
+ * several URLs this is the first; {@link remoteGetUrls} gives them all.
905
1075
  *
906
1076
  * `absent: [2]` — measured: git reports an unknown remote as exit 2, not 1 or
907
1077
  * 128. Outside a repository it exits 128 and this throws.
908
1078
  */
909
- export function remoteGetUrl(name) {
910
- return { argv: ['remote', 'get-url', name], absent: [2], parse: trimmedOrNull };
1079
+ export function remoteGetUrl(name, opts = {}) {
1080
+ const argv = ['remote', 'get-url'];
1081
+ if (opts.push)
1082
+ argv.push('--push');
1083
+ argv.push(positional('remoteGetUrl', 'name', name));
1084
+ return { argv, absent: [2], parse: trimmedOrNull };
1085
+ }
1086
+ /**
1087
+ * Every URL a remote has (`git remote get-url --all <name>`), or `[]` when no
1088
+ * remote by that name is configured (exit 2).
1089
+ *
1090
+ * A remote can carry several URLs (`remote set-url --add`). A caller that
1091
+ * checks a remote before deleting something compares all of them, not the
1092
+ * first.
1093
+ */
1094
+ export function remoteGetUrls(name, opts = {}) {
1095
+ const argv = ['remote', 'get-url', '--all'];
1096
+ if (opts.push)
1097
+ argv.push('--push');
1098
+ argv.push(positional('remoteGetUrls', 'name', name));
1099
+ return { argv, absent: [2], parse: trimmedLines };
1100
+ }
1101
+ /**
1102
+ * The branch a remote's HEAD points at, as a full refname such as
1103
+ * `refs/heads/main` (`git ls-remote --symref <remote> HEAD`).
1104
+ *
1105
+ * It asks the remote over the network. {@link symbolicRef} on
1106
+ * `refs/remotes/<remote>/HEAD` reads the local copy instead, which a clone
1107
+ * may never have set.
1108
+ *
1109
+ * `absent: []`. null means the remote answered but names no default branch:
1110
+ * an empty repository, or a detached HEAD. An unreachable remote exits 128
1111
+ * and throws.
1112
+ */
1113
+ export function remoteHead(remote) {
1114
+ return {
1115
+ argv: ['ls-remote', '--symref', positional('remoteHead', 'remote', remote), 'HEAD'],
1116
+ absent: NONE,
1117
+ parse(r) {
1118
+ // `ref: refs/heads/main<TAB>HEAD`, then the `<sha><TAB>HEAD` row.
1119
+ for (const line of splitLines(r.stdout)) {
1120
+ const m = /^ref: (\S+)\tHEAD$/.exec(line);
1121
+ if (m !== null)
1122
+ return m[1];
1123
+ }
1124
+ return null;
1125
+ },
1126
+ };
911
1127
  }
912
1128
  /**
913
1129
  * `git fetch <remote> [refspec…]`.
@@ -925,9 +1141,16 @@ export function fetch(remote, refspec, opts = {}) {
925
1141
  argv.push('--prune');
926
1142
  if (opts.noWriteFetchHead)
927
1143
  argv.push('--no-write-fetch-head');
928
- argv.push(remote);
929
- if (refspec !== undefined)
930
- argv.push(...(typeof refspec === 'string' ? [refspec] : refspec));
1144
+ if (opts.depth !== undefined)
1145
+ argv.push(depthArg('fetch', opts.depth));
1146
+ if (opts.noTags)
1147
+ argv.push('--no-tags');
1148
+ argv.push(positional('fetch', 'remote', remote));
1149
+ if (refspec !== undefined) {
1150
+ for (const spec of typeof refspec === 'string' ? [refspec] : refspec) {
1151
+ argv.push(positional('fetch', 'refspec', spec));
1152
+ }
1153
+ }
931
1154
  return { argv, absent: NONE, parse: nothing };
932
1155
  }
933
1156
  /** Stage paths. `--` precedes them, so a path starting with `-` is a path, not an option; flags go in `opts`. */
@@ -978,17 +1201,18 @@ export function configSet(key, value, opts = {}) {
978
1201
  const argv = ['config'];
979
1202
  if (opts.scope !== undefined)
980
1203
  argv.push(`--${opts.scope}`);
981
- argv.push(key, value);
1204
+ argv.push(positional('configSet', 'key', key), value);
982
1205
  return { argv, absent: NONE, parse: nothing };
983
1206
  }
984
1207
  /**
985
1208
  * `git config --remove-section <name>`: true when removed, false when there was
986
1209
  * no such section. `absent: [128]` also covers "not a repository", which git
987
- * spells the same way.
1210
+ * spells the same way, so `false` can also mean git could not ask. It is a
1211
+ * mutation, so it has no `soft` form.
988
1212
  */
989
1213
  export function configRemoveSection(name) {
990
1214
  return {
991
- argv: ['config', '--remove-section', name],
1215
+ argv: ['config', '--remove-section', positional('configRemoveSection', 'name', name)],
992
1216
  absent: [128],
993
1217
  parse: (r) => r.exitCode === 0,
994
1218
  };
@@ -1007,12 +1231,13 @@ export function init(path, opts = {}) {
1007
1231
  argv.push('--bare');
1008
1232
  if (opts.initialBranch !== undefined)
1009
1233
  argv.push('-b', opts.initialBranch);
1010
- if (path !== undefined)
1011
- argv.push(path);
1234
+ const target = optionalPositional('init', 'path', path);
1235
+ if (target !== undefined)
1236
+ argv.push(target);
1012
1237
  return { argv, absent: NONE, parse: nothing };
1013
1238
  }
1014
1239
  /**
1015
- * `git clone [--depth n] [--branch ref] [--bare] <url> [dest]`.
1240
+ * `git clone [--depth=n] [--branch ref] [--bare|--mirror] <url> [dest]`.
1016
1241
  *
1017
1242
  * The one verb in this table that does not run inside a repository — it
1018
1243
  * makes one. A client's `cwd` is where a relative `dest` resolves, and both
@@ -1024,13 +1249,20 @@ export function clone(url, dest, opts = {}) {
1024
1249
  argv.push('--quiet');
1025
1250
  if (opts.bare)
1026
1251
  argv.push('--bare');
1252
+ if (opts.mirror)
1253
+ argv.push('--mirror');
1254
+ if (opts.singleBranch)
1255
+ argv.push('--single-branch');
1256
+ if (opts.noTags)
1257
+ argv.push('--no-tags');
1027
1258
  if (opts.depth !== undefined)
1028
- argv.push('--depth', String(opts.depth));
1259
+ argv.push(depthArg('clone', opts.depth));
1029
1260
  if (opts.branch !== undefined)
1030
1261
  argv.push('--branch', opts.branch);
1031
- argv.push(url);
1032
- if (dest !== undefined)
1033
- argv.push(dest);
1262
+ argv.push(positional('clone', 'url', url));
1263
+ const target = optionalPositional('clone', 'dest', dest);
1264
+ if (target !== undefined)
1265
+ argv.push(target);
1034
1266
  return { argv, absent: NONE, parse: nothing };
1035
1267
  }
1036
1268
  /**
@@ -1052,11 +1284,11 @@ export function forceWithLeaseArg(lease) {
1052
1284
  /**
1053
1285
  * Push a refspec.
1054
1286
  *
1055
- * `forceWithLease` takes `true` for the bare `--force-with-lease`, or a
1056
- * {@link ForceLease} for the pinned form. Pin it whenever the expected remote
1057
- * sha is known: the bare flag trusts whatever the local remote-tracking ref
1058
- * happens to say, and a background fetch can quietly bring that up to date —
1059
- * after which the lease vetoes nothing and a concurrent push is erased.
1287
+ * `forceWithLease` takes a {@link ForceLease} for the pinned form, or `true`
1288
+ * for the bare `--force-with-lease`. `true` is the unsafe form. It trusts
1289
+ * whatever the local remote-tracking ref says, and a background fetch can
1290
+ * bring that up to date. The lease then vetoes nothing, and a concurrent push
1291
+ * is erased. Pin the lease whenever the expected remote sha is known.
1060
1292
  */
1061
1293
  export function push(remote, refspec, opts = {}) {
1062
1294
  const argv = ['push'];
@@ -1068,12 +1300,16 @@ export function push(remote, refspec, opts = {}) {
1068
1300
  else if (lease !== undefined && lease !== false) {
1069
1301
  throw new Error(`force-with-lease: unsupported value ${JSON.stringify(lease)}`);
1070
1302
  }
1071
- argv.push(remote, refspec);
1303
+ argv.push(positional('push', 'remote', remote), positional('push', 'refspec', refspec));
1072
1304
  return { argv, absent: NONE, parse: nothing };
1073
1305
  }
1074
1306
  /** `git reset --hard <rev>`. */
1075
1307
  export function resetHard(rev) {
1076
- return { argv: ['reset', '--hard', rev], absent: NONE, parse: nothing };
1308
+ return {
1309
+ argv: ['reset', '--hard', positional('resetHard', 'rev', rev)],
1310
+ absent: NONE,
1311
+ parse: nothing,
1312
+ };
1077
1313
  }
1078
1314
  /**
1079
1315
  * `git clean -f`: delete untracked files, and untracked directories with
@@ -1085,7 +1321,7 @@ export function clean(opts = {}) {
1085
1321
  /** Delete a local branch (`-d`, or `-D` with `{force: true}`). */
1086
1322
  export function deleteBranch(name, opts = {}) {
1087
1323
  return {
1088
- argv: ['branch', opts.force ? '-D' : '-d', name],
1324
+ argv: ['branch', opts.force ? '-D' : '-d', positional('deleteBranch', 'name', name)],
1089
1325
  absent: NONE,
1090
1326
  parse: nothing,
1091
1327
  };
@@ -1098,7 +1334,16 @@ export function deleteBranch(name, opts = {}) {
1098
1334
  * `branch.<name>.*` config section in place.
1099
1335
  */
1100
1336
  export function deleteRef(ref, opts) {
1101
- return { argv: ['update-ref', '-d', ref, opts.expected], absent: NONE, parse: nothing };
1337
+ return {
1338
+ argv: [
1339
+ 'update-ref',
1340
+ '-d',
1341
+ positional('deleteRef', 'ref', ref),
1342
+ positional('deleteRef', 'expected', opts.expected),
1343
+ ],
1344
+ absent: NONE,
1345
+ parse: nothing,
1346
+ };
1102
1347
  }
1103
1348
  /** `git worktree add [--detach] [-b branch] <path> [startPoint]`. */
1104
1349
  export function worktreeAdd(path, opts = {}) {
@@ -1107,9 +1352,10 @@ export function worktreeAdd(path, opts = {}) {
1107
1352
  argv.push('--detach');
1108
1353
  if (opts.branch !== undefined)
1109
1354
  argv.push('-b', opts.branch);
1110
- argv.push(path);
1111
- if (opts.startPoint !== undefined)
1112
- argv.push(opts.startPoint);
1355
+ argv.push(positional('worktreeAdd', 'path', path));
1356
+ const start = optionalPositional('worktreeAdd', 'startPoint', opts.startPoint);
1357
+ if (start !== undefined)
1358
+ argv.push(start);
1113
1359
  return { argv, absent: NONE, parse: nothing };
1114
1360
  }
1115
1361
  /** `git worktree remove [--force] <path>`. */
@@ -1117,7 +1363,7 @@ export function worktreeRemove(path, opts = {}) {
1117
1363
  const argv = ['worktree', 'remove'];
1118
1364
  if (opts.force)
1119
1365
  argv.push('--force');
1120
- argv.push(path);
1366
+ argv.push(positional('worktreeRemove', 'path', path));
1121
1367
  return { argv, absent: NONE, parse: nothing };
1122
1368
  }
1123
1369
  /** `git worktree move [--force] <from> <to>`. */
@@ -1125,7 +1371,7 @@ export function worktreeMove(from, to, opts = {}) {
1125
1371
  const argv = ['worktree', 'move'];
1126
1372
  if (opts.force)
1127
1373
  argv.push('--force');
1128
- argv.push(from, to);
1374
+ argv.push(positional('worktreeMove', 'from', from), positional('worktreeMove', 'to', to));
1129
1375
  return { argv, absent: NONE, parse: nothing };
1130
1376
  }
1131
1377
  /** `git worktree prune`. */
@@ -1134,7 +1380,16 @@ export function worktreePrune() {
1134
1380
  }
1135
1381
  /** `git remote add <name> <url>`. */
1136
1382
  export function remoteAdd(name, url) {
1137
- return { argv: ['remote', 'add', name, url], absent: NONE, parse: nothing };
1383
+ return {
1384
+ argv: [
1385
+ 'remote',
1386
+ 'add',
1387
+ positional('remoteAdd', 'name', name),
1388
+ positional('remoteAdd', 'url', url),
1389
+ ],
1390
+ absent: NONE,
1391
+ parse: nothing,
1392
+ };
1138
1393
  }
1139
1394
  /** `CONFLICT (content|add/add|…): Merge conflict in <path>` */
1140
1395
  const CONFLICT_MERGE_IN = /^CONFLICT \([^)]*\): Merge conflict in (.+)$/;
@@ -1182,9 +1437,10 @@ export function merge(ref, opts = {}) {
1182
1437
  argv.push('--ff-only');
1183
1438
  if (opts.message !== undefined)
1184
1439
  argv.push('-m', opts.message);
1185
- argv.push(ref);
1440
+ argv.push(positional('merge', 'ref', ref));
1186
1441
  const verb = {
1187
1442
  argv,
1443
+ env: C_LOCALE,
1188
1444
  absent: [1],
1189
1445
  parse(r, ranArgv) {
1190
1446
  if (r.exitCode !== 0)
@@ -1218,9 +1474,10 @@ export function rebase(onto, opts = {}) {
1218
1474
  const argv = ['rebase'];
1219
1475
  if (opts.autostash)
1220
1476
  argv.push('--autostash');
1221
- argv.push(onto);
1477
+ argv.push(positional('rebase', 'onto', onto));
1222
1478
  const verb = {
1223
1479
  argv,
1480
+ env: C_LOCALE,
1224
1481
  absent: [1],
1225
1482
  parse(r, ranArgv) {
1226
1483
  if (r.exitCode !== 0)
@@ -1250,6 +1507,7 @@ export function rebaseAbort() {
1250
1507
  export function rebaseContinue() {
1251
1508
  const verb = {
1252
1509
  argv: ['-c', 'core.editor=true', 'rebase', '--continue'],
1510
+ env: C_LOCALE,
1253
1511
  absent: [1],
1254
1512
  parse(r, ranArgv) {
1255
1513
  if (r.exitCode !== 0)
@@ -1290,7 +1548,7 @@ export function mergeTree(ours, theirs, opts = {}) {
1290
1548
  const argv = ['merge-tree', '--write-tree', '--name-only', '-z'];
1291
1549
  if (opts.mergeBase !== undefined)
1292
1550
  argv.push(`--merge-base=${opts.mergeBase}`);
1293
- argv.push(ours, theirs);
1551
+ argv.push(positional('mergeTree', 'ours', ours), positional('mergeTree', 'theirs', theirs));
1294
1552
  return {
1295
1553
  argv,
1296
1554
  absent: [1],
@@ -1353,7 +1611,8 @@ export function lsFilesUnmerged() {
1353
1611
  /**
1354
1612
  * `git checkout --ours|--theirs -- <paths>` — resolve paths to one side.
1355
1613
  *
1356
- * `absent: []` — a checkout that did not happen is not an answer.
1614
+ * `absent: []` — a checkout that did not happen is not an answer. Both
1615
+ * clients return without running git when `paths` is empty.
1357
1616
  *
1358
1617
  * Note the sharp edge git leaves here: on a path that is not conflicted this
1359
1618
  * exits 0 and does nothing. There is no error to catch, so a caller working
@@ -1380,6 +1639,6 @@ export function checkout(ref, opts = {}) {
1380
1639
  argv.push('--force');
1381
1640
  if (opts.branch !== undefined)
1382
1641
  argv.push(opts.resetBranch ? '-B' : '-b', opts.branch);
1383
- argv.push(ref);
1642
+ argv.push(positional('checkout', 'ref', ref));
1384
1643
  return { argv, absent: NONE, parse: nothing };
1385
1644
  }