2
2
3
3
import os
4
4
import os .path
5
+ from collections .abc import Generator
5
6
from difflib import SequenceMatcher
6
7
from operator import itemgetter
7
8
from pathlib import Path
8
- from typing import Callable , cast
9
9
10
10
from commitizen import changelog , defaults , factory , git , out
11
11
from commitizen .changelog_formats import get_changelog_format
@@ -32,9 +32,10 @@ def __init__(self, config: BaseConfig, args):
32
32
if not git .is_git_project ():
33
33
raise NotAGitProjectError ()
34
34
35
- self .config : BaseConfig = config
36
- changelog_file_name = args .get ("file_name" ) or cast (
37
- str , self .config .settings .get ("changelog_file" )
35
+ self .config = config
36
+
37
+ changelog_file_name = args .get ("file_name" ) or self .config .settings .get (
38
+ "changelog_file"
38
39
)
39
40
if not isinstance (changelog_file_name , str ):
40
41
raise NotAllowed (
@@ -114,28 +115,28 @@ def _find_incremental_rev(self, latest_version: str, tags: list[GitTag]) -> str:
114
115
on our experience.
115
116
"""
116
117
SIMILARITY_THRESHOLD = 0.89
117
- tag_ratio = map (
118
- lambda tag : (
119
- SequenceMatcher (
118
+ scores_and_tag_names : Generator [tuple [float , str ]] = (
119
+ (
120
+ score ,
121
+ tag .name ,
122
+ )
123
+ for tag in tags
124
+ if (
125
+ score := SequenceMatcher (
120
126
None , latest_version , strip_local_version (tag .name )
121
- ).ratio (),
122
- tag ,
123
- ),
124
- tags ,
127
+ ).ratio ()
128
+ )
129
+ >= SIMILARITY_THRESHOLD
125
130
)
126
131
try :
127
- score , tag = max (tag_ratio , key = itemgetter (0 ))
132
+ _ , start_rev = max (scores_and_tag_names , key = itemgetter (0 ))
128
133
except ValueError :
129
134
raise NoRevisionError ()
130
- if score < SIMILARITY_THRESHOLD :
131
- raise NoRevisionError ()
132
- start_rev = tag .name
133
135
return start_rev
134
136
135
137
def write_changelog (
136
138
self , changelog_out : str , lines : list [str ], changelog_meta : changelog .Metadata
137
139
):
138
- changelog_hook : Callable | None = self .cz .changelog_hook
139
140
with smart_open (self .file_name , "w" , encoding = self .encoding ) as changelog_file :
140
141
partial_changelog : str | None = None
141
142
if self .incremental :
@@ -145,8 +146,8 @@ def write_changelog(
145
146
changelog_out = "" .join (new_lines )
146
147
partial_changelog = changelog_out
147
148
148
- if changelog_hook :
149
- changelog_out = changelog_hook (changelog_out , partial_changelog )
149
+ if self . cz . changelog_hook :
150
+ changelog_out = self . cz . changelog_hook (changelog_out , partial_changelog )
150
151
151
152
changelog_file .write (changelog_out )
152
153
@@ -221,14 +222,12 @@ def __call__(self):
221
222
extras .update (self .extras )
222
223
changelog_out = changelog .render_changelog (
223
224
tree , loader = self .cz .template_loader , template = self .template , ** extras
224
- )
225
- changelog_out = changelog_out .lstrip ("\n " )
225
+ ).lstrip ("\n " )
226
226
227
227
# Dry_run is executed here to avoid checking and reading the files
228
228
if self .dry_run :
229
- changelog_hook : Callable | None = self .cz .changelog_hook
230
- if changelog_hook :
231
- changelog_out = changelog_hook (changelog_out , "" )
229
+ if self .cz .changelog_hook :
230
+ changelog_out = self .cz .changelog_hook (changelog_out , "" )
232
231
out .write (changelog_out )
233
232
raise DryRunExit ()
234
233
0 commit comments