Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 22 additions & 23 deletions commitizen/commands/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import os
import os.path
from collections.abc import Generator
from difflib import SequenceMatcher
from operator import itemgetter
from pathlib import Path
from typing import Callable, cast

from commitizen import changelog, defaults, factory, git, out
from commitizen.changelog_formats import get_changelog_format
Expand All @@ -32,9 +32,10 @@ def __init__(self, config: BaseConfig, args):
if not git.is_git_project():
raise NotAGitProjectError()

self.config: BaseConfig = config
changelog_file_name = args.get("file_name") or cast(
str, self.config.settings.get("changelog_file")
self.config = config

changelog_file_name = args.get("file_name") or self.config.settings.get(
"changelog_file"
)
if not isinstance(changelog_file_name, str):
raise NotAllowed(
Expand Down Expand Up @@ -114,28 +115,28 @@ def _find_incremental_rev(self, latest_version: str, tags: list[GitTag]) -> str:
on our experience.
"""
SIMILARITY_THRESHOLD = 0.89
tag_ratio = map(
lambda tag: (
SequenceMatcher(
scores_and_tag_names: Generator[tuple[float, str]] = (
(
score,
tag.name,
)
for tag in tags
if (
score := SequenceMatcher(
None, latest_version, strip_local_version(tag.name)
).ratio(),
tag,
),
tags,
).ratio()
)
>= SIMILARITY_THRESHOLD
)
try:
score, tag = max(tag_ratio, key=itemgetter(0))
_, start_rev = max(scores_and_tag_names, key=itemgetter(0))
except ValueError:
raise NoRevisionError()
if score < SIMILARITY_THRESHOLD:
raise NoRevisionError()
start_rev = tag.name
return start_rev

def write_changelog(
self, changelog_out: str, lines: list[str], changelog_meta: changelog.Metadata
):
changelog_hook: Callable | None = self.cz.changelog_hook
with smart_open(self.file_name, "w", encoding=self.encoding) as changelog_file:
partial_changelog: str | None = None
if self.incremental:
Expand All @@ -145,8 +146,8 @@ def write_changelog(
changelog_out = "".join(new_lines)
partial_changelog = changelog_out

if changelog_hook:
changelog_out = changelog_hook(changelog_out, partial_changelog)
if self.cz.changelog_hook:
changelog_out = self.cz.changelog_hook(changelog_out, partial_changelog)

changelog_file.write(changelog_out)

Expand Down Expand Up @@ -221,14 +222,12 @@ def __call__(self):
extras.update(self.extras)
changelog_out = changelog.render_changelog(
tree, loader=self.cz.template_loader, template=self.template, **extras
)
changelog_out = changelog_out.lstrip("\n")
).lstrip("\n")

# Dry_run is executed here to avoid checking and reading the files
if self.dry_run:
changelog_hook: Callable | None = self.cz.changelog_hook
if changelog_hook:
changelog_out = changelog_hook(changelog_out, "")
if self.cz.changelog_hook:
changelog_out = self.cz.changelog_hook(changelog_out, "")
out.write(changelog_out)
raise DryRunExit()

Expand Down