@osdlc/typed-git 0.5.2 → 0.6.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 (50) hide show
  1. package/CHANGELOG.md +36 -1
  2. package/CONTRIBUTING.md +3 -4
  3. package/LICENSE-APACHE +1 -1
  4. package/README.md +40 -16
  5. package/dist/async.d.ts +3 -4
  6. package/dist/async.js +3 -3
  7. package/dist/backend.d.ts +1 -2
  8. package/dist/checkout-root.d.ts +0 -1
  9. package/dist/client.d.ts +2 -2
  10. package/dist/client.js +1 -0
  11. package/dist/failure.d.ts +64 -0
  12. package/dist/failure.js +126 -0
  13. package/dist/git-url.d.ts +0 -1
  14. package/dist/index.d.ts +1 -2
  15. package/dist/index.js +1 -1
  16. package/dist/outcome.d.ts +13 -18
  17. package/dist/outcome.js +21 -16
  18. package/dist/remote-url.d.ts +0 -1
  19. package/dist/soft.d.ts +0 -1
  20. package/dist/sync.d.ts +5 -5
  21. package/dist/sync.js +4 -3
  22. package/dist/testing.d.ts +0 -1
  23. package/dist/verbs.d.ts +5 -7
  24. package/dist/verbs.js +18 -11
  25. package/package.json +10 -23
  26. package/async.ts +0 -753
  27. package/backend.ts +0 -574
  28. package/checkout-root.ts +0 -164
  29. package/client.ts +0 -102
  30. package/dist/async.d.ts.map +0 -1
  31. package/dist/backend.d.ts.map +0 -1
  32. package/dist/checkout-root.d.ts.map +0 -1
  33. package/dist/client.d.ts.map +0 -1
  34. package/dist/git-url.d.ts.map +0 -1
  35. package/dist/index.d.ts.map +0 -1
  36. package/dist/outcome.d.ts.map +0 -1
  37. package/dist/remote-url.d.ts.map +0 -1
  38. package/dist/soft.d.ts.map +0 -1
  39. package/dist/sync.d.ts.map +0 -1
  40. package/dist/testing.d.ts.map +0 -1
  41. package/dist/verbs.d.ts.map +0 -1
  42. package/git-url.ts +0 -102
  43. package/hosted-git-info.d.ts +0 -24
  44. package/index.ts +0 -52
  45. package/outcome.ts +0 -191
  46. package/remote-url.ts +0 -46
  47. package/soft.ts +0 -60
  48. package/sync.ts +0 -827
  49. package/testing.ts +0 -291
  50. package/verbs.ts +0 -2300
package/sync.ts DELETED
@@ -1,827 +0,0 @@
1
- /**
2
- * `Git` — the synchronous client, bound to one working directory and one
3
- * {@link CommandRunner}.
4
- *
5
- * Synchronous on purpose: sequential git one-shots gain nothing from being
6
- * awaited, and staying sync keeps a caller's call graph uncolored. Reach for
7
- * `AsyncGit` when the caller must not block, when calls fan out concurrently,
8
- * or when the work should run against a non-CLI backend.
9
- *
10
- * The contract — declared absence returns a value, everything else throws,
11
- * `try` inspects, `raw` bypasses — is stated once in `./index.ts`. Each verb's
12
- * own absence codes and parsing live in `./verbs.ts`.
13
- *
14
- * Deliberately NOT here: any control-plane protocol with its own conflict
15
- * semantics (a lease service's CAS ref primitives, say) — that is not
16
- * general git.
17
- */
18
-
19
- import { dirname } from 'node:path'
20
-
21
- import {
22
- resolveRunner,
23
- type CommandResult,
24
- type CommandRunner,
25
- type CommandRunOptions,
26
- } from '@osdlc/command-seam'
27
-
28
- import { softSync, triedSync, type Soft, type TriedSync } from './outcome.ts'
29
- import { parseRemoteUrl, type RemoteCoordinate } from './remote-url.ts'
30
- import { SOFT_TABLE } from './soft.ts'
31
- import * as v from './verbs.ts'
32
- import {
33
- gate,
34
- mergeEnv,
35
- parseRefRows,
36
- repoPrefix,
37
- splitLines,
38
- splitTrailingOptions,
39
- type AddOptions,
40
- type AheadBehind,
41
- type CommitOptions,
42
- type CheckoutOptions,
43
- type ConfigGetOptions,
44
- type CloneOptions,
45
- type ConflictSide,
46
- type MergeOptions,
47
- type MergeOutcome,
48
- type MergeTreeOptions,
49
- type MergeTreeOutcome,
50
- type RebaseOptions,
51
- type RebaseOutcome,
52
- type RemoteGetUrlOptions,
53
- type TaggedFile,
54
- type UnmergedEntry,
55
- type ConfigSetOptions,
56
- type DiffEntry,
57
- type DiffNameOnlyOptions,
58
- type DiffStatOptions,
59
- type DiffNameStatusOptions,
60
- type DiffTextOptions,
61
- type DiffTreeOptions,
62
- type FetchOptions,
63
- type ForEachRefOptions,
64
- type ForceLease,
65
- type GitVerb,
66
- type GrepOptions,
67
- type InitOptions,
68
- type LogOptions,
69
- type IgnoreMatch,
70
- type LsFilesOptions,
71
- type LsRemoteOptions,
72
- type LsTreeOptions,
73
- type NumstatEntry,
74
- type RemoteRef,
75
- type CleanOptions,
76
- type DiffCheckOptions,
77
- type DiffCheckResult,
78
- type StatusEntry,
79
- type StatusPorcelainOptions,
80
- type TreeEntry,
81
- type Worktree,
82
- type WorktreeMoveOptions,
83
- } from './verbs.ts'
84
-
85
- /**
86
- * Per-call concerns that are not part of the operation itself.
87
- *
88
- * The synchronous twin of `GitCallOptions`, minus `signal`: a `spawnSync`
89
- * child cannot be cancelled mid-flight, so offering an `AbortSignal` here
90
- * would be a promise the seam cannot keep. `timeoutMs` is the sync answer.
91
- */
92
- export interface GitSyncCallOptions {
93
- /**
94
- * Hard cap on the call, enforced by the runner. Without it there is no cap.
95
- * An invalid value throws, as in command-seam (`RangeError` for 0, negative
96
- * or NaN; `TypeError` for a non-number).
97
- */
98
- timeoutMs?: number | undefined
99
- /** Entries merged over the parent environment for this call only. */
100
- env?: Record<string, string> | undefined
101
- /**
102
- * Cap on each output stream, in bytes; 256 MB when omitted. Past it the
103
- * runner kills the child and the call throws `CommandFailed`. command-seam's
104
- * Bun test capture path (the Bun runtime with `NODE_ENV=test`) does not
105
- * apply it.
106
- */
107
- maxOutputBytes?: number | undefined
108
- }
109
-
110
- /** A typed git client bound to one working directory. */
111
- export class Git {
112
- readonly cwd: string
113
- private readonly runner: CommandRunner
114
- private readonly boundGitDir: string | undefined
115
- private readonly workTree: string | undefined
116
- private readonly env: Record<string, string> | undefined
117
- private tried: TriedSync<Git> | undefined
118
- private softened: Soft<Git> | undefined
119
-
120
- /**
121
- * `gitDir` / `workTree` name which repository this client drives, as
122
- * `--git-dir` / `--work-tree` prefixed onto every invocation — the way to
123
- * operate on a repo that is not the process's own tree (a bare store, or a
124
- * checkout whose `.git` lives elsewhere).
125
- *
126
- * They are a property of the client, not of any one call, because "which
127
- * repository" binds the client the same way `cwd` does. `AsyncGit` takes
128
- * them on its backend instead (`cliBackend({ gitDir })`): a non-CLI backend
129
- * has no `--git-dir` to receive, so the option belongs to the thing that
130
- * spawns git, and for the sync client that thing is the client.
131
- *
132
- * `env` is the same idea for the environment — see `cliBackend` in
133
- * `backend.ts`.
134
- *
135
- * This client takes a `runner`, where `AsyncGit` takes a `backend`. The
136
- * default runner spawns `git`; tests pass a fake `CommandRunner`.
137
- *
138
- * Neither client has a default timeout. This client kills a child whose
139
- * output on one stream passes 256 MB; `AsyncGit` has no output cap. The
140
- * `maxOutputBytes` call option sets the cap per call on both.
141
- */
142
- constructor(
143
- cwd: string,
144
- opts: {
145
- runner?: CommandRunner | undefined
146
- gitDir?: string | undefined
147
- workTree?: string | undefined
148
- env?: Record<string, string> | undefined
149
- } = {},
150
- ) {
151
- this.cwd = cwd
152
- this.runner = resolveRunner('git', opts.runner)
153
- this.boundGitDir = opts.gitDir
154
- this.workTree = opts.workTree
155
- this.env = opts.env
156
- }
157
-
158
- /**
159
- * The same client rooted at a different directory — same runner, same repo
160
- * binding, same client-level `env`.
161
- */
162
- at(cwd: string): Git {
163
- return new Git(cwd, {
164
- runner: this.runner,
165
- gitDir: this.boundGitDir,
166
- workTree: this.workTree,
167
- env: this.env,
168
- })
169
- }
170
-
171
- /**
172
- * The whole surface again, reporting failure instead of throwing:
173
- * `git.try.topLevel()` is `{ ok: true, value } | { ok: false, error }`.
174
- * A declared absence is `ok: true` — see `./outcome.ts`.
175
- *
176
- * The type leaves out `raw`, `rawChecked` and `at`, but at runtime the proxy
177
- * wraps them too: `git.try.raw(argv)` returns `{ ok: true, value }`. Call
178
- * those three on the client itself.
179
- */
180
- get try(): TriedSync<Git> {
181
- return (this.tried ??= triedSync(this))
182
- }
183
-
184
- /**
185
- * The tolerant door: each verb with an absence value (`null`, `[]` or
186
- * `false`) returns that value when git fails instead of throwing. For
187
- * callers that treat "could not ask" the same as "nothing there" —
188
- * `git.soft.resolveRef(x)` is `null` for a missing ref and for a broken
189
- * repo alike. Use `try` when the failure itself matters.
190
- *
191
- * As with `try`, the proxy also wraps `raw`, `rawChecked` and `at` at
192
- * runtime, though the type leaves them out.
193
- */
194
- get soft(): Soft<Git> {
195
- return (this.softened ??= softSync(this, SOFT_TABLE))
196
- }
197
-
198
- // ── escape hatch ──────────────────────────────────────────────────────────
199
-
200
- /** Unchecked passthrough for verbs not yet lifted into the API. */
201
- raw(
202
- argv: string[],
203
- opts: GitSyncCallOptions & { input?: string | undefined } = {},
204
- ): CommandResult {
205
- return this.runner.run(this.argv(argv), this.runOpts(opts))
206
- }
207
-
208
- /**
209
- * Checked passthrough — throws `CommandFailed` on a non-zero exit, and on
210
- * output cut short at the output cap or a timeout, as every verb does.
211
- *
212
- * `CommandFailed.message` interpolates stderr only, and several git commands
213
- * write the sentence that explains the failure to stdout instead — a
214
- * rejecting hook, `commit` on a clean tree, `apply --check`. Report
215
- * `e.detail`, which reads both.
216
- */
217
- rawChecked(
218
- argv: string[],
219
- opts: GitSyncCallOptions & { input?: string | undefined } = {},
220
- ): CommandResult {
221
- // Through the gate, like `AsyncGit.rawChecked`: the seam's `runChecked`
222
- // checks the exit code only, so it would pass cut-short output.
223
- const verb: GitVerb<CommandResult> = { argv, absent: [], parse: (r) => r }
224
- if (opts.input !== undefined) verb.input = opts.input
225
- return this.exec(verb, opts)
226
- }
227
-
228
- /** The verb's argv behind this client's repository binding. */
229
- private argv(argv: string[]): string[] {
230
- const prefix = repoPrefix(this.boundGitDir, this.workTree)
231
- return prefix.length === 0 ? argv : [...prefix, ...argv]
232
- }
233
-
234
- private runOpts(opts: GitSyncCallOptions & { input?: string | undefined }): CommandRunOptions {
235
- const run: CommandRunOptions = { cwd: this.cwd }
236
- if (opts.timeoutMs !== undefined) run.timeoutMs = opts.timeoutMs
237
- if (opts.maxOutputBytes !== undefined) run.maxOutputBytes = opts.maxOutputBytes
238
- // Per-call entries win over the client's.
239
- const env = mergeEnv(this.env, opts.env)
240
- if (env !== undefined) run.env = env
241
- if (opts.input !== undefined) run.input = opts.input
242
- return run
243
- }
244
-
245
- /** Run one verb from the table and gate its result. */
246
- private exec<T>(verb: GitVerb<T>, opts: GitSyncCallOptions = {}): T {
247
- const run = this.runOpts(opts)
248
- if (verb.input !== undefined) run.input = verb.input
249
- // The verb's own entries (the C locale) win over the client's and the call's.
250
- if (verb.env !== undefined) run.env = { ...run.env, ...verb.env }
251
- // The prefixed argv is what ran, so it is what a `CommandFailed` reports.
252
- const argv = this.argv(verb.argv)
253
- return gate(verb, this.runner.run(argv, run), argv)
254
- }
255
-
256
- // ── queries ───────────────────────────────────────────────────────────────
257
-
258
- /** A file's content at a rev (`git show <rev>:<relPath>`), null if absent. */
259
- showAtRev(rev: string, relPath: string, opts: GitSyncCallOptions = {}): string | null {
260
- return this.exec(v.showAtRev(rev, relPath), opts)
261
- }
262
-
263
- /**
264
- * Trimmed `git rev-parse <args>` output — the escape hatch for a query no
265
- * dedicated verb covers. See `revParse` in `verbs.ts`; for a ref, prefer
266
- * {@link Git.resolveRef}.
267
- *
268
- * Variadic. Call options go last, as an object:
269
- * `revParse('--show-toplevel', { timeoutMs: 5000 })`. The args are flags by
270
- * design, so a value that starts with `-` is not refused here.
271
- */
272
- revParse(...args: string[] | [...string[], GitSyncCallOptions]): string | null {
273
- const [argv, opts] = splitTrailingOptions<GitSyncCallOptions>(args)
274
- return this.exec(v.revParse(...argv), opts)
275
- }
276
-
277
- /**
278
- * The sha `ref` resolves to, or null when it does not
279
- * (`rev-parse --verify --quiet`). An unreadable store throws.
280
- */
281
- resolveRef(ref: string, opts: GitSyncCallOptions = {}): string | null {
282
- return this.exec(v.resolveRef(ref), opts)
283
- }
284
-
285
- /**
286
- * True iff this repository has any history — `resolveRef("HEAD") !== null`.
287
- *
288
- * The repository-health probe. False means an unborn HEAD only; an
289
- * unreadable store throws rather than reading as empty, which is why this is
290
- * not `objectExists("HEAD")`.
291
- */
292
- hasHistory(opts: GitSyncCallOptions = {}): boolean {
293
- return this.resolveRef('HEAD', opts) !== null
294
- }
295
-
296
- /**
297
- * HEAD's shortname (`rev-parse --abbrev-ref HEAD`). Reports the literal
298
- * `"HEAD"` on a detached HEAD, where {@link Git.currentBranch} reports null.
299
- * Throws on an unborn HEAD.
300
- */
301
- headRef(opts: GitSyncCallOptions = {}): string | null {
302
- return this.exec(v.headRef(), opts)
303
- }
304
-
305
- /** Absolute repo top-level directory, or null outside a repo. */
306
- topLevel(opts: GitSyncCallOptions = {}): string | null {
307
- return this.exec(v.topLevel(), opts)
308
- }
309
-
310
- /**
311
- * The shared `.git` dir (worktree-aware) as an absolute path, or null outside
312
- * a repo — see `gitCommonDir` in `verbs.ts`.
313
- */
314
- gitCommonDir(opts: GitSyncCallOptions = {}): string | null {
315
- return this.exec(v.gitCommonDir(), opts)
316
- }
317
-
318
- /**
319
- * This worktree's own `.git` dir as an absolute path, or null outside a repo
320
- * — see `gitDir` in `verbs.ts`. Not the constructor's `gitDir` option, which
321
- * binds the client to a repository.
322
- */
323
- gitDir(opts: GitSyncCallOptions = {}): string | null {
324
- return this.exec(v.gitDir(), opts)
325
- }
326
-
327
- /**
328
- * What a symbolic ref points at (full refname), or null when it is not one —
329
- * see `symbolicRef` in `verbs.ts`.
330
- */
331
- symbolicRef(ref: string, opts: GitSyncCallOptions = {}): string | null {
332
- return this.exec(v.symbolicRef(ref), opts)
333
- }
334
-
335
- /**
336
- * The primary checkout that owns this working directory, or null when it is
337
- * not inside a repository at all.
338
- *
339
- * Correct when the cwd is a linked worktree, which is what makes it worth a
340
- * method: `gitCommonDir()` is worktree-aware and absolute, so its parent is
341
- * the primary checkout in both cases. Callers kept rediscovering that pair.
342
- */
343
- mainCheckout(opts: GitSyncCallOptions = {}): string | null {
344
- const common = this.gitCommonDir(opts)
345
- return common === null ? null : dirname(common)
346
- }
347
-
348
- /** Current branch shortname, or null when detached. Throws outside a repo. */
349
- currentBranch(opts: GitSyncCallOptions = {}): string | null {
350
- return this.exec(v.currentBranch(), opts)
351
- }
352
-
353
- /**
354
- * `git status --porcelain` lines; `[]` on a clean tree, and a throw outside
355
- * a repository. `paths` narrows the report to those pathspecs.
356
- */
357
- statusPorcelain(opts: GitSyncCallOptions & StatusPorcelainOptions = {}): string[] {
358
- return this.exec(v.statusPorcelain(opts), opts)
359
- }
360
-
361
- /** Every changed or untracked file, from `status --porcelain -z`. */
362
- statusEntries(opts: GitSyncCallOptions = {}): StatusEntry[] {
363
- return this.exec(v.statusEntries(), opts)
364
- }
365
-
366
- /** True iff the working tree has any staged/unstaged/untracked change. */
367
- isDirty(opts: GitSyncCallOptions & StatusPorcelainOptions = {}): boolean {
368
- return this.statusPorcelain(opts).length > 0
369
- }
370
-
371
- /** Local branch shortnames matching `pattern` (all when omitted). */
372
- branches(pattern?: string | undefined, opts: GitSyncCallOptions = {}): string[] {
373
- return this.exec(v.branches(pattern), opts)
374
- }
375
-
376
- /** True iff local branch `name` exists (exact match). */
377
- branchExists(name: string, opts: GitSyncCallOptions = {}): boolean {
378
- return this.branches(name, opts).includes(name)
379
- }
380
-
381
- /** `git worktree list --porcelain`, parsed. Throws outside a repo. */
382
- worktrees(opts: GitSyncCallOptions = {}): Worktree[] {
383
- return this.exec(v.worktrees(), opts)
384
- }
385
-
386
- /** Merge base of two revs, or null when the histories are unrelated. */
387
- mergeBase(a: string, b: string, opts: GitSyncCallOptions = {}): string | null {
388
- return this.exec(v.mergeBase(a, b), opts)
389
- }
390
-
391
- /** Subject line of a ref's HEAD commit (`log -1 --format=%s`), or null. */
392
- logSubject(ref: string, opts: GitSyncCallOptions = {}): string | null {
393
- return this.exec(v.logSubject(ref), opts)
394
- }
395
-
396
- /** `git config --get <key>`, or null when unset. `scope` reads one file only. */
397
- configGet(key: string, opts: GitSyncCallOptions & ConfigGetOptions = {}): string | null {
398
- return this.exec(v.configGet(key, opts), opts)
399
- }
400
-
401
- /** Every value of a multi-valued key, verbatim; `[]` when unset. */
402
- configGetAll(key: string, opts: GitSyncCallOptions & ConfigGetOptions = {}): string[] {
403
- return this.exec(v.configGetAll(key, opts), opts)
404
- }
405
-
406
- /**
407
- * A key read as a boolean by git, or null when unset. A value git cannot
408
- * read as a boolean throws.
409
- */
410
- configGetBool(key: string, opts: GitSyncCallOptions & ConfigGetOptions = {}): boolean | null {
411
- return this.exec(v.configGetBool(key, opts), opts)
412
- }
413
-
414
- /** True iff `rev` resolves to an object (`cat-file -e`). */
415
- objectExists(rev: string, opts: GitSyncCallOptions = {}): boolean {
416
- return this.exec(v.objectExists(rev), opts)
417
- }
418
-
419
- /**
420
- * `ls-remote` rows for a remote (optionally pattern-filtered). `[]` means
421
- * the remote has no matching refs; an unreachable remote throws.
422
- * `timeoutMs` guards the network call.
423
- */
424
- lsRemote(
425
- remote: string,
426
- pattern?: string | undefined,
427
- opts: GitSyncCallOptions & LsRemoteOptions = {},
428
- ): RemoteRef[] {
429
- return this.exec(v.lsRemote(remote, pattern, opts), opts)
430
- }
431
-
432
- /**
433
- * The subset of `relPaths` that .gitignore rules ignore. Throws outside a
434
- * repository. Fail-closed callers: see `checkIgnore` in `verbs.ts`.
435
- */
436
- checkIgnore(relPaths: string[], opts: GitSyncCallOptions = {}): Set<string> {
437
- if (relPaths.length === 0) return new Set()
438
- return this.exec(v.checkIgnore(relPaths), opts)
439
- }
440
-
441
- /**
442
- * The ignore rule (file, line, pattern) behind each of `relPaths` that a rule mentions;
443
- * see `checkIgnoreVerbose` in `verbs.ts`. Throws outside a repository.
444
- */
445
- checkIgnoreVerbose(relPaths: string[], opts: GitSyncCallOptions = {}): IgnoreMatch[] {
446
- if (relPaths.length === 0) return []
447
- return this.exec(v.checkIgnoreVerbose(relPaths), opts)
448
- }
449
-
450
- /**
451
- * Tracked and/or untracked file paths (`ls-files -z`). Throws outside a
452
- * repository, where `topLevel()` returns null — see `lsFiles` in `verbs.ts`.
453
- */
454
- lsFiles(opts: GitSyncCallOptions & LsFilesOptions = {}): string[] {
455
- return this.exec(v.lsFiles(opts), opts)
456
- }
457
-
458
- /** Tracked files with their `ls-files -v` tag — see `lsFilesTagged` in `verbs.ts`. */
459
- lsFilesTagged(opts: GitSyncCallOptions = {}): TaggedFile[] {
460
- return this.exec(v.lsFilesTagged(), opts)
461
- }
462
-
463
- /**
464
- * The unified diff; `""` when nothing differs. A bad rev, or a cwd outside a
465
- * repository, throws. See `diffText` in `verbs.ts`.
466
- */
467
- diffText(rev?: string | undefined, opts: GitSyncCallOptions & DiffTextOptions = {}): string {
468
- return this.exec(v.diffText(rev, opts), opts)
469
- }
470
-
471
- /**
472
- * One untracked file rendered as an all-added diff. A path git cannot read
473
- * throws. See `diffAddedFile` in `verbs.ts`.
474
- */
475
- diffAddedFile(relPath: string, opts: GitSyncCallOptions = {}): string {
476
- return this.exec(v.diffAddedFile(relPath), opts)
477
- }
478
-
479
- /** `for-each-ref` rows, one line each, in the caller's format. */
480
- forEachRef(
481
- pattern: string | string[],
482
- opts: GitSyncCallOptions & ForEachRefOptions = {},
483
- ): string[] {
484
- return this.exec(v.forEachRef(pattern, opts), opts)
485
- }
486
-
487
- /**
488
- * Local refs matching `pattern` as `{ sha, ref }` rows — `ls-remote`'s
489
- * shape, for the repository you are standing in.
490
- */
491
- refs(pattern: string | string[], opts: GitSyncCallOptions = {}): RemoteRef[] {
492
- return parseRefRows(this.forEachRef(pattern, opts))
493
- }
494
-
495
- /** Changed paths (`diff --name-only -z`); `[]` when nothing changed. */
496
- diffNameOnly(opts: GitSyncCallOptions & DiffNameOnlyOptions = {}): string[] {
497
- return this.exec(v.diffNameOnly(opts), opts)
498
- }
499
-
500
- /** The `diff --stat` summary as raw stdout; empty when nothing changed. */
501
- diffStat(opts: GitSyncCallOptions & DiffStatOptions = {}): string {
502
- return this.exec(v.diffStat(opts), opts)
503
- }
504
-
505
- /** `git diff --check`: whitespace errors and conflict markers; findings are not a throw. */
506
- diffCheck(opts: GitSyncCallOptions & DiffCheckOptions = {}): DiffCheckResult {
507
- return this.exec(v.diffCheck(opts), opts)
508
- }
509
-
510
- /** Changed paths with their status; renames report the destination. */
511
- diffNameStatus(opts: GitSyncCallOptions & DiffNameStatusOptions = {}): DiffEntry[] {
512
- return this.exec(v.diffNameStatus(opts), opts)
513
- }
514
-
515
- /**
516
- * `git log` as raw stdout — the caller's `--format`, unsplit and untrimmed.
517
- * Throws on an empty repository; probe {@link Git.hasHistory} first, or use
518
- * `git.try.log()`. See `log` in `verbs.ts`.
519
- */
520
- log(opts: GitSyncCallOptions & LogOptions = {}): string {
521
- return this.exec(v.log(opts), opts)
522
- }
523
-
524
- /**
525
- * {@link Git.log}, split into non-blank lines.
526
- *
527
- * For a one-record-per-line `--format` only. A format that frames its own
528
- * records with `%x1e`, or interleaves `--name-status` blocks, must read the
529
- * bytes from `log`.
530
- */
531
- logLines(opts: GitSyncCallOptions & LogOptions = {}): string[] {
532
- return splitLines(this.log(opts))
533
- }
534
-
535
- /**
536
- * The entries of a tree at a rev (`ls-tree -z`); `[]` for a pathspec that
537
- * matches nothing, and a throw for a rev that does not resolve.
538
- */
539
- lsTree(rev: string, opts: GitSyncCallOptions & LsTreeOptions = {}): TreeEntry[] {
540
- return this.exec(v.lsTree(rev, opts), opts)
541
- }
542
-
543
- /**
544
- * The paths and statuses one commit changed. Pass `root` for a commit that
545
- * may be the first one — without it a root commit reports nothing.
546
- */
547
- diffTree(rev: string, opts: GitSyncCallOptions & DiffTreeOptions = {}): DiffEntry[] {
548
- return this.exec(v.diffTree(rev, opts), opts)
549
- }
550
-
551
- /**
552
- * Per-file line counts for one commit (`diff-tree --numstat -z`); `null`
553
- * counts for a binary file. Same options as {@link Git.diffTree}.
554
- */
555
- diffTreeNumstat(rev: string, opts: GitSyncCallOptions & DiffTreeOptions = {}): NumstatEntry[] {
556
- return this.exec(v.diffTreeNumstat(rev, opts), opts)
557
- }
558
-
559
- /** Matching lines (`git grep`); `[]` for no matches, a throw for a failure. */
560
- grep(pattern: string, opts: GitSyncCallOptions & GrepOptions = {}): string[] {
561
- return this.exec(v.grep(pattern, opts), opts)
562
- }
563
-
564
- /**
565
- * True iff `a` is an ancestor of `b`. A rev that does not resolve throws
566
- * rather than reading as "no".
567
- */
568
- isAncestor(a: string, b: string, opts: GitSyncCallOptions = {}): boolean {
569
- return this.exec(v.isAncestor(a, b), opts)
570
- }
571
-
572
- /** How far `tip` has diverged from `base`. See {@link AheadBehind}. */
573
- aheadBehind(base: string, tip: string, opts: GitSyncCallOptions = {}): AheadBehind {
574
- return this.exec(v.aheadBehind(base, tip), opts)
575
- }
576
-
577
- /** Configured remote names (`git remote`); `[]` when there are none. */
578
- remotes(opts: GitSyncCallOptions = {}): string[] {
579
- return this.exec(v.remote(), opts)
580
- }
581
-
582
- /**
583
- * The same as {@link Git.remotes}.
584
- *
585
- * @deprecated Use `remotes()`. The name `remote()` reads as one remote, but
586
- * it returns every remote name. It will be removed at 1.0.
587
- */
588
- remote(opts: GitSyncCallOptions = {}): string[] {
589
- return this.remotes(opts)
590
- }
591
-
592
- /**
593
- * A remote's fetch URL (push URL with `push`), or null when no remote by
594
- * that name exists.
595
- */
596
- remoteGetUrl(name: string, opts: GitSyncCallOptions & RemoteGetUrlOptions = {}): string | null {
597
- return this.exec(v.remoteGetUrl(name, opts), opts)
598
- }
599
-
600
- /** Every URL a remote has (push URLs with `push`); `[]` for an unknown remote. */
601
- remoteGetUrls(name: string, opts: GitSyncCallOptions & RemoteGetUrlOptions = {}): string[] {
602
- return this.exec(v.remoteGetUrls(name, opts), opts)
603
- }
604
-
605
- /**
606
- * The full refname the remote's HEAD points at, asked over the network; null
607
- * when the remote names none. See `remoteHead` in `verbs.ts`.
608
- */
609
- remoteHead(remote: string, opts: GitSyncCallOptions = {}): string | null {
610
- return this.exec(v.remoteHead(remote), opts)
611
- }
612
-
613
- /**
614
- * The coordinate (`owner`, `repo`, `host`, `port`) a remote's URL names, or
615
- * null when no such remote exists or its URL names no project. Any host: see
616
- * {@link parseRemoteUrl}. Outside a repository it throws, like
617
- * {@link Git.remoteGetUrl}. A composite helper, not a verb: it parses after
618
- * the verb runs, so it is written once per client.
619
- */
620
- remoteOwnerRepo(remote = 'origin', opts: GitSyncCallOptions = {}): RemoteCoordinate | null {
621
- const url = this.remoteGetUrl(remote, opts)
622
- return url === null ? null : parseRemoteUrl(url)
623
- }
624
-
625
- // ── mutations (nothing is absence — any non-zero exit throws) ─────────────
626
-
627
- /** `git fetch <remote> [refspec…]`. See {@link FetchOptions} for the flags. */
628
- fetch(
629
- remote: string,
630
- refspec?: string | string[] | undefined,
631
- opts: GitSyncCallOptions & FetchOptions = {},
632
- ): void {
633
- this.exec(v.fetch(remote, refspec, opts), opts)
634
- }
635
-
636
- /** Stage paths; `all` / `update` are `git add -A` / `-u`. */
637
- add(paths: string[], opts: GitSyncCallOptions & AddOptions = {}): void {
638
- this.exec(v.add(paths, opts), opts)
639
- }
640
-
641
- /**
642
- * Commit with an inline message, a message file or a stdin message (exactly
643
- * one). A failing hook throws; git writes hook output to stdout, which
644
- * `e.detail` reads.
645
- */
646
- commit(opts: GitSyncCallOptions & CommitOptions): void {
647
- this.exec(v.commit(opts), opts)
648
- }
649
-
650
- /** `git config <key> <value>` — the twin of {@link Git.configGet}. */
651
- configSet(key: string, value: string, opts: GitSyncCallOptions & ConfigSetOptions = {}): void {
652
- this.exec(v.configSet(key, value, opts), opts)
653
- }
654
-
655
- /** `git init [path]`; `path` defaults to this client's `cwd`. */
656
- init(path?: string | undefined, opts: GitSyncCallOptions & InitOptions = {}): void {
657
- this.exec(v.init(path, opts), opts)
658
- }
659
-
660
- /**
661
- * `git clone [--depth n] [--branch ref] <url> [dest]`.
662
- *
663
- * Runs from this client's `cwd`, which is where a relative `dest`
664
- * resolves. The clone itself is not a repository operation, so a client
665
- * bound to a store with `gitDir` is the wrong client to clone with.
666
- */
667
- clone(
668
- url: string,
669
- dest?: string | undefined,
670
- opts: GitSyncCallOptions & CloneOptions = {},
671
- ): void {
672
- this.exec(v.clone(url, dest, opts), opts)
673
- }
674
-
675
- /**
676
- * Push a refspec. A {@link ForceLease} pins `--force-with-lease`, which means
677
- * "nobody has touched this since I looked". `{forceWithLease: true}` adds the
678
- * bare flag, which is the unsafe form: it compares against the
679
- * remote-tracking ref, which a fetch in this same process may have just
680
- * updated. See `push` in `verbs.ts`.
681
- */
682
- push(
683
- remote: string,
684
- refspec: string,
685
- opts: GitSyncCallOptions & { forceWithLease?: boolean | ForceLease | undefined } = {},
686
- ): void {
687
- this.exec(v.push(remote, refspec, opts), opts)
688
- }
689
-
690
- /** `git clean -f`, plus `-d` with `directories`. Ignored files are kept. */
691
- clean(opts: GitSyncCallOptions & CleanOptions = {}): void {
692
- this.exec(v.clean(opts), opts)
693
- }
694
-
695
- /** `git reset --hard <rev>`. */
696
- resetHard(rev: string, opts: GitSyncCallOptions = {}): void {
697
- this.exec(v.resetHard(rev), opts)
698
- }
699
-
700
- /** Delete a local branch (`-d`, or `-D` with `{force: true}`). */
701
- deleteBranch(
702
- name: string,
703
- opts: GitSyncCallOptions & { force?: boolean | undefined } = {},
704
- ): void {
705
- this.exec(v.deleteBranch(name, opts), opts)
706
- }
707
-
708
- /**
709
- * Remove config section `name`; false when it was absent. See
710
- * `configRemoveSection` in `verbs.ts`.
711
- */
712
- configRemoveSection(name: string, opts: GitSyncCallOptions = {}): boolean {
713
- return this.exec(v.configRemoveSection(name), opts)
714
- }
715
-
716
- /** Delete `ref` only while it points at `expected`. See `deleteRef` in `verbs.ts`. */
717
- deleteRef(ref: string, opts: GitSyncCallOptions & { expected: string }): void {
718
- this.exec(v.deleteRef(ref, opts), opts)
719
- }
720
-
721
- /** `git worktree add [--detach] [-b branch] <path> [startPoint]`. */
722
- worktreeAdd(
723
- path: string,
724
- opts: GitSyncCallOptions & {
725
- branch?: string | undefined
726
- startPoint?: string | undefined
727
- detach?: boolean | undefined
728
- } = {},
729
- ): void {
730
- this.exec(v.worktreeAdd(path, opts), opts)
731
- }
732
-
733
- /** `git worktree remove [--force] <path>`. */
734
- worktreeRemove(
735
- path: string,
736
- opts: GitSyncCallOptions & { force?: boolean | undefined } = {},
737
- ): void {
738
- this.exec(v.worktreeRemove(path, opts), opts)
739
- }
740
-
741
- /** `git worktree move [--force] <from> <to>`. */
742
- worktreeMove(
743
- from: string,
744
- to: string,
745
- opts: GitSyncCallOptions & WorktreeMoveOptions = {},
746
- ): void {
747
- this.exec(v.worktreeMove(from, to, opts), opts)
748
- }
749
-
750
- /** `git worktree prune`. */
751
- worktreePrune(opts: GitSyncCallOptions = {}): void {
752
- this.exec(v.worktreePrune(), opts)
753
- }
754
-
755
- /** `git remote add <name> <url>`. */
756
- remoteAdd(name: string, url: string, opts: GitSyncCallOptions = {}): void {
757
- this.exec(v.remoteAdd(name, url), opts)
758
- }
759
-
760
- // ── integration ───────────────────────────────────────────────────────────
761
-
762
- /**
763
- * Merge `ref` into the current branch. A conflict is a value, not a throw:
764
- * the outcome is `clean` or `conflict`. Git refusing to merge at all still
765
- * throws `CommandFailed`.
766
- */
767
- merge(ref: string, opts: MergeOptions & GitSyncCallOptions = {}): MergeOutcome {
768
- return this.exec(v.merge(ref, opts), opts)
769
- }
770
-
771
- /** `git merge --abort`. Throws when no merge is in progress. */
772
- mergeAbort(opts: GitSyncCallOptions = {}): void {
773
- this.exec(v.mergeAbort(), opts)
774
- }
775
-
776
- /** Replay the current branch onto `onto`. Same outcome as {@link Git.merge}. */
777
- rebase(onto: string, opts: RebaseOptions & GitSyncCallOptions = {}): RebaseOutcome {
778
- return this.exec(v.rebase(onto, opts), opts)
779
- }
780
-
781
- /** `git rebase --abort`. Throws when no rebase is in progress. */
782
- rebaseAbort(opts: GitSyncCallOptions = {}): void {
783
- this.exec(v.rebaseAbort(), opts)
784
- }
785
-
786
- /** `git rebase --continue`, editor stubbed. May conflict again. */
787
- rebaseContinue(opts: GitSyncCallOptions = {}): RebaseOutcome {
788
- return this.exec(v.rebaseContinue(), opts)
789
- }
790
-
791
- /**
792
- * What merging `theirs` into `ours` would do — no index, no checkout, no
793
- * worktree. The cheap way to ask "would this conflict, and where", and the
794
- * merged `tree` it returns is real enough to build a commit from.
795
- */
796
- mergeTree(
797
- ours: string,
798
- theirs: string,
799
- opts: MergeTreeOptions & GitSyncCallOptions = {},
800
- ): MergeTreeOutcome {
801
- return this.exec(v.mergeTree(ours, theirs, opts), opts)
802
- }
803
-
804
- /**
805
- * `git ls-files -u`, parsed — which stages each conflicted path has, which
806
- * is what distinguishes both-modified from modify/delete. For the paths
807
- * alone, {@link Git.diffNameOnly} with `diffFilter: "U"` is cheaper.
808
- */
809
- lsFilesUnmerged(opts: GitSyncCallOptions = {}): UnmergedEntry[] {
810
- return this.exec(v.lsFilesUnmerged(), opts)
811
- }
812
-
813
- /**
814
- * Resolve conflicted `paths` to one side wholesale. An empty list returns
815
- * without running git. Silently does nothing for a path that is not
816
- * conflicted — see `checkoutSide` in `verbs.ts`.
817
- */
818
- checkoutSide(side: ConflictSide, paths: string[], opts: GitSyncCallOptions = {}): void {
819
- if (paths.length === 0) return
820
- this.exec(v.checkoutSide(side, paths), opts)
821
- }
822
-
823
- /** `git checkout [-f] [-b|-B <branch>] <ref>`. */
824
- checkout(ref: string, opts: CheckoutOptions & GitSyncCallOptions = {}): void {
825
- this.exec(v.checkout(ref, opts), opts)
826
- }
827
- }