@osdlc/typed-git 0.5.3 → 0.7.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 (49) hide show
  1. package/CHANGELOG.md +44 -1
  2. package/CONTRIBUTING.md +3 -4
  3. package/README.md +40 -16
  4. package/dist/async.d.ts +3 -4
  5. package/dist/async.js +4 -3
  6. package/dist/backend.d.ts +1 -2
  7. package/dist/checkout-root.d.ts +0 -1
  8. package/dist/client.d.ts +2 -2
  9. package/dist/client.js +1 -0
  10. package/dist/failure.d.ts +72 -0
  11. package/dist/failure.js +155 -0
  12. package/dist/git-url.d.ts +0 -1
  13. package/dist/index.d.ts +1 -2
  14. package/dist/index.js +1 -1
  15. package/dist/outcome.d.ts +14 -19
  16. package/dist/outcome.js +21 -16
  17. package/dist/remote-url.d.ts +0 -1
  18. package/dist/soft.d.ts +0 -1
  19. package/dist/sync.d.ts +5 -5
  20. package/dist/sync.js +5 -3
  21. package/dist/testing.d.ts +0 -1
  22. package/dist/verbs.d.ts +5 -7
  23. package/dist/verbs.js +18 -11
  24. package/package.json +13 -23
  25. package/async.ts +0 -753
  26. package/backend.ts +0 -574
  27. package/checkout-root.ts +0 -164
  28. package/client.ts +0 -102
  29. package/dist/async.d.ts.map +0 -1
  30. package/dist/backend.d.ts.map +0 -1
  31. package/dist/checkout-root.d.ts.map +0 -1
  32. package/dist/client.d.ts.map +0 -1
  33. package/dist/git-url.d.ts.map +0 -1
  34. package/dist/index.d.ts.map +0 -1
  35. package/dist/outcome.d.ts.map +0 -1
  36. package/dist/remote-url.d.ts.map +0 -1
  37. package/dist/soft.d.ts.map +0 -1
  38. package/dist/sync.d.ts.map +0 -1
  39. package/dist/testing.d.ts.map +0 -1
  40. package/dist/verbs.d.ts.map +0 -1
  41. package/git-url.ts +0 -102
  42. package/hosted-git-info.d.ts +0 -24
  43. package/index.ts +0 -52
  44. package/outcome.ts +0 -191
  45. package/remote-url.ts +0 -46
  46. package/soft.ts +0 -60
  47. package/sync.ts +0 -827
  48. package/testing.ts +0 -291
  49. package/verbs.ts +0 -2300
package/outcome.ts DELETED
@@ -1,191 +0,0 @@
1
- /**
2
- * The inspection door — `git.try.*`, and the {@link GitOutcome} it returns.
3
- *
4
- * For callers that genuinely tolerate failure: a hook that must never break a
5
- * commit, a status endpoint that must always answer. `try` is a facade over
6
- * the whole client rather than a per-verb option, so a new verb costs nothing
7
- * here. Two rules make it honest:
8
- *
9
- * - It catches `CommandFailed` and nothing else. A `TypeError` from a
10
- * programmer mistake propagates; this is not a blanket catch.
11
- * - A declared absence is `{ ok: true }`. `git.try.configGet("no.such.key")`
12
- * is `{ ok: true, value: null }`, because absence is an answer, not a
13
- * failure.
14
- */
15
-
16
- import { CommandFailed } from '@osdlc/command-seam'
17
-
18
- /** The result of a git call that was allowed to fail. */
19
- export type GitOutcome<T> = { ok: true; value: T } | { ok: false; error: CommandFailed }
20
-
21
- /**
22
- * The methods `try` mirrors: everything but the escape hatches, the
23
- * re-rooting helper, and `try` itself. `raw` already returns a
24
- * `CommandResult` and never throws; `rawChecked` is the deliberate throwing
25
- * door; `at(cwd)` returns a fresh client that has its own `.try`. The types
26
- * omit them, but the runtime proxy wraps every function property, these
27
- * included.
28
- */
29
- type Triable<C> = Omit<C, 'raw' | 'rawChecked' | 'at' | 'try' | 'soft' | 'cwd'>
30
-
31
- /** Every synchronous client method, returning a {@link GitOutcome}. */
32
- export type TriedSync<C> = {
33
- [K in keyof Triable<C>]: Triable<C>[K] extends (...a: infer A) => infer R
34
- ? (...a: A) => GitOutcome<R>
35
- : never
36
- }
37
-
38
- /** Every awaitable client method, promising the same union. */
39
- export type TriedAsync<C> = {
40
- [K in keyof Triable<C>]: Triable<C>[K] extends (...a: infer A) => Promise<infer R>
41
- ? (...a: A) => Promise<GitOutcome<R>>
42
- : never
43
- }
44
-
45
- /** `CommandFailed` becomes an outcome; anything else stays a throw. */
46
- function asOutcome<T>(error: unknown): GitOutcome<T> {
47
- if (error instanceof CommandFailed) return { ok: false, error }
48
- throw error
49
- }
50
-
51
- /**
52
- * A `Proxy` rather than a generated object: it mirrors whatever the client
53
- * has, including composed methods like `isDirty` that call through the gated
54
- * ones, without a per-verb entry to keep in step. Methods are invoked on the
55
- * client itself, not on the proxy, so private state stays reachable.
56
- */
57
- function facade<C extends object, F>(
58
- client: C,
59
- wrap: (call: () => unknown, name: string) => F,
60
- ): unknown {
61
- return new Proxy(client, {
62
- get(target, prop) {
63
- // Read from the target, not the proxy: a getter (`try` itself) must run
64
- // against the real client rather than recursing through the facade.
65
- const value: unknown = Reflect.get(target, prop)
66
- if (typeof value !== 'function') return value
67
- const method = value as (...a: unknown[]) => unknown
68
- const name = String(prop)
69
- return (...args: unknown[]) => wrap(() => method.apply(target, args), name)
70
- },
71
- })
72
- }
73
-
74
- /** Wrap a synchronous client so every method returns a {@link GitOutcome}. */
75
- export function triedSync<C extends object>(client: C): TriedSync<C> {
76
- return facade(client, (call): GitOutcome<unknown> => {
77
- try {
78
- return { ok: true, value: call() }
79
- } catch (error) {
80
- return asOutcome(error)
81
- }
82
- }) as TriedSync<C>
83
- }
84
-
85
- /** Wrap an awaitable client so every method promises a {@link GitOutcome}. */
86
- export function triedAsync<C extends object>(client: C): TriedAsync<C> {
87
- return facade(client, async (call): Promise<GitOutcome<unknown>> => {
88
- try {
89
- return { ok: true, value: await call() }
90
- } catch (error) {
91
- return asOutcome(error)
92
- }
93
- }) as TriedAsync<C>
94
- }
95
-
96
- // ── the soft door ───────────────────────────────────────────────────────────
97
-
98
- /**
99
- * What a verb's absence value is, by return type: `[]` for a list, `null` for
100
- * a nullable, `false` for a boolean. A verb returning anything else (a
101
- * record, a `Set`, free text) has no absence value, so it has no soft form.
102
- *
103
- * Mind the boolean kind: `false` is the answer for "could not ask" too, so
104
- * `soft.isDirty` reads a broken repo as clean and `soft.branchExists` as
105
- * "no such branch". Do not gate a destructive action on a `false` from `soft`;
106
- * use `try` there.
107
- */
108
- type SoftKind<R> = R extends readonly unknown[]
109
- ? 'empty'
110
- : null extends R
111
- ? 'null'
112
- : R extends boolean
113
- ? 'false'
114
- : never
115
-
116
- type Unwrap<R> = R extends Promise<infer U> ? U : R
117
-
118
- /**
119
- * Methods left out of the soft door although their return type has an absence
120
- * value. `configRemoveSection` is a mutation, and no mutation has a soft form.
121
- */
122
- type NoSoftForm = 'configRemoveSection'
123
-
124
- /** The client methods that have an absence value, each mapped to its {@link SoftKind}. */
125
- export type SoftKinds<C> = {
126
- [
127
- K in keyof Triable<C> as K extends NoSoftForm
128
- ? never
129
- : Triable<C>[K] extends (...a: never[]) => infer R
130
- ? [SoftKind<Unwrap<R>>] extends [never]
131
- ? never
132
- : K
133
- : never
134
- ]: Triable<C>[K] extends (...a: never[]) => infer R ? SoftKind<Unwrap<R>> : never
135
- }
136
-
137
- /**
138
- * Every client method that has an absence value, returning that value
139
- * instead of throwing `CommandFailed`. One type for both clients: an
140
- * awaitable method already returns a `Promise`.
141
- */
142
- export type Soft<C> = {
143
- [K in keyof SoftKinds<C>]: Triable<C>[K] extends (...a: infer A) => infer R
144
- ? (...a: A) => R
145
- : never
146
- }
147
-
148
- /**
149
- * The soft door's table type: which absence value each softable verb falls back
150
- * to. Typed against the client so a new list/nullable/boolean verb is a
151
- * compile error here until it is declared, and so a wrong entry cannot
152
- * compile; `Git` and `AsyncGit` read this one table, so they cannot drift.
153
- */
154
- export type SoftTable = Record<string, 'empty' | 'null' | 'false'>
155
-
156
- /** A table that must name exactly the softable methods of `C`, each with the right kind. */
157
- export type SoftTableFor<C> = SoftKinds<C>
158
-
159
- function fallbackFor(table: SoftTable, name: string): unknown {
160
- const kind = table[name]
161
- if (kind === undefined) {
162
- throw new TypeError(
163
- `git.soft.${name} has no absence value; call it through git.try (or the plain verb) instead`,
164
- )
165
- }
166
- return kind === 'empty' ? [] : kind === 'null' ? null : false
167
- }
168
-
169
- /** Wrap a synchronous client so a failed call returns the verb's absence value. */
170
- export function softSync<C extends object>(client: C, table: SoftTable): Soft<C> {
171
- return facade(client, (call, name) => {
172
- try {
173
- return call()
174
- } catch (error) {
175
- if (error instanceof CommandFailed) return fallbackFor(table, name)
176
- throw error
177
- }
178
- }) as Soft<C>
179
- }
180
-
181
- /** Wrap an awaitable client so a failed call resolves to the verb's absence value. */
182
- export function softAsync<C extends object>(client: C, table: SoftTable): Soft<C> {
183
- return facade(client, async (call, name) => {
184
- try {
185
- return await call()
186
- } catch (error) {
187
- if (error instanceof CommandFailed) return fallbackFor(table, name)
188
- throw error
189
- }
190
- }) as Soft<C>
191
- }
package/remote-url.ts DELETED
@@ -1,46 +0,0 @@
1
- /**
2
- * Host-agnostic reading of a git remote URL into its coordinate.
3
- *
4
- * `parseGitUrl` (git-url.ts) is over `hosted-git-info`, which only knows a
5
- * fixed allowlist of hosts and answers null for a self-hosted Forgejo or
6
- * Gitea. A remote read out of a checkout's own config is always a clone URL
7
- * (scp-style or `scheme://`), so this parses that shape for ANY host and
8
- * needs no dependency — which is why it, not `parseGitUrl`, backs
9
- * `Git.remoteOwnerRepo` and stays on the `./client` entry.
10
- */
11
-
12
- /** A remote clone URL split into where it lives and what it names. */
13
- export interface RemoteCoordinate {
14
- owner: string
15
- repo: string
16
- host: string
17
- /** The port of an `http(s)://` remote, or null. scp-style has none; `ssh://` ports are not carried. */
18
- port: number | null
19
- }
20
-
21
- /**
22
- * Parses `git@host:owner/repo.git` (scp-style) and
23
- * `scheme://[user@]host[:port]/owner/repo[.git]`. The last two path segments
24
- * are the owner and repo, so a nested group (`group/sub/repo`) answers
25
- * `owner: "sub"`. Returns null for anything else (a local path, a single
26
- * path segment).
27
- *
28
- * Only an `http(s)://` port is read. An `ssh://` port is dropped, so `port` is
29
- * null for `ssh://git@host:2222/owner/repo.git`.
30
- */
31
- export function parseRemoteUrl(url: string): RemoteCoordinate | null {
32
- const trimmed = url.trim()
33
- const scp = /^[\w.-]+@([^:/]+):(.+)$/.exec(trimmed)
34
- const uri = /^[a-z][a-z0-9+.-]*:\/\/(?:[^@/]+@)?([^/:]+)(?::(\d+))?\/(.+)$/i.exec(trimmed)
35
- const match = scp ?? uri
36
- if (match === null) return null
37
- const host = match[1]!
38
- // `scp` has no port syntax: its `:` separates host and path.
39
- const isHttp = match === uri && /^https?:/i.test(trimmed)
40
- const port = isHttp && match[2] !== undefined ? Number(match[2]) : null
41
- const rest = match === uri ? match[3]! : match[2]!
42
- const path = rest.replace(/\.git$/, '').replace(/^\/+|\/+$/g, '')
43
- const parts = path.split('/').filter((p) => p !== '')
44
- if (parts.length < 2) return null
45
- return { host, port, owner: parts[parts.length - 2]!, repo: parts[parts.length - 1]! }
46
- }
package/soft.ts DELETED
@@ -1,60 +0,0 @@
1
- /**
2
- * The soft door's table — the one place that says which verbs have a
3
- * tolerant form and what they fall back to.
4
- *
5
- * Typed against `Git` and `AsyncGit` both, so adding a list-, nullable- or
6
- * boolean-returning verb to either client is a compile error here until it
7
- * is declared, and the two clients cannot grow different soft doors.
8
- * Mutations have no soft form, so `configRemoveSection` is not here.
9
- */
10
-
11
- import type { AsyncGit } from './async.ts'
12
- import type { SoftTableFor } from './outcome.ts'
13
- import type { Git } from './sync.ts'
14
-
15
- export const SOFT_TABLE = {
16
- showAtRev: 'null',
17
- revParse: 'null',
18
- resolveRef: 'null',
19
- hasHistory: 'false',
20
- headRef: 'null',
21
- topLevel: 'null',
22
- gitCommonDir: 'null',
23
- gitDir: 'null',
24
- symbolicRef: 'null',
25
- mainCheckout: 'null',
26
- currentBranch: 'null',
27
- statusPorcelain: 'empty',
28
- statusEntries: 'empty',
29
- isDirty: 'false',
30
- branches: 'empty',
31
- branchExists: 'false',
32
- worktrees: 'empty',
33
- mergeBase: 'null',
34
- logSubject: 'null',
35
- configGet: 'null',
36
- configGetAll: 'empty',
37
- configGetBool: 'null',
38
- objectExists: 'false',
39
- lsRemote: 'empty',
40
- lsFiles: 'empty',
41
- lsFilesTagged: 'empty',
42
- refs: 'empty',
43
- forEachRef: 'empty',
44
- diffNameOnly: 'empty',
45
- diffNameStatus: 'empty',
46
- logLines: 'empty',
47
- lsTree: 'empty',
48
- diffTree: 'empty',
49
- diffTreeNumstat: 'empty',
50
- grep: 'empty',
51
- isAncestor: 'false',
52
- remotes: 'empty',
53
- remote: 'empty',
54
- remoteGetUrl: 'null',
55
- remoteGetUrls: 'empty',
56
- remoteHead: 'null',
57
- remoteOwnerRepo: 'null',
58
- lsFilesUnmerged: 'empty',
59
- checkIgnoreVerbose: 'empty',
60
- } as const satisfies SoftTableFor<Git> & SoftTableFor<AsyncGit>