Skip to content

Commit 664a38f

Browse files
committed
refactor(bump): cleanup related to update_version_file
1 parent cc981fc commit 664a38f

File tree

1 file changed

+46
-40
lines changed

1 file changed

+46
-40
lines changed

commitizen/bump.py

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
import os
44
import re
55
from collections import OrderedDict
6-
from collections.abc import Iterable
6+
from collections.abc import Callable, Iterable
77
from glob import iglob
88
from logging import getLogger
99
from string import Template
10-
from typing import cast
10+
from typing import Never, TextIO, cast
1111

1212
from commitizen.defaults import BUMP_MESSAGE, ENCODING, MAJOR, MINOR, PATCH
1313
from commitizen.exceptions import CurrentVersionNotFoundError
@@ -77,34 +77,43 @@ def update_version_in_files(
7777
"""
7878
# TODO: separate check step and write step
7979
updated = []
80-
for path, regex in _files_and_regexes(files, current_version):
81-
current_version_found, version_file = _bump_with_regex(
82-
path,
83-
current_version,
84-
new_version,
85-
regex,
86-
encoding=encoding,
87-
)
88-
89-
if check_consistency and not current_version_found:
90-
raise CurrentVersionNotFoundError(
91-
f"Current version {current_version} is not found in {path}.\n"
92-
"The version defined in commitizen configuration and the ones in "
93-
"version_files are possibly inconsistent."
80+
81+
for path, pattern in _resolve_files_and_regexes(files, current_version):
82+
error_on_not_found = None
83+
if check_consistency:
84+
85+
def fn() -> Never:
86+
raise CurrentVersionNotFoundError(
87+
f"Current version {current_version} is not found in {path}.\n"
88+
"The version defined in commitizen configuration and the ones in "
89+
"version_files are possibly inconsistent."
90+
)
91+
92+
error_on_not_found = fn
93+
94+
with open(path, encoding=encoding) as file:
95+
bumped_version_file_content = _bump_line_by_line(
96+
file,
97+
lambda line: line.replace(current_version, new_version)
98+
if pattern.search(line)
99+
else line,
100+
error_on_not_found,
94101
)
95102

96103
# Write the file out again
97104
with smart_open(path, "w", encoding=encoding) as file:
98-
file.write(version_file)
105+
file.write(bumped_version_file_content)
99106
updated.append(path)
100107
return updated
101108

102109

103-
def _files_and_regexes(patterns: Iterable[str], version: str) -> list[tuple[str, str]]:
110+
def _resolve_files_and_regexes(
111+
patterns: Iterable[str], version: str
112+
) -> list[tuple[str, re.Pattern]]:
104113
"""
105114
Resolve all distinct files with their regexp from a list of glob patterns with optional regexp
106115
"""
107-
out: set[tuple[str, str]] = set()
116+
out: set[tuple[str, re.Pattern]] = set()
108117
for pattern in patterns:
109118
drive, tail = os.path.splitdrive(pattern)
110119
path, _, regex = tail.partition(":")
@@ -113,33 +122,30 @@ def _files_and_regexes(patterns: Iterable[str], version: str) -> list[tuple[str,
113122
regex = re.escape(version)
114123

115124
for file in iglob(filepath):
116-
out.add((file, regex))
125+
out.add((file, re.compile(regex)))
117126

118127
return sorted(out)
119128

120129

121-
def _bump_with_regex(
122-
version_filepath: str,
123-
current_version: str,
124-
new_version: str,
125-
regex: str,
126-
encoding: str = ENCODING,
127-
) -> tuple[bool, str]:
130+
def _bump_line_by_line(
131+
version_file: TextIO,
132+
bump_line: Callable[[str], str],
133+
error_on_not_found: Callable[[], Never] | None,
134+
) -> str:
128135
current_version_found = False
129136
lines = []
130-
pattern = re.compile(regex)
131-
with open(version_filepath, encoding=encoding) as f:
132-
for line in f:
133-
if not pattern.search(line):
134-
lines.append(line)
135-
continue
136-
137-
bumped_line = line.replace(current_version, new_version)
138-
if bumped_line != line:
139-
current_version_found = True
140-
lines.append(bumped_line)
141-
142-
return current_version_found, "".join(lines)
137+
138+
for line in version_file:
139+
bumped_line = bump_line(line)
140+
if bumped_line != line:
141+
current_version_found = True
142+
143+
lines.append(bumped_line)
144+
145+
if error_on_not_found is not None and not current_version_found:
146+
error_on_not_found()
147+
148+
return "".join(lines)
143149

144150

145151
def create_commit_message(

0 commit comments

Comments
 (0)