|
| 1 | +#!/usr/bin/env bash |
| 2 | +# SPDX-License-Identifier: MPL-2.0 |
| 3 | +# Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk> |
| 4 | +# |
| 5 | +# check-root-shape.sh — fail when the repository root contains entries that |
| 6 | +# are not on the canonical allowlist (.machine_readable/root-allow.txt). |
| 7 | +# |
| 8 | +# Companion to scripts/validate-template.sh: that script enforces required |
| 9 | +# files; this one enforces that nothing else has crept in. |
| 10 | +# |
| 11 | +# Exit codes: |
| 12 | +# 0 — root matches allowlist |
| 13 | +# 1 — extras found at root (drift) |
| 14 | +# 2 — usage / setup error |
| 15 | + |
| 16 | +set -euo pipefail |
| 17 | + |
| 18 | +REPO_ROOT="${1:-.}" |
| 19 | +ALLOW_FILE="${REPO_ROOT}/.machine_readable/root-allow.txt" |
| 20 | + |
| 21 | +if [ ! -f "$ALLOW_FILE" ]; then |
| 22 | + echo "ERROR: allowlist not found at $ALLOW_FILE" >&2 |
| 23 | + exit 2 |
| 24 | +fi |
| 25 | + |
| 26 | +# Build the allow set: strip comments, trailing slashes, and blank lines. |
| 27 | +mapfile -t ALLOW < <( |
| 28 | + sed -E 's/[[:space:]]*#.*$//' "$ALLOW_FILE" \ |
| 29 | + | sed -E 's|/$||' \ |
| 30 | + | awk 'NF' |
| 31 | +) |
| 32 | + |
| 33 | +declare -A ALLOW_SET=() |
| 34 | +for entry in "${ALLOW[@]}"; do |
| 35 | + ALLOW_SET["$entry"]=1 |
| 36 | +done |
| 37 | + |
| 38 | +# Enumerate everything tracked or present at the repository root. |
| 39 | +mapfile -t ACTUAL < <( |
| 40 | + cd "$REPO_ROOT" && \ |
| 41 | + find . -mindepth 1 -maxdepth 1 \ |
| 42 | + ! -name '.' \ |
| 43 | + -printf '%f\n' \ |
| 44 | + | sort |
| 45 | +) |
| 46 | + |
| 47 | +EXTRAS=() |
| 48 | +for entry in "${ACTUAL[@]}"; do |
| 49 | + if [ -z "${ALLOW_SET[$entry]+x}" ]; then |
| 50 | + EXTRAS+=("$entry") |
| 51 | + fi |
| 52 | +done |
| 53 | + |
| 54 | +if [ ${#EXTRAS[@]} -eq 0 ]; then |
| 55 | + echo "PASS: root matches allowlist (${#ACTUAL[@]} entries, ${#ALLOW[@]} permitted)" |
| 56 | + exit 0 |
| 57 | +fi |
| 58 | + |
| 59 | +echo "FAIL: ${#EXTRAS[@]} root entries are not on the allowlist:" >&2 |
| 60 | +for e in "${EXTRAS[@]}"; do |
| 61 | + if [ -d "$REPO_ROOT/$e" ]; then |
| 62 | + echo " - $e/ (directory)" >&2 |
| 63 | + else |
| 64 | + echo " - $e" >&2 |
| 65 | + fi |
| 66 | +done |
| 67 | +echo "" >&2 |
| 68 | +echo "Either move them into the appropriate subdirectory, or add a justified" >&2 |
| 69 | +echo "entry to .machine_readable/root-allow.txt." >&2 |
| 70 | +exit 1 |
0 commit comments