@indigoai-us/hq-cli 5.345.219 → 5.345.221
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 +170 -0
- package/dist/command-catalog.generated.d.ts +169 -3
- package/dist/command-catalog.generated.js +206 -3
- package/dist/commands/auth.js +4 -1
- package/dist/commands/bot-runtime.d.ts +18 -5
- package/dist/commands/bot-runtime.js +29 -12
- package/dist/commands/bot.d.ts +85 -0
- package/dist/commands/bot.js +245 -16
- package/dist/commands/cloud.d.ts +16 -0
- package/dist/commands/cloud.js +112 -102
- package/dist/commands/core-checkpoint-native.d.ts +13 -0
- package/dist/commands/core-checkpoint-native.js +167 -52
- package/dist/commands/core-hook-registration-plan.js +5 -4
- package/dist/commands/core-stop-autosave.d.ts +21 -0
- package/dist/commands/core-stop-autosave.js +300 -0
- package/dist/commands/core.js +2 -0
- package/dist/commands/daemon.d.ts +5 -0
- package/dist/commands/daemon.js +52 -9
- package/dist/commands/doctor-usage.d.ts +5 -0
- package/dist/commands/doctor-usage.js +25 -0
- package/dist/commands/env.d.ts +49 -4
- package/dist/commands/env.js +355 -5
- package/dist/commands/integrations.js +84 -3
- package/dist/commands/lanes-shared.d.ts +3 -0
- package/dist/commands/lanes.d.ts +3 -0
- package/dist/commands/lanes.js +87 -3
- package/dist/commands/packs.js +23 -0
- package/dist/commands/pkg-update.d.ts +6 -0
- package/dist/commands/pkg-update.js +15 -7
- package/dist/commands/run.js +55 -45
- package/dist/commands/sources.js +4 -3
- package/dist/commands/sync-core.d.ts +1 -0
- package/dist/commands/sync-core.js +77 -3
- package/dist/commands/whoami.js +96 -80
- package/dist/lib/bot/config.d.ts +17 -0
- package/dist/lib/bot/config.js +13 -0
- package/dist/lib/bot/fleet-cron.js +29 -4
- package/dist/lib/bot/fleet-local.d.ts +30 -2
- package/dist/lib/bot/fleet-local.js +60 -1
- package/dist/lib/bot/grok-login-scope.d.ts +36 -0
- package/dist/lib/bot/grok-login-scope.js +93 -0
- package/dist/lib/bot/inflight.d.ts +9 -0
- package/dist/lib/bot/jobs/hot-signal-sync.d.ts +51 -0
- package/dist/lib/bot/jobs/hot-signal-sync.js +90 -9
- package/dist/lib/bot/legacy-provider-login.d.ts +32 -9
- package/dist/lib/bot/legacy-provider-login.js +28 -22
- package/dist/lib/bot/run.d.ts +29 -5
- package/dist/lib/bot/run.js +203 -1
- package/dist/lib/bot/runtime-sign-in-check.js +6 -1
- package/dist/lib/bot/runtime-sign-in.d.ts +2 -0
- package/dist/lib/bot/signals-try-now.d.ts +76 -0
- package/dist/lib/bot/signals-try-now.js +111 -0
- package/dist/lib/capacity/collectors/claude-oauth.d.ts +22 -3
- package/dist/lib/capacity/collectors/claude-oauth.js +78 -22
- package/dist/lib/capacity/scheduler.d.ts +8 -0
- package/dist/lib/capacity/scheduler.js +5 -2
- package/dist/lib/core-utils/sentry-report.js +2 -0
- package/dist/lib/daemon/machine-checks.d.ts +22 -1
- package/dist/lib/daemon/machine-checks.js +54 -12
- package/dist/lib/daemon/run.js +71 -8
- package/dist/lib/daemon/runtime-notifications.d.ts +7 -0
- package/dist/lib/daemon/runtime-notifications.js +50 -0
- package/dist/lib/daemon/stop-autosave-lock.d.ts +19 -0
- package/dist/lib/daemon/stop-autosave-lock.js +166 -0
- package/dist/lib/daemon/stop-autosave.d.ts +28 -0
- package/dist/lib/daemon/stop-autosave.js +111 -0
- package/dist/lib/daemon/stop-diagnostics.d.ts +11 -0
- package/dist/lib/daemon/stop-diagnostics.js +136 -0
- package/dist/lib/daemon/stop-job-executor.d.ts +19 -0
- package/dist/lib/daemon/stop-job-executor.js +126 -0
- package/dist/lib/daemon/stop-jobs.d.ts +93 -0
- package/dist/lib/daemon/stop-jobs.js +599 -0
- package/dist/lib/doctor/checks/fleet-hook-core.d.ts +9 -1
- package/dist/lib/doctor/checks/fleet-hook-core.js +39 -15
- package/dist/lib/doctor/checks/host-core.js +1 -1
- package/dist/lib/environment/access-probes.d.ts +234 -0
- package/dist/lib/environment/access-probes.js +918 -0
- package/dist/lib/environment/profile.d.ts +15 -2
- package/dist/lib/environment/profile.js +55 -20
- package/dist/lib/environment/rfc3339.d.ts +8 -0
- package/dist/lib/environment/rfc3339.js +26 -0
- package/dist/lib/environment/work-requirements.d.ts +169 -0
- package/dist/lib/environment/work-requirements.js +389 -0
- package/dist/lib/hook-engine/coverage.d.ts +137 -7
- package/dist/lib/hook-engine/coverage.js +1424 -4
- package/dist/lib/hook-engine/fleet-registration.d.ts +17 -5
- package/dist/lib/hook-engine/fleet-registration.js +53 -17
- package/dist/lib/hook-engine/git-stop-config.json +513 -0
- package/dist/lib/hook-engine/native-stop-compatibility.json +311 -0
- package/dist/lib/hook-engine/reconcile.js +2 -2
- package/dist/lib/hot-channels/alert.d.ts +10 -1
- package/dist/lib/hot-channels/alert.js +38 -8
- package/dist/lib/hot-channels/signals-claude-login.d.ts +53 -0
- package/dist/lib/hot-channels/signals-claude-login.js +96 -0
- package/dist/lib/hot-channels/signals-pass.d.ts +143 -17
- package/dist/lib/hot-channels/signals-pass.js +324 -17
- package/dist/lib/hot-channels/signals-runtime.d.ts +30 -0
- package/dist/lib/hot-channels/signals-runtime.js +82 -0
- package/dist/lib/install/hook-target.js +1 -1
- package/dist/lib/lanes/host-capacity.d.ts +26 -2
- package/dist/lib/lanes/host-capacity.js +17 -2
- package/dist/lib/lanes/host-compute.d.ts +90 -0
- package/dist/lib/lanes/host-compute.js +185 -0
- package/dist/lib/lanes/index.d.ts +2 -0
- package/dist/lib/lanes/index.js +1 -0
- package/dist/lib/lanes/reconcile.js +22 -1
- package/dist/lib/lanes/resource-usage.d.ts +155 -0
- package/dist/lib/lanes/resource-usage.js +589 -0
- package/dist/lib/lanes/run-size.d.ts +46 -0
- package/dist/lib/lanes/run-size.js +197 -0
- package/dist/lib/lanes/store.js +27 -0
- package/dist/lib/lanes/throttle.d.ts +36 -1
- package/dist/lib/lanes/throttle.js +47 -10
- package/dist/lib/lanes/types.d.ts +40 -0
- package/dist/lib/lanes/types.js +2 -2
- package/dist/lib/lanes/watcher.js +13 -4
- package/dist/lib/lanes/worker-process.d.ts +25 -2
- package/dist/lib/lanes/worker-process.js +71 -19
- package/dist/lib/placement/match.d.ts +192 -0
- package/dist/lib/placement/match.js +667 -0
- package/dist/lib/safe-store.d.ts +14 -0
- package/dist/lib/safe-store.js +77 -4
- package/dist/lib/search-index/index.d.ts +88 -4
- package/dist/lib/search-index/index.js +299 -25
- package/dist/lib/search-index/max-doc-bytes.d.ts +17 -6
- package/dist/lib/search-index/max-doc-bytes.js +48 -23
- package/dist/lib/signals/codex-lockdown.d.ts +79 -0
- package/dist/lib/signals/codex-lockdown.js +271 -0
- package/dist/lib/signals/codex-login.d.ts +37 -0
- package/dist/lib/signals/codex-login.js +83 -0
- package/dist/lib/signals/engine.d.ts +28 -0
- package/dist/lib/signals/engine.js +77 -0
- package/dist/lib/signals/reply.d.ts +48 -0
- package/dist/lib/signals/reply.js +94 -12
- package/dist/lib/signals/run-record.js +6 -1
- package/dist/lib/signals/settings.d.ts +5 -0
- package/dist/lib/signals/settings.js +2 -0
- package/dist/lib/signals/steps/adapters.js +6 -0
- package/dist/lib/signals/steps/extract.d.ts +135 -12
- package/dist/lib/signals/steps/extract.js +416 -84
- package/dist/lib/signals/steps/types.d.ts +21 -0
- package/dist/lib/sync-core/engine-home.d.ts +41 -2
- package/dist/lib/sync-core/engine-home.js +132 -19
- package/dist/lib/sync-core/reconcile.js +1 -1
- package/dist/lib/sync-core/release-compare.d.ts +7 -0
- package/dist/lib/sync-core/release-compare.js +16 -0
- package/dist/lib/sync-core/release.d.ts +2 -1
- package/dist/lib/sync-core/release.js +2 -10
- package/dist/lib/sync-core/rollout-state.d.ts +2 -1
- package/dist/lib/sync-core/rollout-state.js +5 -2
- package/dist/lib/sync-core/vault-uid.d.ts +2 -0
- package/dist/lib/sync-core/vault-uid.js +4 -0
- package/dist/run/schema-env-overrides.d.ts +24 -0
- package/dist/run/schema-env-overrides.js +72 -0
- package/dist/schemas/hq-work-requirements.v1.schema.json +145 -0
- package/dist/utils/auth-session-check.d.ts +16 -0
- package/dist/utils/auth-session-check.js +16 -0
- package/dist/utils/client-health-contract.d.ts +6 -0
- package/dist/utils/client-health-contract.js +13 -0
- package/dist/utils/client-health.d.ts +7 -0
- package/dist/utils/client-health.js +22 -1
- package/dist/utils/command-usage.d.ts +40 -0
- package/dist/utils/command-usage.js +61 -0
- package/dist/utils/duplicate-command-usage.d.ts +72 -0
- package/dist/utils/duplicate-command-usage.js +125 -0
- package/dist/utils/host-registration.d.ts +1 -1
- package/dist/utils/host-registration.js +6 -1
- package/dist/utils/resident-package-preload.js +4 -0
- package/dist/utils/sentry-fingerprint.js +9 -0
- package/dist/utils/windows-bash.d.ts +2 -0
- package/dist/utils/windows-bash.js +2 -1
- package/package.json +15 -10
- package/scripts/bench-core-compare.mjs +313 -0
- package/scripts/bench-core.mjs +399 -12
- package/scripts/check-function-manifest.mjs +121 -7
- package/dist/commands/__fixtures__/access-vault.d.ts +0 -95
- package/dist/commands/__fixtures__/access-vault.js +0 -166
- package/dist/commands/__fixtures__/make-tar.d.ts +0 -38
- package/dist/commands/__fixtures__/make-tar.js +0 -102
- package/dist/commands/secrets-public-types.test-d.d.ts +0 -2
- package/dist/commands/secrets-public-types.test-d.js +0 -18
- package/dist/lib/decisions/fixtures/us001.d.ts +0 -137
- package/dist/lib/decisions/fixtures/us001.js +0 -21
- package/dist/lib/doctor/__testing__/fake-hq-tree.d.ts +0 -226
- package/dist/lib/doctor/__testing__/fake-hq-tree.js +0 -408
- package/dist/lib/doctor/__testing__/run-doctor.d.ts +0 -8
- package/dist/lib/doctor/__testing__/run-doctor.js +0 -69
- package/dist/lib/fingerprint/fixtures.d.ts +0 -19
- package/dist/lib/fingerprint/fixtures.js +0 -61
- package/dist/lib/hot-channels/adapters/__fixtures__/dummy.d.ts +0 -33
- package/dist/lib/hot-channels/adapters/__fixtures__/dummy.js +0 -53
- package/dist/lib/hot-channels/adapters/__fixtures__/meeting-providers.d.ts +0 -38
- package/dist/lib/hot-channels/adapters/__fixtures__/meeting-providers.js +0 -64
- package/dist/lib/mesh/live/daemon/mqtt-events.test-d.d.ts +0 -2
- package/dist/lib/mesh/live/daemon/mqtt-events.test-d.js +0 -10
- package/dist/lib/test-utils/isolated-flag-company-context.d.ts +0 -3
- package/dist/lib/test-utils/isolated-flag-company-context.js +0 -18
- package/scripts/hook-engine-manifest.mjs +0 -22
- package/scripts/mesh-daemon-if-stale.mjs +0 -20
|
@@ -23,11 +23,1399 @@
|
|
|
23
23
|
* cannot be read counts as not covered, and an engine that reports no
|
|
24
24
|
* `covers` covers nothing, so the event stays on the old dispatcher.
|
|
25
25
|
*/
|
|
26
|
+
import { createHash } from 'node:crypto';
|
|
26
27
|
import * as fs from 'node:fs';
|
|
27
28
|
import * as path from 'node:path';
|
|
28
29
|
import { isCoreNextEntry } from './registration.js';
|
|
30
|
+
import stopCompatibility from './native-stop-compatibility.json' with { type: 'json' };
|
|
31
|
+
import gitStopConfig from './git-stop-config.json' with { type: 'json' };
|
|
29
32
|
/** Stands for the hook list when it cannot be read; never covered. */
|
|
30
33
|
export const UNREADABLE = '(the old dispatcher hook list could not be read)';
|
|
34
|
+
/** Hooks whose result depends on Git's environment (their queued jobs do not inherit it). */
|
|
35
|
+
const GIT_ENV_READERS = ['observe-patterns', 'hq-autocommit-bash-sweep', 'checkpoint-stop-gate'];
|
|
36
|
+
/** Ids that run in the minimal hook profile (`MINIMAL` in crates/hq-hook/src/stop/mod.rs). */
|
|
37
|
+
const MINIMAL_PROFILE = ['enforce-capability-link-render', 'enforce-humanize-before-send', 'lanes-senior-monitor-stop-gate', 'conduct-lanes-link-deliver'];
|
|
38
|
+
/** Rust's `char::is_whitespace` (Unicode White_Space): unlike JS `\s`, it has U+0085 and not U+FEFF. */
|
|
39
|
+
const RUST_WS = '\\t\\n\\v\\f\\r \\u0085\\u00a0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000';
|
|
40
|
+
const RUST_TRIM = new RegExp(`^[${RUST_WS}]+|[${RUST_WS}]+$`, 'g');
|
|
41
|
+
/** Rust's `str::trim`. */
|
|
42
|
+
const rustTrim = (text) => text.replace(RUST_TRIM, '');
|
|
43
|
+
/** Rust's `str::to_ascii_lowercase`: non-ASCII letters are left as they are. */
|
|
44
|
+
const asciiLower = (text) => text.replace(/[A-Z]+/g, (letters) => letters.toLowerCase());
|
|
45
|
+
/**
|
|
46
|
+
* Review r18b, as Rust `stop::compat::git_config_parse`: git 2.50's own config tokenizer (config.c
|
|
47
|
+
* `git_parse_source`, `get_base_var`, `get_extended_base_var`, `get_value`, `parse_value`), so a value is read
|
|
48
|
+
* exactly as git reads it: double quotes, the escapes `\"` `\\` `\n` `\t` `\b`, `#` and `;` comments outside
|
|
49
|
+
* quotes, a trailing `\` continuing the value on the next line, and blanks trimmed outside quotes. Section and
|
|
50
|
+
* key are ASCII-lowercased; a subsection keeps its case (`\` escapes the next character). A variable before
|
|
51
|
+
* any header has section `''` (git lists it and ignores it). `text` has no CR or NUL (the caller holds those).
|
|
52
|
+
* Returns null where git stops with "bad config line" (an unterminated quote, an unknown escape, a bad key or
|
|
53
|
+
* header), and also for the legacy `[section.sub]` spelling, which is not modeled.
|
|
54
|
+
*/
|
|
55
|
+
export function gitConfigParse(text) {
|
|
56
|
+
let at = 0;
|
|
57
|
+
let eof = false;
|
|
58
|
+
const next = () => {
|
|
59
|
+
if (at >= text.length) {
|
|
60
|
+
eof = true;
|
|
61
|
+
return '\n';
|
|
62
|
+
}
|
|
63
|
+
return text[at++];
|
|
64
|
+
};
|
|
65
|
+
const space = (c) => c === ' ' || c === '\t' || c === '\n' || c === '\v' || c === '\f' || c === '\r';
|
|
66
|
+
const alpha = (c) => (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
|
|
67
|
+
const keychar = (c) => alpha(c) || (c >= '0' && c <= '9') || c === '-';
|
|
68
|
+
const items = [];
|
|
69
|
+
let section = null;
|
|
70
|
+
let comment = false;
|
|
71
|
+
for (;;) {
|
|
72
|
+
let c = next();
|
|
73
|
+
if (c === '\n') {
|
|
74
|
+
if (eof)
|
|
75
|
+
return items;
|
|
76
|
+
comment = false;
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if (comment || space(c))
|
|
80
|
+
continue;
|
|
81
|
+
if (c === '#' || c === ';') {
|
|
82
|
+
comment = true;
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (c === '[') {
|
|
86
|
+
let name = '';
|
|
87
|
+
let sub = null;
|
|
88
|
+
for (;;) {
|
|
89
|
+
c = next();
|
|
90
|
+
if (eof)
|
|
91
|
+
return null;
|
|
92
|
+
if (c === ']')
|
|
93
|
+
break;
|
|
94
|
+
if (space(c)) {
|
|
95
|
+
do {
|
|
96
|
+
if (c === '\n')
|
|
97
|
+
return null;
|
|
98
|
+
c = next();
|
|
99
|
+
} while (space(c));
|
|
100
|
+
if (c !== '"')
|
|
101
|
+
return null;
|
|
102
|
+
sub = '';
|
|
103
|
+
for (;;) {
|
|
104
|
+
c = next();
|
|
105
|
+
if (c === '\n')
|
|
106
|
+
return null;
|
|
107
|
+
if (c === '"')
|
|
108
|
+
break;
|
|
109
|
+
if (c === '\\') {
|
|
110
|
+
c = next();
|
|
111
|
+
if (c === '\n')
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
sub += c;
|
|
115
|
+
}
|
|
116
|
+
if (next() !== ']')
|
|
117
|
+
return null;
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
if (!keychar(c) && c !== '.')
|
|
121
|
+
return null;
|
|
122
|
+
name += asciiLower(c);
|
|
123
|
+
}
|
|
124
|
+
if (name === '' || name.includes('.'))
|
|
125
|
+
return null;
|
|
126
|
+
section = { name, sub };
|
|
127
|
+
items.push({ header: true, section: name, subsection: sub });
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
if (!alpha(c))
|
|
131
|
+
return null;
|
|
132
|
+
let key = asciiLower(c);
|
|
133
|
+
for (;;) {
|
|
134
|
+
c = next();
|
|
135
|
+
if (eof || !keychar(c))
|
|
136
|
+
break;
|
|
137
|
+
key += asciiLower(c);
|
|
138
|
+
}
|
|
139
|
+
while (c === ' ' || c === '\t')
|
|
140
|
+
c = next();
|
|
141
|
+
let value = null;
|
|
142
|
+
if (c !== '\n') {
|
|
143
|
+
if (c !== '=')
|
|
144
|
+
return null;
|
|
145
|
+
let quote = false;
|
|
146
|
+
let inComment = false;
|
|
147
|
+
let trim = 0;
|
|
148
|
+
let out = '';
|
|
149
|
+
for (;;) {
|
|
150
|
+
c = next();
|
|
151
|
+
if (c === '\n') {
|
|
152
|
+
if (quote)
|
|
153
|
+
return null;
|
|
154
|
+
if (trim)
|
|
155
|
+
out = out.slice(0, trim);
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
if (inComment)
|
|
159
|
+
continue;
|
|
160
|
+
if (space(c) && !quote) {
|
|
161
|
+
if (!trim)
|
|
162
|
+
trim = out.length;
|
|
163
|
+
if (out.length)
|
|
164
|
+
out += c;
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
if (!quote && (c === ';' || c === '#')) {
|
|
168
|
+
inComment = true;
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
if (trim)
|
|
172
|
+
trim = 0;
|
|
173
|
+
if (c === '\\') {
|
|
174
|
+
c = next();
|
|
175
|
+
if (c === '\n')
|
|
176
|
+
continue;
|
|
177
|
+
if (c === 't')
|
|
178
|
+
c = '\t';
|
|
179
|
+
else if (c === 'b')
|
|
180
|
+
c = '\b';
|
|
181
|
+
else if (c === 'n')
|
|
182
|
+
c = '\n';
|
|
183
|
+
else if (c !== '\\' && c !== '"')
|
|
184
|
+
return null;
|
|
185
|
+
out += c;
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
if (c === '"') {
|
|
189
|
+
quote = !quote;
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
out += c;
|
|
193
|
+
}
|
|
194
|
+
value = out;
|
|
195
|
+
}
|
|
196
|
+
items.push({ header: false, section: section?.name ?? '', subsection: section ? section.sub : null, key, value });
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
/** Whether hook `id` runs under `HQ_HOOK_PROFILE` and `HQ_DISABLED_HOOKS` (`stop::enabled_by`). Empty values count as unset. */
|
|
200
|
+
export function enabledBy(env, id) {
|
|
201
|
+
const profile = env.HQ_HOOK_PROFILE || 'standard';
|
|
202
|
+
if (profile === 'minimal' && !MINIMAL_PROFILE.includes(id))
|
|
203
|
+
return false;
|
|
204
|
+
return !(env.HQ_DISABLED_HOOKS || '').split(',').some((entry) => rustTrim(entry) === id);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* The `GIT_*` variables that do not hold the native Stop
|
|
208
|
+
* (`gitEnvironmentAllowlist`, shared with `stop::compat::git_environment_allowlist`).
|
|
209
|
+
* Each is exempt only because it cannot change what the Git-reading Stop hooks'
|
|
210
|
+
* commands (`rev-parse --abbrev-ref HEAD`, `rev-parse --short HEAD`,
|
|
211
|
+
* `rev-parse --show-toplevel`, `log --oneline -50`, `status --porcelain=v1 -z
|
|
212
|
+
* --untracked-files=all`, `check-ignore -q --no-index`, `commit -m`) or `git
|
|
213
|
+
* config -l` print, or whether git dies. Lab, git 2.50.1: each set to empty,
|
|
214
|
+
* `bogus`, `0`, `1`, `false`, `/nonexistent/prog` and a non-UTF-8 value left
|
|
215
|
+
* every one of those commands' stdout, stderr and exit status unchanged.
|
|
216
|
+
*
|
|
217
|
+
* - `GIT_EDITOR`: read only when git starts an editor, which no Stop hook
|
|
218
|
+
* does. Claude Code exports it to the shells and hooks it starts, so without
|
|
219
|
+
* it every native Stop under Claude Code would be refused.
|
|
220
|
+
* - `GIT_PAGER`: read only when git pages to a terminal; hook output is a
|
|
221
|
+
* pipe. (`GIT_PAGER_IN_USE` is a different, boolean variable and holds.)
|
|
222
|
+
* - `GIT_ASKPASS`, `GIT_TERMINAL_PROMPT`: read only when git prompts for
|
|
223
|
+
* credentials, which local reads and commits never do.
|
|
224
|
+
*
|
|
225
|
+
* Everything else under `GIT_` holds, `GIT_CONFIG_COUNT` and its
|
|
226
|
+
* `GIT_CONFIG_KEY_<n>`/`GIT_CONFIG_VALUE_<n>` pairs included (review r17 C3).
|
|
227
|
+
*/
|
|
228
|
+
export const GIT_ENVIRONMENT_ALLOWLIST = stopCompatibility.gitEnvironmentAllowlist;
|
|
229
|
+
/**
|
|
230
|
+
* The first `GIT_*` variable name in `env` (sorted) outside the allowlist, or
|
|
231
|
+
* null. Names only: a variable is present whatever its value, empty included
|
|
232
|
+
* (review r17 C4: Rust reads names with `vars_os`, so a non-UTF-8 value counts
|
|
233
|
+
* on both sides). Windows variable names are not case-sensitive, so there the
|
|
234
|
+
* prefix and the allowlist are compared without case, as in Rust.
|
|
235
|
+
*/
|
|
236
|
+
export function gitEnvironmentOverride(env, platform = process.platform) {
|
|
237
|
+
const fold = (name) => (platform === 'win32' ? name.replace(/[a-z]+/g, (letters) => letters.toUpperCase()) : name);
|
|
238
|
+
const held = Object.keys(env)
|
|
239
|
+
.filter((name) => env[name] !== undefined)
|
|
240
|
+
.filter((name) => fold(name).startsWith('GIT_') && !GIT_ENVIRONMENT_ALLOWLIST.includes(fold(name)))
|
|
241
|
+
.sort();
|
|
242
|
+
return held[0] ?? null;
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Review r20 C1, as `git_discovery::version_zero_extension`: the repository
|
|
246
|
+
* extensions git accepts at `repositoryformatversion = 0` (and 1), other than
|
|
247
|
+
* `worktreeConfig`. `key` is lowercase; `value` is null for a bare key.
|
|
248
|
+
*/
|
|
249
|
+
export function versionZeroExtension(key, value) {
|
|
250
|
+
if (key === 'noop')
|
|
251
|
+
return true;
|
|
252
|
+
// git_discovery's `is_bool`: a bare key, empty, a boolean word, or a plain int of at most nine digits.
|
|
253
|
+
if (key === 'preciousobjects')
|
|
254
|
+
return value === null || value === '' || /^(?:true|false|yes|no|on|off)$/i.test(value) || /^-?(?:0|[1-9][0-9]{0,8})$/.test(value);
|
|
255
|
+
if (key === 'partialclone')
|
|
256
|
+
return value !== null;
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
const GIT_ALLOW = new Map(Object.entries(gitStopConfig.allow));
|
|
260
|
+
const GIT_READ_SECTIONS = new Set(gitStopConfig.readSections);
|
|
261
|
+
const GIT_READ_SUBSECTIONS = new Set(gitStopConfig.readSubsections);
|
|
262
|
+
const GIT_BOOL_WORDS = ['true', 'false', 'yes', 'no', 'on', 'off', '1', '0'];
|
|
263
|
+
const GIT_UNITS = { k: 1024n, m: 1024n ** 2n, g: 1024n ** 3n };
|
|
264
|
+
/** As Rust `git_uint`: unsigned digits (no sign, `-0` included, review r21 C1), with `units` one k/m/g. */
|
|
265
|
+
function gitUint(value, units) {
|
|
266
|
+
const match = /^(0|[1-9][0-9]{0,29})([kmgKMG]?)$/.exec(value);
|
|
267
|
+
if (!match || (match[2] && !units))
|
|
268
|
+
return null;
|
|
269
|
+
return BigInt(match[1]) * (match[2] ? GIT_UNITS[match[2].toLowerCase()] : 1n);
|
|
270
|
+
}
|
|
271
|
+
/** As Rust `Allowed::accepts`: whether the native Stop models `value` (null: a bare key). `home`: HOME is set. */
|
|
272
|
+
export function gitAllowedValue(entry, value, home) {
|
|
273
|
+
if (value === null)
|
|
274
|
+
return entry.bare === true;
|
|
275
|
+
if (value === '')
|
|
276
|
+
return entry.empty === true;
|
|
277
|
+
if ((entry.words ?? []).some((word) => word === value || (entry.fold === true && asciiLower(word) === asciiLower(value))))
|
|
278
|
+
return true;
|
|
279
|
+
switch (entry.type) {
|
|
280
|
+
case 'bool': return GIT_BOOL_WORDS.includes(asciiLower(value));
|
|
281
|
+
case 'color': return gitColor(value);
|
|
282
|
+
case 'text': return true;
|
|
283
|
+
case 'path': return !value.startsWith('~') || ((value === '~' || value.startsWith('~/')) && home);
|
|
284
|
+
case 'uint': {
|
|
285
|
+
const number = gitUint(value, entry.units === true);
|
|
286
|
+
return number !== null && number >= BigInt(entry.min ?? '0') && number <= BigInt(entry.max ?? '0');
|
|
287
|
+
}
|
|
288
|
+
default: return false;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
const GIT_COLORS = ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'];
|
|
292
|
+
const GIT_COLOR_ATTRIBUTES = ['bold', 'dim', 'italic', 'ul', 'blink', 'reverse', 'strike'];
|
|
293
|
+
/** As Rust `git_color`: a subset of git's color grammar (color.c `color_parse_mem`). */
|
|
294
|
+
export function gitColor(value) {
|
|
295
|
+
if (asciiLower(value) === 'reset')
|
|
296
|
+
return true;
|
|
297
|
+
let colors = 0;
|
|
298
|
+
for (const raw of value.split(/[ \t\n\v\f\r]+/).filter((word) => word !== '')) {
|
|
299
|
+
const word = asciiLower(raw);
|
|
300
|
+
const named = word.startsWith('bright') ? word.slice(6) : word;
|
|
301
|
+
if (GIT_COLORS.includes(named) || word === 'normal' || word === 'default') {
|
|
302
|
+
colors += 1;
|
|
303
|
+
if (colors > 2)
|
|
304
|
+
return false;
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
const attribute = word.startsWith('no') ? word.slice(word.startsWith('no-') ? 3 : 2) : word;
|
|
308
|
+
if (!GIT_COLOR_ATTRIBUTES.includes(attribute))
|
|
309
|
+
return false;
|
|
310
|
+
}
|
|
311
|
+
return true;
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Reviews r20 C2 and r21, as Rust `git_key_modeled`: in a section git reads during the legacy
|
|
315
|
+
* observe-patterns' commands (`git-stop-config.json`, derived by running git), a variable is modeled
|
|
316
|
+
* only when it is allowlisted and its value fits the entry's grammar. Variables in other sections
|
|
317
|
+
* pass: the lab shows git reads none of them there. Returns the hold, or null. `section` and `key` are lowercase.
|
|
318
|
+
*/
|
|
319
|
+
export function gitKeyHold(section, subsection, key, value, home) {
|
|
320
|
+
if (!(subsection !== null ? GIT_READ_SUBSECTIONS : GIT_READ_SECTIONS).has(section))
|
|
321
|
+
return null;
|
|
322
|
+
// `section.<subsection>.key` (the subsection keeps its case), then `section.*.key` and `section.*.*`.
|
|
323
|
+
const names = subsection !== null
|
|
324
|
+
? [`${section}.${subsection}.${key}`, `${section}.*.${key}`, `${section}.*.*`]
|
|
325
|
+
: [`${section}.${key}`, `${section}.*`];
|
|
326
|
+
for (const name of names) {
|
|
327
|
+
const entry = GIT_ALLOW.get(name);
|
|
328
|
+
if (entry)
|
|
329
|
+
return gitAllowedValue(entry, value, home) ? null : 'unmodeled Git config value';
|
|
330
|
+
}
|
|
331
|
+
return 'unmodeled Git configuration key';
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Why the native Stop cannot stand in for master-hook in this root, or null.
|
|
335
|
+
* Read-only; no hook registration is written here. The same rules as
|
|
336
|
+
* `crates/hq-hook/src/stop/compat.rs`, from the same policy file:
|
|
337
|
+
*
|
|
338
|
+
* - The native Stop runs the ported hooks the registry lists, so a listed
|
|
339
|
+
* ported hook must keep its canonical relative order and default execution
|
|
340
|
+
* settings. A listed hook with no port is left to `covers` (not covered).
|
|
341
|
+
* - Parallel dispatch and a per-child timeout override are holds.
|
|
342
|
+
* - Any `GIT_*` variable outside the allowlist (`gitEnvironmentOverride`) is
|
|
343
|
+
* a hold only when a Git-reading hook will
|
|
344
|
+
* run; Git configuration files and linked-worktree metadata only when
|
|
345
|
+
* observe-patterns will run. A hook runs when it is listed (or there is no
|
|
346
|
+
* registry) and neither the hook profile nor `HQ_DISABLED_HOOKS` turns it
|
|
347
|
+
* off (`enabledBy`, as `stop::enabled_by`).
|
|
348
|
+
* - A ported script on disk that is not a released version is a hold
|
|
349
|
+
* (`unreleasedScript`, as `stop::compat::released_scripts`).
|
|
350
|
+
* - A Git config file is read as bytes; one that is not valid UTF-8 is a hold
|
|
351
|
+
* ("unmodeled Git config encoding"), and lines are trimmed, split and
|
|
352
|
+
* lowercased with Rust's `str::trim`, `char::is_whitespace` and ASCII-only
|
|
353
|
+
* lowercasing, so both sides read the same keys (`scanGitConfig`). Both
|
|
354
|
+
* sides run the shared cases in
|
|
355
|
+
* crates/hq-hook/tests/fixtures/stop-git-config/cases.json.
|
|
356
|
+
*/
|
|
357
|
+
export function stopCompatibilityHold(root, env = process.env, fixed = (p) => p) {
|
|
358
|
+
const set = (key) => Boolean(env[key]);
|
|
359
|
+
if (env.HQ_HOOK_PARALLEL === '1' && (!env.HQ_HOOK_PARALLEL_EVENTS || env.HQ_HOOK_PARALLEL_EVENTS.split(/[ ,]+/).includes('Stop')))
|
|
360
|
+
return 'parallel Stop dispatch';
|
|
361
|
+
if (stopCompatibility.dispatcher.some(set))
|
|
362
|
+
return 'dispatcher override';
|
|
363
|
+
const canonical = Object.keys(stopCompatibility.registry);
|
|
364
|
+
let listed = null;
|
|
365
|
+
const registryPath = path.join(root, '.claude/hooks/hook-registry.json');
|
|
366
|
+
if (fs.existsSync(registryPath)) {
|
|
367
|
+
try {
|
|
368
|
+
const registry = JSON.parse(fs.readFileSync(registryPath, 'utf8'));
|
|
369
|
+
listed = [];
|
|
370
|
+
let last = -1;
|
|
371
|
+
for (const group of registry.hooks?.Stop ?? []) {
|
|
372
|
+
if (group.matcher && group.matcher !== '*')
|
|
373
|
+
return 'custom Stop matcher';
|
|
374
|
+
for (const hook of group.hooks ?? []) {
|
|
375
|
+
listed.push(hook.id);
|
|
376
|
+
const position = canonical.indexOf(hook.id);
|
|
377
|
+
// No native port: `covers` decides, and an uncovered hook keeps Stop on the old dispatcher.
|
|
378
|
+
if (position < 0)
|
|
379
|
+
continue;
|
|
380
|
+
if (position <= last)
|
|
381
|
+
return 'noncanonical Stop hook order';
|
|
382
|
+
last = position;
|
|
383
|
+
const timeout = stopCompatibility.registry[hook.id];
|
|
384
|
+
// A longer kill timer than the canonical one is never reached (each port bounds its own
|
|
385
|
+
// work within it). The deliver hook without its `Stop` argument exits at once, and the
|
|
386
|
+
// native Stop does not run it either.
|
|
387
|
+
const args = JSON.stringify(hook.args ?? []);
|
|
388
|
+
if (hook.script !== `.claude/hooks/${hook.id}.sh` || (hook.timeout ?? 30) < timeout || hook.gated === false
|
|
389
|
+
|| hook.prefilter !== undefined || hook.after?.length || (hook.runner && hook.runner !== 'exec')
|
|
390
|
+
|| (args !== '[]' && !(hook.id === 'conduct-lanes-link-deliver' && args === '["Stop"]')))
|
|
391
|
+
return 'custom Stop registry execution';
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
if (Object.keys(registry.directory_hook_after?.Stop ?? {}).length)
|
|
395
|
+
return 'custom Stop folder ordering';
|
|
396
|
+
}
|
|
397
|
+
catch {
|
|
398
|
+
return 'unreadable Stop registry';
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
// Only a Git-reading hook that will run counts: listed (or no registry)
|
|
402
|
+
// and not turned off by the hook profile or HQ_DISABLED_HOOKS.
|
|
403
|
+
const runs = (id) => (listed === null || listed.includes(id)) && enabledBy(env, id);
|
|
404
|
+
const script = unreleasedScript(root, (id) => listed !== null && runs(id));
|
|
405
|
+
if (script)
|
|
406
|
+
return script;
|
|
407
|
+
// Any GIT_* name outside the allowlist counts when present, even empty: git dies on an empty GIT_DIR,
|
|
408
|
+
// GIT_WORK_TREE, GIT_COMMON_DIR or GIT_OBJECT_DIRECTORY, where the native readers ignore the
|
|
409
|
+
// environment (#2790 r4 C1), and on many others (review r17 C3, C5).
|
|
410
|
+
if (GIT_ENV_READERS.some(runs) && gitEnvironmentOverride(env) !== null)
|
|
411
|
+
return 'Git environment override';
|
|
412
|
+
if (!runs('observe-patterns'))
|
|
413
|
+
return null;
|
|
414
|
+
return gitFilesHold(root, env, stopCompatibility.gitDeveloperDirLink, fixed);
|
|
415
|
+
}
|
|
416
|
+
const LINKED = 'linked Git worktree metadata';
|
|
417
|
+
const UNREADABLE_METADATA = 'unreadable Git metadata';
|
|
418
|
+
const OUTSIDE = 'a Git repository outside the HQ root';
|
|
419
|
+
const UNDETERMINED = 'undetermined Git system configuration';
|
|
420
|
+
const REWRITE = 'unmodeled Git history rewrite';
|
|
421
|
+
/** A file or folder that is not there, as git's own probes treat it (Rust's `absent`). */
|
|
422
|
+
const absent = (error) => ['ENOENT', 'ENOTDIR'].includes(error.code ?? '');
|
|
423
|
+
const OWNERSHIP = 'a Git repository the current user does not own';
|
|
424
|
+
/**
|
|
425
|
+
* The Git configuration observe-patterns' in-process reader does not apply,
|
|
426
|
+
* as `stop::compat::git_files_with`, with Xcode's developer-directory link
|
|
427
|
+
* at `link`. Exported for tests.
|
|
428
|
+
*/
|
|
429
|
+
export function gitFilesHold(root, env, link, fixed = (p) => p) {
|
|
430
|
+
// Reviews 2712 r22 C1 and r23 C1: git and observe-patterns' gix walk up from `root` to the
|
|
431
|
+
// repository git finds (gitRepository); its config is checked like a root's own. A linked
|
|
432
|
+
// worktree reads the shared `$GIT_COMMON_DIR/config` and its own `$GIT_DIR/config.worktree`.
|
|
433
|
+
const repository = gitRepository(root, env);
|
|
434
|
+
if (typeof repository === 'string')
|
|
435
|
+
return repository;
|
|
436
|
+
if (!repository)
|
|
437
|
+
return null;
|
|
438
|
+
const system = gitSystemConfigs(root, env, link);
|
|
439
|
+
if (typeof system === 'string')
|
|
440
|
+
return system;
|
|
441
|
+
const files = [...system, path.join(repository.common, 'config'), path.join(repository.git, 'config.worktree')];
|
|
442
|
+
// Relative against `root` (observe-patterns runs `cd "$HQ"`); git writes `~/` as `$HOME/`, so an
|
|
443
|
+
// empty HOME reads /.gitconfig; an empty XDG_CONFIG_HOME is ignored.
|
|
444
|
+
if (env.HOME !== undefined)
|
|
445
|
+
files.push(joinRaw(root, `${env.HOME}/.gitconfig`), joinRaw(root, `${env.HOME}/.config/git/config`));
|
|
446
|
+
if (env.XDG_CONFIG_HOME)
|
|
447
|
+
files.push(joinRaw(root, `${env.XDG_CONFIG_HOME}/git/config`));
|
|
448
|
+
// The installation of the git observe-patterns runs comes last, so a config file above is named first.
|
|
449
|
+
const installation = gitInstallationConfigs(root, env, fixed);
|
|
450
|
+
if (typeof installation !== 'string')
|
|
451
|
+
files.push(...installation);
|
|
452
|
+
// `~/` in an include, and a path value's `~/`, are HOME's; a relative HOME is the root's, as
|
|
453
|
+
// above. An empty HOME expands nothing here.
|
|
454
|
+
const home = env.HOME ? joinRaw(root, env.HOME) : undefined;
|
|
455
|
+
for (const file of new Set(files)) {
|
|
456
|
+
const hold = gitConfigFileHold(file, home, 0);
|
|
457
|
+
if (hold)
|
|
458
|
+
return hold;
|
|
459
|
+
}
|
|
460
|
+
return typeof installation === 'string' ? installation : null;
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* As `stop::compat::git_repository`: the repository git run in `root` takes
|
|
464
|
+
* its configuration from, null when it finds none, or the hold. `<root>/.git`
|
|
465
|
+
* as a directory git accepts is the repository; a `.git` symlink, or a `.git`
|
|
466
|
+
* directory holding `commondir`, is linked-worktree metadata; a `.git` file is
|
|
467
|
+
* a gitfile (`gitfile`). When `.git` is missing, the walk up finds the
|
|
468
|
+
* repository (`repositoryAbove`, review r23 C1). When `.git` is a directory
|
|
469
|
+
* git does not accept, any repository above holds and its own config is
|
|
470
|
+
* still scanned.
|
|
471
|
+
*/
|
|
472
|
+
function gitRepository(root, env) {
|
|
473
|
+
const git = path.join(root, '.git');
|
|
474
|
+
let stat;
|
|
475
|
+
try {
|
|
476
|
+
stat = fs.lstatSync(git);
|
|
477
|
+
}
|
|
478
|
+
catch (error) {
|
|
479
|
+
if (error.code !== 'ENOENT')
|
|
480
|
+
return UNREADABLE_METADATA;
|
|
481
|
+
return repositoryAbove(root, env);
|
|
482
|
+
}
|
|
483
|
+
if (stat.isFile())
|
|
484
|
+
return gitfile(root, git, env);
|
|
485
|
+
if (!stat.isDirectory())
|
|
486
|
+
return LINKED;
|
|
487
|
+
try {
|
|
488
|
+
fs.lstatSync(path.join(git, 'commondir'));
|
|
489
|
+
return LINKED;
|
|
490
|
+
}
|
|
491
|
+
catch (error) {
|
|
492
|
+
if (error.code !== 'ENOENT')
|
|
493
|
+
return LINKED;
|
|
494
|
+
}
|
|
495
|
+
if (!gitDirectory(git)) {
|
|
496
|
+
const hold = outsideRepository(root);
|
|
497
|
+
if (hold)
|
|
498
|
+
return hold;
|
|
499
|
+
}
|
|
500
|
+
else if (process.platform !== 'win32') {
|
|
501
|
+
// Review r24 C1: git dies on a repository the user does not own, where gix opens it. Unix only,
|
|
502
|
+
// as Rust: elsewhere the root's own repository is not checked.
|
|
503
|
+
const hold = owned([root, git], env);
|
|
504
|
+
if (hold)
|
|
505
|
+
return hold;
|
|
506
|
+
}
|
|
507
|
+
return historyRewrite(git) ?? { git, common: git };
|
|
508
|
+
}
|
|
509
|
+
/**
|
|
510
|
+
* As `stop::compat::repository_above`: the repository git finds when
|
|
511
|
+
* `<root>/.git` is missing, on the path as given and as resolved, which must
|
|
512
|
+
* agree. A bare-looking `root` or folder on the way up, a `.git` past a
|
|
513
|
+
* file-system boundary, and a `.git` `repositoryAt` does not model hold.
|
|
514
|
+
*/
|
|
515
|
+
function repositoryAbove(root, env) {
|
|
516
|
+
let real;
|
|
517
|
+
// The physical path, as Rust's canonicalize: the JS realpathSync resolves `..` as text first.
|
|
518
|
+
try {
|
|
519
|
+
real = fs.realpathSync.native(root);
|
|
520
|
+
}
|
|
521
|
+
catch {
|
|
522
|
+
return UNREADABLE_METADATA;
|
|
523
|
+
}
|
|
524
|
+
// Rust models git's file-system boundary on Unix only; elsewhere every folder is one device.
|
|
525
|
+
const device = (dir) => { try {
|
|
526
|
+
const { dev } = fs.statSync(dir);
|
|
527
|
+
return process.platform === 'win32' ? 0 : dev;
|
|
528
|
+
}
|
|
529
|
+
catch {
|
|
530
|
+
return null;
|
|
531
|
+
} };
|
|
532
|
+
const found = [];
|
|
533
|
+
for (const start of [root, real]) {
|
|
534
|
+
const bare = bareRepository(start);
|
|
535
|
+
if (bare !== false)
|
|
536
|
+
return bare === true ? OUTSIDE : bare;
|
|
537
|
+
const first = device(start);
|
|
538
|
+
if (first === null)
|
|
539
|
+
return UNREADABLE_METADATA;
|
|
540
|
+
let crossed = false;
|
|
541
|
+
let repository = null;
|
|
542
|
+
for (let dir = path.dirname(start), last = start; dir !== last; last = dir, dir = path.dirname(dir)) {
|
|
543
|
+
const dev = device(dir);
|
|
544
|
+
if (dev === null)
|
|
545
|
+
return UNREADABLE_METADATA;
|
|
546
|
+
crossed ||= dev !== first;
|
|
547
|
+
let exists = true;
|
|
548
|
+
try {
|
|
549
|
+
fs.lstatSync(path.join(dir, '.git'));
|
|
550
|
+
}
|
|
551
|
+
catch (error) {
|
|
552
|
+
if (!absent(error))
|
|
553
|
+
return UNREADABLE_METADATA;
|
|
554
|
+
exists = false;
|
|
555
|
+
}
|
|
556
|
+
if (exists) {
|
|
557
|
+
if (crossed)
|
|
558
|
+
return OUTSIDE;
|
|
559
|
+
const at = repositoryAt(dir, env);
|
|
560
|
+
if (typeof at === 'string')
|
|
561
|
+
return at;
|
|
562
|
+
repository = at;
|
|
563
|
+
break;
|
|
564
|
+
}
|
|
565
|
+
const above = bareRepository(dir);
|
|
566
|
+
if (above !== false)
|
|
567
|
+
return above === true ? OUTSIDE : above;
|
|
568
|
+
}
|
|
569
|
+
found.push(repository);
|
|
570
|
+
}
|
|
571
|
+
const [given, physical] = found;
|
|
572
|
+
if (!given && !physical)
|
|
573
|
+
return null;
|
|
574
|
+
const resolved = (file) => { try {
|
|
575
|
+
return fs.realpathSync.native(file);
|
|
576
|
+
}
|
|
577
|
+
catch {
|
|
578
|
+
return null;
|
|
579
|
+
} };
|
|
580
|
+
if (given && physical && resolved(given.git) === resolved(physical.git) && resolved(given.common) === resolved(physical.common))
|
|
581
|
+
return physical;
|
|
582
|
+
return OUTSIDE;
|
|
583
|
+
}
|
|
584
|
+
/** As `stop::compat::repository_at`: the repository whose `.git` is in `dir`, a parent of the root, or the hold. */
|
|
585
|
+
function repositoryAt(dir, env) {
|
|
586
|
+
const git = path.join(dir, '.git');
|
|
587
|
+
let stat;
|
|
588
|
+
try {
|
|
589
|
+
stat = fs.lstatSync(git);
|
|
590
|
+
}
|
|
591
|
+
catch {
|
|
592
|
+
return UNREADABLE_METADATA;
|
|
593
|
+
}
|
|
594
|
+
if (stat.isFile())
|
|
595
|
+
return gitfile(dir, git, env);
|
|
596
|
+
if (!stat.isDirectory() || !gitDirectory(git))
|
|
597
|
+
return OUTSIDE;
|
|
598
|
+
try {
|
|
599
|
+
fs.lstatSync(path.join(git, 'commondir'));
|
|
600
|
+
return OUTSIDE;
|
|
601
|
+
}
|
|
602
|
+
catch (error) {
|
|
603
|
+
if (!absent(error))
|
|
604
|
+
return OUTSIDE;
|
|
605
|
+
}
|
|
606
|
+
return owned([dir, git], env) ?? historyRewrite(git) ?? { git, common: git };
|
|
607
|
+
}
|
|
608
|
+
/**
|
|
609
|
+
* As `stop::compat::gitfile`: `gitdir: <path>` exactly, trailing line ends
|
|
610
|
+
* removed, relative to `dir`; the git directory it names has its own `HEAD`
|
|
611
|
+
* and holds `objects` and `refs` itself or in its `commondir`. Anything else
|
|
612
|
+
* is linked-worktree metadata.
|
|
613
|
+
*/
|
|
614
|
+
function gitfile(dir, file, env) {
|
|
615
|
+
const line = (bytes) => {
|
|
616
|
+
let text;
|
|
617
|
+
// ignoreBOM keeps a leading byte-order mark, as Rust's from_utf8 does (git then names no directory).
|
|
618
|
+
try {
|
|
619
|
+
text = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(bytes);
|
|
620
|
+
}
|
|
621
|
+
catch {
|
|
622
|
+
return null;
|
|
623
|
+
}
|
|
624
|
+
text = text.replace(/[\n\r]+$/, '');
|
|
625
|
+
return text && !/[\0\n\r]/.test(text) ? text : null;
|
|
626
|
+
};
|
|
627
|
+
let bytes;
|
|
628
|
+
try {
|
|
629
|
+
const stat = fs.statSync(file);
|
|
630
|
+
if (!stat.isFile() || stat.size > 4096)
|
|
631
|
+
return LINKED;
|
|
632
|
+
bytes = fs.readFileSync(file);
|
|
633
|
+
}
|
|
634
|
+
catch {
|
|
635
|
+
return LINKED;
|
|
636
|
+
}
|
|
637
|
+
const prefix = Buffer.from('gitdir: ');
|
|
638
|
+
if (!bytes.subarray(0, prefix.length).equals(prefix))
|
|
639
|
+
return LINKED;
|
|
640
|
+
const target = line(bytes.subarray(prefix.length));
|
|
641
|
+
if (target === null)
|
|
642
|
+
return LINKED;
|
|
643
|
+
// As Rust's `Path::join` then `canonicalize`: an absolute path replaces, and `..` resolves physically, as git's realpath.
|
|
644
|
+
const real = (base, name) => { try {
|
|
645
|
+
return fs.realpathSync.native(path.isAbsolute(name) ? name : `${base}${path.sep}${name}`);
|
|
646
|
+
}
|
|
647
|
+
catch {
|
|
648
|
+
return null;
|
|
649
|
+
} };
|
|
650
|
+
const git = real(dir, target);
|
|
651
|
+
if (git === null)
|
|
652
|
+
return LINKED;
|
|
653
|
+
let common = git;
|
|
654
|
+
try {
|
|
655
|
+
fs.lstatSync(path.join(git, 'commondir'));
|
|
656
|
+
let raw;
|
|
657
|
+
try {
|
|
658
|
+
raw = fs.readFileSync(path.join(git, 'commondir'));
|
|
659
|
+
}
|
|
660
|
+
catch {
|
|
661
|
+
return LINKED;
|
|
662
|
+
}
|
|
663
|
+
const named = line(raw);
|
|
664
|
+
if (named === null)
|
|
665
|
+
return LINKED;
|
|
666
|
+
const shared = real(git, named);
|
|
667
|
+
if (shared === null)
|
|
668
|
+
return LINKED;
|
|
669
|
+
common = shared;
|
|
670
|
+
}
|
|
671
|
+
catch (error) {
|
|
672
|
+
if (!absent(error))
|
|
673
|
+
return LINKED;
|
|
674
|
+
}
|
|
675
|
+
if (!gitDirectory(git, common))
|
|
676
|
+
return LINKED;
|
|
677
|
+
return owned([dir, file, git, common], env) ?? historyRewrite(common)
|
|
678
|
+
?? (common !== git ? historyRewrite(git) : null) ?? { git, common };
|
|
679
|
+
}
|
|
680
|
+
/**
|
|
681
|
+
* As `stop::compat::owned`: git's `ensure_valid_ownership`, each path owned by
|
|
682
|
+
* the effective user (root under sudo is not modeled). Where there is no
|
|
683
|
+
* effective uid (Windows), the hold.
|
|
684
|
+
*/
|
|
685
|
+
function owned(paths, env) {
|
|
686
|
+
const euid = process.geteuid?.();
|
|
687
|
+
if (euid === undefined)
|
|
688
|
+
return OWNERSHIP;
|
|
689
|
+
if (euid === 0 && env.SUDO_UID !== undefined)
|
|
690
|
+
return OWNERSHIP;
|
|
691
|
+
for (const file of paths) {
|
|
692
|
+
try {
|
|
693
|
+
if (fs.lstatSync(file).uid !== euid)
|
|
694
|
+
return OWNERSHIP;
|
|
695
|
+
}
|
|
696
|
+
catch {
|
|
697
|
+
return UNREADABLE_METADATA;
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
return null;
|
|
701
|
+
}
|
|
702
|
+
/**
|
|
703
|
+
* As `stop::compat::history_rewrite`: the hold for repository state that
|
|
704
|
+
* changes what `git log` walks. A replace ref, loose (any entry under
|
|
705
|
+
* `refs/replace`) or packed (a `packed-refs` line naming a `refs/replace/`
|
|
706
|
+
* ref), and `info/grafts`: git applies them, observe-patterns' gix 0.89
|
|
707
|
+
* inverts `core.useReplaceRefs` and ignores grafts.
|
|
708
|
+
*/
|
|
709
|
+
function historyRewrite(git) {
|
|
710
|
+
const replace = path.join(git, 'refs/replace');
|
|
711
|
+
let stat = null;
|
|
712
|
+
try {
|
|
713
|
+
stat = fs.lstatSync(replace);
|
|
714
|
+
}
|
|
715
|
+
catch (error) {
|
|
716
|
+
if (!absent(error))
|
|
717
|
+
return UNREADABLE_METADATA;
|
|
718
|
+
}
|
|
719
|
+
if (stat) {
|
|
720
|
+
if (!stat.isDirectory())
|
|
721
|
+
return REWRITE;
|
|
722
|
+
try {
|
|
723
|
+
if (fs.readdirSync(replace).length > 0)
|
|
724
|
+
return REWRITE;
|
|
725
|
+
}
|
|
726
|
+
catch {
|
|
727
|
+
return UNREADABLE_METADATA;
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
let packed = null;
|
|
731
|
+
try {
|
|
732
|
+
packed = fs.readFileSync(path.join(git, 'packed-refs'));
|
|
733
|
+
}
|
|
734
|
+
catch (error) {
|
|
735
|
+
if (!absent(error))
|
|
736
|
+
return UNREADABLE_METADATA;
|
|
737
|
+
}
|
|
738
|
+
if (packed) {
|
|
739
|
+
for (const line of packed.toString('latin1').split('\n')) {
|
|
740
|
+
const space = line.indexOf(' ');
|
|
741
|
+
if (space !== -1 && line.slice(space + 1).startsWith('refs/replace/'))
|
|
742
|
+
return REWRITE;
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
try {
|
|
746
|
+
fs.lstatSync(path.join(git, 'info/grafts'));
|
|
747
|
+
return REWRITE;
|
|
748
|
+
}
|
|
749
|
+
catch (error) {
|
|
750
|
+
return absent(error) ? null : UNREADABLE_METADATA;
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
/**
|
|
754
|
+
* As `stop::compat::git_directory` (and `git_directory_in`): `objects` and `refs` in `common`, and a
|
|
755
|
+
* regular `HEAD` that is `ref: refs/...` or an object id.
|
|
756
|
+
*/
|
|
757
|
+
function gitDirectory(git, common = git) {
|
|
758
|
+
const dir = (name) => { try {
|
|
759
|
+
return fs.statSync(path.join(common, name)).isDirectory();
|
|
760
|
+
}
|
|
761
|
+
catch {
|
|
762
|
+
return false;
|
|
763
|
+
} };
|
|
764
|
+
if (!dir('objects') || !dir('refs'))
|
|
765
|
+
return false;
|
|
766
|
+
let head;
|
|
767
|
+
try {
|
|
768
|
+
const stat = fs.lstatSync(path.join(git, 'HEAD'));
|
|
769
|
+
if (!stat.isFile() || stat.size > 4096)
|
|
770
|
+
return false;
|
|
771
|
+
head = fs.readFileSync(path.join(git, 'HEAD'));
|
|
772
|
+
}
|
|
773
|
+
catch {
|
|
774
|
+
return false;
|
|
775
|
+
}
|
|
776
|
+
const text = head.toString('latin1');
|
|
777
|
+
if (text.startsWith('ref:'))
|
|
778
|
+
return text.slice(4).replace(/^[\t\n\f\r ]+/, '').startsWith('refs/');
|
|
779
|
+
return /^[0-9a-fA-F]{40}/.test(text);
|
|
780
|
+
}
|
|
781
|
+
/** As `stop::compat::outside_repository`: the hold for a repository git would find for `root` other than `<root>/.git`. */
|
|
782
|
+
function outsideRepository(root) {
|
|
783
|
+
let real;
|
|
784
|
+
// The physical path, as Rust's canonicalize: the JS realpathSync resolves `..` as text first.
|
|
785
|
+
try {
|
|
786
|
+
real = fs.realpathSync.native(root);
|
|
787
|
+
}
|
|
788
|
+
catch {
|
|
789
|
+
return UNREADABLE_METADATA;
|
|
790
|
+
}
|
|
791
|
+
for (const start of [root, real]) {
|
|
792
|
+
const bare = bareRepository(start);
|
|
793
|
+
if (bare !== false)
|
|
794
|
+
return bare === true ? OUTSIDE : bare;
|
|
795
|
+
for (let dir = path.dirname(start), last = start; dir !== last; last = dir, dir = path.dirname(dir)) {
|
|
796
|
+
try {
|
|
797
|
+
fs.lstatSync(path.join(dir, '.git'));
|
|
798
|
+
return OUTSIDE;
|
|
799
|
+
}
|
|
800
|
+
catch (error) {
|
|
801
|
+
if (!absent(error))
|
|
802
|
+
return UNREADABLE_METADATA;
|
|
803
|
+
}
|
|
804
|
+
const above = bareRepository(dir);
|
|
805
|
+
if (above !== false)
|
|
806
|
+
return above === true ? OUTSIDE : above;
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
return null;
|
|
810
|
+
}
|
|
811
|
+
/** As `stop::compat::bare_repository`: whether `dir` has `HEAD`, `objects` and `refs`, or the hold. */
|
|
812
|
+
function bareRepository(dir) {
|
|
813
|
+
for (const name of ['HEAD', 'objects', 'refs']) {
|
|
814
|
+
try {
|
|
815
|
+
fs.lstatSync(path.join(dir, name));
|
|
816
|
+
}
|
|
817
|
+
catch (error) {
|
|
818
|
+
return absent(error) ? false : UNREADABLE_METADATA;
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
return true;
|
|
822
|
+
}
|
|
823
|
+
/**
|
|
824
|
+
* `rest` under `base` as Rust's `Path::join` and the kernel read it: an
|
|
825
|
+
* absolute `rest` stands alone, and nothing is normalized (`a/link/..` is
|
|
826
|
+
* the parent of the link's target). Windows joins with `path.join`.
|
|
827
|
+
*/
|
|
828
|
+
export function joinRaw(base, rest, platform = process.platform) {
|
|
829
|
+
if (platform === 'win32') {
|
|
830
|
+
const root = path.win32.parse(rest).root;
|
|
831
|
+
// A drive or UNC prefix replaces the base, including drive-relative
|
|
832
|
+
// paths. A root without a prefix keeps the base's drive, as Rust does.
|
|
833
|
+
if (root.length > 1)
|
|
834
|
+
return rest;
|
|
835
|
+
return path.win32.join(root ? path.win32.parse(base).root : base, rest);
|
|
836
|
+
}
|
|
837
|
+
if (rest.startsWith('/'))
|
|
838
|
+
return rest;
|
|
839
|
+
return base === '' || base.endsWith('/') ? `${base}${rest}` : `${base}/${rest}`;
|
|
840
|
+
}
|
|
841
|
+
/**
|
|
842
|
+
* As `stop::compat::git_system_configs`: the system-level config files git
|
|
843
|
+
* may read, from the shared policy (fixed paths, paths under the folders
|
|
844
|
+
* `DEVELOPER_DIR` and `PROGRAMDATA` name, a relative one against `root`,
|
|
845
|
+
* and under the developer folder `xcode-select` chose), or the hold when the
|
|
846
|
+
* link cannot be read.
|
|
847
|
+
*/
|
|
848
|
+
function gitSystemConfigs(root, env, link) {
|
|
849
|
+
const paths = [...stopCompatibility.gitSystemConfigs];
|
|
850
|
+
const byVariable = stopCompatibility.gitEnvironmentConfigs;
|
|
851
|
+
for (const [name, suffixes] of Object.entries(byVariable)) {
|
|
852
|
+
const value = env[name];
|
|
853
|
+
if (value)
|
|
854
|
+
paths.push(...suffixes.map((suffix) => joinRaw(joinRaw(root, value), suffix)));
|
|
855
|
+
}
|
|
856
|
+
if (link) {
|
|
857
|
+
try {
|
|
858
|
+
const dir = path.resolve(path.dirname(link), fs.readlinkSync(link));
|
|
859
|
+
paths.push(...byVariable.DEVELOPER_DIR.map((suffix) => path.join(dir, suffix)));
|
|
860
|
+
}
|
|
861
|
+
catch (error) {
|
|
862
|
+
if (!absent(error))
|
|
863
|
+
return UNDETERMINED;
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
return paths;
|
|
867
|
+
}
|
|
868
|
+
/**
|
|
869
|
+
* As `stop::compat::git_installation_configs`: the system config files under
|
|
870
|
+
* the installation of each git observe-patterns may run, or the hold when a
|
|
871
|
+
* PATH entry cannot be read or a git's installation cannot be told.
|
|
872
|
+
*/
|
|
873
|
+
function gitInstallationConfigs(root, env, fixed) {
|
|
874
|
+
const gits = observeGits(root, env, fixed);
|
|
875
|
+
if (typeof gits === 'string')
|
|
876
|
+
return gits;
|
|
877
|
+
const paths = [];
|
|
878
|
+
for (const git of gits) {
|
|
879
|
+
const bases = gitPrefixes(git);
|
|
880
|
+
if (typeof bases === 'string')
|
|
881
|
+
return bases;
|
|
882
|
+
for (const base of bases)
|
|
883
|
+
paths.push(...stopCompatibility.gitPrefixConfigs.map((config) => joinRaw(base, config)));
|
|
884
|
+
}
|
|
885
|
+
return paths;
|
|
886
|
+
}
|
|
887
|
+
/** As `stop::compat::command_names`: `command`, and on Windows (MSYS bash) `command.exe` too. */
|
|
888
|
+
const commandNames = (command) => process.platform === 'win32' ? [command, `${command}.exe`] : [command];
|
|
889
|
+
/**
|
|
890
|
+
* As `stop::compat::observe_gits` (see its comment): each git
|
|
891
|
+
* observe-patterns may run. The first executable `git` bash finds on `PATH`
|
|
892
|
+
* from `root`, and, when node, hq or qmd is missing from `PATH` (searched
|
|
893
|
+
* from this process's working folder), the first git in each folder
|
|
894
|
+
* `hq_augment_path` could put in front. "Executable" is bash's own test
|
|
895
|
+
* (`bashRuns`: mode bits on macOS). An unset `PATH`, no git on it, or a git
|
|
896
|
+
* bash picks that the kernel will not run, is the hold.
|
|
897
|
+
*/
|
|
898
|
+
function observeGits(root, env, fixed) {
|
|
899
|
+
const search = env.PATH;
|
|
900
|
+
if (search === undefined)
|
|
901
|
+
return UNDETERMINED;
|
|
902
|
+
const home = env.HOME;
|
|
903
|
+
const names = commandNames('git');
|
|
904
|
+
const gits = [];
|
|
905
|
+
const dotted = [];
|
|
906
|
+
let all = true;
|
|
907
|
+
for (const tool of ['node', 'hq', 'qmd']) {
|
|
908
|
+
const found = bashSearch(commandNames(tool), search, home, process.cwd(), true);
|
|
909
|
+
if (typeof found === 'string')
|
|
910
|
+
return found;
|
|
911
|
+
if (found === null) {
|
|
912
|
+
all = false;
|
|
913
|
+
break;
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
if (!all) {
|
|
917
|
+
for (const folder of augmentCandidates(env, fixed)) {
|
|
918
|
+
// hq_augment_path tests the folder from master-hook's working folder; observe-patterns' bash searches it from `root`.
|
|
919
|
+
if (isDirectory(folder) && hasTool(folder)) {
|
|
920
|
+
const found = foundIn(joinRaw(root, folder), names);
|
|
921
|
+
if (typeof found === 'string')
|
|
922
|
+
return found;
|
|
923
|
+
if (found.kind === 'runs') {
|
|
924
|
+
gits.push(found.file);
|
|
925
|
+
if (dottedJoin(root, folder))
|
|
926
|
+
dotted.push(found.file);
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
const git = bashSearch(names, search, home, root, false);
|
|
932
|
+
if (typeof git === 'string')
|
|
933
|
+
return git;
|
|
934
|
+
if (git === null)
|
|
935
|
+
return UNDETERMINED;
|
|
936
|
+
gits.push(git.file);
|
|
937
|
+
// As Rust's git_prefixes: a git whose path as found has a `..` component (from PATH, HOME through `~`, a
|
|
938
|
+
// relative entry's working folder or an hq_augment_path folder) is the hold. Git takes its RUNTIME_PREFIX
|
|
939
|
+
// from that path with `..` removed as text, the kernel through the link.
|
|
940
|
+
if (dotted.length > 0 || git.dotted)
|
|
941
|
+
return UNDETERMINED;
|
|
942
|
+
// bash's search takes a git by its mode bits, then runs it: one the kernel will not run (an ACL that denies
|
|
943
|
+
// execute, a noexec mount) fails with 126, and observe-patterns reads no history at all (review 2790 r4 C2).
|
|
944
|
+
if (gits.some((file) => !canExecute(file)))
|
|
945
|
+
return UNDETERMINED;
|
|
946
|
+
return gits;
|
|
947
|
+
}
|
|
948
|
+
const isDirectory = (dir) => { try {
|
|
949
|
+
return fs.statSync(dir).isDirectory();
|
|
950
|
+
}
|
|
951
|
+
catch {
|
|
952
|
+
return false;
|
|
953
|
+
} };
|
|
954
|
+
/**
|
|
955
|
+
* As `stop::compat::bash_search`: the first executable file named one of
|
|
956
|
+
* `names` bash's command search finds on `search`, run from `cwd`; with
|
|
957
|
+
* `commandV`, when there is none, the first existing one unless it is a
|
|
958
|
+
* folder (`command -v`); or the hold. A leading `~` (`~/bin`, `~`) takes
|
|
959
|
+
* `HOME`; an empty or relative entry is searched from `cwd`; `~user`, and
|
|
960
|
+
* `~` with `HOME` unset, are the hold.
|
|
961
|
+
*/
|
|
962
|
+
function bashSearch(names, search, home, cwd, commandV) {
|
|
963
|
+
let first = null;
|
|
964
|
+
for (let entry of search.split(process.platform === 'win32' ? ';' : ':')) {
|
|
965
|
+
if (entry.startsWith('~')) {
|
|
966
|
+
const rest = entry.slice(1);
|
|
967
|
+
if (rest !== '' && !rest.startsWith('/'))
|
|
968
|
+
return UNDETERMINED;
|
|
969
|
+
if (home === undefined)
|
|
970
|
+
return UNDETERMINED;
|
|
971
|
+
entry = `${home}${rest}`;
|
|
972
|
+
}
|
|
973
|
+
const found = foundIn(joinRaw(cwd, entry), names);
|
|
974
|
+
if (typeof found === 'string')
|
|
975
|
+
return found;
|
|
976
|
+
if (found.kind === 'runs')
|
|
977
|
+
return { file: found.file, dotted: dottedJoin(cwd, entry) };
|
|
978
|
+
if (found.kind !== 'nothing')
|
|
979
|
+
first ??= found;
|
|
980
|
+
}
|
|
981
|
+
return commandV && first?.kind === 'cannot' ? { file: first.file, dotted: false } : null;
|
|
982
|
+
}
|
|
983
|
+
/**
|
|
984
|
+
* Whether `rest` joined under `base` as Rust's `Path::join` joins it (an
|
|
985
|
+
* absolute `rest` stands alone) has a `..` component. Checked on the parts,
|
|
986
|
+
* since `joinRaw` normalizes on Windows.
|
|
987
|
+
*/
|
|
988
|
+
function dottedJoin(base, rest) {
|
|
989
|
+
const win32 = process.platform === 'win32';
|
|
990
|
+
const parts = (p) => p.split(win32 ? /[\\/]/ : '/');
|
|
991
|
+
// As joinRaw: a root (with or without a drive or UNC prefix) drops the base's folders.
|
|
992
|
+
const keepsBase = win32 ? path.win32.parse(rest).root === '' : !rest.startsWith('/');
|
|
993
|
+
return parts(rest).includes('..') || (keepsBase && parts(base).includes('..'));
|
|
994
|
+
}
|
|
995
|
+
/**
|
|
996
|
+
* As `stop::compat::found_in`: the first of `names` in `folder` that is there, or the hold. A file that is not a
|
|
997
|
+
* regular one but that bash takes as one it can run (a FIFO with an x bit) is the hold too.
|
|
998
|
+
*/
|
|
999
|
+
function foundIn(folder, names) {
|
|
1000
|
+
for (const name of names) {
|
|
1001
|
+
const file = joinRaw(folder, name);
|
|
1002
|
+
let stat;
|
|
1003
|
+
try {
|
|
1004
|
+
stat = fs.statSync(file);
|
|
1005
|
+
}
|
|
1006
|
+
catch (error) {
|
|
1007
|
+
if (absent(error))
|
|
1008
|
+
continue;
|
|
1009
|
+
return UNDETERMINED;
|
|
1010
|
+
}
|
|
1011
|
+
if (stat.isDirectory())
|
|
1012
|
+
return { kind: 'folder' };
|
|
1013
|
+
if (bashRuns(file, stat))
|
|
1014
|
+
return stat.isFile() ? { kind: 'runs', file } : UNDETERMINED;
|
|
1015
|
+
return { kind: 'cannot', file };
|
|
1016
|
+
}
|
|
1017
|
+
return { kind: 'nothing' };
|
|
1018
|
+
}
|
|
1019
|
+
/**
|
|
1020
|
+
* As `stop::compat::bash_runs`: whether bash's command search takes `file` as one it can run. On macOS bash is
|
|
1021
|
+
* built without `eaccess` and reads the mode bits (`stop::bash_executable`), so an ACL that allows or denies
|
|
1022
|
+
* execute does not change the answer (review 2790 r4 C2); other Unix bash uses `eaccess(X_OK)` (here `access`,
|
|
1023
|
+
* the same for a process that is not setuid); on Windows (MSYS bash) any regular file.
|
|
1024
|
+
*/
|
|
1025
|
+
function bashRuns(file, stat) {
|
|
1026
|
+
if (process.platform === 'win32')
|
|
1027
|
+
return stat.isFile();
|
|
1028
|
+
if (process.platform === 'darwin') {
|
|
1029
|
+
const euid = process.geteuid();
|
|
1030
|
+
const owner = stat.uid === euid;
|
|
1031
|
+
const member = !owner && (stat.gid === process.getgid() || stat.gid === process.getegid() || process.getgroups().includes(stat.gid));
|
|
1032
|
+
return modeBitsExecutable(stat.mode, euid === 0, owner, member);
|
|
1033
|
+
}
|
|
1034
|
+
try {
|
|
1035
|
+
fs.accessSync(file, fs.constants.X_OK);
|
|
1036
|
+
return true;
|
|
1037
|
+
}
|
|
1038
|
+
catch {
|
|
1039
|
+
return false;
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
/**
|
|
1043
|
+
* As Rust `stop::mode_bits_executable`: macOS bash's mode-bit rule. Any x bit for `root`; else the owner bit for
|
|
1044
|
+
* the `owner`; else the group bit for a `member` of the file's group; else the other bit. Setuid, setgid and
|
|
1045
|
+
* sticky are not x bits. Both sides check it on the `modeBits` table in the shared cases.json.
|
|
1046
|
+
*/
|
|
1047
|
+
export function modeBitsExecutable(mode, root, owner, member) {
|
|
1048
|
+
if (root)
|
|
1049
|
+
return (mode & 0o111) !== 0;
|
|
1050
|
+
if (owner)
|
|
1051
|
+
return (mode & 0o100) !== 0;
|
|
1052
|
+
if (member)
|
|
1053
|
+
return (mode & 0o010) !== 0;
|
|
1054
|
+
return (mode & 0o001) !== 0;
|
|
1055
|
+
}
|
|
1056
|
+
/**
|
|
1057
|
+
* As `stop::can_execute`: whether execve of `file` gets past the permission check. Not a directory, an x bit
|
|
1058
|
+
* (the kernel runs no file without one, whatever an ACL says) and `access(X_OK)` (which sees ACLs and noexec
|
|
1059
|
+
* mounts). On Windows a regular file.
|
|
1060
|
+
*/
|
|
1061
|
+
function canExecute(file) {
|
|
1062
|
+
let stat;
|
|
1063
|
+
try {
|
|
1064
|
+
stat = fs.statSync(file);
|
|
1065
|
+
}
|
|
1066
|
+
catch {
|
|
1067
|
+
return false;
|
|
1068
|
+
}
|
|
1069
|
+
if (process.platform === 'win32')
|
|
1070
|
+
return stat.isFile();
|
|
1071
|
+
if (stat.isDirectory() || (stat.mode & 0o111) === 0)
|
|
1072
|
+
return false;
|
|
1073
|
+
try {
|
|
1074
|
+
fs.accessSync(file, fs.constants.X_OK);
|
|
1075
|
+
return true;
|
|
1076
|
+
}
|
|
1077
|
+
catch {
|
|
1078
|
+
return false;
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
/**
|
|
1082
|
+
* As `stop::compat::git_prefixes`: the installation folders under which the
|
|
1083
|
+
* git at `git` may read its system config, or the hold. On macOS
|
|
1084
|
+
* `/usr/bin/git` (`gitAppleShim`) is the xcrun shim, modeled through the
|
|
1085
|
+
* developer folder. Otherwise the resolved file must be named `git`
|
|
1086
|
+
* (`git.exe`) and be a native program (`nativeBinary`); the folders are the
|
|
1087
|
+
* parent and grandparent of its folder, as found and as resolved, and
|
|
1088
|
+
* Homebrew's prefix for `<prefix>/Cellar/<formula>/<version>/bin/git`.
|
|
1089
|
+
*/
|
|
1090
|
+
function gitPrefixes(git) {
|
|
1091
|
+
let real;
|
|
1092
|
+
// The physical path, as Rust's canonicalize (the stored name's case on a case-insensitive volume too).
|
|
1093
|
+
try {
|
|
1094
|
+
real = fs.realpathSync.native(git);
|
|
1095
|
+
}
|
|
1096
|
+
catch {
|
|
1097
|
+
return UNDETERMINED;
|
|
1098
|
+
}
|
|
1099
|
+
if (process.platform === 'darwin' && real === stopCompatibility.gitAppleShim)
|
|
1100
|
+
return [];
|
|
1101
|
+
if (!commandNames('git').includes(path.basename(real)) || !nativeBinary(real))
|
|
1102
|
+
return UNDETERMINED;
|
|
1103
|
+
const bases = [];
|
|
1104
|
+
for (const bin of [path.dirname(git), path.dirname(real)]) {
|
|
1105
|
+
const prefix = path.dirname(bin);
|
|
1106
|
+
bases.push(prefix, path.dirname(prefix));
|
|
1107
|
+
}
|
|
1108
|
+
const cellar = path.dirname(path.dirname(path.dirname(path.dirname(real))));
|
|
1109
|
+
if (path.basename(cellar) === 'Cellar' && path.dirname(cellar) !== cellar)
|
|
1110
|
+
bases.push(path.dirname(cellar));
|
|
1111
|
+
return bases;
|
|
1112
|
+
}
|
|
1113
|
+
/** As `stop::compat::native_binary`: Mach-O (thin or universal) on macOS, ELF on other Unix, PE on Windows. */
|
|
1114
|
+
function nativeBinary(file) {
|
|
1115
|
+
const head = Buffer.alloc(4);
|
|
1116
|
+
let fd;
|
|
1117
|
+
try {
|
|
1118
|
+
fd = fs.openSync(file, 'r');
|
|
1119
|
+
if (fs.readSync(fd, head, 0, 4, 0) !== 4)
|
|
1120
|
+
return false;
|
|
1121
|
+
}
|
|
1122
|
+
catch {
|
|
1123
|
+
return false;
|
|
1124
|
+
}
|
|
1125
|
+
finally {
|
|
1126
|
+
if (fd !== undefined)
|
|
1127
|
+
fs.closeSync(fd);
|
|
1128
|
+
}
|
|
1129
|
+
const [a, b, c, d] = head;
|
|
1130
|
+
if (process.platform === 'win32')
|
|
1131
|
+
return a === 0x4d && b === 0x5a;
|
|
1132
|
+
if (process.platform === 'darwin') {
|
|
1133
|
+
return (a === 0xfe && b === 0xed && c === 0xfa && (d === 0xce || d === 0xcf))
|
|
1134
|
+
|| ((a === 0xce || a === 0xcf) && b === 0xfa && c === 0xed && d === 0xfe)
|
|
1135
|
+
|| (a === 0xca && b === 0xfe && c === 0xba && (d === 0xbe || d === 0xbf));
|
|
1136
|
+
}
|
|
1137
|
+
return a === 0x7f && b === 0x45 && c === 0x4c && d === 0x46;
|
|
1138
|
+
}
|
|
1139
|
+
/**
|
|
1140
|
+
* As `dispatch::gate::augment_candidates`: the folders `hq_augment_path`
|
|
1141
|
+
* considers, highest priority first; none without a home folder.
|
|
1142
|
+
*/
|
|
1143
|
+
function augmentCandidates(env, fixed) {
|
|
1144
|
+
const home = env.HOME || (process.platform === 'win32' ? env.USERPROFILE : undefined);
|
|
1145
|
+
if (!home)
|
|
1146
|
+
return [];
|
|
1147
|
+
const candidates = [];
|
|
1148
|
+
const toolchain = env.HQ_TOOLCHAIN_DIR || joinRaw(home, 'Library/Application Support/Indigo HQ/toolchain');
|
|
1149
|
+
candidates.push(joinRaw(toolchain, 'node/bin'), joinRaw(toolchain, 'npm-global/bin'));
|
|
1150
|
+
if (env.VOLTA_HOME)
|
|
1151
|
+
candidates.push(joinRaw(env.VOLTA_HOME, 'bin'));
|
|
1152
|
+
candidates.push(joinRaw(home, '.volta/bin'));
|
|
1153
|
+
if (env.ASDF_DATA_DIR)
|
|
1154
|
+
candidates.push(joinRaw(env.ASDF_DATA_DIR, 'shims'));
|
|
1155
|
+
candidates.push(joinRaw(home, '.asdf/shims'));
|
|
1156
|
+
const nvm = env.NVM_DIR || joinRaw(home, '.nvm');
|
|
1157
|
+
const nvmVersion = highestVersion(joinRaw(nvm, 'versions/node'));
|
|
1158
|
+
if (nvmVersion !== null)
|
|
1159
|
+
candidates.push(joinRaw(joinRaw(joinRaw(nvm, 'versions/node'), nvmVersion), 'bin'));
|
|
1160
|
+
const fnm = env.FNM_DIR || joinRaw(home, '.fnm');
|
|
1161
|
+
const fnmVersion = highestVersion(joinRaw(fnm, 'node-versions'));
|
|
1162
|
+
if (fnmVersion !== null)
|
|
1163
|
+
candidates.push(joinRaw(joinRaw(joinRaw(fnm, 'node-versions'), fnmVersion), 'installation/bin'));
|
|
1164
|
+
candidates.push(joinRaw(home, '.npm-global/bin'), joinRaw(home, '.local/bin'));
|
|
1165
|
+
if (process.platform === 'win32') {
|
|
1166
|
+
candidates.push(fixed('C:/Program Files/nodejs'));
|
|
1167
|
+
if (env.APPDATA)
|
|
1168
|
+
candidates.push(joinRaw(env.APPDATA, 'npm'));
|
|
1169
|
+
if (env.USERPROFILE)
|
|
1170
|
+
candidates.push(joinRaw(env.USERPROFILE, 'scoop/shims'));
|
|
1171
|
+
candidates.push(fixed('C:/ProgramData/chocolatey/bin'), fixed('C:/ProgramData/scoop/shims'));
|
|
1172
|
+
}
|
|
1173
|
+
else {
|
|
1174
|
+
candidates.push(fixed('/opt/homebrew/bin'), fixed('/usr/local/bin'));
|
|
1175
|
+
}
|
|
1176
|
+
return candidates;
|
|
1177
|
+
}
|
|
1178
|
+
/** As `dispatch::gate::highest_version` (`hq_highest_node_version`): the entry with the highest vMAJOR.MINOR.PATCH. */
|
|
1179
|
+
function highestVersion(dir) {
|
|
1180
|
+
let names;
|
|
1181
|
+
try {
|
|
1182
|
+
names = fs.readdirSync(dir).filter((name) => !name.startsWith('.')).sort();
|
|
1183
|
+
}
|
|
1184
|
+
catch {
|
|
1185
|
+
return null;
|
|
1186
|
+
}
|
|
1187
|
+
const number = (text) => { const digits = /^[0-9]*/.exec(text)[0]; return digits ? Number(digits) : 0; };
|
|
1188
|
+
let best = null;
|
|
1189
|
+
for (const name of names) {
|
|
1190
|
+
const parts = name.split('.');
|
|
1191
|
+
const key = [number((parts[0] ?? '').replace(/^[^0-9]*/, '')), number(parts[1] ?? ''), number(parts[2] ?? '')];
|
|
1192
|
+
const better = best === null || key[0] > best.key[0] || (key[0] === best.key[0] && (key[1] > best.key[1] || (key[1] === best.key[1] && key[2] > best.key[2])));
|
|
1193
|
+
if (better)
|
|
1194
|
+
best = { key, name };
|
|
1195
|
+
}
|
|
1196
|
+
return best?.name ?? null;
|
|
1197
|
+
}
|
|
1198
|
+
/** As `dispatch::gate::has_tool`: node, hq or qmd (or `.exe`) that `[ -x ]` accepts. */
|
|
1199
|
+
function hasTool(dir) {
|
|
1200
|
+
return ['node', 'hq', 'qmd', 'node.exe', 'hq.exe', 'qmd.exe'].some((name) => {
|
|
1201
|
+
const file = joinRaw(dir, name);
|
|
1202
|
+
if (process.platform === 'win32') {
|
|
1203
|
+
try {
|
|
1204
|
+
return fs.statSync(file).isFile();
|
|
1205
|
+
}
|
|
1206
|
+
catch {
|
|
1207
|
+
return false;
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
try {
|
|
1211
|
+
fs.accessSync(file, fs.constants.X_OK);
|
|
1212
|
+
return true;
|
|
1213
|
+
}
|
|
1214
|
+
catch {
|
|
1215
|
+
return false;
|
|
1216
|
+
}
|
|
1217
|
+
});
|
|
1218
|
+
}
|
|
1219
|
+
/** git's include depth limit (`MAX_INCLUDE_DEPTH`). */
|
|
1220
|
+
const GIT_MAX_INCLUDE_DEPTH = 10;
|
|
1221
|
+
/**
|
|
1222
|
+
* As `stop::compat::git_config_file`: one Git config file and the files its `[include]` and
|
|
1223
|
+
* `[includeIf]` sections name (whatever the condition, review 2712 r21). A missing file is skipped,
|
|
1224
|
+
* as git skips it; a symlink is followed to the file it names. `home`: HOME, set and not empty,
|
|
1225
|
+
* resolved against the root.
|
|
1226
|
+
*/
|
|
1227
|
+
function gitConfigFileHold(file, home, depth) {
|
|
1228
|
+
let stat;
|
|
1229
|
+
try {
|
|
1230
|
+
stat = fs.lstatSync(file);
|
|
1231
|
+
}
|
|
1232
|
+
catch (error) {
|
|
1233
|
+
return error.code === 'ENOENT' ? null : 'unreadable Git config';
|
|
1234
|
+
}
|
|
1235
|
+
if (stat.isSymbolicLink()) {
|
|
1236
|
+
try {
|
|
1237
|
+
stat = fs.statSync(file);
|
|
1238
|
+
}
|
|
1239
|
+
catch (error) {
|
|
1240
|
+
return error.code === 'ENOENT' ? null : 'unreadable Git config';
|
|
1241
|
+
}
|
|
1242
|
+
}
|
|
1243
|
+
if (!stat.isFile() || stat.size > 1024 * 1024)
|
|
1244
|
+
return 'unmodeled Git config file';
|
|
1245
|
+
let bytes;
|
|
1246
|
+
try {
|
|
1247
|
+
bytes = fs.readFileSync(file);
|
|
1248
|
+
}
|
|
1249
|
+
catch {
|
|
1250
|
+
return 'unreadable Git config';
|
|
1251
|
+
}
|
|
1252
|
+
let text;
|
|
1253
|
+
// As Rust's String::from_utf8: invalid bytes are a hold, never replaced. The BOM is
|
|
1254
|
+
// kept by the decoder; scanGitConfig skips it at the start of the file, as git does.
|
|
1255
|
+
try {
|
|
1256
|
+
text = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(bytes);
|
|
1257
|
+
}
|
|
1258
|
+
catch {
|
|
1259
|
+
return 'unmodeled Git config encoding';
|
|
1260
|
+
}
|
|
1261
|
+
return scanGitConfig(text, file, home, depth);
|
|
1262
|
+
}
|
|
1263
|
+
/** As Rust `git_include_target`: `~/` from HOME, a relative path from the including file's folder; null holds. */
|
|
1264
|
+
function gitIncludeTarget(from, value, home) {
|
|
1265
|
+
if (!value)
|
|
1266
|
+
return null;
|
|
1267
|
+
if (value.startsWith('~')) {
|
|
1268
|
+
const rest = value.slice(1);
|
|
1269
|
+
if (rest !== '' && !rest.startsWith('/'))
|
|
1270
|
+
return null;
|
|
1271
|
+
if (!home)
|
|
1272
|
+
return null;
|
|
1273
|
+
return joinRaw(home, rest.replace(/^\//, ''));
|
|
1274
|
+
}
|
|
1275
|
+
if (path.isAbsolute(value))
|
|
1276
|
+
return value;
|
|
1277
|
+
return joinRaw(path.dirname(from), value);
|
|
1278
|
+
}
|
|
1279
|
+
/**
|
|
1280
|
+
* One Git config file's text, as `stop::compat::scan_git_config` (see its comment for the git
|
|
1281
|
+
* semantics). A byte-order mark at the start is skipped (review 2712 r18 C2), CR and NUL hold as
|
|
1282
|
+
* syntax, and the rest is read with `gitConfigParse`, git 2.50's own tokenizer (review 2712 r18b).
|
|
1283
|
+
* `extensions.worktreeConfig` (`gitConfigBooleans`) is modeled: each value must be a boolean git
|
|
1284
|
+
* and gix read alike (`gitBoolean`), and the main worktree's `.git/config.worktree` that git then
|
|
1285
|
+
* loads is read under the same rules. Every other `[extensions]` key, and any key under
|
|
1286
|
+
* `[extensions "sub"]`, is a hold unless git accepts it at version 0 (`versionZeroExtension`,
|
|
1287
|
+
* review 2712 r20 C1). Includes are followed, except `hasconfig:` (review 2712 r22 C2); every other
|
|
1288
|
+
* variable goes through the allowlist (`gitKeyHold`, reviews 2712 r20 C2 and r21), in
|
|
1289
|
+
* `.git/config.worktree` as in every other file.
|
|
1290
|
+
*/
|
|
1291
|
+
function scanGitConfig(text, file, home, depth) {
|
|
1292
|
+
const REPOSITORY = 'unmodeled Git repository configuration';
|
|
1293
|
+
const INCLUDE = 'unmodeled Git config include';
|
|
1294
|
+
const body = text.startsWith('') ? text.slice(1) : text;
|
|
1295
|
+
// CR and NUL are not modeled (git reads CRLF as LF; the tokenizer takes LF only).
|
|
1296
|
+
if (/[\r\0]/.test(body))
|
|
1297
|
+
return 'unmodeled Git config syntax';
|
|
1298
|
+
const items = gitConfigParse(body);
|
|
1299
|
+
if (items === null)
|
|
1300
|
+
return 'unmodeled Git config syntax';
|
|
1301
|
+
for (const item of items) {
|
|
1302
|
+
const { section } = item;
|
|
1303
|
+
const subsection = item.subsection !== null;
|
|
1304
|
+
if (stopCompatibility.gitConfigSections.includes(section))
|
|
1305
|
+
return 'unmodeled Git output configuration';
|
|
1306
|
+
if (item.header)
|
|
1307
|
+
continue;
|
|
1308
|
+
const { key, value } = item;
|
|
1309
|
+
const full = `${section}.${key}`;
|
|
1310
|
+
// Review 2712 r22 C2: git dies when a file a `hasconfig:` include names sets remote.<name>.url,
|
|
1311
|
+
// directly or through a nested include, whether or not the condition matches. Not modeled.
|
|
1312
|
+
if (section === 'includeif' && asciiLower(item.subsection ?? '').startsWith('hasconfig:'))
|
|
1313
|
+
return INCLUDE;
|
|
1314
|
+
if (section === 'include' || section === 'includeif') {
|
|
1315
|
+
if (key !== 'path' || subsection !== (section === 'includeif'))
|
|
1316
|
+
return INCLUDE;
|
|
1317
|
+
if (depth + 1 >= GIT_MAX_INCLUDE_DEPTH)
|
|
1318
|
+
return INCLUDE;
|
|
1319
|
+
const target = gitIncludeTarget(file, value, home);
|
|
1320
|
+
if (target === null)
|
|
1321
|
+
return INCLUDE;
|
|
1322
|
+
const hold = gitConfigFileHold(target, home, depth + 1);
|
|
1323
|
+
if (hold)
|
|
1324
|
+
return hold;
|
|
1325
|
+
continue;
|
|
1326
|
+
}
|
|
1327
|
+
// Review 2712 r20 C1: only worktreeConfig and the extensions git accepts at version 0 are modeled.
|
|
1328
|
+
if (section === 'extensions' && subsection)
|
|
1329
|
+
return REPOSITORY;
|
|
1330
|
+
if (!subsection && stopCompatibility.gitConfigBooleans.includes(full)) {
|
|
1331
|
+
if (!gitBoolean(value))
|
|
1332
|
+
return REPOSITORY;
|
|
1333
|
+
continue;
|
|
1334
|
+
}
|
|
1335
|
+
if (!subsection && stopCompatibility.gitConfigKeys.includes(full)) {
|
|
1336
|
+
if (full === 'core.repositoryformatversion' && value === '0')
|
|
1337
|
+
continue;
|
|
1338
|
+
return REPOSITORY;
|
|
1339
|
+
}
|
|
1340
|
+
if (section === 'extensions') {
|
|
1341
|
+
if (!versionZeroExtension(key, value))
|
|
1342
|
+
return REPOSITORY;
|
|
1343
|
+
continue;
|
|
1344
|
+
}
|
|
1345
|
+
// Reviews 2712 r20 C2 and r21: an allowlist, not a model of what git rejects.
|
|
1346
|
+
const hold = gitKeyHold(section, item.subsection, key, value, home !== undefined);
|
|
1347
|
+
if (hold)
|
|
1348
|
+
return hold;
|
|
1349
|
+
}
|
|
1350
|
+
return null;
|
|
1351
|
+
}
|
|
1352
|
+
/**
|
|
1353
|
+
* As `stop::compat::git_boolean`: `value` as `gitConfigParse` reads it (null: a bare key, true), or a
|
|
1354
|
+
* boolean word or 0/1 in any ASCII case. An empty value or another integer is not modeled.
|
|
1355
|
+
*/
|
|
1356
|
+
function gitBoolean(value) {
|
|
1357
|
+
return value === null || ['true', 'yes', 'on', '1', 'false', 'no', 'off', '0'].includes(asciiLower(value));
|
|
1358
|
+
}
|
|
1359
|
+
/**
|
|
1360
|
+
* Review r10 C2, as `stop::compat::released_scripts`: master-hook runs the
|
|
1361
|
+
* ported hooks' scripts from disk and the native Stop runs its port, so a
|
|
1362
|
+
* ported script that is not a released version (`scripts` in the shared
|
|
1363
|
+
* policy) keeps master-hook. Checked: the ported `core/hooks/Stop` leaves
|
|
1364
|
+
* present on disk, and `.claude/hooks/<id>.sh` of each ported registry id
|
|
1365
|
+
* that will run (`runs`). A missing ported script is skipped; a file that
|
|
1366
|
+
* cannot be read is a hold. Review r13 C1: a missing dependency of a present
|
|
1367
|
+
* script is a hold unless `absent` is one of its pinned states (a released
|
|
1368
|
+
* core that does not ship it), reported after any present file that is not
|
|
1369
|
+
* released, as the Rust check orders them.
|
|
1370
|
+
*/
|
|
1371
|
+
function unreleasedScript(root, runs) {
|
|
1372
|
+
const scripts = stopCompatibility.scripts;
|
|
1373
|
+
const dependencies = stopCompatibility.dependencies;
|
|
1374
|
+
const core = Object.keys(scripts).filter((script) => script.startsWith('core/hooks/Stop/'));
|
|
1375
|
+
const registry = Object.keys(stopCompatibility.registry).filter(runs).map((id) => `.claude/hooks/${id}.sh`);
|
|
1376
|
+
// 'present' (a pinned version), 'absent', or why it is neither.
|
|
1377
|
+
const state = (relative) => {
|
|
1378
|
+
const file = path.join(root, relative);
|
|
1379
|
+
if (!fs.existsSync(file))
|
|
1380
|
+
return 'absent';
|
|
1381
|
+
const versions = scripts[relative] ?? [];
|
|
1382
|
+
if (versions.length === 1 && versions[0] === 'absent')
|
|
1383
|
+
return 'unreleased';
|
|
1384
|
+
let bytes;
|
|
1385
|
+
try {
|
|
1386
|
+
bytes = fs.readFileSync(file);
|
|
1387
|
+
}
|
|
1388
|
+
catch {
|
|
1389
|
+
return 'unreadable';
|
|
1390
|
+
}
|
|
1391
|
+
return versions.includes(createHash('sha256').update(bytes).digest('hex')) ? 'present' : 'unreleased';
|
|
1392
|
+
};
|
|
1393
|
+
const seen = new Set();
|
|
1394
|
+
let missing = null;
|
|
1395
|
+
for (const script of [...core, ...registry]) {
|
|
1396
|
+
const own = state(script);
|
|
1397
|
+
if (own === 'absent')
|
|
1398
|
+
continue;
|
|
1399
|
+
if (own !== 'present')
|
|
1400
|
+
return `${own} ported Stop script ${script}`;
|
|
1401
|
+
// Review r11 C1: the libraries and helpers it sources or runs, transitively.
|
|
1402
|
+
const queue = [...(dependencies[script] ?? [])];
|
|
1403
|
+
while (queue.length) {
|
|
1404
|
+
const file = queue.pop();
|
|
1405
|
+
if (seen.has(file))
|
|
1406
|
+
continue;
|
|
1407
|
+
seen.add(file);
|
|
1408
|
+
const found = state(file);
|
|
1409
|
+
if (found === 'present')
|
|
1410
|
+
queue.push(...(dependencies[file] ?? []));
|
|
1411
|
+
else if (found !== 'absent')
|
|
1412
|
+
return `${found} file ${file} that the ported Stop script ${script} sources or runs`;
|
|
1413
|
+
else if (missing === null && !(scripts[file] ?? []).includes('absent'))
|
|
1414
|
+
missing = `missing file ${file} that the ported Stop script ${script} sources or runs`;
|
|
1415
|
+
}
|
|
1416
|
+
}
|
|
1417
|
+
return missing;
|
|
1418
|
+
}
|
|
31
1419
|
/** Folder hook that master-hook.sh never dispatches (retired; `hq rescue` can leave it on disk). */
|
|
32
1420
|
const RETIRED = new Set(['Stop/70-work-mesh-done.sh']);
|
|
33
1421
|
function listDir(dir) {
|
|
@@ -103,13 +1491,30 @@ export function shellHooks(root, host, event) {
|
|
|
103
1491
|
}
|
|
104
1492
|
return out;
|
|
105
1493
|
}
|
|
1494
|
+
/**
|
|
1495
|
+
* Whether the engine's claim `pattern` covers `hook`. A claim may name folder
|
|
1496
|
+
* hooks by pattern, `*` standing for one path segment
|
|
1497
|
+
* (`personal/hooks/Stop/*.sh`); any other claim names one hook exactly.
|
|
1498
|
+
*/
|
|
1499
|
+
export function claimCovers(pattern, hook) {
|
|
1500
|
+
if (!pattern.includes('*'))
|
|
1501
|
+
return pattern === hook;
|
|
1502
|
+
const source = pattern.split('*').map((part) => part.replace(/[.+?^${}()|[\]\\]/g, '\\$&')).join('[^/]*');
|
|
1503
|
+
return new RegExp(`^${source}$`).test(hook);
|
|
1504
|
+
}
|
|
106
1505
|
/** The old dispatcher's hooks for `event` on `host` that the engine does not cover. */
|
|
107
|
-
export function uncoveredHooks(root, host, event, covers) {
|
|
1506
|
+
export function uncoveredHooks(root, host, event, covers, env = process.env) {
|
|
108
1507
|
const hooks = shellHooks(root, host, event);
|
|
109
1508
|
if (!hooks)
|
|
110
1509
|
return [UNREADABLE];
|
|
111
1510
|
const covered = covers?.[event] ?? [];
|
|
112
|
-
|
|
1511
|
+
const missing = hooks.filter((hook) => !covered.some((claim) => claimCovers(claim, hook)));
|
|
1512
|
+
if (event === 'Stop' && covered.includes('master-hook:Stop')) {
|
|
1513
|
+
const hold = stopCompatibilityHold(root, env);
|
|
1514
|
+
if (hold)
|
|
1515
|
+
missing.push(`(native Stop compatibility: ${hold})`);
|
|
1516
|
+
}
|
|
1517
|
+
return missing;
|
|
113
1518
|
}
|
|
114
1519
|
/**
|
|
115
1520
|
* `target` narrowed to the events it covers in full on `host` in this HQ
|
|
@@ -117,10 +1522,25 @@ export function uncoveredHooks(root, host, event, covers) {
|
|
|
117
1522
|
* or when the target's host canary gate refuses `host` (US-019 criterion 4:
|
|
118
1523
|
* no event on that host switches).
|
|
119
1524
|
*/
|
|
120
|
-
|
|
1525
|
+
/**
|
|
1526
|
+
* The environment a target was resolved under (`coreNextTarget`). The Stop
|
|
1527
|
+
* compatibility hold reads the same environment that decided the switch, so a
|
|
1528
|
+
* caller that only holds the target (the Grok trust step) does not fall back
|
|
1529
|
+
* to this process's own environment. Kept beside the target, not on it, so the
|
|
1530
|
+
* target's shape is unchanged.
|
|
1531
|
+
*/
|
|
1532
|
+
const resolvedEnv = new WeakMap();
|
|
1533
|
+
/** Records the environment `target` was resolved under; returns `target`. */
|
|
1534
|
+
export function resolvedUnder(target, env) {
|
|
1535
|
+
if (target)
|
|
1536
|
+
resolvedEnv.set(target, env);
|
|
1537
|
+
return target;
|
|
1538
|
+
}
|
|
1539
|
+
export function forHost(target, root, host, env) {
|
|
121
1540
|
if (!target)
|
|
122
1541
|
return null;
|
|
123
|
-
|
|
1542
|
+
env ??= resolvedEnv.get(target) ?? process.env;
|
|
1543
|
+
const events = target.events.filter((event) => uncoveredHooks(root, host, event, target.covers, env).length === 0);
|
|
124
1544
|
if (!events.length)
|
|
125
1545
|
return null;
|
|
126
1546
|
if (target.canary && !target.canary(root, host))
|