Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
58 lines
2.0 KiB
Python
58 lines
2.0 KiB
Python
"""After `git mv` of Rust files, rewrite the repository-relative paths that tools and gates quote (STRA-338 stage 10).
|
|
|
|
Usage: python scripts/dev/update_path_refs.py REV [--apply]
|
|
|
|
Reads the renames of `.rs` files from `git diff -M --name-status REV` (REV is the commit before the moves) and replaces
|
|
each old path by the new one in `scripts/`, `.github/` and the JSON files at the repository root (the limits gate keys
|
|
its allowlist and baselines by file path). Docs are not touched: a document that cites a path keeps the path of its time.
|
|
Offline refactoring aid, not part of the product.
|
|
"""
|
|
import pathlib
|
|
import subprocess
|
|
import sys
|
|
|
|
ROOT = pathlib.Path(__file__).resolve().parents[2]
|
|
EXTENSIONS = {".py", ".sh", ".json", ".yml", ".yaml", ".toml", ".ps1"}
|
|
|
|
|
|
def renames(rev):
|
|
out = subprocess.run(["git", "diff", "-M", "--name-status", rev], cwd=ROOT, capture_output=True, text=True).stdout
|
|
pairs = {}
|
|
for line in out.splitlines():
|
|
parts = line.split("\t")
|
|
if parts[0].startswith("R") and parts[1].endswith(".rs"):
|
|
pairs[parts[1]] = parts[2]
|
|
return pairs
|
|
|
|
|
|
def targets():
|
|
roots = [ROOT / "scripts", ROOT / ".github"]
|
|
files = [p for r in roots if r.exists() for p in r.rglob("*") if p.is_file() and p.suffix in EXTENSIONS]
|
|
return files + [p for p in ROOT.glob("*.json")]
|
|
|
|
|
|
def main(rev, apply):
|
|
pairs = renames(rev)
|
|
ordered = sorted(pairs, key=len, reverse=True)
|
|
touched = 0
|
|
for path in targets():
|
|
data = path.read_bytes()
|
|
try:
|
|
text = data.decode("utf-8")
|
|
except UnicodeDecodeError:
|
|
continue
|
|
new = text
|
|
for old in ordered:
|
|
if old in new:
|
|
new = new.replace(old, pairs[old])
|
|
if new != text:
|
|
touched += 1
|
|
print("updated", path.relative_to(ROOT).as_posix())
|
|
if apply:
|
|
path.write_bytes(new.encode("utf-8"))
|
|
print(f"{len(pairs)} renamed Rust files; {touched} files quote a moved path")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main(sys.argv[1], "--apply" in sys.argv)
|