askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
changelog_check.py · 2.5 KB · 77 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """List commits since the newest tag that CHANGELOG.md's Unreleased section omits. |
| 3 | |
| 4 | Exits 1 if any are missing, 0 if the section is complete. Standard library only. |
| 5 | |
| 6 | python scripts/changelog_check.py [--repo DIR] [--changelog FILE] |
| 7 | """ |
| 8 | |
| 9 | from __future__ import annotations |
| 10 | |
| 11 | import argparse |
| 12 | import re |
| 13 | import subprocess |
| 14 | import sys |
| 15 | from pathlib import Path |
| 16 | |
| 17 | |
| 18 | def _git(repo: Path, *args: str) -> str: |
| 19 | return subprocess.run( |
| 20 | ["git", "-C", str(repo), *args], check=True, capture_output=True, text=True |
| 21 | ).stdout |
| 22 | |
| 23 | |
| 24 | def newest_tag(repo: Path) -> str | None: |
| 25 | out = _git(repo, "tag", "--merged", "HEAD", "--sort=-creatordate").split() |
| 26 | return out[0] if out else None |
| 27 | |
| 28 | |
| 29 | def unreleased_section(text: str) -> str: |
| 30 | lines, inside = [], False |
| 31 | for line in text.splitlines(): |
| 32 | if re.match(r"^##\s+", line): |
| 33 | if inside: |
| 34 | break |
| 35 | inside = bool(re.match(r"^##\s+Unreleased\b", line, re.I)) |
| 36 | continue |
| 37 | if inside: |
| 38 | lines.append(line) |
| 39 | return "\n".join(lines) |
| 40 | |
| 41 | |
| 42 | def missing(repo: Path, changelog: Path) -> list[tuple[str, str]]: |
| 43 | tag = newest_tag(repo) |
| 44 | rng = f"{tag}..HEAD" if tag else "HEAD" |
| 45 | section = unreleased_section(changelog.read_text(encoding="utf-8")) |
| 46 | found = set(re.findall(r"\b[0-9a-f]{7,40}\b", section)) |
| 47 | out = [] |
| 48 | for row in _git(repo, "log", "--format=%H %h %s", rng).splitlines(): |
| 49 | full, short, subject = row.split(" ", 2) |
| 50 | if any(full.startswith(h) for h in found): |
| 51 | continue |
| 52 | # A commit that only updates the changelog cannot cite its own hash. |
| 53 | touched = _git(repo, "show", "--format=", "--name-only", full).split() |
| 54 | if touched and all(p == changelog.name for p in touched): |
| 55 | continue |
| 56 | out.append((short, subject)) |
| 57 | return out |
| 58 | |
| 59 | |
| 60 | def main(argv: list[str] | None = None) -> int: |
| 61 | root = Path(__file__).resolve().parent.parent |
| 62 | ap = argparse.ArgumentParser(description=__doc__.splitlines()[0]) |
| 63 | ap.add_argument("--repo", type=Path, default=root) |
| 64 | ap.add_argument("--changelog", type=Path, default=None) |
| 65 | args = ap.parse_args(argv) |
| 66 | changelog = args.changelog or args.repo / "CHANGELOG.md" |
| 67 | rows = missing(args.repo, changelog) |
| 68 | for short, subject in rows: |
| 69 | print(f"{short} {subject}") |
| 70 | if rows: |
| 71 | print(f"{len(rows)} commit(s) missing from the Unreleased section", file=sys.stderr) |
| 72 | return 1 |
| 73 | return 0 |
| 74 | |
| 75 | |
| 76 | if __name__ == "__main__": |
| 77 | sys.exit(main()) |