|
| 1 | +#!/usr/bin/env python3 |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import argparse |
| 5 | +import datetime as dt |
| 6 | +import re |
| 7 | +import subprocess |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +TYPE_TITLES = { |
| 11 | + "feat": "Added", |
| 12 | + "fix": "Fixed", |
| 13 | + "perf": "Performance", |
| 14 | + "refactor": "Changed", |
| 15 | + "docs": "Documentation", |
| 16 | + "build": "Build", |
| 17 | + "ci": "CI", |
| 18 | + "test": "Tests", |
| 19 | + "style": "Style", |
| 20 | + "chore": "Chore", |
| 21 | +} |
| 22 | + |
| 23 | +SECTION_ORDER = [ |
| 24 | + "Breaking Changes", |
| 25 | + "Added", |
| 26 | + "Fixed", |
| 27 | + "Changed", |
| 28 | + "Performance", |
| 29 | + "Documentation", |
| 30 | + "Build", |
| 31 | + "CI", |
| 32 | + "Tests", |
| 33 | + "Style", |
| 34 | + "Chore", |
| 35 | + "Other", |
| 36 | +] |
| 37 | + |
| 38 | +SUBJECT_RE = re.compile(r"^(?P<type>[a-zA-Z]+)(\(.+\))?(?P<bang>!)?:\s*(?P<desc>.+)") |
| 39 | + |
| 40 | + |
| 41 | +def run_git(args: list[str], cwd: Path) -> str: |
| 42 | + return subprocess.check_output(["git", *args], cwd=cwd, text=True).strip() |
| 43 | + |
| 44 | + |
| 45 | +def last_tag(repo: Path) -> str | None: |
| 46 | + try: |
| 47 | + return run_git(["describe", "--tags", "--abbrev=0"], repo) |
| 48 | + except subprocess.CalledProcessError: |
| 49 | + return None |
| 50 | + |
| 51 | + |
| 52 | +def commit_log(repo: Path, base_tag: str | None) -> list[tuple[str, str]]: |
| 53 | + if base_tag: |
| 54 | + range_spec = f"{base_tag}..HEAD" |
| 55 | + else: |
| 56 | + range_spec = "HEAD" |
| 57 | + log = run_git(["log", range_spec, "--pretty=format:%s%n%b%n==END=="], repo) |
| 58 | + entries = [entry.strip() for entry in log.split("==END==") if entry.strip()] |
| 59 | + commits = [] |
| 60 | + for entry in entries: |
| 61 | + lines = entry.splitlines() |
| 62 | + subject = lines[0].strip() if lines else "" |
| 63 | + body = "\n".join(lines[1:]) if len(lines) > 1 else "" |
| 64 | + commits.append((subject, body)) |
| 65 | + return commits |
| 66 | + |
| 67 | + |
| 68 | +def normalize_subject(subject: str) -> tuple[str | None, str, bool]: |
| 69 | + if subject.startswith("Merge "): |
| 70 | + return None, "", False |
| 71 | + if subject.startswith("chore(release):"): |
| 72 | + return None, "", False |
| 73 | + match = SUBJECT_RE.match(subject) |
| 74 | + breaking = False |
| 75 | + if match: |
| 76 | + change_type = match.group("type").lower() |
| 77 | + desc = match.group("desc").strip() |
| 78 | + breaking = match.group("bang") == "!" |
| 79 | + return change_type, desc, breaking |
| 80 | + return None, subject.strip(), False |
| 81 | + |
| 82 | + |
| 83 | +def build_sections(commits: list[tuple[str, str]]) -> dict[str, list[str]]: |
| 84 | + sections: dict[str, list[str]] = {} |
| 85 | + for subject, body in commits: |
| 86 | + change_type, desc, breaking = normalize_subject(subject) |
| 87 | + if not desc: |
| 88 | + continue |
| 89 | + if "BREAKING CHANGE" in body: |
| 90 | + breaking = True |
| 91 | + if breaking: |
| 92 | + sections.setdefault("Breaking Changes", []).append(desc) |
| 93 | + continue |
| 94 | + title = TYPE_TITLES.get(change_type, "Other") |
| 95 | + sections.setdefault(title, []).append(desc) |
| 96 | + return sections |
| 97 | + |
| 98 | + |
| 99 | +def load_changelog(path: Path) -> list[str]: |
| 100 | + if not path.exists(): |
| 101 | + return [ |
| 102 | + "# Changelog", |
| 103 | + "", |
| 104 | + "All notable changes to PakFu are documented here.", |
| 105 | + "", |
| 106 | + ] |
| 107 | + return path.read_text(encoding="utf-8").splitlines() |
| 108 | + |
| 109 | + |
| 110 | +def write_changelog(path: Path, lines: list[str]) -> None: |
| 111 | + text = "\n".join(lines).rstrip() + "\n" |
| 112 | + path.write_text(text, encoding="utf-8") |
| 113 | + |
| 114 | + |
| 115 | +def insert_entry(lines: list[str], entry: list[str]) -> list[str]: |
| 116 | + for line in lines: |
| 117 | + if line.startswith(entry[0]): |
| 118 | + return lines |
| 119 | + insert_at = len(lines) |
| 120 | + for idx, line in enumerate(lines): |
| 121 | + if line.startswith("## ["): |
| 122 | + insert_at = idx |
| 123 | + break |
| 124 | + return lines[:insert_at] + entry + [""] + lines[insert_at:] |
| 125 | + |
| 126 | + |
| 127 | +def main() -> int: |
| 128 | + parser = argparse.ArgumentParser(description="Update CHANGELOG.md from git history.") |
| 129 | + parser.add_argument( |
| 130 | + "--version", |
| 131 | + help="Version to add (defaults to VERSION file).", |
| 132 | + ) |
| 133 | + parser.add_argument( |
| 134 | + "--date", |
| 135 | + help="Release date (YYYY-MM-DD). Defaults to UTC today.", |
| 136 | + ) |
| 137 | + args = parser.parse_args() |
| 138 | + |
| 139 | + repo = Path(__file__).resolve().parent.parent |
| 140 | + version_file = repo / "VERSION" |
| 141 | + version = args.version or version_file.read_text(encoding="utf-8").strip() |
| 142 | + version = version.lstrip("vV") |
| 143 | + date = args.date or dt.datetime.utcnow().date().isoformat() |
| 144 | + |
| 145 | + base_tag = last_tag(repo) |
| 146 | + commits = commit_log(repo, base_tag) |
| 147 | + sections = build_sections(commits) |
| 148 | + |
| 149 | + entry = [f"## [{version}] - {date}"] |
| 150 | + if not sections: |
| 151 | + entry += ["### Changed", "- No user-facing changes."] |
| 152 | + else: |
| 153 | + for title in SECTION_ORDER: |
| 154 | + items = sections.get(title, []) |
| 155 | + if not items: |
| 156 | + continue |
| 157 | + entry.append(f"### {title}") |
| 158 | + for item in items: |
| 159 | + entry.append(f"- {item}") |
| 160 | + |
| 161 | + changelog_path = repo / "CHANGELOG.md" |
| 162 | + lines = load_changelog(changelog_path) |
| 163 | + updated = insert_entry(lines, entry) |
| 164 | + write_changelog(changelog_path, updated) |
| 165 | + return 0 |
| 166 | + |
| 167 | + |
| 168 | +if __name__ == "__main__": |
| 169 | + raise SystemExit(main()) |
0 commit comments