import subprocess
import re
import sys
from pathlib import Path
def run(cmd):
try:
return subprocess.run(
cmd, capture_output=True, text=True, encoding="utf-8", check=True
).stdout.strip()
except subprocess.CalledProcessError as e:
print(f"Command failed: {' '.join(cmd)}", file=sys.stderr)
if e.stdout:
print(e.stdout, file=sys.stderr)
if e.stderr:
print(e.stderr, file=sys.stderr)
sys.exit(e.returncode)
def read_cargo_version(root_dir):
cargo_file = root_dir / "Cargo.toml"
try:
for line in cargo_file.read_text(encoding="utf-8").splitlines():
m = re.match(r'version\s*=\s*"([^"]+)"', line)
if m:
return m.group(1)
except OSError:
pass
return None
def main():
try:
sys.stdout.reconfigure(encoding="utf-8")
except (AttributeError, ValueError):
pass
root_dir = Path(__file__).resolve().parent.parent
wip_file = root_dir / "WIP.md"
if not wip_file.exists():
print("WIP.md not found. Skipping update.", file=sys.stderr)
return
commit_hash = run(["git", "rev-parse", "--short", "HEAD"])
commit_msg = run(["git", "log", "-1", "--format=%s"])
text = wip_file.read_text(encoding="utf-8")
text = re.sub(
r'### Active Branch:.*',
'### Active Branch: none (main is current)',
text,
count=1,
)
version = read_cargo_version(root_dir)
version_suffix = f" — **v{version}**" if version else ""
new_head_line = f'**main HEAD**: `{commit_hash}` — {commit_msg}{version_suffix}'
text = re.sub(
r'\*\*main HEAD\*\*:.*',
lambda _m: new_head_line,
text,
count=1,
)
wip_file.write_text(text, encoding="utf-8")
print(f"Updated WIP.md: Active Branch set to none, main HEAD updated to {new_head_line}")
if __name__ == "__main__":
main()