Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bump): Search for version number line by line #568

Merged
merged 3 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 25 additions & 37 deletions commitizen/bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion tests/data/sample_cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_bump_update_version_in_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down