repomatic.changelog module

Changelog parsing, updating, and release lifecycle management.

This module is the single source of truth for parsing and rewriting the changelog. It handles two phases of the release cycle:

Post-release (unfreeze) — Changelog.update():

Decomposes the latest release section via Changelog.decompose_version(), transforms the elements into an unreleased entry (date → unreleased, comparison URL → ...main, body → development warning), renders via the release-notes template, and prepends the result to the changelog.

Release preparation (freeze) — Changelog.freeze():

Decomposes the current unreleased section, sets the release date, freezes the comparison URL to ...vX.Y.Z, clears the development warning, renders via the release-notes template, and replaces the section in place.

Both operations follow the same decompose → modify → render → replace pattern, with the release-notes.md template as the single source of truth for section layout. Both are idempotent: re-running them produces the same result. This is critical for CI workflows that may be retried.

Note

This is a custom implementation. After evaluating all major alternatives — towncrier, commitizen, python-semantic-release, generate-changelog, release-please, scriv, and git-changelog (see issue #94) — none were found to cover even half of the requirements.

Why not use an off-the-shelf tool?

Existing tools fall into two camps, neither of which fits:

Commit-driven tools (python-semantic-release, commitizen, generate-changelog, release-please) auto-generate changelogs from Git history. This conflicts with the project’s philosophy of hand-curated changelogs: entries are written for users, consolidated by hand, and summarize only changes worth knowing about. Auto-generated logs from developer commits are too noisy and don’t account for back-and-forth during development.

Fragment-driven tools (towncrier, scriv) avoid merge conflicts by using per-change files, but handle none of the release orchestration: comparison URL management, GFM warning lifecycle, workflow action reference freezing, or the two-commit freeze/unfreeze release cycle. The multiplication of files across the repo adds complexity, and there is no 1:1 mapping between fragments and changelog entries.

Specific gaps across all evaluated tools:

  • No comparison URL management. None generate GitHub v1.0.0...v1.1.0 diff links, or update them from ...main to ...vX.Y.Z at release time.

  • No unreleased section lifecycle. None manage the [!WARNING] GFM alert warning that the version is under active development, inserting it post-release and removing it at release time.

  • No workflow action reference freezing. None handle the freeze/unfreeze cycle for @main ↔ @vX.Y.Z references in workflow files.

  • No two-commit release workflow. None support the freeze commit ([changelog] Release vX.Y.Z) plus unfreeze commit ([changelog] Post-release bump) pattern that changelog.yaml uses.

  • No citation file integration. None update citation.cff release dates.

  • No version bump eligibility checks. None prevent double version increments by comparing the current version against the latest Git tag with a commit-message fallback.

The custom implementation in this module is tightly integrated with the release workflow. Adopting any external tool would require keeping most of this code and adding a new dependency — more complexity, not less.

repomatic.changelog.resolved_changelog_path(config)[source]

Absolute path of the configured changelog.

The one derivation of [tool.repomatic] changelog.location into a filesystem path, so every consumer (the CLI commands, the release freeze, the metadata singleton, the setup guide) resolves the same file the same way.

Return type:

Path

repomatic.changelog.load_changelog_repo(config)[source]

Load the configured changelog and read the repository URL out of it.

The shared preamble of the two release-notes syncers, which both need the changelog’s path (to read sections from) and the repository it belongs to (to address the GitHub API with). The URL comes from the changelog’s own comparison links rather than from the git remote, so a project whose changelog points elsewhere stays authoritative.

Parameters:

config (Config) – The resolved [tool.repomatic] configuration.

Return type:

tuple[Path, str] | None

Returns:

(changelog_path, repo_url), or None after logging why neither could be determined (no changelog on disk, or no comparison link in it).

repomatic.changelog.AVAILABLE_VERB = 'is available on'

Verb phrase for versions present on a platform.

repomatic.changelog.CHANGELOG_HEADER = '# Changelog\n'

Default changelog header for empty changelogs.

repomatic.changelog.FIRST_AVAILABLE_VERB = 'is the *first version* available on'

Verb phrase for the inaugural release on a platform.

repomatic.changelog.GITHUB_LABEL = '🐙 GitHub'

Display label for GitHub releases in admonitions.

repomatic.changelog.GITHUB_RELEASE_URL = '{repo_url}/releases/tag/v{version}'

GitHub release page URL for a specific version.

repomatic.changelog.NOT_AVAILABLE_VERB = 'is **not available** on'

Verb phrase for versions missing from a platform.

repomatic.changelog.PYPI_LABEL = '🐍 PyPI'

Display label for PyPI in admonitions.

repomatic.changelog.SECTION_START = '##'

Markdown heading level for changelog version sections.

repomatic.changelog.YANKED_DEDUP_MARKER = 'yanked from PyPI'

Dedup marker for the yanked admonition to prevent duplicate insertion.

repomatic.changelog.RELEASE_VERSION_TOKEN = '\\d+\\.\\d+\\.\\d+'

Regex fragment for a final release version, like 1.2.3.

The strict half of the version vocabulary: it deliberately rejects the .devN suffix VERSION_TOKEN accepts, because a changelog documents only final releases. Anything keyed off a published version (a dated heading, a comparison URL) uses this one.

repomatic.changelog.VERSION_TOKEN = '\\d+\\.\\d+\\.\\d+(?:\\.\\w+)?'

Regex fragment for any version a ## heading may carry.

Widens RELEASE_VERSION_TOKEN with the trailing .devN a development section carries between releases. Anything enumerating or locating headings uses this one, so an unreleased section is never invisible to a scan that has to account for it.

repomatic.changelog.GFM_ALERT_RE = re.compile('(?:^>.*$\\n?)+', re.MULTILINE)

A GFM alert block: consecutive lines starting with >.

The shape every changelog admonition takes (the development warning, the PyPI availability note, a yanked caution), matched wherever a section’s blocks are inventoried.

repomatic.changelog.VERSION_COMPARE_PATTERN = re.compile('v\\d+\\.\\d+\\.\\d+\\.\\.\\.v\\d+\\.\\d+\\.\\d+')

Pattern matching GitHub comparison URLs like v1.0.0...v1.0.1.

repomatic.changelog.RELEASED_VERSION_PATTERN = re.compile('^##\\s*\\[`?(?P<version>\\d+\\.\\d+\\.\\d+)`?\\s+\\((?P<date>\\d{4}-\\d{2}-\\d{2})\\)\\]', re.MULTILINE)

Pattern matching released version headings with dates.

Captures version and date from headings like ## [`5.9.1` (2026-02-14)](...). Skips unreleased versions which use (unreleased) instead of a date. Backticks around the version are optional.

repomatic.changelog.HEADING_PARTS_PATTERN = re.compile('^##\\s*\\[`?(?P<version>\\d+\\.\\d+\\.\\d+(?:\\.\\w+)?)`?\\s+\\((?P<date>[^)]+)\\)\\]\\((?P<url>[^)]+)\\)', re.MULTILINE)

Pattern extracting version, date/label, and URL from a heading.

Used by Changelog.decompose_version() to populate the heading fields of VersionElements.

repomatic.changelog.TAG_PREFIXED_HEADING_RE = re.compile('^(?P<prefix>##\\s*\\[`?)v(?P<version>\\d+\\.\\d+\\.\\d+(?:\\.\\w+)?)\\b', re.MULTILINE)

A ## heading naming its version with the v of a git tag.

No scan reads such a heading: VERSION_TOKEN opens on a digit, so the section stays invisible to every check, and its release goes unverified. The prefix belongs to the tag; a heading names the package version, bare.

class repomatic.changelog.VersionElements(compare_url='', date='', version='', availability_admonition='', changes='', development_warning='', editorial_admonition='', yanked_admonition='')[source]

Bases: object

Discrete building blocks of a changelog version section.

Each field is a pre-formatted markdown block (or empty string when absent). Templates compose these elements into the final section layout. Empty variables produce empty strings, which render_template’s 3+ newline collapsing handles gracefully.

Heading fields (compare_url, date, version) are populated by Changelog.decompose_version() and used by the release-notes template to render the ## heading line. Body fields are unchanged.

compare_url: str = ''

GitHub comparison URL from the heading (e.g. repo/compare/vA...vB).

date: str = ''

Release date or unreleased label from the heading.

version: str = ''

Version string extracted from the heading (e.g. 1.2.3).

availability_admonition: str = ''

[!NOTE] or [!WARNING] block for platform availability.

changes: str = ''

Hand-written changelog entries (bullet points, prose).

development_warning: str = ''

[!WARNING] block for unreleased versions under active development.

editorial_admonition: str = ''

Hand-written GFM alert blocks not matching auto-generated patterns.

Multiple blocks are joined with double newlines.

yanked_admonition: str = ''

[!CAUTION] block for releases yanked from PyPI.

property has_body: bool

Whether the section carries anything below its heading.

property bullets: list[str]

The top-level entries of changes, one string per - bullet.

Each entry holds its marker line plus the wrapped continuation lines and indented sub-bullets under it, joined with newlines. Blank lines and prose outside a bullet are dropped.

property tag_range: str

The tag range the comparison URL names, like v1.1.0...v2.0.0.

Empty when the heading carries no comparison URL.

class repomatic.changelog.Changelog(initial_changelog=None, current_version=None)[source]

Bases: object

Helpers to manipulate changelog files written in Markdown.

Every lookup of a section goes through an index of the sections, built in one scan of content and dropped whenever the content changes. The lint loop reads each section several times: a scan of the whole changelog per read would make it quadratic in the number of releases.

property content: str

The Markdown source of the changelog.

update(default_branch='main')[source]

Add a new unreleased entry at the top of the changelog.

Decomposes the current version section, transforms it into an unreleased entry (date set to unreleased, comparison URL retargeted to the default branch, body replaced with the development warning), and prepends it to the changelog.

Idempotent: the content stays unchanged when an unreleased entry already exists, and when there is no current version or no section to clone.

Parameters:

default_branch (str) – Branch name for the comparison URL. Must match what freeze() is later given, since the two halves of a release cycle retarget the same URL in opposite directions: a mismatch leaves the released section pointing at a branch that does not exist.

Return type:

bool

Returns:

True if an entry was added to content.

freeze(release_date=None, default_branch='main')[source]

Freeze the current unreleased section for release.

Decomposes the current version section, sets the release date, freezes the comparison URL to the release tag, clears the development warning, and re-renders via the release-notes template.

Returns False for three different situations, only one of which is benign: an already-frozen section (idempotent no-op), no version to freeze, and a version whose section is missing. The last one is what a release would otherwise ship an (unreleased) heading over, so it is logged as a warning rather than left to look like the no-op.

Parameters:
  • release_date (str | None) – Date in YYYY-MM-DD format. Defaults to today (UTC).

  • default_branch (str) – Branch name for comparison URL. Must match what update() used to write it, per that method’s note.

Return type:

bool

Returns:

True if the content was modified.

classmethod freeze_file(path, version, release_date=None, default_branch='main')[source]

Freeze a changelog file in place.

Reads the file, applies all freeze operations via freeze(), and writes the result back.

Parameters:
  • path (Path) – Path to the changelog file.

  • version (str) – Current version string.

  • release_date (str | None) – Date in YYYY-MM-DD format. Defaults to today (UTC).

  • default_branch (str) – Branch name for comparison URL.

Return type:

bool

Returns:

True if the file was modified.

extract_repo_url()[source]

Extract the repository URL from changelog comparison links.

Parses the first ## `... <<repo_url>/compare/...>`_ heading and returns the base repository URL (e.g. https://github.com/user/repo).

Return type:

str

Returns:

The repository URL, or empty string if not found.

extract_all_releases()[source]

Extract all released versions and their dates from the changelog.

Scans for headings matching ## `X.Y.Z (YYYY-MM-DD) <...>`_. Unreleased versions (with (unreleased)) are skipped.

Return type:

list[tuple[str, str]]

Returns:

List of (version, date) tuples ordered as they appear in the changelog (newest first).

is_first_release(version)[source]

Whether no released section of the changelog predates version.

Parameters:

version (str) – Version string (like 1.2.3).

Return type:

bool

Returns:

False when version does not parse, or when an older version carries a dated section.

extract_all_version_headings()[source]

Extract all version strings from ## headings.

Includes both released and unreleased versions, so the caller can avoid false-positive orphan detection for the current development version.

Return type:

set[str]

Returns:

Set of version strings found in headings.

tag_prefixed_versions()[source]

The versions of the ## headings written with a v tag prefix.

Return type:

list[str]

Returns:

The bare versions, in document order.

strip_tag_prefixes()[source]

Drop the v tag prefix off every ## heading carrying one.

The comparison URL of the heading keeps its v: that one names a tag.

Return type:

int

Returns:

The number of headings rewritten.

insert_version_section(version, date, repo_url, all_versions)[source]

Insert a placeholder section for a missing version.

The section is placed at the correct position in descending version order. The comparison URL points from the next-lower version to this one. After insertion, the next-higher version’s comparison URL base is updated to reference this version, keeping the timeline coherent.

Idempotent: returns False if the version heading already exists.

Parameters:
  • version (str) – Version string (like 1.2.3).

  • date (str) – Release date in YYYY-MM-DD format.

  • repo_url (str) – Repository URL for comparison links.

  • all_versions (list[str]) – All known versions, in any order. A version that does not parse as PEP 440 takes no part in the ordering.

Return type:

bool

Returns:

True if the content was modified.

update_comparison_base(version, new_base)[source]

Replace the base version in a version heading’s comparison URL.

Changes compare/vOLD...vX.Y.Z to compare/vNEW...vX.Y.Z in the heading for the given version.

Parameters:
  • version (str) – The version whose heading to update.

  • new_base (str) – New base version (without v prefix).

Return type:

bool

Returns:

True if the content was modified.

decompose_version(version)[source]

Decompose a version section into discrete elements.

Parses both the heading (version, date, URL) and the body (admonitions, changes).

Classifies each GFM alert block (consecutive > lines) as one of the auto-generated element types. Everything not classified as auto-generated is preserved as changes.

The decomposition is cached until content changes. Each call returns its own copy, which the caller is free to modify.

Parameters:

version (str) – Version string (like 1.2.3).

Return type:

VersionElements

Returns:

A VersionElements with each field populated.

replace_section(version, new_section)[source]

Replace the entire section (heading + body) for a version.

Locates the version heading and replaces everything up to the next ## heading (or EOF) with new_section.

Parameters:
  • version (str) – Version string (like 1.2.3).

  • new_section (str) – New section content including heading.

Return type:

bool

Returns:

True if the content was modified.

repomatic.changelog.availability_admonition(version, *, pypi_url='', github_url='', first_on_pypi=False, first_on_github=False, missing_pypi=False, missing_github=False)[source]

Render the availability admonitions of a release section.

The one renderer of VersionElements.availability_admonition. Two writers share it, so a release reads the same in both: lint-changelog –fix writes it into the changelog, and the draft GitHub release body bakes it in before that fix runs.

A > [!NOTE] links the platforms carrying the version. It uses the first version wording when the version is the first one on every platform it links. A > [!WARNING] names the platforms missing the version. Both blocks appear when a version is on one platform and missing from the other.

Parameters:
  • version (str) – Version string (like 1.2.3).

  • pypi_url (str) – PyPI page of the version, empty to leave PyPI out of the note.

  • github_url (str) – GitHub release page of the version, empty to leave GitHub out of the note.

  • first_on_pypi (bool) – Whether the version is the first one on PyPI.

  • first_on_github (bool) – Whether the version is the first GitHub release.

  • missing_pypi (bool) – Whether the version is missing from PyPI.

  • missing_github (bool) – Whether the version is missing from GitHub.

Return type:

str

Returns:

The admonition blocks, separated by a blank line. Empty when the version is neither linked nor missing anywhere.