From a0330e70f93c8f0c4238f6ad3df8a237ed3b872e Mon Sep 17 00:00:00 2001 From: Yu-Ting Hsiung Date: Fri, 16 May 2025 02:03:30 +0800 Subject: [PATCH] refactor: improve readability and fix typos --- commitizen/bump.py | 18 ++++++++++-------- commitizen/changelog.py | 2 +- commitizen/cli.py | 8 ++------ commitizen/cz/utils.py | 11 +++++------ commitizen/defaults.py | 6 +++--- commitizen/tags.py | 6 +----- tests/test_changelog.py | 2 +- 7 files changed, 23 insertions(+), 30 deletions(-) diff --git a/commitizen/bump.py b/commitizen/bump.py index adfab64cb0..3553ac5edb 100644 --- a/commitizen/bump.py +++ b/commitizen/bump.py @@ -76,7 +76,7 @@ def update_version_in_files( """ # TODO: separate check step and write step updated = [] - for path, regex in files_and_regexs(files, current_version): + for path, regex in _files_and_regexes(files, current_version): current_version_found, version_file = _bump_with_regex( path, current_version, @@ -99,7 +99,7 @@ def update_version_in_files( return updated -def files_and_regexs(patterns: list[str], version: str) -> list[tuple[str, str]]: +def _files_and_regexes(patterns: list[str], version: str) -> list[tuple[str, str]]: """ Resolve all distinct files with their regexp from a list of glob patterns with optional regexp """ @@ -128,13 +128,15 @@ def _bump_with_regex( pattern = re.compile(regex) with open(version_filepath, encoding=encoding) as f: for line in f: - if pattern.search(line): - bumped_line = line.replace(current_version, new_version) - if bumped_line != line: - current_version_found = True - lines.append(bumped_line) - else: + if not pattern.search(line): lines.append(line) + continue + + bumped_line = line.replace(current_version, new_version) + if bumped_line != line: + current_version_found = True + lines.append(bumped_line) + return current_version_found, "".join(lines) diff --git a/commitizen/changelog.py b/commitizen/changelog.py index 704efe6071..d8ab56069b 100644 --- a/commitizen/changelog.py +++ b/commitizen/changelog.py @@ -190,7 +190,7 @@ def process_commit_message( def order_changelog_tree(tree: Iterable, change_type_order: list[str]) -> Iterable: if len(set(change_type_order)) != len(change_type_order): raise InvalidConfigurationError( - f"Change types contain duplicates types ({change_type_order})" + f"Change types contain duplicated types ({change_type_order})" ) sorted_tree = [] diff --git a/commitizen/cli.py b/commitizen/cli.py index 72d824380d..a97cb417cf 100644 --- a/commitizen/cli.py +++ b/commitizen/cli.py @@ -633,14 +633,10 @@ def main(): extra_args = " ".join(unknown_args[1:]) arguments["extra_cli_args"] = extra_args - if args.config: - conf = config.read_cfg(args.config) - else: - conf = config.read_cfg() - + conf = config.read_cfg(args.config) if args.name: conf.update({"name": args.name}) - elif not args.name and not conf.path: + elif not conf.path: conf.update({"name": "cz_conventional_commits"}) if args.debug: diff --git a/commitizen/cz/utils.py b/commitizen/cz/utils.py index 7bc89673c6..cb79d65d1a 100644 --- a/commitizen/cz/utils.py +++ b/commitizen/cz/utils.py @@ -5,6 +5,8 @@ from commitizen import git from commitizen.cz import exceptions +_RE_LOCAL_VERSION = re.compile(r"\+.+") + def required_validator(answer, msg=None): if not answer: @@ -17,16 +19,13 @@ def multiple_line_breaker(answer, sep="|"): def strip_local_version(version: str) -> str: - return re.sub(r"\+.+", "", version) + return _RE_LOCAL_VERSION.sub("", version) def get_backup_file_path() -> str: project_root = git.find_git_project_root() - - if project_root is None: - project = "" - else: - project = project_root.as_posix().replace("/", "%") + project = project_root.as_posix().replace("/", "%") if project_root else "" user = os.environ.get("USER", "") + return os.path.join(tempfile.gettempdir(), f"cz.commit%{user}%{project}.backup") diff --git a/commitizen/defaults.py b/commitizen/defaults.py index 0b78e1b0bb..2f3656c91c 100644 --- a/commitizen/defaults.py +++ b/commitizen/defaults.py @@ -142,7 +142,7 @@ class Settings(TypedDict, total=False): def get_tag_regexes( version_regex: str, ) -> dict[str, str]: - regexs = { + regexes = { "version": version_regex, "major": r"(?P\d+)", "minor": r"(?P\d+)", @@ -151,6 +151,6 @@ def get_tag_regexes( "devrelease": r"(?P\.dev\d+)?", } return { - **{f"${k}": v for k, v in regexs.items()}, - **{f"${{{k}}}": v for k, v in regexs.items()}, + **{f"${k}": v for k, v in regexes.items()}, + **{f"${{{k}}}": v for k, v in regexes.items()}, } diff --git a/commitizen/tags.py b/commitizen/tags.py index 2b9a4b091a..c5f06884fe 100644 --- a/commitizen/tags.py +++ b/commitizen/tags.py @@ -174,11 +174,7 @@ def include_in_changelog(self, tag: GitTag) -> bool: version = self.extract_version(tag) except InvalidVersion: return False - - if self.merge_prereleases and version.is_prerelease: - return False - - return True + return not (self.merge_prereleases and version.is_prerelease) def search_version(self, text: str, last: bool = False) -> VersionTag | None: """ diff --git a/tests/test_changelog.py b/tests/test_changelog.py index 067adb4a91..7e9dde4430 100644 --- a/tests/test_changelog.py +++ b/tests/test_changelog.py @@ -1238,7 +1238,7 @@ def test_order_changelog_tree_raises(): with pytest.raises(InvalidConfigurationError) as excinfo: changelog.order_changelog_tree(COMMITS_TREE, change_type_order) - assert "Change types contain duplicates types" in str(excinfo) + assert "Change types contain duplicated types" in str(excinfo) def test_render_changelog(