#!/usr/bin/env python3 """Move a branch's edits of the old inline command table into the table fragments. Before the table was split, `crates/engine/src/cmd.rs` held every core command in one `vec![...]` inside `fn build()`. The entries now live in `crates/engine/src/cmd/table//.rs`, and `install/command_ids.txt` pins the registry order. A branch that edited the old table carries its edits in `cmd.rs`. This script applies them to the fragments: python3 scripts/engine_split/table_fragments.py --base --head Run it in a checkout of the new main (or of the branch rebased onto it, with its `cmd.rs` hunks for the old table dropped). `--head` is the branch commit from before the rebase (`@{1}` or `ORIG_HEAD` right after it). Per command id, compared between BASE and HEAD: changed the entry in its fragment is replaced by HEAD's text (if the fragment still has BASE's text; otherwise it is reported as a CONFLICT and left alone); added the entry goes after its predecessor's entry, in the predecessor's fragment, and its id goes after the predecessor's id in command_ids.txt; removed the entry and its id are deleted (if the fragment still has BASE's text). The adjustment table and its loop (`ADJ`, between the `vec![...]` and the `v.extend(...)` list) are three-way merged into table/image/adjustments.rs. Any other change inside `build()` (the module table list, the test-only command) is reported as UNHANDLED: `install_tables.py` moves module tables into `TABLES` in install.rs; edit anything else there by hand. Entries are compared without comments and whitespace, so rustfmt does not matter. Names a new entry uses from cmd.rs are added to its fragment's `use crate::cmd::{..}` list. Run `cargo fmt --all` afterwards. Running the script twice gives the same result. CONFLICT, UNHANDLED, UNPLACED, MISSING and DUPLICATE lines mean exit status 1; resolve those by hand. Edits outside `build()` (imports in cmd.rs, helper fns, test modules) are not touched. """ import argparse, os, re, subprocess, sys, tempfile CMD = "crates/engine/src/cmd.rs" TABLE = "crates/engine/src/cmd/table" IDS = "crates/engine/src/install/command_ids.txt" ID_RE = re.compile(r'"([a-zA-Z]+\.[A-Za-z0-9.]+)"') def skip(text, i): """Index after the comment, string or char literal starting at i, or None.""" if text.startswith("//", i): j = text.find("\n", i) return len(text) if j < 0 else j m = re.match(r'r(#*)"', text[i:]) if m and (i == 0 or not (text[i - 1].isalnum() or text[i - 1] == "_")): end = text.index('"' + m.group(1), i + len(m.group(0))) return end + 1 + len(m.group(1)) if text[i] == '"': i += 1 while text[i] != '"': i += 2 if text[i] == "\\" else 1 return i + 1 if text[i] == "'": m = re.match(r"'(\\.[^']*|[^\\'])'", text[i:]) if m: return i + len(m.group(0)) return None def split_entries(body): """The top-level comma-separated entries of a `vec![...]` body.""" entries, depth, start, i = [], 0, 0, 0 while i < len(body): j = skip(body, i) if j is not None: i = j continue c = body[i] if c in "([{": depth += 1 elif c in ")]}": depth -= 1 elif c == "," and depth == 0: entries.append(body[start : i + 1]) start = i + 1 i += 1 if body[start:].strip(): entries.append(body[start:]) return entries def entry_id(entry): code = "\n".join(l for l in entry.splitlines() if not l.strip().startswith("//")) m = ID_RE.search(code) return m.group(1) if m else None def git_show(ref, path): return subprocess.run(["git", "show", f"{ref}:{path}"], capture_output=True, text=True, check=True).stdout class Fragments: """Every `vec![...]` of the fragment files, with its entries.""" def __init__(self): self.files = {} for d, _, names in os.walk(TABLE): for n in names: if n.endswith(".rs") and os.path.dirname(os.path.join(d, n)) != TABLE: p = os.path.join(d, n) if "vec![" in open(p).read(): self.files[p] = open(p).read() def find(self, cid): """(path, entry text) of the entry for a command id, or None.""" for p, t in self.files.items(): for e in self.entries(t): if entry_id(e) == cid: return p, e return None @staticmethod def spans(t): """(start, end) of each vec! body in a fragment file.""" out, pos = [], 0 while True: k = t.find("vec![", pos) if k < 0: return out s, depth, i = k + 5, 1, k + 5 while depth: j = skip(t, i) if j is not None: i = j continue depth += t[i] == "[" depth -= t[i] == "]" i += 1 out.append((s, i - 1)) pos = i def entries(self, t): return [e for s, f in self.spans(t) for e in split_entries(t[s:f])] def replace(self, path, old, new): t = self.files[path] assert t.count(old) == 1, (path, "entry not unique") self.files[path] = t.replace(old, new) def insert_after(self, path, old, new): self.replace(path, old, old + new) def delete(self, path, old): self.replace(path, old, "") def save(self): for p, t in self.files.items(): open(p, "w").write(t) def tidy(e): """Entry text as a fragment stores it: starts at its own line, ends with a comma.""" e = e.strip("\n") return e if e.rstrip().endswith(",") else e.rstrip() + "," def norm(e): """Entry text without comments and whitespace, and without trailing commas, for comparing.""" out, i = [], 0 while i < len(e): j = skip(e, i) if j is None: out.append(e[i]) i += 1 elif e.startswith("//", i): i = j else: out.append(e[i:j]) i = j s = re.sub(r"\s+", "", "".join(out)) return re.sub(r",(?=[)\]}])", "", s).rstrip(",") def words(e): """Identifiers an entry uses as bare names (not paths, fields or inside literals).""" out, i = [], 0 while i < len(e): j = skip(e, i) if j is None: out.append(e[i]) i += 1 else: out.append(" ") i = j return set(re.findall(r"(?