@osdlc/typed-git 0.8.2 → 0.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/sync.d.ts CHANGED
@@ -16,9 +16,10 @@
16
16
  * service's compare-and-swap ref operations. That is not general git.
17
17
  */
18
18
  import { type CommandResult, type CommandRunner } from '@osdlc/command-seam';
19
+ import { type GitVersion } from './git-version.ts';
19
20
  import { type Soft, type TriedSync } from './outcome.ts';
20
21
  import { type RemoteCoordinate } from './remote-url.ts';
21
- import { type AddOptions, type AheadBehind, type CommitOptions, type CheckoutOptions, type ConfigGetOptions, type CloneOptions, type ConflictSide, type MergeOptions, type MergeOutcome, type MergeTreeOptions, type MergeTreeOutcome, type RebaseOptions, type RebaseOutcome, type RemoteGetUrlOptions, type TaggedFile, type UnmergedEntry, type ConfigSetOptions, type DiffEntry, type DiffNameOnlyOptions, type DiffStatOptions, type DiffNameStatusOptions, type DiffTextOptions, type DiffTreeOptions, type FetchOptions, type ForEachRefOptions, type ForceLease, type GrepOptions, type InitOptions, type LogOptions, type IgnoreMatch, type LsFilesOptions, type LsRemoteOptions, type LsTreeOptions, type NumstatEntry, type PushPorcelainOptions, type PushReport, type RemoteRef, type CleanOptions, type DiffCheckOptions, type DiffCheckResult, type StatusEntry, type StatusPorcelainOptions, type TreeEntry, type Worktree, type WorktreeMoveOptions } from './verbs.ts';
22
+ import { type AddOptions, type AheadBehind, type CherryEntry, type CommitTreeOptions, type CommitOptions, type CheckoutOptions, type ConfigGetOptions, type CloneOptions, type ConflictSide, type MergeOptions, type MergeOutcome, type MergeTreeOptions, type MergeTreeOutcome, type RebaseOptions, type RebaseOutcome, type RemoteGetUrlOptions, type TaggedFile, type UnmergedEntry, type ConfigSetOptions, type DiffEntry, type DiffNameOnlyOptions, type DiffStatOptions, type DiffNameStatusOptions, type DiffTextOptions, type DiffTreeOptions, type FetchOptions, type ForEachRefOptions, type GrepOptions, type InitOptions, type LogOptions, type IgnoreMatch, type LsFilesOptions, type LsRemoteOptions, type LsTreeOptions, type NumstatEntry, type PushPorcelainOptions, type PushReport, type RemoteRef, type CleanOptions, type DiffCheckOptions, type DiffCheckResult, type StatusEntry, type StatusPorcelainOptions, type TreeEntry, type Worktree, type WorktreeAddOptions, type WorktreeMoveOptions, type GcOptions, type PushOptions, type RemoteSetBranchesOptions, type RemoteSetHeadOptions } from './verbs.ts';
22
23
  /**
23
24
  * Per-call concerns that are not part of the operation itself.
24
25
  *
@@ -50,6 +51,8 @@ export declare class Git {
50
51
  private readonly boundGitDir;
51
52
  private readonly workTree;
52
53
  private readonly env;
54
+ private readonly gitVersionOption;
55
+ private readonly pinnedVersion;
53
56
  private tried;
54
57
  private softened;
55
58
  /**
@@ -75,16 +78,23 @@ export declare class Git {
75
78
  * Neither client has a default timeout. This client kills a child whose
76
79
  * output on one stream passes 256 MB; `AsyncGit` has no output cap. The
77
80
  * `maxOutputBytes` call option sets the cap per call on both.
81
+ *
82
+ * `gitVersion` pins the git version: `'2.55.0'`, or a whole
83
+ * `git version …` line. Version-gated verbs then use it and never run
84
+ * `git --version`, and {@link Git.version} returns it. Set it with a fake
85
+ * runner, so the fake answers only the verbs under test. An unparsable
86
+ * value throws a `RangeError` here.
78
87
  */
79
88
  constructor(cwd: string, opts?: {
80
89
  runner?: CommandRunner | undefined;
81
90
  gitDir?: string | undefined;
82
91
  workTree?: string | undefined;
83
92
  env?: Record<string, string> | undefined;
93
+ gitVersion?: string | undefined;
84
94
  });
85
95
  /**
86
96
  * The same client in a different directory, with the same runner,
87
- * repository options and client-level `env`.
97
+ * repository options, client-level `env` and `gitVersion`.
88
98
  */
89
99
  at(cwd: string): Git;
90
100
  /**
@@ -127,10 +137,28 @@ export declare class Git {
127
137
  /** The verb's argv, after this client's `--git-dir` / `--work-tree` flags. */
128
138
  private argv;
129
139
  private runOpts;
130
- /** Run one verb from the table and check its result with `gate`. */
140
+ /**
141
+ * Run one verb from the table and check its result with `gate`. A verb that
142
+ * `needs` a git feature first gets the form this runner's git can run (see
143
+ * `versionedVerb` in `features.ts`), which probes the version once.
144
+ */
131
145
  private exec;
146
+ /**
147
+ * The git this client's runner runs, from `git --version`. Probed on the
148
+ * first call (or the first version-gated verb) and cached for the runner,
149
+ * so a client that never asks never spawns it. Throws the classified
150
+ * failure when git cannot run (`not-installed`), and an `exit` failure when
151
+ * the output is not a version line. With the `gitVersion` option it is that
152
+ * version, and nothing runs.
153
+ */
154
+ version(opts?: GitSyncCallOptions): GitVersion;
132
155
  /** A file's content at a rev (`git show <rev>:<relPath>`), null if absent. */
133
156
  showAtRev(rev: string, relPath: string, opts?: GitSyncCallOptions): string | null;
157
+ /**
158
+ * A file's content at a rev, throwing when the path is missing. Use it when
159
+ * a missing file is an error. See `showAtRevChecked` in `verbs.ts`.
160
+ */
161
+ showAtRevChecked(rev: string, relPath: string, opts?: GitSyncCallOptions): string;
134
162
  /**
135
163
  * Trimmed `git rev-parse <args>` output, for a query no other verb covers. See `revParse` in `verbs.ts`; for a ref, prefer
136
164
  * {@link Git.resolveRef}.
@@ -181,9 +209,12 @@ export declare class Git {
181
209
  * The primary checkout that owns this working directory, or null when it is
182
210
  * not inside a repository at all.
183
211
  *
184
- * It is correct when the cwd is a linked worktree too. `gitCommonDir()` is
185
- * worktree-aware and absolute, so its parent is the primary checkout in
186
- * both cases.
212
+ * It is correct when the cwd is a linked worktree too: `gitCommonDir()` is
213
+ * worktree-aware and absolute. When that store is `<checkout>/.git`, its
214
+ * parent is the answer. Otherwise (a submodule's `.git/modules/<name>`, a
215
+ * `--separate-git-dir` clone, a bare store) it is `core.worktree`, this
216
+ * checkout when the cwd is the main worktree, or the first non-bare
217
+ * `worktree list` entry. A bare store with no main checkout answers null.
187
218
  */
188
219
  mainCheckout(opts?: GitSyncCallOptions): string | null;
189
220
  /** Current branch shortname, or null when detached. Throws outside a repo. */
@@ -195,6 +226,12 @@ export declare class Git {
195
226
  statusPorcelain(opts?: GitSyncCallOptions & StatusPorcelainOptions): string[];
196
227
  /** Every changed or untracked file, from `status --porcelain -z`. */
197
228
  statusEntries(opts?: GitSyncCallOptions): StatusEntry[];
229
+ /**
230
+ * True iff anything is staged (`diff --cached --quiet` exits 1). On an
231
+ * unborn HEAD the index is compared against the empty tree. See
232
+ * `hasStagedChanges` in `verbs.ts`.
233
+ */
234
+ hasStagedChanges(opts?: GitSyncCallOptions): boolean;
198
235
  /** True iff the working tree has any staged/unstaged/untracked change. */
199
236
  isDirty(opts?: GitSyncCallOptions & StatusPorcelainOptions): boolean;
200
237
  /** Local branch shortnames matching `pattern` (all when omitted). */
@@ -304,6 +341,13 @@ export declare class Git {
304
341
  isAncestor(a: string, b: string, opts?: GitSyncCallOptions): boolean;
305
342
  /** How far `tip` has diverged from `base`. See {@link AheadBehind}. */
306
343
  aheadBehind(base: string, tip: string, opts?: GitSyncCallOptions): AheadBehind;
344
+ /**
345
+ * Commits on `head` (default `HEAD`) that `upstream` lacks, each marked by
346
+ * whether `upstream` has the same patch. See {@link CherryEntry}.
347
+ */
348
+ cherry(upstream: string, head?: string, opts?: GitSyncCallOptions): CherryEntry[];
349
+ /** Write a commit for `tree` and return its sha; no ref moves. Needs a git identity. */
350
+ commitTree(tree: string, commitOpts: CommitTreeOptions, opts?: GitSyncCallOptions): string;
307
351
  /** Configured remote names (`git remote`); `[]` when there are none. */
308
352
  remotes(opts?: GitSyncCallOptions): string[];
309
353
  /**
@@ -360,11 +404,10 @@ export declare class Git {
360
404
  * "push only if the remote ref is still where I saw it". `{forceWithLease: true}` adds the
361
405
  * bare flag, which is the unsafe form: it compares against the
362
406
  * remote-tracking ref, which a fetch in this same process may have just
363
- * updated. See `push` in `verbs.ts`.
407
+ * updated. `{delete: true}` deletes `refspec` on the remote. See `push` in
408
+ * `verbs.ts`.
364
409
  */
365
- push(remote: string, refspec: string, opts?: GitSyncCallOptions & {
366
- forceWithLease?: boolean | ForceLease | undefined;
367
- }): void;
410
+ push(remote: string, refspec: string, opts?: GitSyncCallOptions & PushOptions): void;
368
411
  /**
369
412
  * `git push --porcelain`, read into one result per ref. A rejected ref comes
370
413
  * back with a typed `rejection` rather than a throw; a failure that reaches
@@ -389,12 +432,11 @@ export declare class Git {
389
432
  deleteRef(ref: string, opts: GitSyncCallOptions & {
390
433
  expected: string;
391
434
  }): void;
392
- /** `git worktree add [--detach] [-b branch] <path> [startPoint]`. */
393
- worktreeAdd(path: string, opts?: GitSyncCallOptions & {
394
- branch?: string | undefined;
395
- startPoint?: string | undefined;
396
- detach?: boolean | undefined;
397
- }): void;
435
+ /**
436
+ * `git worktree add [--detach] [-b|-B branch] <path> [startPoint]`.
437
+ * `resetBranch` picks `-B`: create `branch`, or reset it if it exists.
438
+ */
439
+ worktreeAdd(path: string, opts?: GitSyncCallOptions & WorktreeAddOptions): void;
398
440
  /** `git worktree remove [--force] <path>`. */
399
441
  worktreeRemove(path: string, opts?: GitSyncCallOptions & {
400
442
  force?: boolean | undefined;
@@ -407,6 +449,15 @@ export declare class Git {
407
449
  worktreePrune(opts?: GitSyncCallOptions): void;
408
450
  /** `git remote add <name> <url>`. */
409
451
  remoteAdd(name: string, url: string, opts?: GitSyncCallOptions): void;
452
+ /**
453
+ * `git remote set-head <remote> <branch> | --auto | --delete` — exactly one
454
+ * of `branch`, `auto` or `delete`. See `remoteSetHead` in `verbs.ts`.
455
+ */
456
+ remoteSetHead(remote: string, opts: GitSyncCallOptions & RemoteSetHeadOptions): void;
457
+ /** `git remote set-branches [--add] <remote> <branch…>`; an empty list throws. */
458
+ remoteSetBranches(remote: string, branches: string[], opts?: GitSyncCallOptions & RemoteSetBranchesOptions): void;
459
+ /** `git gc [--auto] [--aggressive] [--prune=<date> | --no-prune] [--quiet]`. */
460
+ gc(opts?: GitSyncCallOptions & GcOptions): void;
410
461
  /**
411
462
  * Merge `ref` into the current branch. A conflict is a value, not a throw:
412
463
  * the outcome is `clean` or `conflict`. Git refusing to merge at all still
package/dist/sync.js CHANGED
@@ -15,8 +15,10 @@
15
15
  * Not here: any protocol with its own conflict rules, such as a lease
16
16
  * service's compare-and-swap ref operations. That is not general git.
17
17
  */
18
- import { dirname } from 'node:path';
18
+ import { basename, dirname, resolve } from 'node:path';
19
19
  import { spawnRunner, } from '@osdlc/command-seam';
20
+ import { versionedVerb } from "./features.js";
21
+ import { pinnedGitVersion, probeGitVersion } from "./git-version.js";
20
22
  import { softSync, triedSync } from "./outcome.js";
21
23
  import { parseRemoteUrl } from "./remote-url.js";
22
24
  import { SOFT_TABLE } from "./soft.js";
@@ -29,6 +31,8 @@ export class Git {
29
31
  boundGitDir;
30
32
  workTree;
31
33
  env;
34
+ gitVersionOption;
35
+ pinnedVersion;
32
36
  tried;
33
37
  softened;
34
38
  /**
@@ -54,9 +58,17 @@ export class Git {
54
58
  * Neither client has a default timeout. This client kills a child whose
55
59
  * output on one stream passes 256 MB; `AsyncGit` has no output cap. The
56
60
  * `maxOutputBytes` call option sets the cap per call on both.
61
+ *
62
+ * `gitVersion` pins the git version: `'2.55.0'`, or a whole
63
+ * `git version …` line. Version-gated verbs then use it and never run
64
+ * `git --version`, and {@link Git.version} returns it. Set it with a fake
65
+ * runner, so the fake answers only the verbs under test. An unparsable
66
+ * value throws a `RangeError` here.
57
67
  */
58
68
  constructor(cwd, opts = {}) {
59
69
  this.cwd = cwd;
70
+ this.gitVersionOption = opts.gitVersion;
71
+ this.pinnedVersion = pinnedGitVersion(opts.gitVersion);
60
72
  this.runner = opts.runner ?? spawnRunner('git', { absolutePathOnly: true });
61
73
  this.boundGitDir = opts.gitDir;
62
74
  this.workTree = opts.workTree;
@@ -64,7 +76,7 @@ export class Git {
64
76
  }
65
77
  /**
66
78
  * The same client in a different directory, with the same runner,
67
- * repository options and client-level `env`.
79
+ * repository options, client-level `env` and `gitVersion`.
68
80
  */
69
81
  at(cwd) {
70
82
  return new Git(cwd, {
@@ -72,6 +84,7 @@ export class Git {
72
84
  gitDir: this.boundGitDir,
73
85
  workTree: this.workTree,
74
86
  env: this.env,
87
+ gitVersion: this.gitVersionOption,
75
88
  });
76
89
  }
77
90
  /**
@@ -141,9 +154,25 @@ export class Git {
141
154
  run.input = opts.input;
142
155
  return run;
143
156
  }
144
- /** Run one verb from the table and check its result with `gate`. */
145
- exec(verb, opts = {}) {
157
+ /**
158
+ * Run one verb from the table and check its result with `gate`. A verb that
159
+ * `needs` a git feature first gets the form this runner's git can run (see
160
+ * `versionedVerb` in `features.ts`), which probes the version once.
161
+ */
162
+ exec(gated, opts = {}) {
146
163
  const run = this.runOpts(opts);
164
+ let verb = gated;
165
+ if (gated.needs !== undefined) {
166
+ const prefix = repoPrefix(this.boundGitDir, this.workTree);
167
+ // A failed probe names this verb's argv, not `--version`.
168
+ const have = this.pinnedVersion ??
169
+ probeGitVersion(this.runner, {
170
+ env: run.env,
171
+ timeoutMs: run.timeoutMs,
172
+ report: [...prefix, ...gated.argv],
173
+ });
174
+ verb = versionedVerb(gated, have, prefix);
175
+ }
147
176
  if (verb.input !== undefined)
148
177
  run.input = verb.input;
149
178
  // The verb's own entries (the C locale) win over the client's and the call's.
@@ -154,10 +183,35 @@ export class Git {
154
183
  return gate(verb, this.runner.run(argv, run), argv);
155
184
  }
156
185
  // ── queries ───────────────────────────────────────────────────────────────
186
+ /**
187
+ * The git this client's runner runs, from `git --version`. Probed on the
188
+ * first call (or the first version-gated verb) and cached for the runner,
189
+ * so a client that never asks never spawns it. Throws the classified
190
+ * failure when git cannot run (`not-installed`), and an `exit` failure when
191
+ * the output is not a version line. With the `gitVersion` option it is that
192
+ * version, and nothing runs.
193
+ */
194
+ version(opts = {}) {
195
+ if (this.pinnedVersion !== undefined)
196
+ return this.pinnedVersion;
197
+ const run = this.runOpts(opts);
198
+ return probeGitVersion(this.runner, {
199
+ env: run.env,
200
+ timeoutMs: run.timeoutMs,
201
+ report: ['--version'],
202
+ });
203
+ }
157
204
  /** A file's content at a rev (`git show <rev>:<relPath>`), null if absent. */
158
205
  showAtRev(rev, relPath, opts = {}) {
159
206
  return this.exec(v.showAtRev(rev, relPath), opts);
160
207
  }
208
+ /**
209
+ * A file's content at a rev, throwing when the path is missing. Use it when
210
+ * a missing file is an error. See `showAtRevChecked` in `verbs.ts`.
211
+ */
212
+ showAtRevChecked(rev, relPath, opts = {}) {
213
+ return this.exec(v.showAtRevChecked(rev, relPath), opts);
214
+ }
161
215
  /**
162
216
  * Trimmed `git rev-parse <args>` output, for a query no other verb covers. See `revParse` in `verbs.ts`; for a ref, prefer
163
217
  * {@link Git.resolveRef}.
@@ -225,13 +279,26 @@ export class Git {
225
279
  * The primary checkout that owns this working directory, or null when it is
226
280
  * not inside a repository at all.
227
281
  *
228
- * It is correct when the cwd is a linked worktree too. `gitCommonDir()` is
229
- * worktree-aware and absolute, so its parent is the primary checkout in
230
- * both cases.
282
+ * It is correct when the cwd is a linked worktree too: `gitCommonDir()` is
283
+ * worktree-aware and absolute. When that store is `<checkout>/.git`, its
284
+ * parent is the answer. Otherwise (a submodule's `.git/modules/<name>`, a
285
+ * `--separate-git-dir` clone, a bare store) it is `core.worktree`, this
286
+ * checkout when the cwd is the main worktree, or the first non-bare
287
+ * `worktree list` entry. A bare store with no main checkout answers null.
231
288
  */
232
289
  mainCheckout(opts = {}) {
233
290
  const common = this.gitCommonDir(opts);
234
- return common === null ? null : dirname(common);
291
+ if (common === null)
292
+ return null;
293
+ if (basename(common) === '.git')
294
+ return dirname(common);
295
+ const configured = this.configGet('core.worktree', opts);
296
+ if (configured !== null)
297
+ return resolve(common, configured);
298
+ if (this.gitDir(opts) === common)
299
+ return this.topLevel(opts);
300
+ const main = this.worktrees(opts)[0];
301
+ return main === undefined || main.bare ? null : main.path;
235
302
  }
236
303
  /** Current branch shortname, or null when detached. Throws outside a repo. */
237
304
  currentBranch(opts = {}) {
@@ -248,6 +315,14 @@ export class Git {
248
315
  statusEntries(opts = {}) {
249
316
  return this.exec(v.statusEntries(), opts);
250
317
  }
318
+ /**
319
+ * True iff anything is staged (`diff --cached --quiet` exits 1). On an
320
+ * unborn HEAD the index is compared against the empty tree. See
321
+ * `hasStagedChanges` in `verbs.ts`.
322
+ */
323
+ hasStagedChanges(opts = {}) {
324
+ return this.exec(v.hasStagedChanges(), opts);
325
+ }
251
326
  /** True iff the working tree has any staged/unstaged/untracked change. */
252
327
  isDirty(opts = {}) {
253
328
  return this.statusPorcelain(opts).length > 0;
@@ -423,6 +498,17 @@ export class Git {
423
498
  aheadBehind(base, tip, opts = {}) {
424
499
  return this.exec(v.aheadBehind(base, tip), opts);
425
500
  }
501
+ /**
502
+ * Commits on `head` (default `HEAD`) that `upstream` lacks, each marked by
503
+ * whether `upstream` has the same patch. See {@link CherryEntry}.
504
+ */
505
+ cherry(upstream, head, opts = {}) {
506
+ return this.exec(v.cherry(upstream, head), opts);
507
+ }
508
+ /** Write a commit for `tree` and return its sha; no ref moves. Needs a git identity. */
509
+ commitTree(tree, commitOpts, opts = {}) {
510
+ return this.exec(v.commitTree(tree, commitOpts), opts);
511
+ }
426
512
  /** Configured remote names (`git remote`); `[]` when there are none. */
427
513
  remotes(opts = {}) {
428
514
  return this.exec(v.remote(), opts);
@@ -505,7 +591,8 @@ export class Git {
505
591
  * "push only if the remote ref is still where I saw it". `{forceWithLease: true}` adds the
506
592
  * bare flag, which is the unsafe form: it compares against the
507
593
  * remote-tracking ref, which a fetch in this same process may have just
508
- * updated. See `push` in `verbs.ts`.
594
+ * updated. `{delete: true}` deletes `refspec` on the remote. See `push` in
595
+ * `verbs.ts`.
509
596
  */
510
597
  push(remote, refspec, opts = {}) {
511
598
  this.exec(v.push(remote, refspec, opts), opts);
@@ -542,7 +629,10 @@ export class Git {
542
629
  deleteRef(ref, opts) {
543
630
  this.exec(v.deleteRef(ref, opts), opts);
544
631
  }
545
- /** `git worktree add [--detach] [-b branch] <path> [startPoint]`. */
632
+ /**
633
+ * `git worktree add [--detach] [-b|-B branch] <path> [startPoint]`.
634
+ * `resetBranch` picks `-B`: create `branch`, or reset it if it exists.
635
+ */
546
636
  worktreeAdd(path, opts = {}) {
547
637
  this.exec(v.worktreeAdd(path, opts), opts);
548
638
  }
@@ -566,6 +656,21 @@ export class Git {
566
656
  remoteAdd(name, url, opts = {}) {
567
657
  this.exec(v.remoteAdd(name, url), opts);
568
658
  }
659
+ /**
660
+ * `git remote set-head <remote> <branch> | --auto | --delete` — exactly one
661
+ * of `branch`, `auto` or `delete`. See `remoteSetHead` in `verbs.ts`.
662
+ */
663
+ remoteSetHead(remote, opts) {
664
+ this.exec(v.remoteSetHead(remote, opts), opts);
665
+ }
666
+ /** `git remote set-branches [--add] <remote> <branch…>`; an empty list throws. */
667
+ remoteSetBranches(remote, branches, opts = {}) {
668
+ this.exec(v.remoteSetBranches(remote, branches, opts), opts);
669
+ }
670
+ /** `git gc [--auto] [--aggressive] [--prune=<date> | --no-prune] [--quiet]`. */
671
+ gc(opts = {}) {
672
+ this.exec(v.gc(opts), opts);
673
+ }
569
674
  // ── integration ───────────────────────────────────────────────────────────
570
675
  /**
571
676
  * Merge `ref` into the current branch. A conflict is a value, not a throw: