@osdlc/typed-git 0.10.0 → 0.12.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.
package/dist/verbs.d.ts CHANGED
@@ -13,6 +13,7 @@
13
13
  * `GitVerb` and `gate` are internal: the package entries do not export them.
14
14
  */
15
15
  import { type CommandResult } from '@osdlc/command-seam';
16
+ import type { GitFeature } from './features.ts';
16
17
  /**
17
18
  * One git invocation: what to run, which exits are answers, and how to read
18
19
  * what came back.
@@ -46,6 +47,20 @@ export interface GitVerb<T> {
46
47
  * so every failure names the command that ran.
47
48
  */
48
49
  parse(result: CommandResult, argv: string[]): T;
50
+ /**
51
+ * The git feature this argv needs, from the table in `./features.ts`. The
52
+ * client probes the git version (once per runner) before it runs a verb
53
+ * that sets this, and runs {@link GitVerb.fallback} or throws the
54
+ * `unsupported-git` failure when the git is too old. A verb without it
55
+ * never probes.
56
+ */
57
+ needs?: GitFeature | undefined;
58
+ /**
59
+ * The verb to run instead on a git older than `needs` requires. It is used
60
+ * only when the feature's table entry has a `fallback`, so a verb cannot
61
+ * fall back where the table says no fallback exists.
62
+ */
63
+ fallback?: GitVerb<T> | undefined;
49
64
  }
50
65
  /**
51
66
  * Turn one git result into a value or a thrown failure: a `FailureError`
@@ -111,7 +126,9 @@ export interface Worktree {
111
126
  locked: boolean;
112
127
  /**
113
128
  * The reason given to `git worktree lock --reason`, or null when none was.
114
- * Verbatim porcelain: git C-quotes a reason holding a newline or other
129
+ * On git 2.36 or later it is the reason as written, newlines and all (read
130
+ * from `worktree list --porcelain -z`). On older git it is the newline
131
+ * porcelain's text: git C-quotes a reason holding a newline or other
115
132
  * special characters, and the quotes stay.
116
133
  */
117
134
  lockReason: string | null;
@@ -289,7 +306,14 @@ export declare function statusEntries(): GitVerb<StatusEntry[]>;
289
306
  * `heads/<name>` when a tag shares the name.
290
307
  */
291
308
  export declare function branches(pattern?: string | undefined): GitVerb<string[]>;
292
- /** `git worktree list --porcelain`, parsed. Throws outside a repository. */
309
+ /**
310
+ * `git worktree list --porcelain -z`, parsed. Throws outside a repository.
311
+ *
312
+ * `-z` (git 2.36) ends each line with NUL, so a path or lock reason holding
313
+ * a newline reads as written. On an older git the client runs the
314
+ * newline-split form instead (the `worktree list -z` row in `./features.ts`),
315
+ * which breaks on such a path and C-quotes such a reason.
316
+ */
293
317
  export declare function worktrees(): GitVerb<Worktree[]>;
294
318
  /**
295
319
  * Merge base of two revs, or null when the two histories are unrelated
@@ -771,6 +795,39 @@ export interface AheadBehind {
771
795
  /** Commits on `base` that are not on `tip`. */
772
796
  behind: number;
773
797
  }
798
+ /** One line of `git cherry`: a commit on the head side and whether the upstream has its patch. */
799
+ export interface CherryEntry {
800
+ /** The full sha of the commit that is on the head side only. */
801
+ sha: string;
802
+ /** True for `-`: a commit on the upstream side has the same patch id. False for `+`. */
803
+ equivalent: boolean;
804
+ }
805
+ /**
806
+ * Which commits `head` has that `upstream` lacks, and for each one whether
807
+ * `upstream` carries a commit with the same patch id (`git cherry`). Oldest
808
+ * first. Merge commits are not listed.
809
+ *
810
+ * `absent: []`: an unresolvable rev exits 128 and throws. Empty output is a
811
+ * real answer (`head` has nothing `upstream` lacks), so a caller that needs at
812
+ * least one commit checks the length. A line that is not `+ <sha>` or
813
+ * `- <sha>` throws rather than reading as an entry or being skipped.
814
+ */
815
+ export declare function cherry(upstream: string, head?: string): GitVerb<CherryEntry[]>;
816
+ /** Options for {@link commitTree}. */
817
+ export interface CommitTreeOptions {
818
+ /** Parent commits, in order (`-p`). None makes a root commit. */
819
+ parents?: string[] | undefined;
820
+ /** The commit message (`-m`). */
821
+ message: string;
822
+ }
823
+ /**
824
+ * Write a commit object for `tree` and print its sha (`git commit-tree`).
825
+ *
826
+ * It writes an object and no ref, and leaves the index and working tree alone.
827
+ * git needs an author and committer identity; without one it exits 128 and
828
+ * this throws. `absent: []`, and a reply that is not a sha throws.
829
+ */
830
+ export declare function commitTree(tree: string, opts: CommitTreeOptions): GitVerb<string>;
774
831
  /**
775
832
  * How far `tip` has diverged from `base`
776
833
  * (`git rev-list --left-right --count base...tip`).
@@ -1026,33 +1083,21 @@ export interface ForceLease {
1026
1083
  export declare function forceWithLeaseArg(lease: ForceLease): string;
1027
1084
  /** Options for {@link push}. */
1028
1085
  export interface PushOptions {
1029
- /** `--force-with-lease`: `true` for the bare flag, a {@link ForceLease} to pin it. */
1030
- forceWithLease?: boolean | ForceLease | undefined;
1031
1086
  /**
1032
- * `--delete` — delete `refspec` on the remote rather than pushing to it.
1033
- * Combines with `forceWithLease`, so a pinned lease deletes the remote ref
1034
- * only while it still holds the expected sha.
1087
+ * `--force-with-lease`: a {@link ForceLease} to pin it, one lease per ref
1088
+ * for a multi-ref push, or `true` for the bare flag. `true` is the unsafe
1089
+ * form. It trusts whatever the local remote-tracking ref says, and a
1090
+ * background fetch can bring that up to date. The lease then blocks
1091
+ * nothing, and a concurrent push is overwritten. Pin the lease whenever the
1092
+ * expected remote sha is known. An empty list throws a `TypeError`.
1035
1093
  */
1036
- delete?: boolean | undefined;
1037
- }
1038
- /**
1039
- * Push a refspec, or delete it on the remote with `{ delete: true }`.
1040
- *
1041
- * `forceWithLease` takes a {@link ForceLease} for the pinned form, or `true`
1042
- * for the bare `--force-with-lease`. `true` is the unsafe form. It trusts
1043
- * whatever the local remote-tracking ref says, and a background fetch can
1044
- * bring that up to date. The lease then blocks nothing, and a concurrent push
1045
- * is overwritten. Pin the lease whenever the expected remote sha is known.
1046
- */
1047
- export declare function push(remote: string, refspec: string, opts?: PushOptions): GitVerb<void>;
1048
- /** Options for {@link pushPorcelain}. */
1049
- export interface PushPorcelainOptions {
1094
+ forceWithLease?: boolean | ForceLease | readonly ForceLease[] | undefined;
1050
1095
  /**
1051
- * `--force-with-lease`: `true` for the bare form, a {@link ForceLease}, or
1052
- * one lease per ref for a multi-ref push. See {@link push} for why `true` is
1053
- * the unsafe form. An empty list throws a `TypeError`.
1096
+ * `--delete` — delete each `refspec` on the remote rather than pushing to
1097
+ * it. Combines with `forceWithLease`, so a pinned lease deletes the remote
1098
+ * ref only while it still holds the expected sha.
1054
1099
  */
1055
- forceWithLease?: boolean | ForceLease | ForceLease[] | undefined;
1100
+ delete?: boolean | undefined;
1056
1101
  /** `--atomic` — the remote updates every ref or none. */
1057
1102
  atomic?: boolean | undefined;
1058
1103
  }
@@ -1077,13 +1122,37 @@ export type PushFlag = ' ' | '+' | '-' | '*' | '!' | '=';
1077
1122
  * - `lock-failed`: another writer moved or deleted the ref at the remote
1078
1123
  * after the lease check passed.
1079
1124
  * - `hook-declined`: a remote hook refused the ref.
1125
+ * - `atomic-failed`: another ref of an `--atomic` push failed, so this one
1126
+ * was not updated. The other ref carries the cause.
1080
1127
  * - `other`: anything else, kept in `rawReason` and `detail`. This includes
1081
- * a lock file in the way, an unwritable refs directory, the collateral refs
1082
- * of a failed `--atomic` push, and git's `new shallow roots not allowed`,
1083
- * `remote does not support deleting refs` and `remote failed to report
1084
- * status`.
1128
+ * a lock file in the way, an unwritable refs directory, and git's `new
1129
+ * shallow roots not allowed`, `remote does not support deleting refs` and
1130
+ * `remote failed to report status`.
1131
+ *
1132
+ * {@link PUSH_REJECTIONS} explains each one and links git's documentation.
1133
+ */
1134
+ export type PushRejection = 'stale' | 'non-fast-forward' | 'tag-exists' | 'already-exists' | 'ref-conflict' | 'lock-failed' | 'hook-declined' | 'atomic-failed' | 'other';
1135
+ /** One entry of {@link PUSH_REJECTIONS}. */
1136
+ export interface PushRejectionInfo {
1137
+ /** One line: what happened. */
1138
+ readonly message: string;
1139
+ /** Why git refuses it, and how git words it by version. */
1140
+ readonly explanation: string;
1141
+ /** What to do next. */
1142
+ readonly hint: string;
1143
+ /** The section of git's documentation that covers it. */
1144
+ readonly docs: string;
1145
+ }
1146
+ /**
1147
+ * A reference entry for every {@link PushRejection}: a one-line `message`, a
1148
+ * longer `explanation`, a `hint` and a link to git's documentation.
1149
+ *
1150
+ * ```ts
1151
+ * const info = PUSH_REJECTIONS[ref.rejection]
1152
+ * console.error(`${ref.dst}: ${info.message} ${info.hint} (${info.docs})`)
1153
+ * ```
1085
1154
  */
1086
- export type PushRejection = 'stale' | 'non-fast-forward' | 'tag-exists' | 'already-exists' | 'ref-conflict' | 'lock-failed' | 'hook-declined' | 'other';
1155
+ export declare const PUSH_REJECTIONS: Readonly<Record<PushRejection, PushRejectionInfo>>;
1087
1156
  /** The fields every ref line of `git push --porcelain` has. */
1088
1157
  interface PushRefFields {
1089
1158
  /** The local side as git printed it: a ref, `HEAD`, a sha, or `''` for a delete. */
@@ -1116,7 +1185,7 @@ export type PushRefResult = (PushRefFields & {
1116
1185
  rejection?: never;
1117
1186
  detail?: never;
1118
1187
  });
1119
- /** What {@link pushPorcelain} returns. */
1188
+ /** What {@link push} returns. */
1120
1189
  export interface PushReport {
1121
1190
  /** One entry per ref line. A remote with several push URLs repeats its refs. */
1122
1191
  refs: PushRefResult[];
@@ -1124,21 +1193,22 @@ export interface PushReport {
1124
1193
  stderr: string;
1125
1194
  }
1126
1195
  /**
1127
- * `git push --porcelain [--atomic] [--force-with-lease…] <remote> <refspec…>`,
1128
- * read into one result per ref.
1196
+ * `git push --porcelain [--delete] [--atomic] [--force-with-lease…] <remote>
1197
+ * <refspec…>`, read into one result per ref. `{ delete: true }` deletes each
1198
+ * refspec on the remote instead.
1129
1199
  *
1130
- * A rejected ref is a value, not a throw. git exits 1 when any ref is
1131
- * rejected, so exit 1 is declared absent, and the parse throws unless a ref
1132
- * line says `!`. A local pre-push hook decline and a refspec that matches
1133
- * nothing exit 1 with no ref lines, so they throw. So does every transport
1134
- * failure (exit 128: an unknown remote, auth, network, TLS), as the
1135
- * classified failure.
1200
+ * A rejected ref is a value, not a throw: check each ref's `flag`, or
1201
+ * whether any is `'!'`. git exits 1 when any ref is rejected, so exit 1 is
1202
+ * declared absent, and the parse throws unless a ref line says `!`. A local
1203
+ * pre-push hook decline and a refspec that matches nothing exit 1 with no
1204
+ * ref lines, so they throw. So does every transport failure (exit 128: an
1205
+ * unknown remote, auth, network, TLS), as the classified failure.
1136
1206
  *
1137
1207
  * Runs with `LC_ALL=C`, so the caller's locale does not change the reason
1138
1208
  * text. An empty refspec list throws a `TypeError`: git would push its
1139
1209
  * default set.
1140
1210
  */
1141
- export declare function pushPorcelain(remote: string, refspec: string | string[], opts?: PushPorcelainOptions): GitVerb<PushReport>;
1211
+ export declare function push(remote: string, refspec: string | readonly string[], opts?: PushOptions): GitVerb<PushReport>;
1142
1212
  /** `git reset --hard <rev>`. */
1143
1213
  export declare function resetHard(rev: string): GitVerb<void>;
1144
1214
  /** Options for {@link clean}. */
@@ -1391,6 +1461,10 @@ export interface MergeTreeOptions {
1391
1461
  *
1392
1462
  * A name git cannot merge exits 1 exactly like a conflict, so again only
1393
1463
  * stdout separates an answer from a refusal.
1464
+ *
1465
+ * Needs git 2.38, or 2.40 with `mergeBase` (the `merge-tree` rows in
1466
+ * `./features.ts`). There is no fallback: on an older git it throws the
1467
+ * `unsupported-git` failure without running git.
1394
1468
  */
1395
1469
  export declare function mergeTree(ours: string, theirs: string, opts?: MergeTreeOptions): GitVerb<MergeTreeOutcome>;
1396
1470
  /** One `git ls-files -u` row — the same path appears once per present stage. */
package/dist/verbs.js CHANGED
@@ -410,54 +410,69 @@ export function branches(pattern) {
410
410
  argv.push(positional('branches', 'pattern', pattern));
411
411
  return { argv, absent: NONE, parse: trimmedLines };
412
412
  }
413
- /** `git worktree list --porcelain`, parsed. Throws outside a repository. */
413
+ /**
414
+ * `git worktree list --porcelain -z`, parsed. Throws outside a repository.
415
+ *
416
+ * `-z` (git 2.36) ends each line with NUL, so a path or lock reason holding
417
+ * a newline reads as written. On an older git the client runs the
418
+ * newline-split form instead (the `worktree list -z` row in `./features.ts`),
419
+ * which breaks on such a path and C-quotes such a reason.
420
+ */
414
421
  export function worktrees() {
415
422
  return {
416
- argv: ['worktree', 'list', '--porcelain'],
423
+ argv: ['worktree', 'list', '--porcelain', '-z'],
417
424
  absent: NONE,
418
- parse(r) {
419
- const out = [];
420
- let cur = null;
421
- const flush = () => {
422
- if (cur?.path !== undefined) {
423
- out.push({
424
- path: cur.path,
425
- head: cur.head ?? null,
426
- branch: cur.branch ?? null,
427
- detached: cur.detached ?? false,
428
- bare: cur.bare ?? false,
429
- locked: cur.locked ?? false,
430
- lockReason: cur.lockReason ?? null,
431
- });
432
- }
433
- };
434
- for (const line of r.stdout.split('\n')) {
435
- if (line.startsWith('worktree ')) {
436
- flush();
437
- cur = { path: line.slice('worktree '.length) };
438
- }
439
- else if (cur !== null && line.startsWith('HEAD ')) {
440
- cur.head = line.slice('HEAD '.length);
441
- }
442
- else if (cur !== null && line.startsWith('branch ')) {
443
- cur.branch = line.slice('branch '.length).replace(/^refs\/heads\//, '');
444
- }
445
- else if (cur !== null && line === 'detached') {
446
- cur.detached = true;
447
- }
448
- else if (cur !== null && line === 'bare') {
449
- cur.bare = true;
450
- }
451
- else if (cur !== null && (line === 'locked' || line.startsWith('locked '))) {
452
- cur.locked = true;
453
- cur.lockReason = line.slice('locked '.length) || null;
454
- }
455
- }
456
- flush();
457
- return out;
425
+ parse: (r) => worktreeRecords(r.stdout.split('\x00')),
426
+ needs: 'worktree list -z',
427
+ fallback: {
428
+ argv: ['worktree', 'list', '--porcelain'],
429
+ absent: NONE,
430
+ parse: (r) => worktreeRecords(r.stdout.split('\n')),
458
431
  },
459
432
  };
460
433
  }
434
+ /** `worktree list --porcelain` lines, however they were split, into rows. */
435
+ function worktreeRecords(lines) {
436
+ const out = [];
437
+ let cur = null;
438
+ const flush = () => {
439
+ if (cur?.path !== undefined) {
440
+ out.push({
441
+ path: cur.path,
442
+ head: cur.head ?? null,
443
+ branch: cur.branch ?? null,
444
+ detached: cur.detached ?? false,
445
+ bare: cur.bare ?? false,
446
+ locked: cur.locked ?? false,
447
+ lockReason: cur.lockReason ?? null,
448
+ });
449
+ }
450
+ };
451
+ for (const line of lines) {
452
+ if (line.startsWith('worktree ')) {
453
+ flush();
454
+ cur = { path: line.slice('worktree '.length) };
455
+ }
456
+ else if (cur !== null && line.startsWith('HEAD ')) {
457
+ cur.head = line.slice('HEAD '.length);
458
+ }
459
+ else if (cur !== null && line.startsWith('branch ')) {
460
+ cur.branch = line.slice('branch '.length).replace(/^refs\/heads\//, '');
461
+ }
462
+ else if (cur !== null && line === 'detached') {
463
+ cur.detached = true;
464
+ }
465
+ else if (cur !== null && line === 'bare') {
466
+ cur.bare = true;
467
+ }
468
+ else if (cur !== null && (line === 'locked' || line.startsWith('locked '))) {
469
+ cur.locked = true;
470
+ cur.lockReason = line.slice('locked '.length) || null;
471
+ }
472
+ }
473
+ flush();
474
+ return out;
475
+ }
461
476
  /**
462
477
  * Merge base of two revs, or null when the two histories are unrelated
463
478
  * (exit 1). A rev that does not resolve exits 128 and throws.
@@ -1074,6 +1089,61 @@ export function isAncestor(a, b) {
1074
1089
  parse: (r) => r.exitCode === 0,
1075
1090
  };
1076
1091
  }
1092
+ /**
1093
+ * Which commits `head` has that `upstream` lacks, and for each one whether
1094
+ * `upstream` carries a commit with the same patch id (`git cherry`). Oldest
1095
+ * first. Merge commits are not listed.
1096
+ *
1097
+ * `absent: []`: an unresolvable rev exits 128 and throws. Empty output is a
1098
+ * real answer (`head` has nothing `upstream` lacks), so a caller that needs at
1099
+ * least one commit checks the length. A line that is not `+ <sha>` or
1100
+ * `- <sha>` throws rather than reading as an entry or being skipped.
1101
+ */
1102
+ export function cherry(upstream, head) {
1103
+ const argv = ['cherry', positional('cherry', 'upstream', upstream)];
1104
+ if (head !== undefined)
1105
+ argv.push(positional('cherry', 'head', head));
1106
+ return {
1107
+ argv,
1108
+ absent: NONE,
1109
+ parse(r, ranArgv) {
1110
+ const out = [];
1111
+ for (const line of r.stdout.split('\n')) {
1112
+ if (line === '')
1113
+ continue;
1114
+ const m = /^([+-]) ([0-9a-f]{40}|[0-9a-f]{64})$/.exec(line);
1115
+ if (m === null)
1116
+ throw new CommandFailed(ranArgv, r);
1117
+ out.push({ sha: m[2], equivalent: m[1] === '-' });
1118
+ }
1119
+ return out;
1120
+ },
1121
+ };
1122
+ }
1123
+ /**
1124
+ * Write a commit object for `tree` and print its sha (`git commit-tree`).
1125
+ *
1126
+ * It writes an object and no ref, and leaves the index and working tree alone.
1127
+ * git needs an author and committer identity; without one it exits 128 and
1128
+ * this throws. `absent: []`, and a reply that is not a sha throws.
1129
+ */
1130
+ export function commitTree(tree, opts) {
1131
+ const argv = ['commit-tree', positional('commitTree', 'tree', tree)];
1132
+ for (const parent of opts.parents ?? []) {
1133
+ argv.push('-p', positional('commitTree', 'parent', parent));
1134
+ }
1135
+ argv.push('-m', opts.message);
1136
+ return {
1137
+ argv,
1138
+ absent: NONE,
1139
+ parse(r, ranArgv) {
1140
+ const sha = r.stdout.trim();
1141
+ if (!/^([0-9a-f]{40}|[0-9a-f]{64})$/.test(sha))
1142
+ throw new CommandFailed(ranArgv, r);
1143
+ return sha;
1144
+ },
1145
+ };
1146
+ }
1077
1147
  /**
1078
1148
  * How far `tip` has diverged from `base`
1079
1149
  * (`git rev-list --left-right --count base...tip`).
@@ -1333,23 +1403,6 @@ export function forceWithLeaseArg(lease) {
1333
1403
  }
1334
1404
  return `--force-with-lease=${lease.ref}:${lease.expect ?? ''}`;
1335
1405
  }
1336
- /**
1337
- * Push a refspec, or delete it on the remote with `{ delete: true }`.
1338
- *
1339
- * `forceWithLease` takes a {@link ForceLease} for the pinned form, or `true`
1340
- * for the bare `--force-with-lease`. `true` is the unsafe form. It trusts
1341
- * whatever the local remote-tracking ref says, and a background fetch can
1342
- * bring that up to date. The lease then blocks nothing, and a concurrent push
1343
- * is overwritten. Pin the lease whenever the expected remote sha is known.
1344
- */
1345
- export function push(remote, refspec, opts = {}) {
1346
- const argv = ['push'];
1347
- if (opts.delete)
1348
- argv.push('--delete');
1349
- argv.push(...forceWithLeaseArgs('push', opts.forceWithLease));
1350
- argv.push(positional('push', 'remote', remote), positional('push', 'refspec', refspec));
1351
- return { argv, absent: NONE, parse: nothing };
1352
- }
1353
1406
  /**
1354
1407
  * The `--force-with-lease` arguments for a `forceWithLease` option: none, the
1355
1408
  * bare flag, or one per pinned lease. An empty lease list throws a
@@ -1369,6 +1422,72 @@ function forceWithLeaseArgs(verb, lease) {
1369
1422
  return [forceWithLeaseArg(lease)];
1370
1423
  throw new Error(`force-with-lease: unsupported value ${JSON.stringify(lease)}`);
1371
1424
  }
1425
+ const GIT_PUSH_DOCS = 'https://git-scm.com/docs/git-push';
1426
+ /**
1427
+ * A reference entry for every {@link PushRejection}: a one-line `message`, a
1428
+ * longer `explanation`, a `hint` and a link to git's documentation.
1429
+ *
1430
+ * ```ts
1431
+ * const info = PUSH_REJECTIONS[ref.rejection]
1432
+ * console.error(`${ref.dst}: ${info.message} ${info.hint} (${info.docs})`)
1433
+ * ```
1434
+ */
1435
+ export const PUSH_REJECTIONS = Object.freeze({
1436
+ stale: {
1437
+ message: 'The remote ref is not where the lease expected it.',
1438
+ explanation: 'With `--force-with-lease`, git compares the remote ref with the expected value before it sends anything. They differed, so someone else pushed to the ref since you last looked, and git refused rather than overwrite their work. A create-only lease fails this way when the ref exists. `remote ref updated since checkout` is the same check under `--force-if-includes`.',
1439
+ hint: 'Fetch, look at what changed, and push again with the new expected value.',
1440
+ docs: `${GIT_PUSH_DOCS}#Documentation/git-push.txt---force-with-lease`,
1441
+ },
1442
+ 'non-fast-forward': {
1443
+ message: 'The push would discard commits on the remote.',
1444
+ explanation: 'The remote ref has commits that the pushed commit does not contain, and the push was not forced. `fetch first` says the same when this git does not have the remote commits at all; `needs force` when the remote ref or the pushed object is not a commit.',
1445
+ hint: 'Fetch and integrate the remote commits, or force the push with a pinned lease.',
1446
+ docs: `${GIT_PUSH_DOCS}#_note_about_fast_forwards`,
1447
+ },
1448
+ 'tag-exists': {
1449
+ message: 'The tag already exists on the remote.',
1450
+ explanation: 'git does not move an existing tag unless the push is forced, so a retry fails the same way.',
1451
+ hint: 'Push a new tag name, or force the push if replacing the tag is intended.',
1452
+ docs: `${GIT_PUSH_DOCS}#_push_rules`,
1453
+ },
1454
+ 'already-exists': {
1455
+ message: 'Another push created the ref first.',
1456
+ explanation: 'The push would have created the ref, but when the remote locked it, it already existed: another writer won a race on the remote. git before 2.51 reports this as `failed to update ref`, with `reference already exists` in a `remote: error:` line.',
1457
+ hint: 'Fetch, look at the ref the other push created, and decide whether to push again.',
1458
+ docs: `${GIT_PUSH_DOCS}#Documentation/git-push.txt---force-with-lease`,
1459
+ },
1460
+ 'ref-conflict': {
1461
+ message: 'The ref name collides with another ref on the remote.',
1462
+ explanation: 'Ref names are paths, so `refs/heads/x` and `refs/heads/x/child` cannot both exist: one would have to be a file and a directory at once. The remote already has one of them. git before 2.51 reports this as `failed to update ref`, with the cause in a `remote: error:` line.',
1463
+ hint: 'Pick a name that is not a prefix or an extension of an existing ref, or delete the other ref first.',
1464
+ docs: `${GIT_PUSH_DOCS}#_output`,
1465
+ },
1466
+ 'lock-failed': {
1467
+ message: 'The remote ref moved while the push was updating it.',
1468
+ explanation: 'The lease check passed, but when the remote locked the ref to update it, the ref no longer held the expected value, or no longer existed. Another push won a race on the remote. git before 2.51 reports this as `failed to update ref`, with `is at … but expected …` in a `remote: error:` line.',
1469
+ hint: 'Treat it like a stale lease: fetch, look at what changed, and push again.',
1470
+ docs: `${GIT_PUSH_DOCS}#Documentation/git-push.txt---force-with-lease`,
1471
+ },
1472
+ 'hook-declined': {
1473
+ message: 'A hook on the remote refused the push.',
1474
+ explanation: "The remote's `pre-receive` hook refuses the whole push; its `update` hook refuses one ref. GitHub reports branch protection as `protected branch hook declined`. The hook's own message is in the `remote:` lines on stderr.",
1475
+ hint: "Read the hook's message on stderr, which says what the remote requires.",
1476
+ docs: 'https://git-scm.com/docs/githooks#pre-receive',
1477
+ },
1478
+ 'atomic-failed': {
1479
+ message: 'Another ref in this atomic push failed, so this one was not updated.',
1480
+ explanation: 'With `--atomic` the remote updates every ref or none. This ref was fine on its own; another ref in the same push carries the real reason. The client says `atomic push failed` when it refused locally, the remote `atomic transaction failed`.',
1481
+ hint: 'Look at the other refs in the report for the cause.',
1482
+ docs: `${GIT_PUSH_DOCS}#Documentation/git-push.txt---atomic`,
1483
+ },
1484
+ other: {
1485
+ message: 'git refused the ref for a reason this library does not classify.',
1486
+ explanation: "The reason text is kept verbatim. Common ones are a stale lock file or an unwritable refs directory on the remote, and the remote's safety settings, such as `receive.denyCurrentBranch` or `receive.denyDeletes`.",
1487
+ hint: 'Read `rawReason` and `detail` on the ref, and the remote lines on stderr.',
1488
+ docs: `${GIT_PUSH_DOCS}#_output`,
1489
+ },
1490
+ });
1372
1491
  /** `<flag>\t<src>:<dst>\t<summary>[ (<reason>)]` */
1373
1492
  const PUSH_REF_LINE = /^([ +\-*!=])\t([^\t]*)\t(.*)$/;
1374
1493
  /**
@@ -1401,6 +1520,8 @@ function pushRejection(reason, detail) {
1401
1520
  return 'tag-exists';
1402
1521
  if (r.includes('hook declined'))
1403
1522
  return 'hook-declined';
1523
+ if (r === 'atomic push failed' || r === 'atomic transaction failed')
1524
+ return 'atomic-failed';
1404
1525
  if (r === 'refname conflict')
1405
1526
  return 'ref-conflict';
1406
1527
  if (r === 'incorrect old value provided' || r === 'reference does not exist') {
@@ -1458,31 +1579,34 @@ function parsePushReport(stdout, stderr) {
1458
1579
  return { refs, stderr };
1459
1580
  }
1460
1581
  /**
1461
- * `git push --porcelain [--atomic] [--force-with-lease…] <remote> <refspec…>`,
1462
- * read into one result per ref.
1582
+ * `git push --porcelain [--delete] [--atomic] [--force-with-lease…] <remote>
1583
+ * <refspec…>`, read into one result per ref. `{ delete: true }` deletes each
1584
+ * refspec on the remote instead.
1463
1585
  *
1464
- * A rejected ref is a value, not a throw. git exits 1 when any ref is
1465
- * rejected, so exit 1 is declared absent, and the parse throws unless a ref
1466
- * line says `!`. A local pre-push hook decline and a refspec that matches
1467
- * nothing exit 1 with no ref lines, so they throw. So does every transport
1468
- * failure (exit 128: an unknown remote, auth, network, TLS), as the
1469
- * classified failure.
1586
+ * A rejected ref is a value, not a throw: check each ref's `flag`, or
1587
+ * whether any is `'!'`. git exits 1 when any ref is rejected, so exit 1 is
1588
+ * declared absent, and the parse throws unless a ref line says `!`. A local
1589
+ * pre-push hook decline and a refspec that matches nothing exit 1 with no
1590
+ * ref lines, so they throw. So does every transport failure (exit 128: an
1591
+ * unknown remote, auth, network, TLS), as the classified failure.
1470
1592
  *
1471
1593
  * Runs with `LC_ALL=C`, so the caller's locale does not change the reason
1472
1594
  * text. An empty refspec list throws a `TypeError`: git would push its
1473
1595
  * default set.
1474
1596
  */
1475
- export function pushPorcelain(remote, refspec, opts = {}) {
1597
+ export function push(remote, refspec, opts = {}) {
1476
1598
  const specs = typeof refspec === 'string' ? [refspec] : refspec;
1477
1599
  if (specs.length === 0)
1478
- throw new TypeError('typed-git: pushPorcelain: refspec list is empty');
1600
+ throw new TypeError('typed-git: push: refspec list is empty');
1479
1601
  const argv = ['push', '--porcelain'];
1602
+ if (opts.delete)
1603
+ argv.push('--delete');
1480
1604
  if (opts.atomic)
1481
1605
  argv.push('--atomic');
1482
- argv.push(...forceWithLeaseArgs('pushPorcelain', opts.forceWithLease));
1483
- argv.push(positional('pushPorcelain', 'remote', remote));
1606
+ argv.push(...forceWithLeaseArgs('push', opts.forceWithLease));
1607
+ argv.push(positional('push', 'remote', remote));
1484
1608
  for (const spec of specs)
1485
- argv.push(positional('pushPorcelain', 'refspec', spec));
1609
+ argv.push(positional('push', 'refspec', spec));
1486
1610
  return {
1487
1611
  argv,
1488
1612
  env: C_LOCALE,
@@ -1811,6 +1935,10 @@ export function rebaseContinue() {
1811
1935
  *
1812
1936
  * A name git cannot merge exits 1 exactly like a conflict, so again only
1813
1937
  * stdout separates an answer from a refusal.
1938
+ *
1939
+ * Needs git 2.38, or 2.40 with `mergeBase` (the `merge-tree` rows in
1940
+ * `./features.ts`). There is no fallback: on an older git it throws the
1941
+ * `unsupported-git` failure without running git.
1814
1942
  */
1815
1943
  export function mergeTree(ours, theirs, opts = {}) {
1816
1944
  const argv = ['merge-tree', '--write-tree', '--name-only', '-z'];
@@ -1819,6 +1947,7 @@ export function mergeTree(ours, theirs, opts = {}) {
1819
1947
  argv.push(positional('mergeTree', 'ours', ours), positional('mergeTree', 'theirs', theirs));
1820
1948
  return {
1821
1949
  argv,
1950
+ needs: opts.mergeBase === undefined ? 'merge-tree --write-tree' : 'merge-tree --merge-base',
1822
1951
  absent: [1],
1823
1952
  parse(r, ranArgv) {
1824
1953
  const fields = r.stdout.split('\x00');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osdlc/typed-git",
3
- "version": "0.10.0",
3
+ "version": "0.12.0",
4
4
  "description": "A typed git client for TypeScript, sync and awaitable. Each verb declares what absence means for it; every other failure throws. git.try.* inspects, raw() bypasses — over an injectable backend that drives the git CLI or any other implementation.",
5
5
  "license": "MIT OR Apache-2.0",
6
6
  "author": "sksizer",