Skip to content
27 changes: 20 additions & 7 deletions commitizen/bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
from collections import OrderedDict
from string import Template

from commitizen.defaults import MAJOR, MINOR, PATCH, bump_message
from commitizen.defaults import MAJOR, MINOR, PATCH, bump_message, encoding
from commitizen.exceptions import CurrentVersionNotFoundError
from commitizen.git import GitCommit, smart_open
from commitizen.version_schemes import DEFAULT_SCHEME, VersionScheme, Version
from commitizen.version_schemes import DEFAULT_SCHEME, Version, VersionScheme


def find_increment(
Expand Down Expand Up @@ -45,7 +45,12 @@ def find_increment(


def update_version_in_files(
current_version: str, new_version: str, files: list[str], *, check_consistency=False
current_version: str,
new_version: str,
files: list[str],
*,
check_consistency: bool = False,
encoding: str = encoding,
) -> None:
"""Change old version to the new one in every file given.

Expand All @@ -62,7 +67,11 @@ def update_version_in_files(
regex = _version_to_regex(current_version)

current_version_found, version_file = _bump_with_regex(
filepath, current_version, new_version, regex
filepath,
current_version,
new_version,
regex,
encoding=encoding,
)

if check_consistency and not current_version_found:
Expand All @@ -73,17 +82,21 @@ def update_version_in_files(
)

# Write the file out again
with smart_open(filepath, "w") as file:
with smart_open(filepath, "w", encoding=encoding) as file:
file.write(version_file)


def _bump_with_regex(
version_filepath: str, current_version: str, new_version: str, regex: str
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, "r") as f:
with open(version_filepath, "r", encoding=encoding) as f:
for line in f:
if pattern.search(line):
bumped_line = line.replace(current_version, new_version)
Expand Down
7 changes: 5 additions & 2 deletions commitizen/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

from commitizen import out
from commitizen.bump import normalize_tag
from commitizen.defaults import encoding
from commitizen.exceptions import InvalidConfigurationError, NoCommitsFoundError
from commitizen.git import GitCommit, GitTag
from commitizen.version_schemes import (
Expand Down Expand Up @@ -213,7 +214,9 @@ def parse_title_type_of_line(value: str) -> str | None:
return m.groupdict().get("title")


def get_metadata(filepath: str, scheme: VersionScheme = Pep440) -> dict:
def get_metadata(
filepath: str, scheme: VersionScheme = Pep440, encoding: str = encoding
) -> dict:
unreleased_start: int | None = None
unreleased_end: int | None = None
unreleased_title: str | None = None
Expand All @@ -227,7 +230,7 @@ def get_metadata(filepath: str, scheme: VersionScheme = Pep440) -> dict:
"latest_version_position": None,
}

with open(filepath, "r") as changelog_file:
with open(filepath, "r", encoding=encoding) as changelog_file:
for index, line in enumerate(changelog_file):
line = line.strip().lower()

Expand Down
6 changes: 4 additions & 2 deletions commitizen/changelog_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from collections import defaultdict
from typing import Generator, Iterable

from commitizen.defaults import encoding

MD_VERSION_RE = r"^##\s(?P<version>[a-zA-Z0-9.+]+)\s?\(?(?P<date>[0-9-]+)?\)?"
MD_CHANGE_TYPE_RE = r"^###\s(?P<change_type>[a-zA-Z0-9.+\s]+)"
MD_MESSAGE_RE = (
Expand All @@ -36,7 +38,7 @@
]


def find_version_blocks(filepath: str) -> Generator:
def find_version_blocks(filepath: str, encoding: str = encoding) -> Generator:
"""Find version block (version block: contains all the information about a version.)

E.g:
Expand All @@ -53,7 +55,7 @@ def find_version_blocks(filepath: str) -> Generator:

```
"""
with open(filepath, "r") as f:
with open(filepath, "r", encoding=encoding) as f:
block: list = []
for line in f:
line = line.strip("\n")
Expand Down
6 changes: 4 additions & 2 deletions commitizen/commands/bump.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from __future__ import annotations

import os
from logging import getLogger
import warnings
from logging import getLogger

import questionary

Expand All @@ -23,7 +23,7 @@
NoVersionSpecifiedError,
)
from commitizen.providers import get_provider
from commitizen.version_schemes import get_version_scheme, InvalidVersion
from commitizen.version_schemes import InvalidVersion, get_version_scheme

logger = getLogger("commitizen")

Expand All @@ -36,6 +36,7 @@ def __init__(self, config: BaseConfig, arguments: dict):
raise NotAGitProjectError()

self.config: BaseConfig = config
self.encoding = config.settings["encoding"]
self.arguments: dict = arguments
self.bump_settings: dict = {
**config.settings,
Expand Down Expand Up @@ -298,6 +299,7 @@ def __call__(self): # noqa: C901
str(new_version),
version_files,
check_consistency=self.check_consistency,
encoding=self.encoding,
)

provider.set_version(str(new_version))
Expand Down
11 changes: 8 additions & 3 deletions commitizen/commands/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def __init__(self, config: BaseConfig, args):
raise NotAGitProjectError()

self.config: BaseConfig = config
self.encoding = self.config.settings["encoding"]
self.cz = factory.commiter_factory(self.config)

self.start_rev = args.get("start_rev") or self.config.settings.get(
Expand Down Expand Up @@ -101,7 +102,7 @@ def write_changelog(
)

changelog_hook: Callable | None = self.cz.changelog_hook
with smart_open(self.file_name, "w") as changelog_file:
with smart_open(self.file_name, "w", encoding=self.encoding) as changelog_file:
partial_changelog: str | None = None
if self.incremental:
new_lines = changelog.incremental_build(
Expand Down Expand Up @@ -141,7 +142,11 @@ def __call__(self):

end_rev = ""
if self.incremental:
changelog_meta = changelog.get_metadata(self.file_name, self.scheme)
changelog_meta = changelog.get_metadata(
self.file_name,
self.scheme,
encoding=self.encoding,
)
latest_version = changelog_meta.get("latest_version")
if latest_version:
latest_tag_version: str = bump.normalize_tag(
Expand Down Expand Up @@ -187,7 +192,7 @@ def __call__(self):

lines = []
if self.incremental and os.path.isfile(self.file_name):
with open(self.file_name, "r") as changelog_file:
with open(self.file_name, "r", encoding=self.encoding) as changelog_file:
lines = changelog_file.readlines()

self.write_changelog(changelog_out, lines, changelog_meta)
3 changes: 2 additions & 1 deletion commitizen/commands/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def __init__(self, config: BaseConfig, arguments: dict[str, Any], cwd=os.getcwd(
self._valid_command_argument()

self.config: BaseConfig = config
self.encoding = config.settings["encoding"]
self.cz = factory.commiter_factory(self.config)

def _valid_command_argument(self):
Expand Down Expand Up @@ -97,7 +98,7 @@ def _get_commits(self):
# Get commit message from file (--commit-msg-file)
if self.commit_msg_file is not None:
# Enter this branch if commit_msg_file is "".
with open(self.commit_msg_file, "r", encoding="utf-8") as commit_file:
with open(self.commit_msg_file, "r", encoding=self.encoding) as commit_file:
msg = commit_file.read()
# Get commit message from command line (--message)
elif self.commit_msg is not None:
Expand Down
7 changes: 4 additions & 3 deletions commitizen/commands/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def __init__(self, config: BaseConfig, arguments: dict):
raise NotAGitProjectError()

self.config: BaseConfig = config
self.encoding = config.settings["encoding"]
self.cz = factory.commiter_factory(self.config)
self.arguments = arguments
self.temp_file: str = os.path.join(
Expand All @@ -41,7 +42,7 @@ def read_backup_message(self) -> str:
raise NoCommitBackupError()

# Read commit message from backup
with open(self.temp_file, "r") as f:
with open(self.temp_file, "r", encoding=self.encoding) as f:
return f.read().strip()

def prompt_commit_questions(self) -> str:
Expand Down Expand Up @@ -82,7 +83,7 @@ def __call__(self):
out.info(f"\n{m}\n")

if write_message_to_file:
with smart_open(write_message_to_file, "w") as file:
with smart_open(write_message_to_file, "w", encoding=self.encoding) as file:
file.write(m)

if dry_run:
Expand All @@ -99,7 +100,7 @@ def __call__(self):
out.error(c.err)

# Create commit backup
with smart_open(self.temp_file, "w") as f:
with smart_open(self.temp_file, "w", encoding=self.encoding) as f:
f.write(m)

raise CommitError()
Expand Down
9 changes: 7 additions & 2 deletions commitizen/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def is_pre_commit_installed(self) -> bool:
class Init:
def __init__(self, config: BaseConfig, *args):
self.config: BaseConfig = config
self.encoding = config.settings["encoding"]
self.cz = factory.commiter_factory(self.config)
self.project_info = ProjectInfo()

Expand Down Expand Up @@ -324,7 +325,9 @@ def _install_pre_commit_hook(self, hook_types: list[str] | None = None):
# .pre-commit-config.yaml does not exist
config_data["repos"] = [cz_hook_config]
else:
with open(pre_commit_config_filename) as config_file:
with open(
pre_commit_config_filename, encoding=self.encoding
) as config_file:
yaml_data = yaml.safe_load(config_file)
if yaml_data:
config_data = yaml_data
Expand All @@ -340,7 +343,9 @@ def _install_pre_commit_hook(self, hook_types: list[str] | None = None):
# .pre-commit-config.yaml exists but there's no "repos" key
config_data["repos"] = [cz_hook_config]

with smart_open(pre_commit_config_filename, "w") as config_file:
with smart_open(
pre_commit_config_filename, "w", encoding=self.encoding
) as config_file:
yaml.safe_dump(config_data, stream=config_file)

if not self.project_info.is_pre_commit_installed:
Expand Down
1 change: 1 addition & 0 deletions commitizen/config/base_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class BaseConfig:
def __init__(self):
self._settings: Settings = DEFAULT_SETTINGS.copy()
self.encoding = self.settings["encoding"]
self._path: Path | None = None

@property
Expand Down
4 changes: 2 additions & 2 deletions commitizen/config/json_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self, *, data: bytes | str, path: Path | str):
self._parse_setting(data)

def init_empty_config_content(self):
with smart_open(self.path, "a") as json_file:
with smart_open(self.path, "a", encoding=self.encoding) as json_file:
json.dump({"commitizen": {}}, json_file)

def set_key(self, key, value):
Expand All @@ -30,7 +30,7 @@ def set_key(self, key, value):
parser = json.load(f)

parser["commitizen"][key] = value
with smart_open(self.path, "w") as f:
with smart_open(self.path, "w", encoding=self.encoding) as f:
json.dump(parser, f, indent=2)
return self

Expand Down
4 changes: 2 additions & 2 deletions commitizen/config/toml_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def init_empty_config_content(self):
if parser.get("tool") is None:
parser["tool"] = table()
parser["tool"]["commitizen"] = table()
output_toml_file.write(parser.as_string().encode("utf-8"))
output_toml_file.write(parser.as_string().encode(self.encoding))

def set_key(self, key, value):
"""Set or update a key in the conf.
Expand All @@ -39,7 +39,7 @@ def set_key(self, key, value):

parser["tool"]["commitizen"][key] = value
with open(self.path, "wb") as f:
f.write(parser.as_string().encode("utf-8"))
f.write(parser.as_string().encode(self.encoding))
return self

def _parse_setting(self, data: bytes | str) -> None:
Expand Down
4 changes: 2 additions & 2 deletions commitizen/config/yaml_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self, *, data: bytes | str, path: Path | str):
self.add_path(path)

def init_empty_config_content(self):
with smart_open(self.path, "a") as json_file:
with smart_open(self.path, "a", encoding=self.encoding) as json_file:
yaml.dump({"commitizen": {}}, json_file, explicit_start=True)

def _parse_setting(self, data: bytes | str) -> None:
Expand All @@ -44,7 +44,7 @@ def set_key(self, key, value):
parser = yaml.load(yaml_file, Loader=yaml.FullLoader)

parser["commitizen"][key] = value
with smart_open(self.path, "w") as yaml_file:
with smart_open(self.path, "w", encoding=self.encoding) as yaml_file:
yaml.dump(parser, yaml_file, explicit_start=True)

return self
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def schema_pattern(self) -> str:
def info(self) -> str:
dir_path = os.path.dirname(os.path.realpath(__file__))
filepath = os.path.join(dir_path, "conventional_commits_info.txt")
with open(filepath, "r") as f:
with open(filepath, "r", encoding=self.config.settings["encoding"]) as f:
content = f.read()
return content

Expand Down
2 changes: 1 addition & 1 deletion commitizen/cz/customize/customize.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def info(self) -> str | None:
info_path = self.custom_settings.get("info_path")
info = self.custom_settings.get("info")
if info_path:
with open(info_path, "r") as f:
with open(info_path, "r", encoding=self.config.settings["encoding"]) as f:
content = f.read()
return content
elif info:
Expand Down
2 changes: 1 addition & 1 deletion commitizen/cz/jira/jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ def schema_pattern(self) -> str:
def info(self) -> str:
dir_path = os.path.dirname(os.path.realpath(__file__))
filepath = os.path.join(dir_path, "jira_info.txt")
with open(filepath, "r") as f:
with open(filepath, "r", encoding=self.config.settings["encoding"]) as f:
content = f.read()
return content
5 changes: 4 additions & 1 deletion commitizen/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class Settings(TypedDict, total=False):
pre_bump_hooks: list[str] | None
post_bump_hooks: list[str] | None
prerelease_offset: int
encoding: str


name: str = "cz_conventional_commits"
Expand All @@ -66,9 +67,10 @@ class Settings(TypedDict, total=False):
".cz.yaml",
"cz.yaml",
]
encoding: str = "utf-8"

DEFAULT_SETTINGS: Settings = {
"name": "cz_conventional_commits",
"name": name,
"version": None,
"version_files": [],
"version_provider": "commitizen",
Expand All @@ -93,6 +95,7 @@ class Settings(TypedDict, total=False):
"pre_bump_hooks": [],
"post_bump_hooks": [],
"prerelease_offset": 0,
"encoding": encoding,
}

MAJOR = "MAJOR"
Expand Down
Loading