| 1 | #!/usr/bin/env python3 |
| 2 | """Require a behaviour statement in every behaviour-changing commit. |
| 3 | |
| 4 | A commit whose subject starts with feat/fix/perf/refactor must say what changed |
| 5 | for users, either: |
| 6 | |
| 7 | Behaviour: skills index budget default 25600 -> 2400 chars (0.05 -> 0.002) |
| 8 | |
| 9 | or, deliberately: |
| 10 | |
| 11 | No-behaviour-change: refactor only; no default, limit or rendered block changed |
| 12 | |
| 13 | Merge, revert, chore, docs, test, ci, build and style commits are exempt: they |
| 14 | do not ask a user to expect anything different. This is a statement check, not |
| 15 | a semantic one. It makes silence a decision; the regression pass reads the |
| 16 | statement next to the diff. |
| 17 | |
| 18 | Usage: |
| 19 | scripts/check-behaviour-line.py [--warn-only] [--strict-guards|--guards-only] [base] [head] |
| 20 | scripts/check-behaviour-line.py [--warn-only] --range base..head |
| 21 | |
| 22 | --strict-guards additionally fails any commit, whatever its subject, that |
| 23 | modifies crates/engine/tests/behavioural_guards.rs without a `Behaviour:` line: |
| 24 | a guard changes only when behaviour is deliberately changed, so |
| 25 | `No-behaviour-change:` does not satisfy it. |
| 26 | |
| 27 | Defaults: base = BEFORE_SHA when set, else the merge base with origin/main; |
| 28 | head = HEAD. |
| 29 | """ |
| 30 | |
| 31 | from __future__ import annotations |
| 32 | |
| 33 | import argparse |
| 34 | import os |
| 35 | import re |
| 36 | import subprocess |
| 37 | import sys |
| 38 | |
| 39 | BEHAVIOUR_RE = re.compile(r"^Behaviour:\s*\S", re.MULTILINE) |
| 40 | NO_CHANGE_RE = re.compile(r"^No-behaviour-change:\s*\S", re.MULTILINE) |
| 41 | GUARDS_FILE = "crates/engine/tests/behavioural_guards.rs" |
| 42 | SUBJECT_RE = re.compile(r"^(feat|fix|perf|refactor)(\([^)]*\))?!?:") |
| 43 | |
| 44 | |
| 45 | def git(*args: str) -> str: |
| 46 | result = subprocess.run( |
| 47 | ["git", *args], capture_output=True, text=True, check=False |
| 48 | ) |
| 49 | if result.returncode != 0: |
| 50 | sys.exit( |
| 51 | f"check-behaviour-line: git {' '.join(args)} failed: " |
| 52 | f"{result.stderr.strip()}" |
| 53 | ) |
| 54 | return result.stdout |
| 55 | |
| 56 | |
| 57 | def default_base() -> str: |
| 58 | before = os.environ.get("BEFORE_SHA", "").strip() |
| 59 | if before and set(before) != {"0"}: |
| 60 | return before |
| 61 | return git("merge-base", "HEAD", "origin/main").strip() |
| 62 | |
| 63 | |
| 64 | def commits(base: str, head: str) -> list[tuple[str, str, str]]: |
| 65 | raw = git("log", "--no-merges", "--format=%H%x1f%s%x1f%b%x1e", f"{base}..{head}") |
| 66 | records: list[tuple[str, str, str]] = [] |
| 67 | for record in raw.split("\x1e"): |
| 68 | record = record.strip("\n") |
| 69 | if not record.strip(): |
| 70 | continue |
| 71 | parts = record.split("\x1f", 2) |
| 72 | if len(parts) == 3: |
| 73 | records.append((parts[0], parts[1], parts[2])) |
| 74 | return records |
| 75 | |
| 76 | |
| 77 | def touches_guards(sha: str) -> bool: |
| 78 | files = git("diff-tree", "--no-commit-id", "--name-only", "-r", sha).split() |
| 79 | return GUARDS_FILE in files |
| 80 | |
| 81 | |
| 82 | def main(argv: list[str]) -> int: |
| 83 | parser = argparse.ArgumentParser( |
| 84 | description="Require Behaviour: or No-behaviour-change: in behaviour-changing commits." |
| 85 | ) |
| 86 | parser.add_argument("base", nargs="?", help="range base (default: BEFORE_SHA or origin/main merge base)") |
| 87 | parser.add_argument("head", nargs="?", default="HEAD", help="range head (default: HEAD)") |
| 88 | parser.add_argument("--range", dest="range_spec", help="BASE..HEAD") |
| 89 | parser.add_argument( |
| 90 | "--warn-only", |
| 91 | action="store_true", |
| 92 | help="print the findings but exit 0 (preflight and the release-candidate report)", |
| 93 | ) |
| 94 | parser.add_argument( |
| 95 | "--strict-guards", |
| 96 | action="store_true", |
| 97 | help="also require a Behaviour: line on every commit that modifies behavioural_guards.rs", |
| 98 | ) |
| 99 | parser.add_argument( |
| 100 | "--guards-only", |
| 101 | action="store_true", |
| 102 | help="implies --strict-guards and skips the feat/fix/perf/refactor statement check", |
| 103 | ) |
| 104 | args = parser.parse_args(argv) |
| 105 | if args.guards_only: |
| 106 | args.strict_guards = True |
| 107 | |
| 108 | if args.range_spec: |
| 109 | if ".." not in args.range_spec: |
| 110 | parser.error("--range must be BASE..HEAD") |
| 111 | base, head = args.range_spec.split("..", 1) |
| 112 | else: |
| 113 | base = args.base or default_base() |
| 114 | head = args.head |
| 115 | |
| 116 | missing: list[tuple[str, str]] = [] |
| 117 | unguarded: list[tuple[str, str]] = [] |
| 118 | checked = 0 |
| 119 | for sha, subject, body in commits(base, head): |
| 120 | if args.strict_guards and not BEHAVIOUR_RE.search(body) and touches_guards(sha): |
| 121 | unguarded.append((sha, subject)) |
| 122 | if args.guards_only or not SUBJECT_RE.match(subject): |
| 123 | continue |
| 124 | checked += 1 |
| 125 | if BEHAVIOUR_RE.search(body) or NO_CHANGE_RE.search(body): |
| 126 | continue |
| 127 | missing.append((sha, subject)) |
| 128 | |
| 129 | if unguarded: |
| 130 | print( |
| 131 | f"check-behaviour-line: {len(unguarded)} commit(s) modify {GUARDS_FILE} " |
| 132 | "without a 'Behaviour:' line:", |
| 133 | file=sys.stderr, |
| 134 | ) |
| 135 | for sha, subject in unguarded: |
| 136 | print(f" - {sha[:12]} {subject}", file=sys.stderr) |
| 137 | |
| 138 | if missing: |
| 139 | print( |
| 140 | f"check-behaviour-line: {len(missing)} of {checked} behaviour-changing " |
| 141 | f"commit(s) in {base[:12]}..{head} do not state what changed:", |
| 142 | file=sys.stderr, |
| 143 | ) |
| 144 | for sha, subject in missing: |
| 145 | print(f" - {sha[:12]} {subject}", file=sys.stderr) |
| 146 | print( |
| 147 | "Add a body line: 'Behaviour: <old> -> <new>' or " |
| 148 | "'No-behaviour-change: <why not>'", |
| 149 | file=sys.stderr, |
| 150 | ) |
| 151 | return 0 if args.warn_only else 1 |
| 152 | |
| 153 | if unguarded: |
| 154 | return 0 if args.warn_only else 1 |
| 155 | |
| 156 | if args.guards_only: |
| 157 | print(f"check-behaviour-line: no unstated {GUARDS_FILE} change ({base[:12]}..{head})") |
| 158 | return 0 |
| 159 | |
| 160 | print( |
| 161 | f"check-behaviour-line: {checked} behaviour-changing commit(s) carry a " |
| 162 | f"statement ({base[:12]}..{head})" |
| 163 | ) |
| 164 | return 0 |
| 165 | |
| 166 | |
| 167 | if __name__ == "__main__": |
| 168 | raise SystemExit(main(sys.argv[1:])) |
| 169 |