-
-
Notifications
You must be signed in to change notification settings - Fork 297
refactor(bump): cleanup related to update_version_file #1594
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,13 @@ | |
import os | ||
import re | ||
from collections import OrderedDict | ||
from collections.abc import Iterable | ||
from collections.abc import Generator, Iterable | ||
from glob import iglob | ||
from logging import getLogger | ||
from string import Template | ||
from typing import cast | ||
|
||
from commitizen.defaults import BUMP_MESSAGE, ENCODING, MAJOR, MINOR, PATCH | ||
from commitizen.defaults import BUMP_MESSAGE, MAJOR, MINOR, PATCH | ||
from commitizen.exceptions import CurrentVersionNotFoundError | ||
from commitizen.git import GitCommit, smart_open | ||
from commitizen.version_schemes import Increment, Version | ||
|
@@ -64,8 +64,8 @@ def update_version_in_files( | |
new_version: str, | ||
files: Iterable[str], | ||
*, | ||
check_consistency: bool = False, | ||
encoding: str = ENCODING, | ||
check_consistency: bool, | ||
encoding: str, | ||
) -> list[str]: | ||
"""Change old version to the new one in every file given. | ||
|
||
|
@@ -75,16 +75,22 @@ def update_version_in_files( | |
|
||
Returns the list of updated files. | ||
""" | ||
# TODO: separate check step and write step | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this TODO is already addressed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Lee-W you left this TODO according to the git history, could you help to confirm this? Thanks. |
||
updated = [] | ||
for path, regex in _files_and_regexes(files, current_version): | ||
current_version_found, version_file = _bump_with_regex( | ||
path, | ||
current_version, | ||
new_version, | ||
regex, | ||
encoding=encoding, | ||
) | ||
updated_files = [] | ||
|
||
for path, pattern in _resolve_files_and_regexes(files, current_version): | ||
current_version_found = False | ||
bumped_lines = [] | ||
|
||
with open(path, encoding=encoding) as version_file: | ||
for line in version_file: | ||
bumped_line = ( | ||
line.replace(current_version, new_version) | ||
if pattern.search(line) | ||
else line | ||
) | ||
|
||
current_version_found = current_version_found or bumped_line != line | ||
bumped_lines.append(bumped_line) | ||
|
||
if check_consistency and not current_version_found: | ||
raise CurrentVersionNotFoundError( | ||
|
@@ -93,53 +99,32 @@ def update_version_in_files( | |
"version_files are possibly inconsistent." | ||
) | ||
|
||
bumped_version_file_content = "".join(bumped_lines) | ||
|
||
# Write the file out again | ||
with smart_open(path, "w", encoding=encoding) as file: | ||
file.write(version_file) | ||
updated.append(path) | ||
return updated | ||
file.write(bumped_version_file_content) | ||
updated_files.append(path) | ||
|
||
return updated_files | ||
|
||
|
||
def _files_and_regexes(patterns: Iterable[str], version: str) -> list[tuple[str, str]]: | ||
def _resolve_files_and_regexes( | ||
patterns: Iterable[str], version: str | ||
) -> Generator[tuple[str, re.Pattern], None, None]: | ||
""" | ||
Resolve all distinct files with their regexp from a list of glob patterns with optional regexp | ||
""" | ||
out: set[tuple[str, str]] = set() | ||
filepath_set: set[tuple[str, str]] = set() | ||
for pattern in patterns: | ||
drive, tail = os.path.splitdrive(pattern) | ||
path, _, regex = tail.partition(":") | ||
filepath = drive + path | ||
if not regex: | ||
regex = re.escape(version) | ||
regex = regex or re.escape(version) | ||
|
||
for file in iglob(filepath): | ||
out.add((file, regex)) | ||
filepath_set.update((path, regex) for path in iglob(filepath)) | ||
|
||
return sorted(out) | ||
|
||
|
||
def _bump_with_regex( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe we don't need this extra layer of abstraction. |
||
version_filepath: str, | ||
current_version: str, | ||
new_version: str, | ||
regex: str, | ||
encoding: str = ENCODING, | ||
) -> tuple[bool, str]: | ||
current_version_found = False | ||
lines = [] | ||
pattern = re.compile(regex) | ||
with open(version_filepath, encoding=encoding) as f: | ||
for line in f: | ||
if not pattern.search(line): | ||
lines.append(line) | ||
continue | ||
|
||
bumped_line = line.replace(current_version, new_version) | ||
if bumped_line != line: | ||
current_version_found = True | ||
lines.append(bumped_line) | ||
|
||
return current_version_found, "".join(lines) | ||
return ((path, re.compile(regex)) for path, regex in sorted(filepath_set)) | ||
|
||
|
||
def create_commit_message( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we don't need this default argument. These only introduce unnecessary complexity.