AtlatestRepositorysigil-doc

sigil-doc / tree / testgate-portability.sh

1#!/usr/bin/env bash
2set -euo pipefail
3
4repo_dir="$(cd "$(dirname "$0")/.." && pwd)"
5cd "$repo_dir"
6
7# POPULATION. `test/sabotage-*.sh` was absent from this list until 2026-08-23,
8# so seven sabotage scripts were swept by NOTHING -- no absolute-path check, no
9# `bash -n` parse. A syntax error in one surfaced only when somebody ran it, and
10# those are exactly the scripts run rarely and under time pressure. Adding the
11# glob costs one entry; every check below already applies to whatever is in it.
12active_scripts=(test/gate-*.sh test/sabotage-*.sh test/build-d3-bytecode-base.sh tools/*.sh)
14# EVERY ENTRY MUST BE A REAL FILE, because an unexpanded glob is not an error.
16# When a glob matches nothing, bash leaves the LITERAL PATTERN in the array. Both
17# consumers below would then quietly examine less than they claim: the mode loop
18# skips it via `test -f`, and a reader seeing "30 scripts" would have no way to
19# know one of four globs contributed zero. A floor on the total cannot catch it
20# either -- dropping `tools/*.sh` takes 30 to 28, comfortably above any floor
21# worth setting. Checking each entry is what makes the denominator honest, and it
22# is the check that catches a DIRECTORY being renamed out from under this file.
23for entry in "${active_scripts[@]}"; do
24 test -f "$entry" || {
25 echo "PORTABILITY_GATE: '$entry' is not a file." >&2
26 echo " A glob in active_scripts matched nothing, so bash left the pattern itself" >&2
27 echo " in the array and everything below is examining fewer scripts than it says." >&2
28 exit 1; }
29done
31absolute_path_pattern='(^|[^$A-Za-z0-9_])/(bin|sbin|usr|opt|Users|home|tmp|var|etc|run|nix|gnu)/'
33if ! printf 'tool=/%s\n' 'opt/portability-sensitivity/tool' \
34 | rg -q "$absolute_path_pattern"; then
35 echo 'PORTABILITY_GATE: absolute-path detector did not reject its sensitivity probe' >&2
36 exit 1
37fi
38absolute_path_hits=$(rg -n "$absolute_path_pattern" "${active_scripts[@]}" \
39 | rg -v ':1:#!' \
40 | rg -v 'test/gate-d6\.sh:.*#read.*etc/passwd' || true)
41if test -n "$absolute_path_hits"; then
42 printf '%s\n' "$absolute_path_hits"
43 echo 'PORTABILITY_GATE: active scripts contain a host-specific absolute path' >&2
44 exit 1
45fi
46if rg -n -- '-L[[:space:]]+\.\./sigil-sxml/src' "${active_scripts[@]}"; then
47 echo 'PORTABILITY_GATE: active scripts contain an unconfigurable sigil-sxml sibling path' >&2
48 exit 1
49fi
51# SD-P8 added `test/gate-sd-p8.sh`, which resolves sigil-sxml and the dev
52# compiler exactly as these do. Adding it here rather than leaving it to the
53# absolute-path sweep alone is the population check this file exists to be: a
54# new gate that took a sibling path unconfigurably would pass the regex above
55# and still be unportable.
56rg -q 'SIGIL_SXML_ROOT' test/gate-sd-p8.sh
57rg -q 'SIGIL_D1_COMPILER' test/gate-sd-p8.sh
58rg -q 'SIGIL_D1_COMPILER' test/sabotage-sd-p8.sh
59rg -q 'SIGIL_SXML_ROOT' test/sabotage-sd-p8.sh
61for gate in test/gate-d4a.sh test/gate-d6.sh test/gate-d6-customization.sh \
62 test/gate-semantic-structures.sh test/gate-glossary.sh; do
63 rg -q 'SIGIL_SXML_ROOT' "$gate"
64done
65for helper in test/d4a-trusted-presentation.sgl test/build-d6-pressure.sgl; do
66 rg -q 'getenv "SIGIL_D1_COMPILER"' "$helper"
67done
68for gate in test/gate-d6.sh test/gate-d6-customization.sh; do
69 rg -q 'TYPST_BIN' "$gate"
70 rg -q 'PDFINFO_BIN' "$gate"
71 rg -q 'PDFTOTEXT_BIN' "$gate"
72 rg -q 'PDFTOPPM_BIN' "$gate"
73 rg -q 'QPDF_BIN' "$gate"
74 rg -q 'CONVERT_BIN' "$gate"
75 rg -q 'SHA256SUM_BIN' "$gate"
76done
77rg -q 'SIGIL_COMPILER_REPO' test/build-d3-bytecode-base.sh
78rg -q 'D3_BYTECODE_BASE_DIR' test/build-d3-bytecode-base.sh
81# EVERY SCRIPT WITH A SHEBANG MUST BE EXECUTABLE IN THE INDEX.
83# Nothing in this repository checked file MODE until 2026-08-23, and the gap is
84# structural rather than an oversight: every check here looks at CONTENT. `bash
85# -n` parses, `rg` greps, `sha256sum` hashes, `git diff` shows. **None of them
86# can see a permission bit.** So a rewrite-and-replace -- `awk … > tmp && mv`, a
87# non-inplace `sed`, an editor's atomic save, a `cp` from a scratch copy -- drops
88# the execute bit silently and no instrument in the tree notices. It surfaces
89# only in git's own `create mode 100644` line at merge time, which nobody reads
90# adversarially. Two independent instances in this estate on one day.
92# THE INDEX, NOT THE FILESYSTEM. `git ls-files -s` is what ships and what a
93# fresh clone gets; a local `chmod +x` that was never staged would make a
94# filesystem check pass and still hand the next person a non-executable file.
96# IT ITERATES `active_scripts` -- THE SAME ARRAY THE SWEEP USES -- so the two
97# cannot drift. An earlier version re-listed two of the four globs and its
98# comment claimed the population was "derived"; it was not, and extending
99# `active_scripts` would have silently left the mode check behind. Sharing the
100# array makes that structural rather than a promise. The shebang test does the
101# filtering, which is why widening it to `tools/*.sh` and the d3 base script
102# cost nothing: measured, all three are already 100755 with shebangs.
103mode_checked=0
104mode_bad=0
105for script in "${active_scripts[@]}"; do
106 test -f "$script" || continue
107 head -1 "$script" | rg -q '^#!' || continue
108 mode_checked=$((mode_checked + 1))
109 mode=$(git ls-files -s -- "$script" | awk '{print $1}')
110 test -n "$mode" || { echo "PORTABILITY_GATE: $script is not tracked, so its mode cannot ship" >&2; mode_bad=$((mode_bad + 1)); continue; }
111 if [ "$mode" != "100755" ]; then
112 echo "PORTABILITY_GATE: $script has a shebang but is $mode in the index, not 100755" >&2
113 echo " A rewrite-and-replace drops the execute bit and no content check can see it." >&2
114 echo " Fix with: chmod +x $script && git update-index --chmod=+x $script" >&2
115 mode_bad=$((mode_bad + 1))
116 fi
117done
118test "$mode_checked" -ge 20 \
119 || { echo "PORTABILITY_GATE: the mode check examined only $mode_checked scripts; its globs are not matching the tree" >&2; exit 1; }
120test "$mode_bad" -eq 0 || { echo "PORTABILITY_GATE: $mode_bad script(s) with a shebang are not executable in the index" >&2; exit 1; }
121echo "PORTABILITY_GATE_MODES $mode_checked scripts with a shebang, all 100755 in the index"
123bash -n "${active_scripts[@]}"
124echo PORTABILITY_GATE_GREEN