repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

chore: make publish jj-aware

a6de390084f4e2f74271675c61be5452dd7bb682

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (1)

scripts/extensions.py

Modified
277 +def jj(*args: str, check: bool = True, capture: bool = False) -> subprocess.CompletedProcess[str]:
278 + return subprocess.run(
279 + ["jj", *args],
280 + cwd=ROOT,
281 + check=check,
282 + text=True,
283 + stdout=subprocess.PIPE if capture else None,
284 + stderr=subprocess.PIPE if capture else None,
288 +def use_jj() -> bool:
289 + proc = jj("root", check=False, capture=True)
290 + return proc.returncode == 0 and Path(proc.stdout.strip()).resolve() == ROOT
277 293 def git_changes(path: Path, since: str | None) -> list[str]:
278 294 cmd = ["log", "--format=%h %s"]
279 295 if since:
504 520 return drivers
523 +def demo_installed(driver: str, targets: list[str]) -> bool:
524 + for target in targets:
525 + ext = EXT_ROOT / target
526 + if not (ext / "README.md").exists() or not (ext / "assets" / f"{driver}.gif").exists():
527 + return False
528 + return True
531 +def installed_drivers_for(exts: list[Path]) -> list[tuple[str, list[str]]]:
532 + return [(driver, targets) for driver, targets in drivers_for(exts) if demo_installed(driver, targets)]
507 535 def update_demos(exts: list[Path]) -> list[Path]:
508 - drivers = drivers_for(exts)
536 + drivers = installed_drivers_for(exts)
509 537 if not drivers:
510 538 print("OK demo none")
511 539 return []
517 545 return sorted(path for path in touched if path.exists())
548 +def jj_commit_id(rev: str) -> str:
549 + return jj("log", "-r", rev, "--no-graph", "--template", "commit_id", capture=True).stdout.strip()
552 +def release_bookmark() -> str:
553 + names = jj("bookmark", "list", capture=True).stdout
554 + for name in ("trunk", "main", "master"):
555 + if re.search(rf"^{re.escape(name)}:", names, re.M):
556 + return name
557 + return "trunk"
560 +def changed_paths() -> list[Path]:
561 + if use_jj():
562 + out = jj("diff", "--name-only", capture=True).stdout
563 + else:
564 + out = git("diff", "--name-only", capture=True).stdout + "\n" + git("diff", "--cached", "--name-only", capture=True).stdout
565 + return sorted({Path(line) for line in out.splitlines() if line.strip()})
568 +def allowed_change(path: Path, roots: list[Path]) -> bool:
569 + for root in roots:
570 + rel = root.relative_to(ROOT)
571 + if path == rel or rel in path.parents:
572 + return True
573 + return False
576 +def assert_only_release_changes(paths_to_add: list[Path]) -> None:
577 + roots = [path for path in paths_to_add if path.exists()]
578 + extra = [path for path in changed_paths() if not allowed_change(path, roots)]
579 + if extra:
580 + raise SystemExit("refusing to publish unrelated change(s): " + ", ".join(str(path) for path in extra))
520 583 def create_tag() -> str:
521 584 tag = f"v{root_version()}"
522 585 exists = git("rev-parse", "--verify", "--quiet", f"refs/tags/{tag}", check=False).returncode == 0
523 586 if exists:
524 587 print(f"OK tag {tag} exists")
526 - git("tag", tag)
589 + if use_jj():
590 + jj("git", "export")
591 + target = jj_commit_id("@-")
592 + else:
593 + target = "HEAD"
594 + git("tag", tag, target)
527 595 print(f"OK tag {tag}")
528 596 return tag
531 599 def commit_release(paths_to_add: list[Path]) -> None:
600 + assert_only_release_changes(paths_to_add)
601 + if use_jj():
602 + if jj("diff", "--quiet", check=False).returncode == 0:
603 + print("OK commit none")
604 + return
605 + jj("commit", "-m", f"release: v{root_version()}")
606 + bookmark = release_bookmark()
607 + jj("bookmark", "set", bookmark, "-r", "@-")
608 + print(f"OK commit v{root_version()} ({bookmark})")
609 + return
532 611 paths = sorted({str(path.relative_to(ROOT)) for path in paths_to_add if path.exists()})
533 612 git("add", *paths)
534 613 if git("diff", "--cached", "--quiet", "--", *paths, check=False).returncode == 0:
541 620 def push_release(tag: str) -> None:
542 - git("push")
543 - git("push", "origin", tag)
621 + if use_jj():
622 + jj("git", "push", "--bookmark", release_bookmark())
623 + git("push", "origin", tag)
624 + else:
625 + git("push")
626 + git("push", "origin", tag)
544 627 print("OK push")
645 728 return tested
647 730 if dry_run:
648 - demos = drivers_for(dirs)
731 + demos = installed_drivers_for(dirs)
649 732 print("DRY RUN publish")
650 733 print(f"version: v{root_version()}")
651 734 print("demos: " + (", ".join(name for name, _targets in demos) if demos else "none"))