@ai-ecoverse/slicc-shared-web 1.30.1 → 1.30.3
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/README.md +4 -3
- package/bin/slicc-install-hooks.sh +19 -10
- package/bin/slicc-unit-cover.mjs +19 -5
- package/package.json +1 -1
- package/unit/merge.mjs +117 -0
package/README.md
CHANGED
|
@@ -215,12 +215,12 @@ Without it, every worktree of a repo shares `.git/hooks/` with the main clone, a
|
|
|
215
215
|
|
|
216
216
|
and the PR commits the `.githooks/` directory that the first `npm install` writes.
|
|
217
217
|
|
|
218
|
-
- **What it does:** sets one shared, relative `core.hooksPath` of `.githooks` (`<dir>/.githooks` for a package in a subdirectory of the repo),
|
|
218
|
+
- **What it does:** sets one shared, relative `core.hooksPath` of `.githooks` (`<dir>/.githooks` for a package in a subdirectory of the repo), writes a hook into this worktree's gitignored `node_modules/.git-hooks/` for every git hook in the config `lefthook dump` prints, `extends` included, and a shim into `.githooks/` for each one that has none yet. Each hook runs `lefthook run --no-auto-install <hook>` with the lefthook binary from its own `node_modules/`, found relative to the hook, so lefthook never tries to sync hooks into `core.hooksPath`. It sets no git config from the command line or the environment, so the AI git wrapper lets it run. It never overwrites an existing shim. Git resolves a relative `core.hooksPath` against the top of the worktree a commit runs in, so each worktree runs its own committed shim, and the shim runs that worktree's own `node_modules/.git-hooks/<hook>`. No `extensions.worktreeConfig`, no per-worktree config, and `core.bare` stays where it is, so worktrees of a bare repo work too. Running it again changes nothing.
|
|
219
219
|
- **Hooks are never shared:** whatever another worktree installs later, removing a worktree removes only its own hooks, and a moved checkout keeps working. Lefthook's own `postinstall` refuses the custom `core.hooksPath`, writes nothing and doesn't fail the install, and git never reads `.git/hooks/` once `core.hooksPath` is set, so `allowScripts` needs no entry for lefthook.
|
|
220
220
|
- **A worktree that never ran `npm install`** fails every commit with `pre-commit: run npm install`, since its shim finds no `node_modules/.git-hooks/pre-commit`.
|
|
221
221
|
- **A checkout without the committed `.githooks/`** (a branch cut before the rollout PR) has no shim, so its commits run no hooks until it merges or rebases onto the rollout.
|
|
222
|
-
- **A hook type added to `lefthook.yml` later** needs `npm install` again in each worktree, and its new shim committed
|
|
223
|
-
- **
|
|
222
|
+
- **A hook type added to `lefthook.yml` later** needs `npm install` again in each worktree, and its new shim committed.
|
|
223
|
+
- **Don't unset `core.hooksPath`** when `lefthook install` suggests it: that sends every worktree back to the shared `.git/hooks/`.
|
|
224
224
|
- **Clones set up by the previous version** (2.1.1, 1.29.3), which set an absolute per-worktree `core.hooksPath` into a `worktree-hooks/` directory: it unsets that in the current worktree's `config.worktree`. It leaves `extensions.worktreeConfig` and the moved `core.bare` alone.
|
|
225
225
|
- **Outside a git checkout** (a packed tarball, say) it does nothing and exits 0.
|
|
226
226
|
|
|
@@ -230,6 +230,7 @@ The diff-coverage hook, `slicc-unit-cover`, runs only the unit tests that cover
|
|
|
230
230
|
|
|
231
231
|
- **Which directories are source:** `src/` by default, anywhere in the path, as before. A repo whose code lives elsewhere lists its directories in `package.json`: `"slicc": { "unitSources": ["extension", "six", "scripts"] }`, repo-relative, without `.` or `..` segments. `mcr.config.mjs` then collects coverage only from those directories, and the hook treats a staged file outside them as no code. A malformed list fails the hook.
|
|
232
232
|
- **What counts as changed:** the staged files (`git diff --cached --name-only`), and nothing else. `test/unit/` is gitignored, and repos that keep their unit tests in a store outside git sync the whole store into it before a commit, which brings in tests for code that is on other branches; so a unit test that is new or edited runs only when its imports reach a staged file, like any other. One that reaches nothing staged adds no coverage to the staged lines.
|
|
233
|
+
- **Committing a merge, cherry-pick or rebase:** while `MERGE_HEAD` or `CHERRY_PICK_HEAD` exists, or a rebase is running and has stopped at `REBASE_HEAD`, only lines that are new against `HEAD` and against each of those commits count: conflict resolutions and edits made during the merge, not what the other side brought, which was covered where it was written. Diff coverage checks only those lines; the tests that run and the missing-lcov check follow the files that hold them, and files where the merge itself deleted lines. A merge with no changes of its own is no code staged. A revert counts as usual.
|
|
233
234
|
- **A unit test for code this tree doesn't have:** a test whose imports reach a staged file but also a code module under the sources that doesn't exist, isn't gitignored and isn't a staged deletion (a store test for a module on another branch) is skipped, and the hook names it in one line before the count. A missing `new URL(…, import.meta.url)` target, a missing non-code file such as `.wasm` or `.json`, and a missing file outside the sources or gitignored (a build output such as `dist/` or a bundled worker) don't skip the test.
|
|
234
235
|
- **Which tests run:** every `*.test.*` file in `test/unit/` that is changed, or whose static imports reach a changed file. The import graph is parsed with [acorn](https://github.com/acornjs/acorn), TypeScript after Node's type stripping, or amaro's transform where stripping isn't enough. It follows `import`, `export … from`, `import()`, `require()` and `new URL(…, import.meta.url)` with literal paths, relative or through the `paths` of `tsconfig.json` (and the files it extends). A path may leave out its extension or name `index`, and `.js` also finds `.ts`. Type-only imports don't count, and nothing is followed into `node_modules/`.
|
|
235
236
|
- **Barrels are followed by name:** a test that imports `{ a, b }` from a module follows only what provides `a` and `b`: `export { a } from`, `export { default as a } from`, an imported binding exported again, and `export * from`, by the target's own exports. `const { a, b } = await import('…')` with a literal path is followed by name too. A name the module declares itself follows all of that module's imports. A namespace import (`import * as`), any other `import()` (bound to a namespace, with `.then`, or destructured with a computed key or a rest element), a `require()`, and an `export *` that leads outside the repo or into CommonJS (any `.cjs` or `.cts` file) all count as the whole module.
|
|
@@ -8,16 +8,25 @@ if [ -f "$own" ]; then
|
|
|
8
8
|
esac
|
|
9
9
|
fi
|
|
10
10
|
git config core.hooksPath "$(git rev-parse --show-prefix).githooks"
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
)
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
11
|
+
config="$(lefthook dump)"
|
|
12
|
+
githooks=" applypatch-msg pre-applypatch post-applypatch pre-commit pre-merge-commit prepare-commit-msg commit-msg post-commit pre-rebase post-checkout post-merge pre-push pre-receive update proc-receive post-receive post-update reference-transaction push-to-checkout pre-auto-gc post-rewrite sendemail-validate fsmonitor-watchman p4-changelist p4-prepare-changelist p4-post-changelist p4-pre-submit post-index-change "
|
|
13
|
+
mkdir -p node_modules/.git-hooks .githooks
|
|
14
|
+
for name in $(printf '%s\n' "$config" | sed -n 's/^\([a-z][a-z0-9-]*\):.*/\1/p'); do
|
|
15
|
+
case "$githooks" in *" $name "*) ;; *) continue ;; esac
|
|
16
|
+
hook="node_modules/.git-hooks/$name"
|
|
17
|
+
sed "s/@HOOK@/$name/g" >"$hook" <<'HOOK'
|
|
18
|
+
#!/bin/sh
|
|
19
|
+
if [ "${LEFTHOOK_VERBOSE:-}" = 1 ] || [ "${LEFTHOOK_VERBOSE:-}" = true ]; then set -x; fi
|
|
20
|
+
modules="$(cd "${0%/*}/.." && pwd -P)"
|
|
21
|
+
os="$(uname -s | tr '[:upper:]' '[:lower:]')"
|
|
22
|
+
cpu="$(uname -m | sed 's/aarch64/arm64/;s/x86_64/x64/')"
|
|
23
|
+
lefthook="$modules/lefthook-$os-$cpu/bin/lefthook"
|
|
24
|
+
[ -x "$lefthook" ] || lefthook="$modules/.bin/lefthook"
|
|
25
|
+
[ -x "$lefthook" ] || { echo "@HOOK@: run npm install" >&2; exit 1; }
|
|
26
|
+
exec "$lefthook" run --no-auto-install "@HOOK@" "$@"
|
|
27
|
+
HOOK
|
|
28
|
+
chmod +x "$hook"
|
|
29
|
+
[ ! -e ".githooks/$name" ] || continue
|
|
21
30
|
cat >".githooks/$name" <<'SHIM'
|
|
22
31
|
#!/bin/sh
|
|
23
32
|
hook="${0%/*}/../node_modules/.git-hooks/${0##*/}"
|
package/bin/slicc-unit-cover.mjs
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { spawn, spawnSync } from 'node:child_process';
|
|
3
|
-
import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs';
|
|
3
|
+
import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
4
4
|
import { tmpdir } from 'node:os';
|
|
5
5
|
import { join } from 'node:path';
|
|
6
6
|
import { fileURLToPath } from 'node:url';
|
|
7
7
|
import { code, ParseError, select } from '../unit/graph.mjs';
|
|
8
8
|
import { inject } from '../unit/inject.mjs';
|
|
9
|
+
import { merged } from '../unit/merge.mjs';
|
|
9
10
|
import { inSources } from '../unit/sources.mjs';
|
|
10
11
|
|
|
11
12
|
const root = process.cwd();
|
|
@@ -75,9 +76,12 @@ function selection(staged) {
|
|
|
75
76
|
}
|
|
76
77
|
}
|
|
77
78
|
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
79
|
+
const merge = merged(root);
|
|
80
|
+
const staged =
|
|
81
|
+
merge?.files ??
|
|
82
|
+
(git('diff', '--cached', '--name-only', '--relative', '--no-renames', '-z') ?? '')
|
|
83
|
+
.split('\0')
|
|
84
|
+
.filter(Boolean);
|
|
81
85
|
if (
|
|
82
86
|
process.env.SLICC_UNIT_ALL !== '1' &&
|
|
83
87
|
!staged.some((file) => code.test(file) && inSources(file, root))
|
|
@@ -163,6 +167,16 @@ const lcov = (reports.length > 0 ? reports : [join('coverage', 'unit', 'lcov.inf
|
|
|
163
167
|
const seen = new Set(lcov.match(/^SF:.*$/gm)?.map((line) => line.slice(3)) ?? []);
|
|
164
168
|
const missing = measured.filter((file) => !seen.has(file));
|
|
165
169
|
if (missing.length > 0) verdict(false, `no coverage was collected for ${missing.join(' ')}`);
|
|
166
|
-
|
|
170
|
+
let args = reports;
|
|
171
|
+
if (merge !== undefined) {
|
|
172
|
+
const diff = join(scratch, 'diff');
|
|
173
|
+
writeFileSync(diff, merge.diff);
|
|
174
|
+
args = [
|
|
175
|
+
...(reports.length > 0 ? reports : [join('coverage', 'unit', 'lcov.info')]),
|
|
176
|
+
'--diff-file',
|
|
177
|
+
diff,
|
|
178
|
+
];
|
|
179
|
+
}
|
|
180
|
+
const covered = run(cover, args, process.env);
|
|
167
181
|
if (covered !== 0) verdict(false, `diff coverage is not 100% (${files} in ${counted})`);
|
|
168
182
|
verdict(true, `${files} in ${counted}, diff coverage 100%`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-ecoverse/slicc-shared-web",
|
|
3
|
-
"version": "1.30.
|
|
3
|
+
"version": "1.30.3",
|
|
4
4
|
"description": "Shared Renovate, Biome, TypeScript, lefthook and GitHub Actions configuration, plus the CDP integration-test harness, for SLICC's web repos",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "Apache-2.0",
|
package/unit/merge.mjs
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
3
|
+
import { resolve } from 'node:path';
|
|
4
|
+
|
|
5
|
+
const diff = [
|
|
6
|
+
'-c',
|
|
7
|
+
'diff.mnemonicprefix=no',
|
|
8
|
+
'-c',
|
|
9
|
+
'diff.noprefix=no',
|
|
10
|
+
'-c',
|
|
11
|
+
'diff.srcPrefix=a/',
|
|
12
|
+
'-c',
|
|
13
|
+
'diff.dstPrefix=b/',
|
|
14
|
+
'diff',
|
|
15
|
+
'--cached',
|
|
16
|
+
'--no-color',
|
|
17
|
+
'--no-ext-diff',
|
|
18
|
+
'--no-relative',
|
|
19
|
+
'-U0',
|
|
20
|
+
];
|
|
21
|
+
const escapes = { a: 7, b: 8, f: 12, n: 10, r: 13, t: 9, v: 11 };
|
|
22
|
+
const hunk = /^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@/;
|
|
23
|
+
|
|
24
|
+
function git(root, args) {
|
|
25
|
+
const run = spawnSync('git', args, { cwd: root, encoding: 'utf8', maxBuffer: 2 ** 30 });
|
|
26
|
+
return run.status === 0 ? run.stdout : undefined;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function unquote(text) {
|
|
30
|
+
if (!text.startsWith('"')) return text;
|
|
31
|
+
const bytes = [];
|
|
32
|
+
for (let at = 1; at < text.length - 1; at++) {
|
|
33
|
+
if (text[at] !== '\\') {
|
|
34
|
+
bytes.push(text.charCodeAt(at));
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
const next = text[++at];
|
|
38
|
+
if (/[0-7]/.test(next)) {
|
|
39
|
+
bytes.push(Number.parseInt(text.slice(at, at + 3), 8));
|
|
40
|
+
at += 2;
|
|
41
|
+
} else bytes.push(escapes[next] ?? next.charCodeAt(0));
|
|
42
|
+
}
|
|
43
|
+
return Buffer.from(bytes).toString('utf8');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function path(side) {
|
|
47
|
+
const named = side.replace(/\t$/, '');
|
|
48
|
+
return named === '/dev/null' ? undefined : unquote(named).slice(2);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function mark(file, found) {
|
|
52
|
+
file.hunks = true;
|
|
53
|
+
const start = Number(found[1]);
|
|
54
|
+
const count = found[2] === undefined ? 1 : Number(found[2]);
|
|
55
|
+
if (count === 0) file.marks.add(`^${start}`);
|
|
56
|
+
for (let at = start; at < start + count; at++) file.marks.add(`+${at}`);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function marks(text) {
|
|
60
|
+
const files = new Map();
|
|
61
|
+
let file;
|
|
62
|
+
for (const line of text.split('\n')) {
|
|
63
|
+
if (line.startsWith('diff --git ')) {
|
|
64
|
+
file = { header: line, paths: [], marks: new Set(), hunks: false };
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if (!file) continue;
|
|
68
|
+
const found = hunk.exec(line);
|
|
69
|
+
if (found) mark(file, found);
|
|
70
|
+
else if (!file.hunks && (line.startsWith('--- ') || line.startsWith('+++ '))) {
|
|
71
|
+
const named = path(line.slice(4));
|
|
72
|
+
if (named !== undefined) file.paths.push(named);
|
|
73
|
+
if (line.startsWith('+++ ')) files.set(file.paths.at(-1), file);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return files;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function where(root, name) {
|
|
80
|
+
const found = git(root, ['rev-parse', '--git-path', name]);
|
|
81
|
+
return found === undefined ? undefined : resolve(root, found.trim());
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const present = (file) => file !== undefined && existsSync(file);
|
|
85
|
+
|
|
86
|
+
export function parents(root) {
|
|
87
|
+
const head = where(root, 'MERGE_HEAD');
|
|
88
|
+
const merging = present(head) ? readFileSync(head, 'utf8').split('\n').filter(Boolean) : [];
|
|
89
|
+
const rebasing = ['rebase-merge', 'rebase-apply'].some((dir) => present(where(root, dir)));
|
|
90
|
+
const picked = (rebasing ? ['CHERRY_PICK_HEAD', 'REBASE_HEAD'] : ['CHERRY_PICK_HEAD'])
|
|
91
|
+
.map((ref) => git(root, ['rev-parse', '-q', '--verify', `${ref}^{commit}`])?.trim())
|
|
92
|
+
.filter(Boolean);
|
|
93
|
+
return [...merging, ...picked];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function merged(root = process.cwd()) {
|
|
97
|
+
const others = parents(root);
|
|
98
|
+
if (others.length === 0) return undefined;
|
|
99
|
+
const views = ['HEAD', ...others].map((parent) => git(root, [...diff, parent]));
|
|
100
|
+
if (views.includes(undefined)) return undefined;
|
|
101
|
+
const [base, ...rest] = views.map(marks);
|
|
102
|
+
const prefix = git(root, ['rev-parse', '--show-prefix'])?.trim() ?? '';
|
|
103
|
+
const files = new Set();
|
|
104
|
+
let text = '';
|
|
105
|
+
for (const [name, file] of base) {
|
|
106
|
+
const kept = [...file.marks].filter((entry) =>
|
|
107
|
+
rest.every((other) => other.get(name)?.marks.has(entry))
|
|
108
|
+
);
|
|
109
|
+
if (kept.length === 0) continue;
|
|
110
|
+
for (const each of file.paths)
|
|
111
|
+
if (each.startsWith(prefix)) files.add(each.slice(prefix.length));
|
|
112
|
+
const lines = kept.filter((entry) => entry.startsWith('+')).map((entry) => entry.slice(1));
|
|
113
|
+
if (lines.length > 0)
|
|
114
|
+
text += `${file.header}\n${lines.map((at) => `@@ -0,0 +${at} @@\n+\n`).join('')}`;
|
|
115
|
+
}
|
|
116
|
+
return { files: [...files], diff: text };
|
|
117
|
+
}
|