Skip to content

Commit 10d5b2f

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

File tree

1 file changed

+47
-40
lines changed

1 file changed

+47
-40
lines changed

commitizen/bump.py

Lines changed: 47 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 TextIO, cast
1111

1212
from commitizen.defaults import BUMP_MESSAGE, ENCODING, MAJOR, MINOR, PATCH
1313
from commitizen.exceptions import CurrentVersionNotFoundError
@@ -75,32 +75,42 @@ def update_version_in_files(
7575
7676
Returns the list of updated files.
7777
"""
78-
# TODO: separate check step and write step
7978
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."
79+
80+
for path, pattern in _resolve_files_and_regexes(files, current_version):
81+
82+
def on_current_version_not_found() -> None:
83+
if check_consistency:
84+
raise CurrentVersionNotFoundError(
85+
f"Current version {current_version} is not found in {path}.\n"
86+
"The version defined in commitizen configuration and the ones in "
87+
"version_files are possibly inconsistent."
88+
)
89+
90+
def bump_line(line: str) -> str:
91+
return (
92+
line.replace(current_version, new_version)
93+
if pattern.search(line)
94+
else line
95+
)
96+
97+
with open(path, encoding=encoding) as file:
98+
bumped_version_file_content = _get_bumped_version_file_content(
99+
file,
100+
bump_line,
101+
on_current_version_not_found,
94102
)
95103

96104
# Write the file out again
97105
with smart_open(path, "w", encoding=encoding) as file:
98-
file.write(version_file)
106+
file.write(bumped_version_file_content)
99107
updated.append(path)
100108
return updated
101109

102110

103-
def _files_and_regexes(patterns: Iterable[str], version: str) -> list[tuple[str, str]]:
111+
def _resolve_files_and_regexes(
112+
patterns: Iterable[str], version: str
113+
) -> list[tuple[str, re.Pattern]]:
104114
"""
105115
Resolve all distinct files with their regexp from a list of glob patterns with optional regexp
106116
"""
@@ -115,31 +125,28 @@ def _files_and_regexes(patterns: Iterable[str], version: str) -> list[tuple[str,
115125
for file in iglob(filepath):
116126
out.add((file, regex))
117127

118-
return sorted(out)
128+
return [(file, re.compile(regex)) for file, regex in sorted(out)]
119129

120130

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]:
131+
def _get_bumped_version_file_content(
132+
version_file: TextIO,
133+
bump_line: Callable[[str], str],
134+
on_current_version_not_found: Callable[[], None],
135+
) -> str:
128136
current_version_found = False
129137
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)
138+
139+
for line in version_file:
140+
bumped_line = bump_line(line)
141+
if bumped_line != line:
142+
current_version_found = True
143+
144+
lines.append(bumped_line)
145+
146+
if not current_version_found:
147+
on_current_version_not_found()
148+
149+
return "".join(lines)
143150

144151

145152
def create_commit_message(

0 commit comments

Comments
 (0)