@williacj/rigger 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +370 -2
- package/package.json +56 -4
- package/scripts/doc-reference-check.mjs +59 -0
- package/scripts/gate-check.mjs +249 -0
- package/scripts/lib/pack-result.mjs +9 -0
- package/scripts/record-commit.mjs +67 -0
- package/src/cli/doctor.mjs +1021 -0
- package/src/cli/init.mjs +384 -0
- package/src/cli/once.mjs +489 -0
- package/src/cli/pause.mjs +56 -0
- package/src/cli/plan.mjs +106 -0
- package/src/cli/recording.mjs +52 -0
- package/src/cli/report.mjs +242 -0
- package/src/cli/resume.mjs +96 -0
- package/src/cli/rigger.mjs +12 -0
- package/src/cli/run.mjs +10 -0
- package/src/cli/setup-board.mjs +108 -0
- package/src/cli/verbs.mjs +82 -0
- package/src/cli/verdict.mjs +180 -0
- package/src/config/validate.mjs +960 -0
- package/src/execution/groups.mjs +185 -0
- package/src/execution/kept-output.mjs +21 -0
- package/src/execution/role.mjs +171 -0
- package/src/execution/run.mjs +490 -0
- package/src/execution/step.mjs +26 -0
- package/src/execution/workspace.mjs +866 -0
- package/src/observation/push.mjs +103 -0
- package/src/observation/report.mjs +415 -0
- package/src/observation/sink.mjs +271 -0
- package/src/package-commit.json +1 -0
- package/src/scheduling/admission.mjs +390 -0
- package/src/scheduling/clock.mjs +98 -0
- package/src/scheduling/lane.mjs +160 -0
- package/src/scheduling/loop.mjs +1738 -0
- package/src/scheduling/pull-order.mjs +46 -0
- package/src/substrate/data-ref.mjs +147 -0
- package/src/substrate/forge/assets.mjs +5 -0
- package/src/substrate/forge/graphql.mjs +258 -0
- package/src/substrate/forge/item-write.mjs +74 -0
- package/src/substrate/forge/read.mjs +998 -0
- package/src/substrate/forge/repository-write.mjs +152 -0
- package/src/substrate/forge/runners.mjs +510 -0
- package/src/substrate/forge/schema-write.mjs +70 -0
- package/src/substrate/forge/write-made.mjs +40 -0
- package/src/substrate/git-environment.mjs +54 -0
- package/src/substrate/host.mjs +23 -0
- package/src/substrate/launchd.mjs +23 -0
- package/src/substrate/process-table.sh +48 -0
- package/src/substrate/process.mjs +2474 -0
- package/src/substrate/providers/adapters.mjs +8 -0
- package/src/substrate/providers/claude.mjs +427 -0
- package/src/substrate/providers/codex.mjs +349 -0
- package/src/substrate/standard-error.mjs +134 -0
- package/src/substrate/worktrees.mjs +517 -0
- package/src/workflow/clock-mark.mjs +40 -0
- package/src/workflow/escalation.mjs +110 -0
- package/src/workflow/facts.mjs +92 -0
- package/src/workflow/first-above.mjs +17 -0
- package/src/workflow/form-check.mjs +88 -0
- package/src/workflow/gate.mjs +62 -0
- package/src/workflow/judges.mjs +695 -0
- package/src/workflow/marker.mjs +265 -0
- package/src/workflow/next-action.mjs +456 -0
- package/src/workflow/status-issue.mjs +268 -0
- package/src/workflow/transitions.mjs +588 -0
- package/templates/claude/agents/architect.md +116 -0
- package/templates/claude/agents/engineer.md +57 -0
- package/templates/claude/agents/pm.md +114 -0
- package/templates/claude/agents/reviewer.md +37 -0
- package/templates/claude/agents/spike-engineer.md +80 -0
- package/templates/claude/hooks/refuse-reserved-git-commands.mjs +909 -0
- package/templates/claude/settings.json +50 -0
- package/templates/claude/skills/acceptance/SKILL.md +275 -0
- package/templates/claude/skills/agent-style/SKILL.md +78 -0
- package/templates/claude/skills/code-review/SKILL.md +242 -0
- package/templates/claude/skills/proposal/SKILL.md +175 -0
- package/templates/claude/skills/spec-style/SKILL.md +112 -0
- package/templates/claude/skills/tdd/SKILL.md +230 -0
- package/templates/codex/config.toml +19 -0
- package/templates/doc-reference-check.md +25 -0
- package/templates/githooks/pre-push +24 -0
- package/templates/github/workflows/rigger-gate.yml +209 -0
- package/templates/launchd/com.williacj.rigger.plist +31 -0
- package/templates/rigger.config.mjs +78 -0
package/src/cli/init.mjs
ADDED
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
// ABOUTME: The `init` verb: what Rigger forks into a consumer's repository, read from the
|
|
2
|
+
// templates the package ships, what a second run leaves alone, and the source tree it refuses.
|
|
3
|
+
|
|
4
|
+
import { existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, realpathSync, statSync, writeFileSync } from 'node:fs';
|
|
5
|
+
import { basename, isAbsolute, join, resolve, dirname } from 'node:path';
|
|
6
|
+
import { fileURLToPath } from 'node:url';
|
|
7
|
+
|
|
8
|
+
import { PLACEHOLDER } from '../config/validate.mjs';
|
|
9
|
+
import { EVENT_REFUSED } from '../substrate/process.mjs';
|
|
10
|
+
import { assets as forgeAssets } from '../substrate/forge/assets.mjs';
|
|
11
|
+
import { ADAPTERS } from '../substrate/providers/adapters.mjs';
|
|
12
|
+
import { PACKAGE, gitAnswer, real, repoRoot, sameTree, sourceTreeRefusal } from './doctor.mjs';
|
|
13
|
+
import { recording } from './recording.mjs';
|
|
14
|
+
|
|
15
|
+
/** The file a consumer's repository declares Rigger in. */
|
|
16
|
+
export const CONFIG = 'rigger.config.mjs';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Where each provider reads its assets from, which is the whole of what decides a fork's
|
|
20
|
+
* destination, as each provider's adapter names it in `assets` (ruling 1 Q1 on #467).
|
|
21
|
+
*
|
|
22
|
+
* `ARCHITECTURE.md`'s "Where provider assets live" row: a role names its agent file by path, so
|
|
23
|
+
* the directory is whatever the provider reads — Claude Code reads `.claude/`, and a second
|
|
24
|
+
* adapter reads its own — and `init` forks each template where its provider looks for it. A
|
|
25
|
+
* provider's templates therefore ship under `templates/<provider>/`, mirroring the tree that
|
|
26
|
+
* lands in the directory named here.
|
|
27
|
+
*/
|
|
28
|
+
export const PROVIDER_ASSETS = Object.fromEntries(Object.entries(ADAPTERS).map(([provider, adapter]) => [provider, adapter.assets]));
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The git hook destination belongs to `init`; providers own only their own asset destinations, and
|
|
32
|
+
* the forge adapter its own (`src/substrate/forge/assets.mjs`), where the gate workflow lands.
|
|
33
|
+
*/
|
|
34
|
+
export const ASSET_DESTINATIONS = { ...PROVIDER_ASSETS, githooks: '.githooks', github: forgeAssets };
|
|
35
|
+
|
|
36
|
+
/** `ARCHITECTURE.md`, "Triggers": host assets shipped under templates/ that init never forks. */
|
|
37
|
+
const HOST_TEMPLATES = new Set(['launchd']);
|
|
38
|
+
|
|
39
|
+
/** The forge's templates, which ship under `templates/github/` and hold the gate workflow alone. */
|
|
40
|
+
const FORGE_TEMPLATES = 'github';
|
|
41
|
+
|
|
42
|
+
/** What the gate workflow's template holds where the forked workflow pins Rigger's commit. */
|
|
43
|
+
export const PIN = 'PINNED_COMMIT';
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Where a package records the commit it was packed from, as `scripts/record-commit.mjs` writes it
|
|
47
|
+
* (`{ "commit": "<full SHA>" }`), relative to the package's root.
|
|
48
|
+
*/
|
|
49
|
+
export const COMMIT_RECORD = 'src/package-commit.json';
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The commit the package at `packageRoot` records, as `{ commit }`, or `{ why }` saying in one
|
|
53
|
+
* line why it records none: no record, one that is not JSON, or one naming no full SHA.
|
|
54
|
+
*/
|
|
55
|
+
export function recordedCommit(packageRoot) {
|
|
56
|
+
let text;
|
|
57
|
+
try {
|
|
58
|
+
text = readFileSync(join(packageRoot, COMMIT_RECORD), 'utf8');
|
|
59
|
+
} catch (failure) {
|
|
60
|
+
return { why: failure.code === 'ENOENT' ? `it holds no \`${COMMIT_RECORD}\`` : `\`${COMMIT_RECORD}\` could not be read: ${failure.code}` };
|
|
61
|
+
}
|
|
62
|
+
let commit;
|
|
63
|
+
try {
|
|
64
|
+
commit = JSON.parse(text)?.commit;
|
|
65
|
+
} catch {
|
|
66
|
+
return { why: `\`${COMMIT_RECORD}\` is not JSON` };
|
|
67
|
+
}
|
|
68
|
+
return typeof commit === 'string' && /^[0-9a-f]{40}$/.test(commit) ? { commit } : { why: `\`${COMMIT_RECORD}\` names no commit's full SHA` };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The templates this package ships. Everything `init` writes is read from here and from nowhere
|
|
73
|
+
* else (`R-SAFE-6`): the assets in this repository's own `.claude/` are what `init` produced for
|
|
74
|
+
* it, so reading them back would make the source and the copy one circle.
|
|
75
|
+
*/
|
|
76
|
+
export const TEMPLATES = resolve(dirname(fileURLToPath(import.meta.url)), '..', '..', 'templates');
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Where a remote names its host, and any userinfo with it: a URL's scheme and authority, or the
|
|
80
|
+
* `user@host:` a scp-style remote opens with, up to the last colon ahead of the first slash, so a
|
|
81
|
+
* colon inside the userinfo is not taken for the one that ends the host. It is dropped before the
|
|
82
|
+
* owner and name are read, so that neither the host nor a credential can be read as either.
|
|
83
|
+
*/
|
|
84
|
+
const HOST = /^(?:[a-z][a-z0-9+.-]*:\/\/[^/]*|[^/]*:)/i;
|
|
85
|
+
|
|
86
|
+
/** The owner and name at the end of a remote's path. */
|
|
87
|
+
const SLUG = /(?:^|\/)([^/]+)\/([^/]+?)(?:\.git)?\/?$/;
|
|
88
|
+
|
|
89
|
+
/** Git can echo a remote URL in an error; the report keeps the cause without its userinfo. */
|
|
90
|
+
const printableGitReason = (why) => why.replace(/\b([a-z][a-z0-9+.-]*:\/\/)[^\s/]*@/gi, '$1<userinfo>@');
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The `owner/name` git says this repository's `origin` remote points at, as `{ repo }`, or
|
|
94
|
+
* `{ repo: null, why }` where git answers with nothing, `why` saying in one line what it answered
|
|
95
|
+
* instead.
|
|
96
|
+
*
|
|
97
|
+
* Git owns what `origin` is, so it is asked rather than guessed from a directory name (`D16`
|
|
98
|
+
* rule 1). Where its answer can differ from what is read here (`D16` rule 3), each measured by
|
|
99
|
+
* asking it rather than reasoned about:
|
|
100
|
+
*
|
|
101
|
+
* - a directory that is no repository answers 128; a repository with no `origin` answers 2, which
|
|
102
|
+
* is what any absent remote name answers. Both write to stderr and leave stdout empty, so both
|
|
103
|
+
* yield null here, and nothing reads the two apart. Whoever needs to tell them apart asks git
|
|
104
|
+
* for the status rather than taking either number from this list, because a number in a
|
|
105
|
+
* comment is only as good as the run behind it — these are from git 2.55.0;
|
|
106
|
+
* - a host with no git to run never starts it, and a git its timeout ended answers nothing that
|
|
107
|
+
* can be read, so both yield null here, `why` saying which;
|
|
108
|
+
* - a directory inside another repository answers with that outer repository's remote, because
|
|
109
|
+
* `git -C` walks up as every git command does, so `init` in a subdirectory names the repository
|
|
110
|
+
* above it;
|
|
111
|
+
* - a remote carrying more path segments than GitHub uses — `host/group/sub/widgets.git` —
|
|
112
|
+
* yields the last two, which is not what a forge with a nested namespace would want read;
|
|
113
|
+
* - a remote with one segment after its host — `https://host/widgets` — yields null, because the
|
|
114
|
+
* host is never read as an owner, so a credential in front of it never reaches the config.
|
|
115
|
+
*
|
|
116
|
+
* Git is asked through L0 (`gitAnswer`), with `emitter` for L0's kills and `timeout` for the call.
|
|
117
|
+
* A kill the sink refused rejects, as the adapter rejects.
|
|
118
|
+
*/
|
|
119
|
+
export async function repoSlug(dir, options) {
|
|
120
|
+
const { stdout, why } = await gitAnswer(['-C', dir, 'remote', 'get-url', 'origin'], options);
|
|
121
|
+
if (why !== undefined) return { repo: null, why };
|
|
122
|
+
const found = stdout.trim().replace(HOST, '').match(SLUG);
|
|
123
|
+
// The remote is never quoted: a URL can carry a credential, and this line is printed.
|
|
124
|
+
if (!found) return { repo: null, why: 'the remote it named ends in no owner and name; `git remote get-url origin` shows it' };
|
|
125
|
+
return { repo: `${found[1]}/${found[2]}` };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Every file one provider's templates become, as paths relative to the consumer's repository.
|
|
130
|
+
*
|
|
131
|
+
* The tree under `templates/<provider>/` is the tree the provider reads, so the relative path is
|
|
132
|
+
* carried across unchanged and only the root moves. A directory naming a provider Rigger has no
|
|
133
|
+
* destination for is refused rather than forked somewhere plausible: a consumer whose assets
|
|
134
|
+
* landed where nothing reads them has a Rigger that dispatches an agent with no prompt.
|
|
135
|
+
*/
|
|
136
|
+
function forks(templates, provider, within = '') {
|
|
137
|
+
// Asked of the table's own keys, as `verbs.mjs` asks of `LANDED` and `readShape` of a
|
|
138
|
+
// consumer's declarations. A destination this table inherits was stated by nobody: read through
|
|
139
|
+
// the prototype chain, every name `Object.prototype` carries answers as a provider Rigger has a
|
|
140
|
+
// destination for, so the refusal below never fires for one and `constructor` forks the assets
|
|
141
|
+
// to `function Object() { [native code] }/` — the landing place this refusal exists to prevent.
|
|
142
|
+
if (!Object.hasOwn(ASSET_DESTINATIONS, provider)) {
|
|
143
|
+
throw new Error(
|
|
144
|
+
`\`templates/${provider}/\` ships assets for \`${provider}\`, which is no provider Rigger ` +
|
|
145
|
+
'reads assets for, so there is nowhere to fork them to.',
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
const directory = ASSET_DESTINATIONS[provider];
|
|
149
|
+
return readdirSync(join(templates, provider, within), { withFileTypes: true })
|
|
150
|
+
.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0))
|
|
151
|
+
.flatMap((entry) => {
|
|
152
|
+
const next = within === '' ? entry.name : `${within}/${entry.name}`;
|
|
153
|
+
if (entry.isDirectory()) return forks(templates, provider, next);
|
|
154
|
+
return [{
|
|
155
|
+
path: `${directory}/${next}`,
|
|
156
|
+
content: readFileSync(join(templates, provider, next), 'utf8'),
|
|
157
|
+
}];
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* The starter config with each value the caller knows written in, and each it does not left
|
|
163
|
+
* holding the name the template ships.
|
|
164
|
+
*
|
|
165
|
+
* The placeholder is replaced with its quotes, and the value written back as the literal it is,
|
|
166
|
+
* so a board number lands as `project: 6` rather than `project: '6'`. The template has to carry
|
|
167
|
+
* it quoted either way: an unquoted name is no valid module, and a consumer's first `doctor` on a
|
|
168
|
+
* config that throws on import is a crash where a refusal naming the field belongs.
|
|
169
|
+
*/
|
|
170
|
+
const fill = (starter, values) =>
|
|
171
|
+
Object.entries(values).reduce(
|
|
172
|
+
(text, [key, value]) => (value === undefined || value === null
|
|
173
|
+
? text
|
|
174
|
+
: text.replace(`'${PLACEHOLDER[key]}'`, typeof value === 'number' ? String(value) : `'${value}'`)),
|
|
175
|
+
starter,
|
|
176
|
+
);
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Every file `init` would write, as a path relative to the consumer's repository, each with the
|
|
180
|
+
* content it would be written with.
|
|
181
|
+
*
|
|
182
|
+
* The config comes first because it is what names the rest: the roles it declares name their
|
|
183
|
+
* agent files by path, and those paths are the assets forked below it.
|
|
184
|
+
*
|
|
185
|
+
* `repo` is what git says `origin` points at, and `project` is the board number, which no caller
|
|
186
|
+
* inside `init` can know: nothing it reads names a board, so it passes none and the consumer
|
|
187
|
+
* answers it in the file.
|
|
188
|
+
*
|
|
189
|
+
* `commit` is the commit the gate workflow pins Rigger to, the one the package records. With none,
|
|
190
|
+
* the forge's templates are left out, since a workflow pinned to nothing runs no known gate.
|
|
191
|
+
*/
|
|
192
|
+
export function plan({ templates = TEMPLATES, repo, project, commit } = {}) {
|
|
193
|
+
const starter = readFileSync(join(templates, CONFIG), 'utf8');
|
|
194
|
+
return [
|
|
195
|
+
{ path: CONFIG, content: fill(starter, { repo, project }) },
|
|
196
|
+
...readdirSync(templates, { withFileTypes: true })
|
|
197
|
+
.filter((entry) => entry.isDirectory() && !HOST_TEMPLATES.has(entry.name) && (entry.name !== FORGE_TEMPLATES || commit !== undefined))
|
|
198
|
+
.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0))
|
|
199
|
+
.flatMap((entry) => forks(templates, entry.name))
|
|
200
|
+
.map((file) => (file.path.startsWith(`${forgeAssets}/`) ? { ...file, content: file.content.replaceAll(PIN, commit) } : file)),
|
|
201
|
+
];
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** The paths the forge's templates fork to, as a plan with a commit would write them. */
|
|
205
|
+
const forgeForks = (templates) => (existsSync(join(templates, FORGE_TEMPLATES)) ? forks(templates, FORGE_TEMPLATES).map((file) => file.path) : []);
|
|
206
|
+
|
|
207
|
+
/** A heading and the paths under it, or nothing at all where there are no paths. */
|
|
208
|
+
const listing = (heading, paths) => (paths.length === 0 ? [] : [heading, ...paths.map((path) => ` ${path}`), '']);
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Forks the templates into a target, writing nothing over a file that is already there.
|
|
212
|
+
*
|
|
213
|
+
* A file that exists is left exactly as it is and named in the report. That is what makes a
|
|
214
|
+
* second run safe: the assets are the consumer's from the moment they land (`R-SAFE-6`), so an
|
|
215
|
+
* edited role prompt is the one thing `init` must never quietly replace, and it cannot tell an
|
|
216
|
+
* edit from an original without keeping a copy of what it wrote — which is state, and state a
|
|
217
|
+
* restart would have to rebuild (`D1`).
|
|
218
|
+
*
|
|
219
|
+
* `init` refuses a target that is the running package's source tree, lies within it, or holds it,
|
|
220
|
+
* as `sameTree` answers against `packageRoot`, before it spawns or writes anything (`R-SAFE-5`).
|
|
221
|
+
* So the check asks no `git`: git would name the repository above a subdirectory, and `init`
|
|
222
|
+
* writes into `target` whatever git names. For the same reason it names `<target>/.rigger` as its
|
|
223
|
+
* state directory before its one spawn (the architect's ruling 3, §5, on #332), which the sink
|
|
224
|
+
* creates only at its first write, so a run that kills nothing creates none. A kill the sink
|
|
225
|
+
* refused stops `init` before it writes anything, and it exits non-zero naming the kill.
|
|
226
|
+
*/
|
|
227
|
+
export const init = (options) => recording((opened) => forking(opened, options));
|
|
228
|
+
|
|
229
|
+
/** An entry that exists or cannot be inspected; uncertainty must not authorize a fork. */
|
|
230
|
+
function mayHold(path) {
|
|
231
|
+
try {
|
|
232
|
+
lstatSync(path);
|
|
233
|
+
return true;
|
|
234
|
+
} catch (failure) {
|
|
235
|
+
return failure.code !== 'ENOENT';
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Evidence against treating Git's exit 128 as proof that a target is outside a repository.
|
|
241
|
+
*
|
|
242
|
+
* Git owns repository discovery (D16). This scan only vetoes a fork after Git could not name a
|
|
243
|
+
* worktree. The real-Git tests measure three ways exit 128 differs from "not a repository": an
|
|
244
|
+
* unsafe repository and an invalid gitfile have a `.git` entry, while a bare repository has its
|
|
245
|
+
* administrative files at its root. A nonrepository with these same entries is also refused;
|
|
246
|
+
* this guard does not claim that their presence establishes a usable repository.
|
|
247
|
+
*/
|
|
248
|
+
function gitMetadataAbove(dir) {
|
|
249
|
+
for (let here = resolve(dir); ; here = dirname(here)) {
|
|
250
|
+
if (mayHold(join(here, '.git'))) return true;
|
|
251
|
+
if (['HEAD', 'config', 'objects', 'refs'].every((entry) => mayHold(join(here, entry)))) return true;
|
|
252
|
+
if (dirname(here) === here) return false;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** `init`'s work, recording through the sink `opened` holds. */
|
|
257
|
+
async function forking({ sink, name }, {
|
|
258
|
+
target = process.cwd(), templates = TEMPLATES, packageRoot = PACKAGE, timeout, ask,
|
|
259
|
+
} = {}) {
|
|
260
|
+
if (sameTree(target, packageRoot)) return sourceTreeRefusal('init', real(target));
|
|
261
|
+
const hooksEntry = lstatSync(join(target, '.githooks'), { throwIfNoEntry: false });
|
|
262
|
+
if (hooksEntry?.isSymbolicLink()) {
|
|
263
|
+
return { text: 'rigger init: .githooks is a symlink; refusing to write hooks.', code: 1 };
|
|
264
|
+
}
|
|
265
|
+
if (hooksEntry?.isFile()) {
|
|
266
|
+
return { text: 'rigger init: .githooks is a regular file; refusing to write hooks.', code: 1 };
|
|
267
|
+
}
|
|
268
|
+
name(target);
|
|
269
|
+
let read;
|
|
270
|
+
try {
|
|
271
|
+
read = await repoSlug(target, { ask, emitter: sink.emitter({ layer: 'L0' }), timeout });
|
|
272
|
+
} catch (failure) {
|
|
273
|
+
if (failure.code !== EVENT_REFUSED) throw failure;
|
|
274
|
+
return { text: `rigger init: ${failure.message}`, code: 1 };
|
|
275
|
+
}
|
|
276
|
+
const { repo, why } = read;
|
|
277
|
+
const git = { ask, emitter: sink.emitter({ layer: 'L0' }), timeout };
|
|
278
|
+
let probe = target;
|
|
279
|
+
while (!existsSync(probe) && dirname(probe) !== probe) probe = dirname(probe);
|
|
280
|
+
const found = await repoRoot(probe, git);
|
|
281
|
+
const targetPath = real(target);
|
|
282
|
+
if (found.root === null && (found.status !== 128 || gitMetadataAbove(probe))) {
|
|
283
|
+
return { text: `rigger init: cannot inspect the repository: ${found.why}`, code: 1 };
|
|
284
|
+
}
|
|
285
|
+
if (found.root !== null && found.root !== targetPath) {
|
|
286
|
+
return { text: `rigger init: ${targetPath} is inside the repository at ${found.root}; refusing to install hooks below its top level.`, code: 1 };
|
|
287
|
+
}
|
|
288
|
+
let setHooks = false;
|
|
289
|
+
if (found.root === targetPath) {
|
|
290
|
+
const current = await gitAnswer(['-C', target, 'config', '--show-scope', '--get', 'core.hooksPath'], git);
|
|
291
|
+
if (current.why === undefined) {
|
|
292
|
+
const [scope, value] = current.stdout.trim().split('\t', 2);
|
|
293
|
+
let pointsHere = value === '.githooks';
|
|
294
|
+
if (isAbsolute(value)) {
|
|
295
|
+
try {
|
|
296
|
+
pointsHere = basename(value) === '.githooks' && realpathSync.native(dirname(value)) === targetPath;
|
|
297
|
+
} catch {
|
|
298
|
+
pointsHere = false;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
if (!['local', 'worktree'].includes(scope) || !pointsHere) {
|
|
302
|
+
return { text: `rigger init: core.hooksPath is set in ${scope} to ${value}; refusing to change it.`, code: 1 };
|
|
303
|
+
}
|
|
304
|
+
} else if (current.status === 1) {
|
|
305
|
+
setHooks = true;
|
|
306
|
+
} else {
|
|
307
|
+
return { text: `rigger init: cannot inspect core.hooksPath: ${current.why}`, code: 1 };
|
|
308
|
+
}
|
|
309
|
+
if (setHooks) {
|
|
310
|
+
const gitHooks = await gitAnswer(['-C', target, 'rev-parse', '--git-path', 'hooks'], git);
|
|
311
|
+
if (gitHooks.why !== undefined) return { text: `rigger init: ${gitHooks.why}`, code: 1 };
|
|
312
|
+
if (gitHooks.stdout.trim() === '') return { text: 'rigger init: git named no hooks directory.', code: 1 };
|
|
313
|
+
const hooks = resolve(target, gitHooks.stdout.trim());
|
|
314
|
+
let entries;
|
|
315
|
+
try {
|
|
316
|
+
entries = readdirSync(hooks);
|
|
317
|
+
} catch (failure) {
|
|
318
|
+
if (failure.code !== 'ENOENT') return { text: `rigger init: cannot read git hooks: ${failure.message}`, code: 1 };
|
|
319
|
+
entries = [];
|
|
320
|
+
}
|
|
321
|
+
const existing = entries.filter((entry) => {
|
|
322
|
+
if (entry.endsWith('.sample')) return false;
|
|
323
|
+
try {
|
|
324
|
+
const file = statSync(join(hooks, entry));
|
|
325
|
+
return file.isFile() && Boolean(file.mode & 0o111);
|
|
326
|
+
} catch {
|
|
327
|
+
return false;
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
if (existing.length > 0) {
|
|
331
|
+
return { text: `rigger init: executable hooks in Git's hooks directory would be disabled by core.hooksPath: ${existing.join(', ')}`, code: 1 };
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
const pinned = recordedCommit(packageRoot);
|
|
336
|
+
const files = plan({ templates, repo, commit: pinned.commit });
|
|
337
|
+
const wrote = [];
|
|
338
|
+
const skipped = [];
|
|
339
|
+
for (const file of files) {
|
|
340
|
+
const path = join(target, file.path);
|
|
341
|
+
if (file.path === '.githooks/pre-push'
|
|
342
|
+
? lstatSync(path, { throwIfNoEntry: false })
|
|
343
|
+
: existsSync(path)) {
|
|
344
|
+
skipped.push(file.path);
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
348
|
+
writeFileSync(path, file.content, { mode: file.path === '.githooks/pre-push' ? 0o755 : 0o666 });
|
|
349
|
+
wrote.push(file.path);
|
|
350
|
+
}
|
|
351
|
+
if (setHooks) {
|
|
352
|
+
const configured = await gitAnswer(['-C', target, 'config', '--local', 'core.hooksPath', '.githooks'], git);
|
|
353
|
+
if (configured.why !== undefined) return { text: `rigger init: ${configured.why}`, code: 1 };
|
|
354
|
+
}
|
|
355
|
+
const skippedHook = skipped.includes('.githooks/pre-push');
|
|
356
|
+
const hook = join(target, '.githooks/pre-push');
|
|
357
|
+
const hookStats = skippedHook ? statSync(hook, { throwIfNoEntry: false }) : null;
|
|
358
|
+
const matchesHook = hookStats?.isFile() && readFileSync(hook, 'utf8')
|
|
359
|
+
=== files.find((file) => file.path === '.githooks/pre-push').content;
|
|
360
|
+
const executableHook = hookStats?.isFile() && Boolean(hookStats.mode & 0o111);
|
|
361
|
+
return {
|
|
362
|
+
text: [
|
|
363
|
+
`rigger init: wrote ${wrote.length} of ${files.length} files into ${target}`,
|
|
364
|
+
...wrote.map((path) => ` ${path}`),
|
|
365
|
+
...listing(`left these ${skipped.length} alone, because they are already there:`, skipped),
|
|
366
|
+
...(skippedHook ? [`\`.githooks/pre-push\` ${matchesHook ? 'matches Rigger\'s hook' : 'is not Rigger\'s hook'}; it was left in place.`] : []),
|
|
367
|
+
...(skippedHook && !executableHook
|
|
368
|
+
? ['`.githooks/pre-push` is not executable; git will not run it.'] : []),
|
|
369
|
+
...(found.root === null ? [`did not set core.hooksPath because ${found.why}.`] : []),
|
|
370
|
+
// Pinned to no commit, the workflow would run whatever Rigger is newest, so it is not forked.
|
|
371
|
+
...forgeForks(templates).filter(() => pinned.commit === undefined)
|
|
372
|
+
.map((path) => `did not fork \`${path}\`, because the Rigger package records no commit to pin it to: ${pinned.why}.`),
|
|
373
|
+
...(repo ? [] : [`\`${CONFIG}\` names \`${PLACEHOLDER.repo}\`, because git named no \`origin\` remote to read it from: ${printableGitReason(why)}.`]),
|
|
374
|
+
// Said only where the config was written, because a run that skipped it would be claiming
|
|
375
|
+
// something about a file that is the consumer's by then and that `init` never read.
|
|
376
|
+
...(wrote.includes(CONFIG)
|
|
377
|
+
? [`\`${CONFIG}\` names \`${PLACEHOLDER.project}\` as its board, because a board number is `
|
|
378
|
+
+ `GitHub's and nothing here names it. Set \`board.project\` to the number your board's `
|
|
379
|
+
+ 'URL ends in: Rigger refuses this config until you do.']
|
|
380
|
+
: []),
|
|
381
|
+
].join('\n'),
|
|
382
|
+
code: 0,
|
|
383
|
+
};
|
|
384
|
+
}
|