Skip to content

Commit 81aecc3

Browse files
authored
Merge pull request #568 from Kurt-von-Laven/line-by-line
fix(bump): Search for version number line by line
2 parents 8cb1e22 + 7f7a606 commit 81aecc3

File tree

6 files changed

+30
-42
lines changed

6 files changed

+30
-42
lines changed

commitizen/bump.py

+25-37
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from collections import OrderedDict
33
from itertools import zip_longest
44
from string import Template
5-
from typing import List, Optional, Union
5+
from typing import List, Optional, Tuple, Union
66

77
from packaging.version import Version
88

@@ -142,18 +142,12 @@ def update_version_in_files(
142142
# TODO: separate check step and write step
143143
for location in files:
144144
filepath, _, regex = location.partition(":")
145+
if not regex:
146+
regex = _version_to_regex(current_version)
145147

146-
with open(filepath, "r") as f:
147-
version_file = f.read()
148-
149-
if regex:
150-
current_version_found, version_file = _bump_with_regex(
151-
version_file, current_version, new_version, regex
152-
)
153-
else:
154-
current_version_regex = _version_to_regex(current_version)
155-
current_version_found = bool(current_version_regex.search(version_file))
156-
version_file = current_version_regex.sub(new_version, version_file)
148+
current_version_found, version_file = _bump_with_regex(
149+
filepath, current_version, new_version, regex
150+
)
157151

158152
if check_consistency and not current_version_found:
159153
raise CurrentVersionNotFoundError(
@@ -167,32 +161,26 @@ def update_version_in_files(
167161
file.write("".join(version_file))
168162

169163

170-
def _bump_with_regex(version_file_contents, current_version, new_version, regex):
164+
def _bump_with_regex(
165+
version_filepath: str, current_version: str, new_version: str, regex: str
166+
) -> Tuple[bool, str]:
171167
current_version_found = False
172-
# Bumping versions that change the string length move the offset on the file contents as finditer keeps a
173-
# reference to the initial string that was used and calling search many times would lead in infinite loops
174-
# e.g.: 1.1.9 -> 1.1.20
175-
offset = 0
176-
for match in re.finditer(regex, version_file_contents, re.MULTILINE):
177-
left = version_file_contents[: match.end() + offset]
178-
right = version_file_contents[match.end() + offset :]
179-
180-
line_break = right.find("\n")
181-
middle = right[:line_break]
182-
right = right[line_break:]
183-
184-
if current_version in middle:
185-
offset += len(new_version) - len(current_version)
186-
current_version_found = True
187-
version_file_contents = (
188-
left + middle.replace(current_version, new_version) + right
189-
)
190-
return current_version_found, version_file_contents
191-
192-
193-
def _version_to_regex(version: str):
194-
clean_regex = version.replace(".", r"\.").replace("+", r"\+")
195-
return re.compile(f"{clean_regex}")
168+
lines = []
169+
pattern = re.compile(regex)
170+
with open(version_filepath, "r") as f:
171+
for line in f:
172+
if pattern.search(line):
173+
bumped_line = line.replace(current_version, new_version)
174+
if bumped_line != line:
175+
current_version_found = True
176+
lines.append(bumped_line)
177+
else:
178+
lines.append(line)
179+
return current_version_found, "".join(lines)
180+
181+
182+
def _version_to_regex(version: str) -> str:
183+
return version.replace(".", r"\.").replace("+", r"\+")
196184

197185

198186
def normalize_tag(

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ tag_format = "v$version"
44
version_files = [
55
"pyproject.toml:version",
66
"commitizen/__version__.py",
7-
".pre-commit-config.yaml:rev:.\\s+(?=[^\\n]+Commitizen)"
7+
".pre-commit-config.yaml:rev:.+Commitizen"
88
]
99

1010
[tool.black]

tests/data/sample_cargo.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version = "1.2.3"
44

55
[[package]]
66
name = "there-i-fixed-it"
7-
version = "1.2.3"
7+
version = "1.2.3" # automatically bumped by Commitizen
88

99
[[package]]
1010
name = "other-project"

tests/test_bump_update_version_in_files.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def test_partial_update_of_file(version_repeated_file, file_regression):
125125
def test_random_location(random_location_version_file, file_regression):
126126
old_version = "1.2.3"
127127
new_version = "2.0.0"
128-
location = f"{random_location_version_file}:there-i-fixed-it.+\nversion"
128+
location = f"{random_location_version_file}:version.+Commitizen"
129129

130130
bump.update_version_in_files(old_version, new_version, [location])
131131
with open(random_location_version_file, "r") as f:

tests/test_bump_update_version_in_files/test_duplicates_are_change_with_no_regex.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version = "2.0.0"
44

55
[[package]]
66
name = "there-i-fixed-it"
7-
version = "2.0.0"
7+
version = "2.0.0" # automatically bumped by Commitizen
88

99
[[package]]
1010
name = "other-project"

tests/test_bump_update_version_in_files/test_random_location.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version = "1.2.3"
44

55
[[package]]
66
name = "there-i-fixed-it"
7-
version = "2.0.0"
7+
version = "2.0.0" # automatically bumped by Commitizen
88

99
[[package]]
1010
name = "other-project"

0 commit comments

Comments
 (0)