Skip to content

Commit 9a2622f

Browse files
authored
Merge branch 'v4-9-0-test' into feat/Add-config-option-for-line-length-warning
2 parents d639a86 + a69d441 commit 9a2622f

File tree

13 files changed

+152
-159
lines changed

13 files changed

+152
-159
lines changed

.pre-commit-config.yaml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ repos:
2424
- id: debug-statements
2525
- id: no-commit-to-branch
2626
- id: check-merge-conflict
27-
- id: check-toml
27+
- id: check-toml # TOML linter (syntax checker)
2828
- id: check-yaml
2929
args: [ '--unsafe' ] # for mkdocs.yml
3030
- id: detect-private-key
@@ -55,17 +55,22 @@ repos:
5555
stages:
5656
- post-commit
5757

58+
- repo: https://github.com/ComPWA/taplo-pre-commit
59+
rev: v0.9.3
60+
hooks:
61+
- id: taplo-format
62+
5863
- repo: local
5964
hooks:
6065
- id: format
61-
name: Format
66+
name: Format Python code via Poetry
6267
language: system
6368
pass_filenames: false
6469
entry: poetry format
6570
types: [ python ]
6671

6772
- id: linter and test
68-
name: Linters
73+
name: Linters via Poetry
6974
language: system
7075
pass_filenames: false
7176
entry: poetry lint

.taplo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
include = ["pyproject.toml", ".taplo.toml"]
2+
3+
[formatting]
4+
indent_string = " "

commitizen/changelog.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
import re
3131
from collections import OrderedDict, defaultdict
32-
from collections.abc import Generator, Iterable, Mapping, Sequence
32+
from collections.abc import Generator, Iterable, Mapping, MutableMapping, Sequence
3333
from dataclasses import dataclass
3434
from datetime import date
3535
from typing import TYPE_CHECKING, Any
@@ -167,8 +167,8 @@ def process_commit_message(
167167
hook: MessageBuilderHook | None,
168168
parsed: re.Match[str],
169169
commit: GitCommit,
170-
changes: dict[str | None, list],
171-
change_type_map: dict[str, str] | None = None,
170+
ref_changes: MutableMapping[str | None, list],
171+
change_type_map: Mapping[str, str] | None = None,
172172
) -> None:
173173
message: dict[str, Any] = {
174174
"sha1": commit.rev,
@@ -178,13 +178,16 @@ def process_commit_message(
178178
**parsed.groupdict(),
179179
}
180180

181-
if processed := hook(message, commit) if hook else message:
182-
messages = [processed] if isinstance(processed, dict) else processed
183-
for msg in messages:
184-
change_type = msg.pop("change_type", None)
185-
if change_type_map:
186-
change_type = change_type_map.get(change_type, change_type)
187-
changes[change_type].append(msg)
181+
processed_msg = hook(message, commit) if hook else message
182+
if not processed_msg:
183+
return
184+
185+
messages = [processed_msg] if isinstance(processed_msg, dict) else processed_msg
186+
for msg in messages:
187+
change_type = msg.pop("change_type", None)
188+
if change_type_map:
189+
change_type = change_type_map.get(change_type, change_type)
190+
ref_changes[change_type].append(msg)
188191

189192

190193
def generate_ordered_changelog_tree(
@@ -251,6 +254,7 @@ def incremental_build(
251254
unreleased_start = metadata.unreleased_start
252255
unreleased_end = metadata.unreleased_end
253256
latest_version_position = metadata.latest_version_position
257+
254258
skip = False
255259
output_lines: list[str] = []
256260
for index, line in enumerate(lines):
@@ -260,9 +264,7 @@ def incremental_build(
260264
skip = False
261265
if (
262266
latest_version_position is None
263-
or isinstance(latest_version_position, int)
264-
and isinstance(unreleased_end, int)
265-
and latest_version_position > unreleased_end
267+
or latest_version_position > unreleased_end
266268
):
267269
continue
268270

@@ -271,13 +273,15 @@ def incremental_build(
271273

272274
if index == latest_version_position:
273275
output_lines.extend([new_content, "\n"])
274-
275276
output_lines.append(line)
276-
if not isinstance(latest_version_position, int):
277-
if output_lines and output_lines[-1].strip():
278-
# Ensure at least one blank line between existing and new content.
279-
output_lines.append("\n")
280-
output_lines.append(new_content)
277+
278+
if latest_version_position is not None:
279+
return output_lines
280+
281+
if output_lines and output_lines[-1].strip():
282+
# Ensure at least one blank line between existing and new content.
283+
output_lines.append("\n")
284+
output_lines.append(new_content)
281285
return output_lines
282286

283287

commitizen/cli.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,13 @@ def __call__(
5252
) -> None:
5353
if not isinstance(values, str):
5454
return
55-
if "=" not in values:
55+
56+
key, sep, value = values.partition("=")
57+
if not key or not sep:
5658
raise InvalidCommandArgumentError(
5759
f"Option {option_string} expect a key=value format"
5860
)
5961
kwargs = getattr(namespace, self.dest, None) or {}
60-
key, value = values.split("=", 1)
61-
if not key:
62-
raise InvalidCommandArgumentError(
63-
f"Option {option_string} expect a key=value format"
64-
)
6562
kwargs[key] = value.strip("'\"")
6663
setattr(namespace, self.dest, kwargs)
6764

commitizen/commands/bump.py

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
NoPatternMapError,
2424
NotAGitProjectError,
2525
NotAllowed,
26-
NoVersionSpecifiedError,
2726
)
2827
from commitizen.providers import get_provider
2928
from commitizen.tags import TagRules
@@ -163,11 +162,7 @@ def _find_increment(self, commits: list[git.GitCommit]) -> Increment | None:
163162
def __call__(self) -> None:
164163
"""Steps executed to bump."""
165164
provider = get_provider(self.config)
166-
167-
try:
168-
current_version = self.scheme(provider.get_version())
169-
except TypeError:
170-
raise NoVersionSpecifiedError()
165+
current_version = self.scheme(provider.get_version())
171166

172167
increment = self.arguments["increment"]
173168
prerelease = self.arguments["prerelease"]
@@ -177,36 +172,22 @@ def __call__(self) -> None:
177172
build_metadata = self.arguments["build_metadata"]
178173
get_next = self.arguments["get_next"]
179174
allow_no_commit = self.arguments["allow_no_commit"]
175+
major_version_zero = self.arguments["major_version_zero"]
180176

181177
if manual_version:
182-
if increment:
183-
raise NotAllowed("--increment cannot be combined with MANUAL_VERSION")
184-
185-
if prerelease:
186-
raise NotAllowed("--prerelease cannot be combined with MANUAL_VERSION")
187-
188-
if devrelease is not None:
189-
raise NotAllowed("--devrelease cannot be combined with MANUAL_VERSION")
190-
191-
if is_local_version:
192-
raise NotAllowed(
193-
"--local-version cannot be combined with MANUAL_VERSION"
194-
)
195-
196-
if build_metadata:
197-
raise NotAllowed(
198-
"--build-metadata cannot be combined with MANUAL_VERSION"
199-
)
200-
201-
if self.bump_settings["major_version_zero"]:
202-
raise NotAllowed(
203-
"--major-version-zero cannot be combined with MANUAL_VERSION"
204-
)
205-
206-
if get_next:
207-
raise NotAllowed("--get-next cannot be combined with MANUAL_VERSION")
208-
209-
if self.bump_settings["major_version_zero"] and current_version.release[0]:
178+
for val, option in (
179+
(increment, "--increment"),
180+
(prerelease, "--prerelease"),
181+
(devrelease is not None, "--devrelease"),
182+
(is_local_version, "--local-version"),
183+
(build_metadata, "--build-metadata"),
184+
(major_version_zero, "--major-version-zero"),
185+
(get_next, "--get-next"),
186+
):
187+
if val:
188+
raise NotAllowed(f"{option} cannot be combined with MANUAL_VERSION")
189+
190+
if major_version_zero and current_version.release[0]:
210191
raise NotAllowed(
211192
f"--major-version-zero is meaningless for current version {current_version}"
212193
)
@@ -215,11 +196,13 @@ def __call__(self) -> None:
215196
raise NotAllowed("--local-version cannot be combined with --build-metadata")
216197

217198
if get_next:
218-
# if trying to use --get-next, we should not allow --changelog or --changelog-to-stdout
219-
if self.changelog_flag or self.changelog_to_stdout:
220-
raise NotAllowed(
221-
"--changelog or --changelog-to-stdout is not allowed with --get-next"
222-
)
199+
for value, option in (
200+
(self.changelog_flag, "--changelog"),
201+
(self.changelog_to_stdout, "--changelog-to-stdout"),
202+
):
203+
if value:
204+
raise NotAllowed(f"{option} cannot be combined with --get-next")
205+
223206
# --get-next is a special case, taking precedence over config for 'update_changelog_on_bump'
224207
self.changelog_config = False
225208
# Setting dry_run to prevent any unwanted changes to the repo or files

commitizen/commands/changelog.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from commitizen import changelog, defaults, factory, git, out
1212
from commitizen.changelog_formats import get_changelog_format
1313
from commitizen.config import BaseConfig
14-
from commitizen.cz.base import ChangelogReleaseHook, MessageBuilderHook
1514
from commitizen.cz.utils import strip_local_version
1615
from commitizen.exceptions import (
1716
DryRunExit,
@@ -174,28 +173,23 @@ def _write_changelog(
174173

175174
changelog_file.write(changelog_out)
176175

177-
def _export_template(self) -> None:
178-
tpl = changelog.get_changelog_template(self.cz.template_loader, self.template)
179-
# TODO: fix the following type ignores
180-
src = Path(tpl.filename) # type: ignore[arg-type]
181-
Path(self.export_template_to).write_text(src.read_text()) # type: ignore[arg-type]
176+
def _export_template(self, dist: str) -> None:
177+
filename = changelog.get_changelog_template(
178+
self.cz.template_loader, self.template
179+
).filename
180+
if filename is None:
181+
raise NotAllowed("Template filename is not set")
182+
183+
text = Path(filename).read_text()
184+
Path(dist).write_text(text)
182185

183186
def __call__(self) -> None:
184187
commit_parser = self.cz.commit_parser
185188
changelog_pattern = self.cz.changelog_pattern
186189
start_rev = self.start_rev
187-
unreleased_version = self.unreleased_version
188-
changelog_meta = changelog.Metadata()
189-
change_type_map: dict[str, str] | None = self.change_type_map
190-
changelog_message_builder_hook: MessageBuilderHook | None = (
191-
self.cz.changelog_message_builder_hook
192-
)
193-
changelog_release_hook: ChangelogReleaseHook | None = (
194-
self.cz.changelog_release_hook
195-
)
196190

197191
if self.export_template_to:
198-
return self._export_template()
192+
return self._export_template(self.export_template_to)
199193

200194
if not changelog_pattern or not commit_parser:
201195
raise NoPatternMapError(
@@ -209,45 +203,53 @@ def __call__(self) -> None:
209203
assert self.file_name
210204

211205
tags = self.tag_rules.get_version_tags(git.get_tags(), warn=True)
212-
end_rev = ""
206+
changelog_meta = changelog.Metadata()
213207
if self.incremental:
214208
changelog_meta = self.changelog_format.get_metadata(self.file_name)
215209
if changelog_meta.latest_version:
216210
start_rev = self._find_incremental_rev(
217211
strip_local_version(changelog_meta.latest_version_tag or ""), tags
218212
)
213+
214+
end_rev = ""
219215
if self.rev_range:
220216
start_rev, end_rev = changelog.get_oldest_and_newest_rev(
221217
tags,
222218
self.rev_range,
223219
self.tag_rules,
224220
)
221+
225222
commits = git.get_commits(start=start_rev, end=end_rev, args="--topo-order")
226223
if not commits and (
227224
self.current_version is None or not self.current_version.is_prerelease
228225
):
229226
raise NoCommitsFoundError("No commits found")
227+
230228
tree = changelog.generate_tree_from_commits(
231229
commits,
232230
tags,
233231
commit_parser,
234232
changelog_pattern,
235-
unreleased_version,
236-
change_type_map=change_type_map,
237-
changelog_message_builder_hook=changelog_message_builder_hook,
238-
changelog_release_hook=changelog_release_hook,
233+
self.unreleased_version,
234+
change_type_map=self.change_type_map,
235+
changelog_message_builder_hook=self.cz.changelog_message_builder_hook,
236+
changelog_release_hook=self.cz.changelog_release_hook,
239237
rules=self.tag_rules,
240238
)
241239
if self.change_type_order:
242240
tree = changelog.generate_ordered_changelog_tree(
243241
tree, self.change_type_order
244242
)
245243

246-
extras = self.cz.template_extras.copy()
247-
extras.update(self.config.settings["extras"])
248-
extras.update(self.extras)
249244
changelog_out = changelog.render_changelog(
250-
tree, loader=self.cz.template_loader, template=self.template, **extras
245+
tree,
246+
self.cz.template_loader,
247+
self.template,
248+
**{
249+
**self.cz.template_extras,
250+
**self.config.settings["extras"],
251+
**self.extras,
252+
},
251253
).lstrip("\n")
252254

253255
# Dry_run is executed here to avoid checking and reading the files

0 commit comments

Comments
 (0)