@zabaca/zbc 0.20.0 → 0.21.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/package.json +1 -1
- package/templates/apps/walgit/CONTEXT.md +37 -0
- package/templates/apps/walgit/README.md +193 -4
- package/templates/apps/walgit/e2e/harness.ts +2 -2
- package/templates/apps/walgit/e2e/scenarios.ts +302 -2
- package/templates/apps/walgit/package.json +5 -1
- package/templates/apps/walgit/registry.json +1 -1
- package/templates/apps/walgit/shared/browse.ts +733 -0
- package/templates/apps/walgit/shared/capabilities.ts +17 -0
- package/templates/apps/walgit/shared/container-env.ts +9 -0
- package/templates/apps/walgit/shared/landing.ts +8 -27
- package/templates/apps/walgit/shared/llms.ts +41 -0
- package/templates/apps/walgit/shared/mcp.ts +286 -0
- package/templates/apps/walgit/shared/policy.ts +21 -0
- package/templates/apps/walgit/shared/pooled.ts +31 -0
- package/templates/apps/walgit/shared/protocol.ts +140 -0
- package/templates/apps/walgit/shared/repo-list.ts +478 -0
- package/templates/apps/walgit/shared/store.ts +307 -0
- package/templates/apps/walgit/shared/telemetry.ts +79 -6
- package/templates/apps/walgit/shared/wal-index.ts +249 -0
- package/templates/apps/walgit/shared/web.ts +46 -0
- package/templates/apps/walgit/src/announce-main.ts +1 -1
- package/templates/apps/walgit/src/announce.ts +1 -1
- package/templates/apps/walgit/src/append-only.ts +5 -8
- package/templates/apps/walgit/src/browse.ts +230 -0
- package/templates/apps/walgit/src/cache.ts +4 -4
- package/templates/apps/walgit/src/compact.ts +6 -12
- package/templates/apps/walgit/src/delete-repo.ts +4 -3
- package/templates/apps/walgit/src/expire.ts +3 -3
- package/templates/apps/walgit/src/gc.ts +4 -3
- package/templates/apps/walgit/src/git.ts +127 -10
- package/templates/apps/walgit/src/hook-main.ts +1 -1
- package/templates/apps/walgit/src/http.ts +386 -3
- package/templates/apps/walgit/src/limits.ts +1 -1
- package/templates/apps/walgit/src/materialize.bench.ts +4 -10
- package/templates/apps/walgit/src/materialize.ts +11 -4
- package/templates/apps/walgit/src/orphans.ts +3 -3
- package/templates/apps/walgit/src/pending.ts +1 -1
- package/templates/apps/walgit/src/private.ts +1 -1
- package/templates/apps/walgit/src/proposals.ts +3 -3
- package/templates/apps/walgit/src/push-cert.ts +10 -1
- package/templates/apps/walgit/src/push.ts +11 -9
- package/templates/apps/walgit/src/reconcile.ts +4 -3
- package/templates/apps/walgit/src/server.ts +29 -2
- package/templates/apps/walgit/src/signers.ts +9 -3
- package/templates/apps/walgit/src/store-env.ts +8 -21
- package/templates/apps/walgit/src/store.ts +12 -241
- package/templates/apps/walgit/src/sync.ts +2 -2
- package/templates/apps/walgit/src/usage.ts +4 -18
- package/templates/apps/walgit/src/verify.ts +2 -2
- package/templates/apps/walgit/src/wal-index.ts +20 -239
- package/templates/apps/walgit/worker/index.ts +278 -1
- package/templates/apps/walgit/worker/mcp.ts +281 -0
- /package/templates/apps/walgit/{src → shared}/keys.ts +0 -0
- /package/templates/apps/walgit/{src → shared}/ulid.ts +0 -0
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `index.json` — the source of truth for one repository, and how to READ it.
|
|
3
|
+
*
|
|
4
|
+
* It carries the full ref state, so there is no database beside it. A push is
|
|
5
|
+
* published by writing a new version of this object under a compare-and-swap;
|
|
6
|
+
* until that write lands, the packfile already uploaded to the WAL is present
|
|
7
|
+
* but unpublished, and nothing can see it.
|
|
8
|
+
*
|
|
9
|
+
* The Index’s types and its reader live in the Shared Kernel because reading
|
|
10
|
+
* the log is not a container privilege: the edge answers questions off
|
|
11
|
+
* `index.json` too, and the answer must be the same object model on both sides
|
|
12
|
+
* (docs/adr/0010). Writing it is not here — `src/wal-index.ts` owns the
|
|
13
|
+
* compare-and-swap, the `apply*` helpers and `sha256`, all of which belong to
|
|
14
|
+
* the process that holds the disk.
|
|
15
|
+
*
|
|
16
|
+
* See this repository’s docs/adr/0007-walgit-object-storage-holds-the-log.md.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { indexKey } from './keys'
|
|
20
|
+
import type { ObjectStore } from './store'
|
|
21
|
+
|
|
22
|
+
export type WalEntryKind = 'push' | 'compaction'
|
|
23
|
+
|
|
24
|
+
export interface WalEntry {
|
|
25
|
+
seq: number
|
|
26
|
+
/** Object key of the packfile, e.g. `wal/000000000042-01J….pack`. */
|
|
27
|
+
key: string
|
|
28
|
+
kind: WalEntryKind
|
|
29
|
+
size: number
|
|
30
|
+
sha256: string
|
|
31
|
+
ts: string
|
|
32
|
+
/** Compaction only: entries at or below this seq are no longer needed. */
|
|
33
|
+
supersedes_through?: number
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* A superseded WAL object, scheduled for deletion but not yet deleted.
|
|
38
|
+
*
|
|
39
|
+
* The delay is the whole point. A compaction's compare-and-swap advances the
|
|
40
|
+
* frontier instantly, but a restore that read `index.json` a moment earlier is
|
|
41
|
+
* still downloading the entries that CAS just superseded — and deleting them
|
|
42
|
+
* out from under it fails the restore with a missing object, which is ranked
|
|
43
|
+
* risk #2 in docs/adr/0007 precisely because nothing shouts. So the CAS records
|
|
44
|
+
* an intent to delete, and collection happens later, out of band.
|
|
45
|
+
*/
|
|
46
|
+
export interface Tombstone {
|
|
47
|
+
/** The object key. Its sibling `.idx` is collected with it. */
|
|
48
|
+
key: string
|
|
49
|
+
/** The compaction entry whose pack now contains these objects. */
|
|
50
|
+
superseded_by: number
|
|
51
|
+
/** ISO instant before which this key must not be deleted. */
|
|
52
|
+
collect_after: string
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* A repository scheduled for deletion.
|
|
57
|
+
*
|
|
58
|
+
* Deletion is deferred for the same reason a tombstone is: a clone that read
|
|
59
|
+
* this index a moment ago is still downloading the packs it names, and pulling
|
|
60
|
+
* them out from under it fails the clone with a missing object. The marker is
|
|
61
|
+
* written under compare-and-swap like everything else, so an operator asking
|
|
62
|
+
* twice does not shorten the wait — the first request's `collect_after` stands.
|
|
63
|
+
*/
|
|
64
|
+
export interface RepoDeletion {
|
|
65
|
+
/** ISO instant the operator asked for the repository to go. */
|
|
66
|
+
requested_at: string
|
|
67
|
+
/** ISO instant before which nothing under the repo prefix may be deleted. */
|
|
68
|
+
collect_after: string
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface WalIndex {
|
|
72
|
+
version: 1
|
|
73
|
+
repo_id: string
|
|
74
|
+
/** Highest WAL entry applied. Monotonic. */
|
|
75
|
+
seq: number
|
|
76
|
+
entries: WalEntry[]
|
|
77
|
+
/** Full ref state after applying every entry. `refs/heads/main` → oid. */
|
|
78
|
+
refs: Record<string, string>
|
|
79
|
+
/** Entries below this are not needed to restore. */
|
|
80
|
+
compaction_frontier: number
|
|
81
|
+
/**
|
|
82
|
+
* Superseded keys awaiting collection. Optional because an index written
|
|
83
|
+
* before compaction existed does not have the field; absent reads as empty.
|
|
84
|
+
*/
|
|
85
|
+
tombstones?: Tombstone[]
|
|
86
|
+
/**
|
|
87
|
+
* Set when this repository has been scheduled for deletion. Its presence is
|
|
88
|
+
* what makes the second run of `walgit delete` a collection rather than a
|
|
89
|
+
* second request.
|
|
90
|
+
*/
|
|
91
|
+
deletion?: RepoDeletion
|
|
92
|
+
/**
|
|
93
|
+
* Push provenance: ref → who signed the push that moved it to the sha `refs`
|
|
94
|
+
* currently holds, and when (docs/adr/0011).
|
|
95
|
+
*
|
|
96
|
+
* A second map beside `refs` rather than a widening of it, for three reasons
|
|
97
|
+
* that each rule out the alternatives. It is not hung off a `WalEntry`: a
|
|
98
|
+
* ref-only push appends none, so provenance would be blind to a whole class
|
|
99
|
+
* of push. It is not kept on the certificate blob: that lives on the Cache,
|
|
100
|
+
* which is wiped on restart. And it is not a richer value inside `refs`,
|
|
101
|
+
* because every existing reader of the Index — reconcile, materialize, the
|
|
102
|
+
* event handshake — would then have to change to keep reading a sha.
|
|
103
|
+
*
|
|
104
|
+
* Latest-state per ref, like everything else here: there is no provenance
|
|
105
|
+
* history, because the audit trail of *content* is the commit graph and a
|
|
106
|
+
* second ledger would be a second thing to keep true.
|
|
107
|
+
*
|
|
108
|
+
* Optional and absent when empty, so an unsigned deployment's index.json is
|
|
109
|
+
* byte-for-byte what it was before this field existed.
|
|
110
|
+
*/
|
|
111
|
+
provenance?: Record<string, Provenance>
|
|
112
|
+
/**
|
|
113
|
+
* The repository's Signer List, as the push that last moved `refs/walgit/signers`
|
|
114
|
+
* resolved it (docs/adr/0012). Absent means unclaimed, which is every
|
|
115
|
+
* repository until someone writes one.
|
|
116
|
+
*
|
|
117
|
+
* Repo-level rather than per-ref, unlike `provenance` above it: the list
|
|
118
|
+
* governs the name, not a branch.
|
|
119
|
+
*
|
|
120
|
+
* The ref is authoritative and this is a derived copy, which is safe rather
|
|
121
|
+
* than a second source of truth — it is written by the same compare-and-swap
|
|
122
|
+
* that publishes the ref move, derived from bytes in that same push, and a
|
|
123
|
+
* restore replays the ref and this field together because both live here. The
|
|
124
|
+
* copy exists because the refusal that will read it runs in `pre-receive`,
|
|
125
|
+
* which already loads the Index; resolving from the Cache instead would make
|
|
126
|
+
* ownership depend on the Cache being materialized, and the Cache is
|
|
127
|
+
* disposable by definition (ADR-0007).
|
|
128
|
+
*
|
|
129
|
+
* It cannot be derived from `provenance`, which is latest-state per ref and
|
|
130
|
+
* overwritten by every push — by the time it mattered it would name the most
|
|
131
|
+
* recent Signer rather than the founding one.
|
|
132
|
+
*/
|
|
133
|
+
claim?: Claim
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Who moved a ref, and when it landed. */
|
|
137
|
+
export interface Provenance {
|
|
138
|
+
/**
|
|
139
|
+
* The SSH key fingerprint that signed the push, as `ssh-keygen` spells it:
|
|
140
|
+
* `SHA256:` and 43 characters of base64. A key, never a user or an account —
|
|
141
|
+
* neither exists here, and naming it either would imply a registry walgit
|
|
142
|
+
* deliberately does not have.
|
|
143
|
+
*/
|
|
144
|
+
signer: string
|
|
145
|
+
/** ISO instant the push was received. */
|
|
146
|
+
ts: string
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** The keys a repository's Signer List names, and when that list landed. */
|
|
150
|
+
export interface Claim {
|
|
151
|
+
/**
|
|
152
|
+
* Fingerprints, in the order the file names them and with duplicates already
|
|
153
|
+
* collapsed — the list as `parseKeyList` read it, never the raw file. What
|
|
154
|
+
* is stored is the resolved answer, so a reader never re-parses and cannot
|
|
155
|
+
* reach a different one.
|
|
156
|
+
*/
|
|
157
|
+
signers: string[]
|
|
158
|
+
/**
|
|
159
|
+
* The keys the repository lets READ it — its Reader List (docs/adr/0013) —
|
|
160
|
+
* as `parseKeyList` read the `readers` file beside `signers` in the same
|
|
161
|
+
* commit. Absent when that file is absent, which is every repository until
|
|
162
|
+
* someone writes one: presence is the switch, so there is no `private`
|
|
163
|
+
* marker and no third state.
|
|
164
|
+
*
|
|
165
|
+
* `[]` is a value rather than an absence, and the difference is the whole
|
|
166
|
+
* capability: an empty Reader List is valid and means the Signer List reads
|
|
167
|
+
* this repository and nobody else, while no field at all means anyone does.
|
|
168
|
+
*
|
|
169
|
+
* Maintained only while the Private seed is set, exactly as the Claim around
|
|
170
|
+
* it is maintained only under the Signer List flag — the ref is what is
|
|
171
|
+
* authoritative and this is a derived copy (see `applyClaim`).
|
|
172
|
+
*/
|
|
173
|
+
readers?: string[]
|
|
174
|
+
/** ISO instant the push that wrote this list was received. */
|
|
175
|
+
ts: string
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* What one push records about who made it — applied by the same compare-and-swap
|
|
180
|
+
* that publishes the push, which is why they travel together rather than as two
|
|
181
|
+
* nullable tail arguments nobody can keep straight at a call site.
|
|
182
|
+
*/
|
|
183
|
+
export interface PushRecord {
|
|
184
|
+
/** The Signer of this push, or `null` for an anonymous one. */
|
|
185
|
+
provenance: Provenance | null
|
|
186
|
+
/** The Signer List this push writes, or `null` when it writes none. */
|
|
187
|
+
claim: Claim | null
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** A ref change as `reference-transaction` reports it on stdin. */
|
|
191
|
+
export interface RefChange {
|
|
192
|
+
ref: string
|
|
193
|
+
oldOid: string
|
|
194
|
+
newOid: string
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export function emptyIndex(repoId: string): WalIndex {
|
|
198
|
+
return {
|
|
199
|
+
version: 1,
|
|
200
|
+
repo_id: repoId,
|
|
201
|
+
seq: 0,
|
|
202
|
+
entries: [],
|
|
203
|
+
refs: {},
|
|
204
|
+
compaction_frontier: 0,
|
|
205
|
+
tombstones: [],
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// ── Reading ─────────────────────────────────────────────────────────────────
|
|
210
|
+
|
|
211
|
+
export interface LoadedIndex {
|
|
212
|
+
index: WalIndex
|
|
213
|
+
/** ETag to guard the next write with. `null` when the object does not exist. */
|
|
214
|
+
etag: string | null
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export async function loadIndex(store: ObjectStore, repoId: string): Promise<LoadedIndex> {
|
|
218
|
+
const found = await store.get(indexKey(repoId))
|
|
219
|
+
if (!found) return { index: emptyIndex(repoId), etag: null }
|
|
220
|
+
return { index: parseIndex(found.body, repoId), etag: found.etag }
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Cheap currency check for the read path: `not-modified` costs one metadata
|
|
225
|
+
* round trip, which is what lets a node serve from its local cache without
|
|
226
|
+
* trusting it blindly.
|
|
227
|
+
*/
|
|
228
|
+
export async function loadIndexIfChanged(
|
|
229
|
+
store: ObjectStore,
|
|
230
|
+
repoId: string,
|
|
231
|
+
knownEtag: string,
|
|
232
|
+
): Promise<LoadedIndex | 'current'> {
|
|
233
|
+
const res = await store.getIfNoneMatch(indexKey(repoId), knownEtag)
|
|
234
|
+
if (res.status === 'not-modified') return 'current'
|
|
235
|
+
if (res.status === 'absent') return { index: emptyIndex(repoId), etag: null }
|
|
236
|
+
return { index: parseIndex(res.body, repoId), etag: res.etag }
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function parseIndex(body: Uint8Array, repoId: string): WalIndex {
|
|
240
|
+
const parsed = JSON.parse(new TextDecoder().decode(body)) as WalIndex
|
|
241
|
+
// Guard the two invariants a corrupted or misrouted object would break. A
|
|
242
|
+
// wrong repo_id means the key routing is wrong, which silently serving would
|
|
243
|
+
// turn into cross-repository data loss.
|
|
244
|
+
if (parsed.version !== 1) throw new Error(`index.json: unsupported version ${parsed.version}`)
|
|
245
|
+
if (parsed.repo_id !== repoId) {
|
|
246
|
+
throw new Error(`index.json at ${repoId} declares repo_id "${parsed.repo_id}"`)
|
|
247
|
+
}
|
|
248
|
+
return parsed
|
|
249
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the two web-view pages share: the look, and the one cache rule.
|
|
3
|
+
*
|
|
4
|
+
* `shared/repo-list.ts` and `shared/browse.ts` are one product — a reader moves
|
|
5
|
+
* between them by clicking — and they were each carrying their own copy of the
|
|
6
|
+
* stylesheet and of the "cachable only where a read takes no credential" line.
|
|
7
|
+
* A token change then needed two edits, and the second one is the edit nobody
|
|
8
|
+
* makes: the review of the list itself found four such copies (`shortBytes`,
|
|
9
|
+
* `escapeHtml`, the pool, the store reading) and ended them, so a fifth is not
|
|
10
|
+
* a thing to start.
|
|
11
|
+
*
|
|
12
|
+
* Runtime-neutral like everything here (docs/adr/0010): a stylesheet is a
|
|
13
|
+
* string and a cache header is a string.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import type { Capabilities } from './capabilities'
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The web view's stylesheet — one inline block, no build step, no request.
|
|
20
|
+
*
|
|
21
|
+
* Every page that uses it carries a CSP that permits `style-src 'unsafe-inline'`
|
|
22
|
+
* and nothing else, which is what makes an inline block the whole of the
|
|
23
|
+
* styling rather than the first of several resources.
|
|
24
|
+
*/
|
|
25
|
+
export const WEB_STYLE = ` :root { color-scheme: light dark; --ink: #14100e; --ground: #ede6de; --muted: #6b635c; --accent: #c56a3e; }
|
|
26
|
+
@media (prefers-color-scheme: dark) { :root { --ink: #ede6de; --ground: #14100e; --muted: #9a9088; } }
|
|
27
|
+
body { margin: 0; padding: 2rem 1.25rem 4rem; background: var(--ground); color: var(--ink);
|
|
28
|
+
font: 15px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace; }
|
|
29
|
+
main { max-width: 52rem; margin: 0 auto; }
|
|
30
|
+
h1 { font-size: 1rem; font-weight: 600; margin: 0 0 1rem; }
|
|
31
|
+
table { width: 100%; border-collapse: collapse; }
|
|
32
|
+
th, td { text-align: left; padding: .35rem .5rem; border-bottom: 1px solid color-mix(in srgb, var(--muted) 30%, transparent); }
|
|
33
|
+
th { font-weight: 600; color: var(--muted); }
|
|
34
|
+
td.f, th.f { text-align: right; white-space: nowrap; color: var(--muted); }
|
|
35
|
+
a { color: var(--accent); }
|
|
36
|
+
.tag { font-size: .8em; color: var(--muted); border: 1px solid var(--muted); border-radius: 3px; padding: 0 .3em; }
|
|
37
|
+
.note { color: var(--muted); }`
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* A minute where a read takes no credential, nothing otherwise.
|
|
41
|
+
*
|
|
42
|
+
* The second half is the one that matters: a shared cache must not hold a
|
|
43
|
+
* document that exists only because THIS request presented a token.
|
|
44
|
+
*/
|
|
45
|
+
export const webCacheControl = (caps: Capabilities): string =>
|
|
46
|
+
caps.publicAccess ? 'public, max-age=60' : 'private, no-store'
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
|
|
24
24
|
import { eventsFromChanges } from '../shared/events'
|
|
25
25
|
import { ANNOUNCE_PATH, SIGNERS_REF } from '../shared/protocol'
|
|
26
|
-
import type { RefChange } from '
|
|
26
|
+
import type { RefChange } from '../shared/wal-index'
|
|
27
27
|
|
|
28
28
|
export interface AnnounceConfig {
|
|
29
29
|
/** The deployment's own public origin — the Worker in front of this container. */
|
|
@@ -24,7 +24,7 @@ import * as crypto from 'node:crypto'
|
|
|
24
24
|
import { git } from './git'
|
|
25
25
|
import { flagEnabled } from '../shared/policy'
|
|
26
26
|
import { ZERO_OID } from '../shared/protocol'
|
|
27
|
-
import type { RefChange } from '
|
|
27
|
+
import type { RefChange } from '../shared/wal-index'
|
|
28
28
|
|
|
29
29
|
/** The env flag an instance sets to make its repositories append-only. */
|
|
30
30
|
export function appendOnlyEnabled(env: Record<string, string | undefined> = process.env): boolean {
|
|
@@ -56,14 +56,11 @@ export type RefVerdict =
|
|
|
56
56
|
export function judgeRefChange(gitDir: string, change: RefChange): RefVerdict {
|
|
57
57
|
if (change.oldOid === ZERO_OID) return { allowed: true }
|
|
58
58
|
if (change.newOid === ZERO_OID) return { allowed: false, kind: 'delete', ref: change.ref }
|
|
59
|
-
const res = git([
|
|
60
|
-
'--git-dir',
|
|
59
|
+
const res = git(['merge-base', '--is-ancestor'], {
|
|
61
60
|
gitDir,
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
change.newOid,
|
|
66
|
-
])
|
|
61
|
+
inheritObjects: true,
|
|
62
|
+
operands: [change.oldOid, change.newOid],
|
|
63
|
+
})
|
|
67
64
|
if (res.status === 0) return { allowed: true }
|
|
68
65
|
return { allowed: false, kind: 'rewrite', ref: change.ref }
|
|
69
66
|
}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One level of one directory, read off the Cache.
|
|
3
|
+
*
|
|
4
|
+
* This is the whole reason a browse is answered by the container rather than at
|
|
5
|
+
* the edge like the repository list: the Index carries the full ref state and
|
|
6
|
+
* not a single object, so the only thing that can say what is inside a tree is
|
|
7
|
+
* git, on the disk the log was Materialized onto (docs/adr/0007).
|
|
8
|
+
*
|
|
9
|
+
* ── one level, never recursive ──────────────────────────────────────────────
|
|
10
|
+
*
|
|
11
|
+
* `ls-tree` without `-r`. A recursive read of a large repository is unbounded
|
|
12
|
+
* work on the request path, in the one container that is also serving pushes,
|
|
13
|
+
* for a page that shows one directory. Clicking into a directory is another
|
|
14
|
+
* request, which is what pagination looks like when the tree is the index.
|
|
15
|
+
*
|
|
16
|
+
* ── every argument is data ──────────────────────────────────────────────────
|
|
17
|
+
*
|
|
18
|
+
* The revision goes in `operands` and the directory in `paths`, so git's own
|
|
19
|
+
* `--end-of-options` and `--` fences stand between an untrusted URL and a
|
|
20
|
+
* command line (`src/git.ts`). Nothing here concatenates either into `args`.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { OID, type Commit, type TreeEntry } from '../shared/browse'
|
|
24
|
+
import { git, gitBytes } from './git'
|
|
25
|
+
|
|
26
|
+
/** git's modes, as the four kinds a reader can actually see apart. */
|
|
27
|
+
const SYMLINK_MODE = '120000'
|
|
28
|
+
const GITLINK_MODE = '160000'
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* One level of the tree at `rev`:`dir`, or `null` when that is not a directory.
|
|
32
|
+
*
|
|
33
|
+
* `null` for a blob, for a path that is not there and for a revision this Cache
|
|
34
|
+
* does not hold — all three are "there is no directory to show", and git has no
|
|
35
|
+
* empty tree, so empty output is the absence rather than an empty listing.
|
|
36
|
+
*
|
|
37
|
+
* Async because the caller is (`src/http.ts` awaits it beside two store reads),
|
|
38
|
+
* not because git is: `spawnSync` is what every other git call here uses, and a
|
|
39
|
+
* browse is one command.
|
|
40
|
+
*/
|
|
41
|
+
export async function listTree(
|
|
42
|
+
gitDir: string,
|
|
43
|
+
rev: string,
|
|
44
|
+
dir: string,
|
|
45
|
+
): Promise<TreeEntry[] | null> {
|
|
46
|
+
// The trailing slash is what makes this list a directory's CONTENTS rather
|
|
47
|
+
// than print the directory's own entry — git's own spelling, and the
|
|
48
|
+
// difference between a page and a one-row page.
|
|
49
|
+
const paths = dir === '' ? [] : [`${dir}/`]
|
|
50
|
+
const res = git(['ls-tree', '--long', '-z'], { gitDir, operands: [rev], paths })
|
|
51
|
+
if (res.status !== 0) return null
|
|
52
|
+
|
|
53
|
+
const records = res.stdout.split('\0').filter((record) => record !== '')
|
|
54
|
+
if (records.length === 0) return null
|
|
55
|
+
|
|
56
|
+
const entries: TreeEntry[] = []
|
|
57
|
+
for (const record of records) {
|
|
58
|
+
const entry = parseRecord(record, dir)
|
|
59
|
+
if (entry) entries.push(entry)
|
|
60
|
+
}
|
|
61
|
+
// Directories first, then by name — the order a reader expects of a file
|
|
62
|
+
// browser, and one `ls-tree` does not make (it is in tree order, which is
|
|
63
|
+
// git's own byte ordering with a trailing slash on directory names).
|
|
64
|
+
entries.sort((a, b) => {
|
|
65
|
+
if ((a.kind === 'tree') !== (b.kind === 'tree')) return a.kind === 'tree' ? -1 : 1
|
|
66
|
+
return a.name < b.name ? -1 : 1
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
for (const entry of entries) {
|
|
70
|
+
if (entry.kind !== 'symlink') continue
|
|
71
|
+
// One `cat-file` per symlink rather than a batch: a symlink's target is its
|
|
72
|
+
// whole content, symlinks are a handful per directory at most, and batching
|
|
73
|
+
// would put an object-id list on a subprocess's stdin to save a spawn on a
|
|
74
|
+
// page that already spawned one.
|
|
75
|
+
const target = git(['cat-file', 'blob'], { gitDir, operands: [entry.oid] })
|
|
76
|
+
if (target.status === 0) entry.target = target.stdout
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return entries
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* One `--long -z` record: `<mode> <type> <oid> <size>\t<path>`.
|
|
84
|
+
*
|
|
85
|
+
* The size column is `-` for everything git does not weigh (a tree, a gitlink),
|
|
86
|
+
* and the path is FULL — relative to the repository root, not to the directory
|
|
87
|
+
* asked for — so the prefix comes back off here rather than at the renderer.
|
|
88
|
+
*/
|
|
89
|
+
function parseRecord(record: string, dir: string): TreeEntry | null {
|
|
90
|
+
const tab = record.indexOf('\t')
|
|
91
|
+
if (tab === -1) return null
|
|
92
|
+
const fields = record.slice(0, tab).split(/\s+/)
|
|
93
|
+
const [mode, type, oid, size] = fields
|
|
94
|
+
if (!mode || !type || !oid) return null
|
|
95
|
+
|
|
96
|
+
const full = record.slice(tab + 1)
|
|
97
|
+
const name = dir === '' ? full : full.slice(dir.length + 1)
|
|
98
|
+
if (name === '' || name.includes('/')) return null
|
|
99
|
+
|
|
100
|
+
const kind =
|
|
101
|
+
mode === SYMLINK_MODE
|
|
102
|
+
? 'symlink'
|
|
103
|
+
: mode === GITLINK_MODE
|
|
104
|
+
? 'submodule'
|
|
105
|
+
: type === 'tree'
|
|
106
|
+
? 'tree'
|
|
107
|
+
: 'blob'
|
|
108
|
+
const bytes = Number(size)
|
|
109
|
+
return {
|
|
110
|
+
name,
|
|
111
|
+
kind,
|
|
112
|
+
oid,
|
|
113
|
+
// Only a blob has one. `-` and anything unparseable read as "git did not
|
|
114
|
+
// say", which is what `null` means here and never zero.
|
|
115
|
+
size: kind === 'blob' && Number.isFinite(bytes) ? bytes : null,
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* What git knows about `rev`:`path` without reading it — its object id and its
|
|
121
|
+
* size — or `null` when that is not a blob.
|
|
122
|
+
*
|
|
123
|
+
* `--batch-check` rather than `cat-file -s`: one spawn answers both the TYPE
|
|
124
|
+
* and the size, and the type is what makes a directory a `null` here rather
|
|
125
|
+
* than a file of some size. The size is the whole point of asking separately
|
|
126
|
+
* from `readBlob` — it is what the cap is enforced against before any content
|
|
127
|
+
* reaches this process (`BLOB_MAX_BYTES`).
|
|
128
|
+
*
|
|
129
|
+
* The revision goes in on STDIN rather than as an operand, which is where
|
|
130
|
+
* `--batch-check` reads it, so nothing about the reader's spelling reaches a
|
|
131
|
+
* command line at all.
|
|
132
|
+
*/
|
|
133
|
+
export async function statBlob(
|
|
134
|
+
gitDir: string,
|
|
135
|
+
rev: string,
|
|
136
|
+
path: string,
|
|
137
|
+
): Promise<{ oid: string; size: number } | null> {
|
|
138
|
+
if (path === '') return null
|
|
139
|
+
const res = git(['cat-file', '--batch-check'], { gitDir, input: `${rev}:${path}\n` })
|
|
140
|
+
if (res.status !== 0) return null
|
|
141
|
+
// `<oid> <type> <size>`, or `<what was asked> missing`.
|
|
142
|
+
const [oid, type, size] = res.stdout.trim().split(' ')
|
|
143
|
+
if (!oid || type !== 'blob') return null
|
|
144
|
+
const bytes = Number(size)
|
|
145
|
+
return Number.isFinite(bytes) ? { oid, size: bytes } : null
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* The bytes of `rev`:`path`, or `null` when there are none to read.
|
|
150
|
+
*
|
|
151
|
+
* Bytes, never a string: see `gitBytes`. The caller has already asked
|
|
152
|
+
* `statBlob` what it is and how big, so this does no checking of its own —
|
|
153
|
+
* `cat-file blob` on a tree fails, and a failure is `null`.
|
|
154
|
+
*
|
|
155
|
+
* One bound the caller does NOT set: a raw read is uncapped by design, but this
|
|
156
|
+
* buffers, so a blob over `MAX_BUFFER` (`src/git.ts`, 64 MiB) fails here and is
|
|
157
|
+
* answered 404. That is above the 250 MiB a deployment lets a whole repository
|
|
158
|
+
* be and well above anything a page offers a link to; a file past it is one to
|
|
159
|
+
* clone. Streaming it would mean handing a subprocess's stdout to a response,
|
|
160
|
+
* which is the shape `git-backend.ts` has and this module deliberately does
|
|
161
|
+
* not.
|
|
162
|
+
*/
|
|
163
|
+
export async function readBlob(
|
|
164
|
+
gitDir: string,
|
|
165
|
+
rev: string,
|
|
166
|
+
path: string,
|
|
167
|
+
): Promise<Uint8Array | null> {
|
|
168
|
+
if (path === '') return null
|
|
169
|
+
const res = gitBytes(['cat-file', 'blob'], { gitDir, operands: [`${rev}:${path}`] })
|
|
170
|
+
return res.status === 0 ? res.stdout : null
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* The field separator inside one `git log` record, and the record separator
|
|
175
|
+
* between them.
|
|
176
|
+
*
|
|
177
|
+
* Unit and record separators rather than anything a human types: a commit
|
|
178
|
+
* subject is arbitrary text a pusher chose, and every printable delimiter is
|
|
179
|
+
* one somebody can put in a subject line and split a record with.
|
|
180
|
+
*/
|
|
181
|
+
const FIELD = '\x1f'
|
|
182
|
+
const RECORD = '\x1e'
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* One page of history walking back from `before` when the reader is paging, or
|
|
186
|
+
* from `rev` when they are not. `null` when git cannot walk it.
|
|
187
|
+
*
|
|
188
|
+
* `--no-walk` is deliberately NOT used: this is the first-parent-and-all
|
|
189
|
+
* history a reader expects of a branch. `limit` is the caller's bound
|
|
190
|
+
* (`LOG_PAGE`), because the bound is on what one request may do.
|
|
191
|
+
*
|
|
192
|
+
* The cursor is the LAST oid the previous page showed rather than the first one
|
|
193
|
+
* it did not, so a next page is built only from commits the reader has actually
|
|
194
|
+
* been shown. That makes it inclusive to git and exclusive to a reader, which
|
|
195
|
+
* is reconciled here: one extra commit is asked for and the cursor itself is
|
|
196
|
+
* dropped. It is validated as a full oid before it ever arrives
|
|
197
|
+
* (`src/http.ts`), which is belt to this module's braces — like every other
|
|
198
|
+
* revision it goes in as an operand.
|
|
199
|
+
*/
|
|
200
|
+
export async function listCommits(
|
|
201
|
+
gitDir: string,
|
|
202
|
+
rev: string,
|
|
203
|
+
before: string | null,
|
|
204
|
+
limit: number,
|
|
205
|
+
): Promise<Commit[] | null> {
|
|
206
|
+
// Defence in depth on the one argument that comes from a query parameter:
|
|
207
|
+
// the handler has refused anything but a full oid already, and this refuses
|
|
208
|
+
// it again rather than trusting that it did.
|
|
209
|
+
if (before !== null && !OID.test(before)) return null
|
|
210
|
+
const res = git(
|
|
211
|
+
[
|
|
212
|
+
'log',
|
|
213
|
+
`--max-count=${before === null ? limit : limit + 1}`,
|
|
214
|
+
`--format=%H${FIELD}%an <%ae>${FIELD}%aI${FIELD}%s${RECORD}`,
|
|
215
|
+
],
|
|
216
|
+
{ gitDir, operands: [before ?? rev] },
|
|
217
|
+
)
|
|
218
|
+
if (res.status !== 0) return null
|
|
219
|
+
const commits: Commit[] = []
|
|
220
|
+
for (const record of res.stdout.split(RECORD)) {
|
|
221
|
+
const line = record.trim()
|
|
222
|
+
if (line === '') continue
|
|
223
|
+
const [oid, author, date, subject] = line.split(FIELD)
|
|
224
|
+
if (!oid || author === undefined || date === undefined) continue
|
|
225
|
+
// The cursor commit itself: already shown on the page that named it.
|
|
226
|
+
if (oid === before) continue
|
|
227
|
+
commits.push({ oid, author, date, subject: subject ?? '' })
|
|
228
|
+
}
|
|
229
|
+
return commits
|
|
230
|
+
}
|
|
@@ -49,7 +49,7 @@ import type { ResolvedRepo } from './repo'
|
|
|
49
49
|
*/
|
|
50
50
|
function ensureConfig(gitDir: string, key: string, value: string): void {
|
|
51
51
|
if (readConfig(gitDir, key) === value) return
|
|
52
|
-
const res = git(['
|
|
52
|
+
const res = git(['config'], { gitDir, operands: [key, value] })
|
|
53
53
|
if (res.status === 0) return
|
|
54
54
|
if (readConfig(gitDir, key) === value) return
|
|
55
55
|
throw new Error(`git config ${key} ${value} failed: ${res.stderr.trim()}`)
|
|
@@ -58,12 +58,12 @@ function ensureConfig(gitDir: string, key: string, value: string): void {
|
|
|
58
58
|
/** Remove a config key, if it is set. Losing the lock race is not fatal here. */
|
|
59
59
|
function clearConfig(gitDir: string, key: string): void {
|
|
60
60
|
if (readConfig(gitDir, key) === undefined) return
|
|
61
|
-
git(['
|
|
61
|
+
git(['config', '--unset'], { gitDir, operands: [key] })
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
/** The configured value, or undefined when the key is unset. */
|
|
65
65
|
function readConfig(gitDir: string, key: string): string | undefined {
|
|
66
|
-
const res = git(['
|
|
66
|
+
const res = git(['config', '--get'], { gitDir, operands: [key] })
|
|
67
67
|
return res.status === 0 ? res.stdout.trim() : undefined
|
|
68
68
|
}
|
|
69
69
|
|
|
@@ -85,7 +85,7 @@ export function ensureBareRepo(repo: ResolvedRepo): ResolvedRepo {
|
|
|
85
85
|
// `main` as the default HEAD, because HEAD on a bare repo is what a clone
|
|
86
86
|
// checks out: created as `master` and pushed to as `main`, a clone succeeds
|
|
87
87
|
// and leaves an empty working tree, which reads as data loss.
|
|
88
|
-
gitOrThrow(['init', '--bare', '--quiet', '--initial-branch=main', repo.dir])
|
|
88
|
+
gitOrThrow(['init', '--bare', '--quiet', '--initial-branch=main'], { operands: [repo.dir] })
|
|
89
89
|
}
|
|
90
90
|
// Re-applied on every call, not just at creation: `receive.unpackLimit=0`
|
|
91
91
|
// is what makes a small push arrive as a packfile rather than exploding into
|
|
@@ -31,19 +31,13 @@ import * as fs from 'node:fs'
|
|
|
31
31
|
import * as path from 'node:path'
|
|
32
32
|
|
|
33
33
|
import { gitOrThrow } from './git'
|
|
34
|
-
import { leaseKey, walKey } from '
|
|
34
|
+
import { leaseKey, walKey } from '../shared/keys'
|
|
35
35
|
import { materialize, packBasename, packDirOf } from './materialize'
|
|
36
36
|
import type { ResolvedRepo } from './repo'
|
|
37
|
-
import type { ObjectStore } from '
|
|
38
|
-
import { ulid } from '
|
|
39
|
-
import {
|
|
40
|
-
|
|
41
|
-
sha256,
|
|
42
|
-
updateIndex,
|
|
43
|
-
type Tombstone,
|
|
44
|
-
type WalEntry,
|
|
45
|
-
type WalIndex,
|
|
46
|
-
} from './wal-index'
|
|
37
|
+
import type { ObjectStore } from '../shared/store'
|
|
38
|
+
import { ulid } from '../shared/ulid'
|
|
39
|
+
import { loadIndex, type Tombstone, type WalEntry, type WalIndex } from '../shared/wal-index'
|
|
40
|
+
import { sha256, updateIndex } from './wal-index'
|
|
47
41
|
|
|
48
42
|
/**
|
|
49
43
|
* How many un-superseded entries a repository may accumulate before it is
|
|
@@ -236,7 +230,7 @@ export async function compact(
|
|
|
236
230
|
const supersedesThrough = index.seq
|
|
237
231
|
|
|
238
232
|
const packDir = packDirOf(repo.dir)
|
|
239
|
-
gitOrThrow(['
|
|
233
|
+
gitOrThrow(['repack', '-adfq'], { gitDir: repo.dir })
|
|
240
234
|
|
|
241
235
|
const onDisk = fs.readdirSync(packDir)
|
|
242
236
|
// A cruft pack is not a second copy of the repository — it is git's
|
|
@@ -27,9 +27,10 @@
|
|
|
27
27
|
|
|
28
28
|
import * as fs from 'node:fs'
|
|
29
29
|
|
|
30
|
-
import type { ObjectStore } from '
|
|
31
|
-
import { indexKey, repoPrefix } from '
|
|
32
|
-
import { loadIndex,
|
|
30
|
+
import type { ObjectStore } from '../shared/store'
|
|
31
|
+
import { indexKey, repoPrefix } from '../shared/keys'
|
|
32
|
+
import { loadIndex, type WalIndex } from '../shared/wal-index'
|
|
33
|
+
import { updateIndex } from './wal-index'
|
|
33
34
|
|
|
34
35
|
/**
|
|
35
36
|
* How long a repository sits tombstoned before its objects may be deleted.
|
|
@@ -26,9 +26,9 @@
|
|
|
26
26
|
*/
|
|
27
27
|
|
|
28
28
|
import { deleteRepo, type DeleteResult } from './delete-repo'
|
|
29
|
-
import type { ObjectStore } from '
|
|
30
|
-
import { listRepoIds } from '
|
|
31
|
-
import { loadIndex, type WalIndex } from '
|
|
29
|
+
import type { ObjectStore } from '../shared/store'
|
|
30
|
+
import { listRepoIds } from '../shared/keys'
|
|
31
|
+
import { loadIndex, type WalIndex } from '../shared/wal-index'
|
|
32
32
|
|
|
33
33
|
/**
|
|
34
34
|
* Expiry is off unless a window is configured. Absent means "this instance
|