|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Contract: a shell script invoked as a COMMAND must be executable in git. |
| 3 | +# |
| 4 | +# This exists because it cost a release build phase. scripts/ci/check-binary- |
| 5 | +# composition.sh was committed 100644 while being invoked directly from |
| 6 | +# scripts/package-release.sh, so every unix build leg died with: |
| 7 | +# |
| 8 | +# scripts/package-release.sh: line 190: .../check-binary-composition.sh: Permission denied |
| 9 | +# |
| 10 | +# It passed every local check because the WORKING COPY had the bit — only the |
| 11 | +# committed mode was wrong, which no amount of running it locally can reveal. |
| 12 | +# scripts/ci/append-vt-notes.sh had the identical defect queued up for the verify |
| 13 | +# step at the very end of the release. |
| 14 | +# |
| 15 | +# The rule is about the CALL SITE, not the file: `bash foo.sh` is equally correct |
| 16 | +# and mode-independent (and is what most of this repo does). What must not happen |
| 17 | +# again is a call site that needs the bit paired with a file that lacks it. |
| 18 | +# |
| 19 | +# Python, not shell: the analysis is "is this path the first word of a command", |
| 20 | +# and expressing that in `case` patterns is how the first version of this file |
| 21 | +# got a quoting bug of its own. |
| 22 | +set -euo pipefail |
| 23 | + |
| 24 | +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 25 | +cd "$ROOT" |
| 26 | + |
| 27 | +python3 - "$ROOT" <<'PY' |
| 28 | +import pathlib |
| 29 | +import re |
| 30 | +import subprocess |
| 31 | +import sys |
| 32 | +
|
| 33 | +root = pathlib.Path(sys.argv[1]) |
| 34 | +
|
| 35 | +def git(*args): |
| 36 | + return subprocess.run(["git", "-C", str(root), *args], |
| 37 | + capture_output=True, text=True, check=True).stdout |
| 38 | +
|
| 39 | +# Tracked .sh files whose COMMITTED mode is non-executable. |
| 40 | +non_exec = set() |
| 41 | +for line in git("ls-files", "-s", "*.sh").splitlines(): |
| 42 | + mode, _, _, path = line.split(maxsplit=3) |
| 43 | + if mode == "100644": |
| 44 | + non_exec.add(path) |
| 45 | +
|
| 46 | +# Places that actually execute things. |
| 47 | +search_roots = [root / ".github" / "workflows", root / "scripts", |
| 48 | + root / "test-infrastructure"] |
| 49 | +search_files = [root / "Makefile.cbm", root / "Makefile"] |
| 50 | +for base in search_roots: |
| 51 | + if base.is_dir(): |
| 52 | + search_files += [p for p in base.rglob("*") |
| 53 | + if p.is_file() and p.suffix in {".yml", ".yaml", ".sh", ""}] |
| 54 | +
|
| 55 | +# Strip shell/YAML/Make prefixes that mean "not the first word of a command". |
| 56 | +INTERPRETED = re.compile(r"^(bash|sh|zsh|source|\.)\s") |
| 57 | +LEADERS = re.compile(r"^(?:[-@\t ]*)(?:run:\s*)?(?:then\s+|else\s+|do\s+|&&\s*|\|\|\s*|;\s*)*") |
| 58 | +
|
| 59 | +failures = [] |
| 60 | +for path in search_files: |
| 61 | + try: |
| 62 | + text = path.read_text(errors="replace") |
| 63 | + except OSError: |
| 64 | + continue |
| 65 | + # Join backslash continuations into LOGICAL lines first. Without this, |
| 66 | + # ... && bash \ |
| 67 | + # test-infrastructure/vm/vm-run-tests.sh --soak |
| 68 | + # reads as a bare script path at the start of a line and reports a false |
| 69 | + # positive — which is how the first draft of this contract flagged a call |
| 70 | + # site that was already correct. |
| 71 | + logical = [] |
| 72 | + pending, start = "", 0 |
| 73 | + for number, raw in enumerate(text.splitlines(), 1): |
| 74 | + if not pending: |
| 75 | + start = number |
| 76 | + stripped_end = raw.rstrip() |
| 77 | + if stripped_end.endswith("\\"): |
| 78 | + pending += stripped_end[:-1] + " " |
| 79 | + continue |
| 80 | + logical.append((start, pending + raw)) |
| 81 | + pending = "" |
| 82 | + if pending: |
| 83 | + logical.append((start, pending)) |
| 84 | +
|
| 85 | + for number, raw in logical: |
| 86 | + line = raw.strip() |
| 87 | + if not line or line.startswith("#"): |
| 88 | + continue |
| 89 | + for script in non_exec: |
| 90 | + if script not in line: |
| 91 | + continue |
| 92 | + # Position of the reference; everything before it must be prefix-y. |
| 93 | + for match in re.finditer(re.escape(script), line): |
| 94 | + head = line[:match.start()] |
| 95 | + head_wo_leaders = LEADERS.sub("", head).lstrip("./") |
| 96 | + if head_wo_leaders.strip(): |
| 97 | + continue # something real precedes it → an argument |
| 98 | + if INTERPRETED.match(head.strip() + " "): |
| 99 | + continue # bash/sh/source foo.sh → mode irrelevant |
| 100 | + if head.strip() in {"", "-", "@"} or LEADERS.fullmatch(head): |
| 101 | + rel = path.relative_to(root) |
| 102 | + failures.append( |
| 103 | + f"{rel}:{number}: executes {script} directly, but its " |
| 104 | + f"committed mode is 100644\n" |
| 105 | + f" fix EITHER side: " |
| 106 | + f"'git update-index --chmod=+x {script}' or 'bash {script}'") |
| 107 | + break |
| 108 | +
|
| 109 | +if failures: |
| 110 | + for f in sorted(set(failures)): |
| 111 | + print("FAIL: " + f, file=sys.stderr) |
| 112 | + print(f"script exec-bit contract FAILED with {len(set(failures))} violation(s)", |
| 113 | + file=sys.stderr) |
| 114 | + sys.exit(1) |
| 115 | +print("PASS: no non-executable script is invoked as a command") |
| 116 | +PY |
0 commit comments