@zabaca/zbc 0.12.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +63 -0
- package/package.json +2 -1
- package/templates/apps/inbox/package.json +1 -1
- package/templates/apps/walgit/CONTEXT.md +135 -0
- package/templates/apps/walgit/Dockerfile +40 -18
- package/templates/apps/walgit/README.md +518 -55
- package/templates/apps/walgit/e2e/README.md +100 -6
- package/templates/apps/walgit/e2e/harness.ts +262 -1
- package/templates/apps/walgit/e2e/live.ts +588 -0
- package/templates/apps/walgit/e2e/scenarios.ts +367 -5
- package/templates/apps/walgit/e2e/suite.ts +5 -5
- package/templates/apps/walgit/examples/watch.ts +209 -0
- package/templates/apps/walgit/package.json +12 -3
- package/templates/apps/walgit/registry.json +6 -11
- package/templates/apps/walgit/shared/capabilities.ts +189 -0
- package/templates/apps/walgit/shared/container-env.ts +114 -0
- package/templates/apps/walgit/shared/credentials.ts +81 -0
- package/templates/apps/walgit/shared/events.ts +277 -0
- package/templates/apps/walgit/shared/landing.ts +1241 -0
- package/templates/apps/walgit/shared/llms.ts +463 -0
- package/templates/apps/walgit/shared/outbox.ts +266 -0
- package/templates/apps/walgit/shared/policy.ts +59 -0
- package/templates/apps/walgit/shared/protocol.ts +197 -0
- package/templates/apps/walgit/shared/provenance.ts +154 -0
- package/templates/apps/walgit/shared/ref-cache.ts +181 -0
- package/templates/apps/walgit/shared/telemetry.ts +219 -0
- package/templates/apps/walgit/src/announce-main.ts +37 -0
- package/templates/apps/walgit/src/announce.ts +98 -0
- package/templates/apps/walgit/src/append-only.ts +112 -0
- package/templates/apps/walgit/src/cache.ts +172 -0
- package/templates/apps/walgit/src/cli.ts +493 -0
- package/templates/apps/walgit/src/compact.ts +25 -23
- package/templates/apps/walgit/src/delete-repo.ts +237 -0
- package/templates/apps/walgit/src/expire.ts +248 -0
- package/templates/apps/walgit/src/gc.ts +8 -16
- package/templates/apps/walgit/src/git-backend.ts +29 -0
- package/templates/apps/walgit/src/git.ts +78 -0
- package/templates/apps/walgit/src/hook-main.ts +259 -17
- package/templates/apps/walgit/src/hooks.ts +4 -3
- package/templates/apps/walgit/src/http.ts +237 -45
- package/templates/apps/walgit/src/instructions.ts +240 -0
- package/templates/apps/walgit/src/keys.ts +126 -0
- package/templates/apps/walgit/src/limits.ts +158 -0
- package/templates/apps/walgit/src/materialize.bench.ts +10 -10
- package/templates/apps/walgit/src/materialize.ts +48 -40
- package/templates/apps/walgit/src/mkdir-lock.ts +66 -0
- package/templates/apps/walgit/src/orphans.ts +2 -5
- package/templates/apps/walgit/src/pending.ts +201 -0
- package/templates/apps/walgit/src/push-cert.ts +254 -0
- package/templates/apps/walgit/src/push.ts +233 -46
- package/templates/apps/walgit/src/reconcile.ts +5 -8
- package/templates/apps/walgit/src/repo.ts +22 -77
- package/templates/apps/walgit/src/server.ts +148 -26
- package/templates/apps/walgit/src/signers.ts +577 -0
- package/templates/apps/walgit/src/store-env.ts +5 -4
- package/templates/apps/walgit/src/store.ts +47 -24
- package/templates/apps/walgit/src/usage.ts +404 -0
- package/templates/apps/walgit/src/verify.ts +171 -0
- package/templates/apps/walgit/src/wal-index.ts +189 -11
- package/templates/apps/walgit/tsconfig.json +8 -2
- package/templates/apps/walgit/tsconfig.worker.json +24 -0
- package/templates/apps/walgit/worker/events-do.ts +259 -0
- package/templates/apps/walgit/worker/index.ts +572 -0
- package/templates/apps/walgit/wrangler.jsonc +95 -0
- package/templates/apps/warehouse/package.json +1 -1
- package/templates/infra/modules/cloudflare/index.ts +77 -6
- package/templates/infra/modules/fly/index.ts +150 -10
- package/templates/infra/modules/turso/index.ts +43 -3
- package/templates/infra/package.json +1 -1
- package/templates/apps/walgit/entrypoint.sh +0 -59
- package/templates/apps/walgit/fly.toml +0 -52
- package/templates/apps/walgit/src/ssh-shell.ts +0 -36
- package/templates/apps/walgit/sshd_config +0 -38
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one place walgit runs a git plumbing command.
|
|
3
|
+
*
|
|
4
|
+
* walgit's whole premise is that vanilla git runs on local disk, so git is
|
|
5
|
+
* spawned constantly — `init --bare`, `config`, `index-pack`, `for-each-ref`,
|
|
6
|
+
* `cat-file --batch-check`, `repack -adf`. Before this module those six lived in
|
|
7
|
+
* four files behind two byte-identical private `run` helpers and two bare
|
|
8
|
+
* `spawnSync` calls, each with its own idea of what a failure looks like.
|
|
9
|
+
*
|
|
10
|
+
* It covers the commands walgit runs and READS. It deliberately does not cover
|
|
11
|
+
* the two places git is not a subprocess to be read but a transport to be
|
|
12
|
+
* handed the socket: `git-backend.ts` runs `git http-backend`
|
|
13
|
+
* with inherited stdio, and `git-backend.ts` streams `git http-backend` as CGI.
|
|
14
|
+
* Both speak the pack protocol on their own stdio and must never be buffered.
|
|
15
|
+
*
|
|
16
|
+
* One thing belongs here and is deliberately NOT done yet, because this module
|
|
17
|
+
* arrived as a pure extraction: **ambient configuration is still inherited.** A
|
|
18
|
+
* global or system git config that sets `core.hooksPath` redirects
|
|
19
|
+
* `git-receive-pack` away from `$GIT_DIR/hooks`, so walgit's `pre-receive` and
|
|
20
|
+
* `reference-transaction` never run and a push is acknowledged with nothing
|
|
21
|
+
* written to the log — the one outcome docs/adr/0007 exists to prevent.
|
|
22
|
+
* `e2e/harness.ts` already strips it (`GIT_CONFIG_GLOBAL=/dev/null`) for
|
|
23
|
+
* exactly this class of reason. This module is where that fix becomes one line.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import { spawnSync } from 'node:child_process'
|
|
27
|
+
|
|
28
|
+
export interface GitResult {
|
|
29
|
+
/** git's exit code. `null` from spawnSync (killed by signal) reads as 1. */
|
|
30
|
+
status: number
|
|
31
|
+
stdout: string
|
|
32
|
+
stderr: string
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface GitOptions {
|
|
36
|
+
/** Fed to the command on stdin, e.g. the oid list `cat-file` batches over. */
|
|
37
|
+
input?: string
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Run git and return what it said. A non-zero exit is a VALUE, not a throw —
|
|
42
|
+
* several callers branch on it (`config --get` on an unset key, a `verify` that
|
|
43
|
+
* reports rather than fails), and turning those into exceptions would make the
|
|
44
|
+
* normal case an error path.
|
|
45
|
+
*/
|
|
46
|
+
/**
|
|
47
|
+
* How much output one git command may produce before it is cut off.
|
|
48
|
+
*
|
|
49
|
+
* Raised from the runtime's 1 MiB default, which `for-each-ref` on a repository
|
|
50
|
+
* with tens of thousands of refs exceeds. Overrunning it is not the clean
|
|
51
|
+
* failure it looks like: the buffer can come back TRUNCATED with a zero exit,
|
|
52
|
+
* so a caller that trusts the exit status reads a shorter answer than git gave.
|
|
53
|
+
* Every caller here reads git's output for meaning, so the cap is set well past
|
|
54
|
+
* anything walgit asks for rather than left where a real repository meets it.
|
|
55
|
+
*/
|
|
56
|
+
const MAX_BUFFER = 64 * 1024 * 1024
|
|
57
|
+
|
|
58
|
+
export function git(args: readonly string[], opts: GitOptions = {}): GitResult {
|
|
59
|
+
const res = spawnSync('git', [...args], {
|
|
60
|
+
encoding: 'utf8',
|
|
61
|
+
input: opts.input,
|
|
62
|
+
maxBuffer: MAX_BUFFER,
|
|
63
|
+
})
|
|
64
|
+
return {
|
|
65
|
+
status: res.status ?? 1,
|
|
66
|
+
stdout: res.stdout ?? '',
|
|
67
|
+
stderr: res.stderr ?? '',
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** git, where a non-zero exit is a bug rather than an answer. */
|
|
72
|
+
export function gitOrThrow(args: readonly string[], opts: GitOptions = {}): GitResult {
|
|
73
|
+
const res = git(args, opts)
|
|
74
|
+
if (res.status !== 0) {
|
|
75
|
+
throw new Error(`git ${args.join(' ')} failed: ${res.stderr.trim()}`)
|
|
76
|
+
}
|
|
77
|
+
return res
|
|
78
|
+
}
|
|
@@ -14,12 +14,32 @@
|
|
|
14
14
|
*/
|
|
15
15
|
|
|
16
16
|
import { spawn } from 'node:child_process'
|
|
17
|
+
import * as fs from 'node:fs'
|
|
17
18
|
import * as path from 'node:path'
|
|
18
19
|
|
|
20
|
+
import { capabilitiesFrom } from '../shared/capabilities'
|
|
21
|
+
import { announceConfigFromEnv } from './announce'
|
|
22
|
+
import { appendOnlyEnabled, checkAppendOnly } from './append-only'
|
|
19
23
|
import { configuredThreshold, isCompactionDue } from './compact'
|
|
20
|
-
import {
|
|
24
|
+
import { checkSize, limitsEnforced, limitsOf, liveBytes } from './limits'
|
|
25
|
+
import { clearPending, invocationId, markConsumed, readPending, sweepPending } from './pending'
|
|
26
|
+
import {
|
|
27
|
+
establishSigner,
|
|
28
|
+
parseRefChanges,
|
|
29
|
+
preReceive,
|
|
30
|
+
publishPush,
|
|
31
|
+
quarantinePack,
|
|
32
|
+
type PublishResult,
|
|
33
|
+
} from './push'
|
|
34
|
+
import {
|
|
35
|
+
checkSignerAllowed,
|
|
36
|
+
checkSignerList,
|
|
37
|
+
describeSigner,
|
|
38
|
+
gitSignersSource,
|
|
39
|
+
signerListsEnabled,
|
|
40
|
+
} from './signers'
|
|
21
41
|
import { requireStore, storeFromEnv } from './store-env'
|
|
22
|
-
import { loadIndex } from './wal-index'
|
|
42
|
+
import { loadIndex, type LoadedIndex, type RefChange } from './wal-index'
|
|
23
43
|
|
|
24
44
|
const hook = process.argv[2]
|
|
25
45
|
const phase = process.argv[3]
|
|
@@ -38,35 +58,174 @@ function fault(point: string): void {
|
|
|
38
58
|
}
|
|
39
59
|
}
|
|
40
60
|
|
|
61
|
+
/**
|
|
62
|
+
* Test-only widening of the window between `pre-receive` finishing and
|
|
63
|
+
* `reference-transaction` running, off unless `WALGIT_STALL_MS` is set. The
|
|
64
|
+
* hand-off race between two `git-receive-pack` invocations is a real race with
|
|
65
|
+
* a window measured in milliseconds; a test that waits for it to happen by luck
|
|
66
|
+
* is a test that passes for the wrong reason.
|
|
67
|
+
*/
|
|
68
|
+
async function stall(): Promise<void> {
|
|
69
|
+
const ms = Number(process.env.WALGIT_STALL_MS ?? '')
|
|
70
|
+
if (Number.isFinite(ms) && ms > 0) await Bun.sleep(ms)
|
|
71
|
+
}
|
|
72
|
+
|
|
41
73
|
async function main(): Promise<number> {
|
|
42
74
|
const stdin = await Bun.stdin.text()
|
|
43
75
|
|
|
44
76
|
if (hook === 'pre-receive') {
|
|
77
|
+
// Who signed this push, settled here and handed down. It is read in this
|
|
78
|
+
// hook because this is the only one git shows the certificate to — the
|
|
79
|
+
// blob lives in the push's quarantine and is gone by the time the refs
|
|
80
|
+
// move — and it is settled at the TOP because a verdict that turns on the
|
|
81
|
+
// Signer has to be reachable before the upload: reached after it, every
|
|
82
|
+
// push it refused would leave an Orphan behind. It becomes the Provenance
|
|
83
|
+
// the publish records, and the gate's input just below.
|
|
84
|
+
//
|
|
85
|
+
// Fail-open still (docs/adr/0011): a certificate that is missing, stale,
|
|
86
|
+
// malformed or unverifiable, and an `ssh-keygen` that is absent or throws,
|
|
87
|
+
// are all `null` here — and a `null` Signer is the anonymous push walgit
|
|
88
|
+
// accepted before any of this existed. Its one exception is confined to a
|
|
89
|
+
// repository that holds a Signer List, where `null` refuses, because
|
|
90
|
+
// otherwise breaking verification would be how one bypasses the gate.
|
|
91
|
+
//
|
|
92
|
+
// Described here rather than at the one call that judges it, because the
|
|
93
|
+
// publish judges it a second time and the certificate is gone by then: the
|
|
94
|
+
// three-way answer rides down through the pending record.
|
|
95
|
+
const signer = describeSigner(establishSigner())
|
|
96
|
+
const changes = parseRefChanges(stdin).filter((c) => c.ref.startsWith('refs/'))
|
|
97
|
+
|
|
98
|
+
const quarantineDir = process.env.GIT_QUARANTINE_PATH || process.env.GIT_OBJECT_DIRECTORY
|
|
45
99
|
const store = requireStore()
|
|
100
|
+
|
|
101
|
+
// The Index, read at most once for the whole hook and only by a verdict
|
|
102
|
+
// that needs it. Two of them do — ownership needs the list this name
|
|
103
|
+
// already holds, and the size cap needs the repository's current total —
|
|
104
|
+
// and reading it twice would let them judge one push against two different
|
|
105
|
+
// states of the log for no reason either could explain.
|
|
106
|
+
let loaded: LoadedIndex | null = null
|
|
107
|
+
const index = async () => (loaded ??= await loadIndex(store, repoId)).index
|
|
108
|
+
|
|
109
|
+
// Ownership, FIRST, and before the store is written to: while a repository
|
|
110
|
+
// holds a Signer List, a push not signed by a listed key is refused
|
|
111
|
+
// (docs/adr/0012). It leads the verdicts because every one below it is
|
|
112
|
+
// about WHAT was pushed, and telling a stranger their push would rewrite a
|
|
113
|
+
// branch — advice they cannot act on, because they may not push here at
|
|
114
|
+
// all — is a refusal that sends them somewhere there is nothing to find.
|
|
115
|
+
//
|
|
116
|
+
// The list that judges is the one that stood BEFORE this push, which is
|
|
117
|
+
// what makes a grant govern the next push rather than its own. Off entirely
|
|
118
|
+
// without the flag, and on an unclaimed name it refuses nothing.
|
|
119
|
+
//
|
|
120
|
+
// Asked here and asked AGAIN at the compare-and-swap that publishes: this
|
|
121
|
+
// answer is read before the pack uploads, so it can be stale by the time
|
|
122
|
+
// the push lands. Here is where a refusal is free; there is where it is
|
|
123
|
+
// true. See `publishPush`.
|
|
124
|
+
if (signerListsEnabled()) {
|
|
125
|
+
const verdict = checkSignerAllowed(
|
|
126
|
+
repoId,
|
|
127
|
+
signer,
|
|
128
|
+
(await index()).claim?.signers ?? null,
|
|
129
|
+
changes,
|
|
130
|
+
)
|
|
131
|
+
if (!verdict.ok) {
|
|
132
|
+
process.stderr.write(`${verdict.message}\n`)
|
|
133
|
+
return 1
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Append-only, for the same reason at the same moment: a push that will be
|
|
138
|
+
// refused must not cost an object-store write. git's own deny rules run
|
|
139
|
+
// after this hook, so leaving it to them would upload a pack nothing will
|
|
140
|
+
// ever reference.
|
|
141
|
+
if (appendOnlyEnabled()) {
|
|
142
|
+
const verdict = checkAppendOnly(gitDir, repoId, changes)
|
|
143
|
+
if (!verdict.ok) {
|
|
144
|
+
process.stderr.write(`${verdict.message}\n`)
|
|
145
|
+
return 1
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// The Signer List this push WRITES, resolved here for the same reason and
|
|
150
|
+
// at the same moment. Two of its answers refuse the push — a list that is
|
|
151
|
+
// empty, and one walgit cannot read — and both have to be reachable before
|
|
152
|
+
// the upload or every push they refused would leave an Orphan behind. The
|
|
153
|
+
// third answer is the list itself, which rides down to the compare-and-swap
|
|
154
|
+
// that publishes the ref move.
|
|
155
|
+
//
|
|
156
|
+
// Below the gate, deliberately: whether a stranger's list is well-formed is
|
|
157
|
+
// not the answer a stranger needs. Without the flag `refs/walgit/signers`
|
|
158
|
+
// is an ordinary ref like any other.
|
|
159
|
+
let signerList: string[] | null = null
|
|
160
|
+
if (signerListsEnabled()) {
|
|
161
|
+
const verdict = checkSignerList(repoId, changes, gitSignersSource(gitDir))
|
|
162
|
+
if (!verdict.ok) {
|
|
163
|
+
process.stderr.write(`${verdict.message}\n`)
|
|
164
|
+
return 1
|
|
165
|
+
}
|
|
166
|
+
signerList = verdict.signers
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Size, for the same reason and at the same moment. The pack is in the
|
|
170
|
+
// quarantine and its size is exact, so the answer costs one stat and (only
|
|
171
|
+
// when a repository total is configured) one index read — against the ~37 s
|
|
172
|
+
// an oversized push otherwise spends uploading before the EDGE refuses it
|
|
173
|
+
// with something that reads like a dropped connection. See src/limits.ts.
|
|
174
|
+
// This is a separate OS process git spawns per hook, with its own
|
|
175
|
+
// `process.env` — so the read is here rather than threaded through
|
|
176
|
+
// `PreReceiveContext`, and it is the same derivation the three documents
|
|
177
|
+
// state their caps from.
|
|
178
|
+
const limits = limitsOf(capabilitiesFrom(process.env))
|
|
179
|
+
if (limitsEnforced(limits)) {
|
|
180
|
+
const found = quarantineDir ? quarantinePack(quarantineDir) : null
|
|
181
|
+
const pushBytes = found ? fs.statSync(found.pack).size : 0
|
|
182
|
+
// Skipped for a ref-only push: it adds nothing, so neither cap can move.
|
|
183
|
+
if (pushBytes > 0) {
|
|
184
|
+
const repoBytes = limits.maxRepoBytes === null ? 0 : liveBytes(await index())
|
|
185
|
+
const verdict = checkSize({ repoId, pushBytes, repoBytes, limits })
|
|
186
|
+
if (!verdict.ok) {
|
|
187
|
+
process.stderr.write(`${verdict.message}\n`)
|
|
188
|
+
return 1
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
46
193
|
await preReceive({
|
|
47
194
|
store,
|
|
48
195
|
repoId,
|
|
49
196
|
gitDir,
|
|
50
|
-
quarantineDir
|
|
197
|
+
quarantineDir,
|
|
198
|
+
signer,
|
|
199
|
+
signerList,
|
|
51
200
|
})
|
|
52
201
|
fault('after-upload')
|
|
202
|
+
await stall()
|
|
53
203
|
return 0
|
|
54
204
|
}
|
|
55
205
|
|
|
56
206
|
if (hook === 'reference-transaction') {
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
//
|
|
60
|
-
//
|
|
207
|
+
const invocation = invocationId()
|
|
208
|
+
const pending = readPending(gitDir, invocation)
|
|
209
|
+
// No pending record for THIS `git-receive-pack` means this ref update did
|
|
210
|
+
// not come from a push — an administrative edit on this node. It has
|
|
211
|
+
// nothing to publish, and publishing it would let a stale cache overwrite
|
|
212
|
+
// the index. A concurrent push's record is invisible here by construction,
|
|
213
|
+
// which is the whole point of keying the record by invocation.
|
|
61
214
|
if (!pending) return 0
|
|
62
215
|
|
|
63
|
-
if (phase === 'committed'
|
|
64
|
-
|
|
216
|
+
if (phase === 'committed') return 0
|
|
217
|
+
if (phase === 'aborted') {
|
|
218
|
+
if (pending.entry && !pending.consumed) {
|
|
65
219
|
// The uploaded pack is now unreferenced. It is not lost: `findOrphans`
|
|
66
220
|
// recovers it by diffing the WAL prefix against index.json.
|
|
67
221
|
process.stderr.write(`walgit: push rejected; orphaned WAL object ${pending.entry.key}\n`)
|
|
222
|
+
markConsumed(gitDir, invocation)
|
|
68
223
|
}
|
|
69
|
-
|
|
224
|
+
// The record is deliberately NOT deleted here: a push whose refs arrive
|
|
225
|
+
// in several transactions still has transactions to come, and a deleted
|
|
226
|
+
// record would make the next one look like an administrative edit — the
|
|
227
|
+
// silent acknowledgement this path exists to prevent. `post-receive` and
|
|
228
|
+
// the sweep clean up.
|
|
70
229
|
return 0
|
|
71
230
|
}
|
|
72
231
|
if (phase !== 'prepared') return 0
|
|
@@ -76,16 +235,32 @@ async function main(): Promise<number> {
|
|
|
76
235
|
|
|
77
236
|
fault('before-cas')
|
|
78
237
|
const store = requireStore()
|
|
79
|
-
|
|
238
|
+
// The pack belongs to the first transaction that publishes; later ones in
|
|
239
|
+
// the same push carry ref changes only — but they carry the same Signer and
|
|
240
|
+
// the same Signer List, because they are the same push. Dropping the Signer
|
|
241
|
+
// here would attribute the first ref of a multi-ref push and silently leave
|
|
242
|
+
// the rest anonymous; dropping the list would lose a claim outright
|
|
243
|
+
// whenever git happened to move the list ref in a later transaction than
|
|
244
|
+
// the one that published the pack. `applyClaim` is what keeps carrying it
|
|
245
|
+
// in every transaction from writing it in the wrong one. The Signer rides
|
|
246
|
+
// along for the third reason: the publish re-asks the ownership question,
|
|
247
|
+
// and a later transaction dropping it would be judged as an anonymous push.
|
|
248
|
+
const toPublish = pending.consumed
|
|
249
|
+
? {
|
|
250
|
+
entry: null,
|
|
251
|
+
provenance: pending.provenance,
|
|
252
|
+
claim: pending.claim,
|
|
253
|
+
signer: pending.signer,
|
|
254
|
+
}
|
|
255
|
+
: pending
|
|
256
|
+
const result = await publishPush(store, repoId, toPublish, changes, {
|
|
257
|
+
signerLists: signerListsEnabled(),
|
|
258
|
+
})
|
|
80
259
|
if (!result.ok) {
|
|
81
|
-
process.stderr.write(
|
|
82
|
-
result.reason === 'ref-conflict'
|
|
83
|
-
? `walgit: ${result.ref} moved under this push (index has ${result.actual}, ` +
|
|
84
|
-
`push expected ${result.expected}) — fetch and retry\n`
|
|
85
|
-
: 'walgit: the write-ahead log stayed contended — retry the push\n',
|
|
86
|
-
)
|
|
260
|
+
process.stderr.write(`${publishRefusal(result)}\n`)
|
|
87
261
|
return 1
|
|
88
262
|
}
|
|
263
|
+
if (toPublish.entry) markConsumed(gitDir, invocation)
|
|
89
264
|
fault('after-cas')
|
|
90
265
|
return 0
|
|
91
266
|
}
|
|
@@ -93,6 +268,21 @@ async function main(): Promise<number> {
|
|
|
93
268
|
if (hook === 'post-receive') {
|
|
94
269
|
// Everything below is best-effort by construction: the push is already
|
|
95
270
|
// acknowledged, so a failure here must be invisible to the client.
|
|
271
|
+
clearPending(gitDir, invocationId())
|
|
272
|
+
sweepPending(gitDir)
|
|
273
|
+
|
|
274
|
+
// Ref events, announced here and not from `reference-transaction`, because
|
|
275
|
+
// here is the first moment the push is certainly durable: git only runs
|
|
276
|
+
// this hook once the ref transaction committed, which is only once the
|
|
277
|
+
// compare-and-swap on index.json won. A push that lost it is rejected and
|
|
278
|
+
// never reaches this line, so nobody is ever told about one. See
|
|
279
|
+
// src/announce.ts, which swallows every failure for the same reason the
|
|
280
|
+
// rest of this branch does.
|
|
281
|
+
const events = announceConfigFromEnv()
|
|
282
|
+
if (events) {
|
|
283
|
+
const changes = parseRefChanges(stdin).filter((c) => c.ref.startsWith('refs/'))
|
|
284
|
+
if (changes.length > 0) spawnAnnounce(repoId, changes)
|
|
285
|
+
}
|
|
96
286
|
try {
|
|
97
287
|
const store = storeFromEnv()
|
|
98
288
|
if (!store) return 0
|
|
@@ -124,3 +314,55 @@ try {
|
|
|
124
314
|
process.stderr.write(`walgit: ${(err as Error).message}\n`)
|
|
125
315
|
process.exit(1)
|
|
126
316
|
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* What a refused publish says, in the words its own reason earned.
|
|
320
|
+
*
|
|
321
|
+
* The ownership refusal carries its message from the gate rather than being
|
|
322
|
+
* reworded here, because it IS the gate's refusal — reached late, but the same
|
|
323
|
+
* one, and an agent that met it in `pre-receive` yesterday must not have to
|
|
324
|
+
* recognise a second spelling of it today. Calling it a ref conflict would be
|
|
325
|
+
* worse than terse: it would send the pusher to fetch and rebase, which cannot
|
|
326
|
+
* help, and would hide that the name is now held.
|
|
327
|
+
*/
|
|
328
|
+
function publishRefusal(result: Extract<PublishResult, { ok: false }>): string {
|
|
329
|
+
switch (result.reason) {
|
|
330
|
+
case 'ref-conflict':
|
|
331
|
+
return (
|
|
332
|
+
`walgit: ${result.ref} moved under this push (index has ${result.actual}, ` +
|
|
333
|
+
`push expected ${result.expected}) — fetch and retry`
|
|
334
|
+
)
|
|
335
|
+
case 'not-allowed':
|
|
336
|
+
return result.message
|
|
337
|
+
case 'contended':
|
|
338
|
+
return 'walgit: the write-ahead log stayed contended — retry the push'
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Hand the announcement to a detached process and return.
|
|
344
|
+
*
|
|
345
|
+
* `post-receive` holds the client's connection until it exits, so the
|
|
346
|
+
* announcement must not be awaited here: a fan-out that is slow or unreachable
|
|
347
|
+
* would be paid for by the pusher, on the path this project keeps fast. The
|
|
348
|
+
* refs are passed as one JSON argument — a push moves a handful of them, and
|
|
349
|
+
* the alternative (a pipe to a disowned child) would keep this process alive to
|
|
350
|
+
* write it.
|
|
351
|
+
*
|
|
352
|
+
* A spawn that fails is logged and dropped, like every other failure in
|
|
353
|
+
* `post-receive`: the push is already durable and `index.json` already holds
|
|
354
|
+
* what this would have announced, so a subscriber's next handshake reads it
|
|
355
|
+
* there anyway.
|
|
356
|
+
*/
|
|
357
|
+
function spawnAnnounce(repo: string, changes: readonly RefChange[]): void {
|
|
358
|
+
try {
|
|
359
|
+
const child = spawn(
|
|
360
|
+
process.execPath,
|
|
361
|
+
[path.join(import.meta.dir, 'announce-main.ts'), repo, JSON.stringify(changes)],
|
|
362
|
+
{ detached: true, stdio: 'ignore' },
|
|
363
|
+
)
|
|
364
|
+
child.unref()
|
|
365
|
+
} catch (err) {
|
|
366
|
+
process.stderr.write(`walgit: ref-event announce not spawned: ${(err as Error).message}\n`)
|
|
367
|
+
}
|
|
368
|
+
}
|
|
@@ -21,10 +21,11 @@ const BUN = process.execPath
|
|
|
21
21
|
const HOOK_MAIN = path.join(import.meta.dir, 'hook-main.ts')
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
|
-
* `post-receive` is here for compaction and
|
|
24
|
+
* `post-receive` is here for compaction and for ref events. It runs after the
|
|
25
25
|
* refs have moved and after the push is durable, which makes it the only hook
|
|
26
|
-
* where doing work cannot cost correctness —
|
|
27
|
-
*
|
|
26
|
+
* where doing work cannot cost correctness — compaction is handed to a detached
|
|
27
|
+
* process so it does not cost latency either, and the ref-event announcement is
|
|
28
|
+
* bounded and swallows its own failures (src/announce.ts).
|
|
28
29
|
*/
|
|
29
30
|
const HOOKS = ['pre-receive', 'reference-transaction', 'post-receive'] as const
|
|
30
31
|
|