diff --git a/commitizen/bump.py b/commitizen/bump.py index 2314b8d0e..0c821ce2d 100644 --- a/commitizen/bump.py +++ b/commitizen/bump.py @@ -2,7 +2,7 @@ from collections import OrderedDict from itertools import zip_longest from string import Template -from typing import List, Optional, Union +from typing import List, Optional, Tuple, Union from packaging.version import Version @@ -142,18 +142,12 @@ def update_version_in_files( # TODO: separate check step and write step for location in files: filepath, _, regex = location.partition(":") + if not regex: + regex = _version_to_regex(current_version) - with open(filepath, "r") as f: - version_file = f.read() - - if regex: - current_version_found, version_file = _bump_with_regex( - version_file, current_version, new_version, regex - ) - else: - current_version_regex = _version_to_regex(current_version) - current_version_found = bool(current_version_regex.search(version_file)) - version_file = current_version_regex.sub(new_version, version_file) + current_version_found, version_file = _bump_with_regex( + filepath, current_version, new_version, regex + ) if check_consistency and not current_version_found: raise CurrentVersionNotFoundError( @@ -167,32 +161,26 @@ def update_version_in_files( file.write("".join(version_file)) -def _bump_with_regex(version_file_contents, current_version, new_version, regex): +def _bump_with_regex( + version_filepath: str, current_version: str, new_version: str, regex: str +) -> Tuple[bool, str]: current_version_found = False - # Bumping versions that change the string length move the offset on the file contents as finditer keeps a - # reference to the initial string that was used and calling search many times would lead in infinite loops - # e.g.: 1.1.9 -> 1.1.20 - offset = 0 - for match in re.finditer(regex, version_file_contents, re.MULTILINE): - left = version_file_contents[: match.end() + offset] - right = version_file_contents[match.end() + offset :] - - line_break = right.find("\n") - middle = right[:line_break] - right = right[line_break:] - - if current_version in middle: - offset += len(new_version) - len(current_version) - current_version_found = True - version_file_contents = ( - left + middle.replace(current_version, new_version) + right - ) - return current_version_found, version_file_contents - - -def _version_to_regex(version: str): - clean_regex = version.replace(".", r"\.").replace("+", r"\+") - return re.compile(f"{clean_regex}") + lines = [] + pattern = re.compile(regex) + with open(version_filepath, "r") 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: + lines.append(line) + return current_version_found, "".join(lines) + + +def _version_to_regex(version: str) -> str: + return version.replace(".", r"\.").replace("+", r"\+") def normalize_tag( diff --git a/pyproject.toml b/pyproject.toml index 43ba51525..c6fb3f5c4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ tag_format = "v$version" version_files = [ "pyproject.toml:version", "commitizen/__version__.py", - ".pre-commit-config.yaml:rev:.\\s+(?=[^\\n]+Commitizen)" + ".pre-commit-config.yaml:rev:.+Commitizen" ] [tool.black] diff --git a/tests/data/sample_cargo.lock b/tests/data/sample_cargo.lock index a3a0b1bb7..d9dbd792b 100644 --- a/tests/data/sample_cargo.lock +++ b/tests/data/sample_cargo.lock @@ -4,7 +4,7 @@ version = "1.2.3" [[package]] name = "there-i-fixed-it" -version = "1.2.3" +version = "1.2.3" # automatically bumped by Commitizen [[package]] name = "other-project" diff --git a/tests/test_bump_update_version_in_files.py b/tests/test_bump_update_version_in_files.py index a00286df7..311363800 100644 --- a/tests/test_bump_update_version_in_files.py +++ b/tests/test_bump_update_version_in_files.py @@ -125,7 +125,7 @@ def test_partial_update_of_file(version_repeated_file, file_regression): def test_random_location(random_location_version_file, file_regression): old_version = "1.2.3" new_version = "2.0.0" - location = f"{random_location_version_file}:there-i-fixed-it.+\nversion" + location = f"{random_location_version_file}:version.+Commitizen" bump.update_version_in_files(old_version, new_version, [location]) with open(random_location_version_file, "r") as f: diff --git a/tests/test_bump_update_version_in_files/test_duplicates_are_change_with_no_regex.lock b/tests/test_bump_update_version_in_files/test_duplicates_are_change_with_no_regex.lock index eed8f4c79..8fe75b80c 100644 --- a/tests/test_bump_update_version_in_files/test_duplicates_are_change_with_no_regex.lock +++ b/tests/test_bump_update_version_in_files/test_duplicates_are_change_with_no_regex.lock @@ -4,7 +4,7 @@ version = "2.0.0" [[package]] name = "there-i-fixed-it" -version = "2.0.0" +version = "2.0.0" # automatically bumped by Commitizen [[package]] name = "other-project" diff --git a/tests/test_bump_update_version_in_files/test_random_location.lock b/tests/test_bump_update_version_in_files/test_random_location.lock index 94422116a..20dfe7f65 100644 --- a/tests/test_bump_update_version_in_files/test_random_location.lock +++ b/tests/test_bump_update_version_in_files/test_random_location.lock @@ -4,7 +4,7 @@ version = "1.2.3" [[package]] name = "there-i-fixed-it" -version = "2.0.0" +version = "2.0.0" # automatically bumped by Commitizen [[package]] name = "other-project"