Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a3d30ad

Browse files
feat(unicode): add unicode support
This will allow commiting, e.g., emoji's and parsing commit messages for unicode characters when creating change logs.
1 parent 838878e commit a3d30ad

25 files changed

+210
-108
lines changed

commitizen/bump.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,12 @@ def generate_version(
146146

147147

148148
def update_version_in_files(
149-
current_version: str, new_version: str, files: List[str], *, check_consistency=False
149+
current_version: str,
150+
new_version: str,
151+
files: List[str],
152+
encoding: str,
153+
*,
154+
check_consistency=False,
150155
) -> None:
151156
"""Change old version to the new one in every file given.
152157
@@ -163,7 +168,11 @@ def update_version_in_files(
163168
regex = _version_to_regex(current_version)
164169

165170
current_version_found, version_file = _bump_with_regex(
166-
filepath, current_version, new_version, regex
171+
filepath,
172+
current_version,
173+
new_version,
174+
regex,
175+
encoding,
167176
)
168177

169178
if check_consistency and not current_version_found:
@@ -174,17 +183,21 @@ def update_version_in_files(
174183
)
175184

176185
# Write the file out again
177-
with smart_open(filepath, "w") as file:
186+
with smart_open(filepath, "w", encoding=encoding) as file:
178187
file.write(version_file)
179188

180189

181190
def _bump_with_regex(
182-
version_filepath: str, current_version: str, new_version: str, regex: str
191+
version_filepath: str,
192+
current_version: str,
193+
new_version: str,
194+
regex: str,
195+
encoding: str,
183196
) -> Tuple[bool, str]:
184197
current_version_found = False
185198
lines = []
186199
pattern = re.compile(regex)
187-
with open(version_filepath, "r") as f:
200+
with open(version_filepath, "r", encoding=encoding) as f:
188201
for line in f:
189202
if pattern.search(line):
190203
bumped_line = line.replace(current_version, new_version)

commitizen/changelog.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def parse_title_type_of_line(value: str) -> Optional[str]:
164164
return m.groupdict().get("title")
165165

166166

167-
def get_metadata(filepath: str) -> Dict:
167+
def get_metadata(filepath: str, encoding: str) -> Dict:
168168
unreleased_start: Optional[int] = None
169169
unreleased_end: Optional[int] = None
170170
unreleased_title: Optional[str] = None
@@ -178,7 +178,7 @@ def get_metadata(filepath: str) -> Dict:
178178
"latest_version_position": None,
179179
}
180180

181-
with open(filepath, "r") as changelog_file:
181+
with open(filepath, "r", encoding=encoding) as changelog_file:
182182
for index, line in enumerate(changelog_file):
183183
line = line.strip().lower()
184184

commitizen/changelog_parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
]
3535

3636

37-
def find_version_blocks(filepath: str) -> Generator:
37+
def find_version_blocks(filepath: str, encoding: str) -> Generator:
3838
"""Find version block (version block: contains all the information about a version.)
3939
4040
E.g:
@@ -51,7 +51,7 @@ def find_version_blocks(filepath: str) -> Generator:
5151
5252
```
5353
"""
54-
with open(filepath, "r") as f:
54+
with open(filepath, "r", encoding=encoding) as f:
5555
block: list = []
5656
for line in f:
5757
line = line.strip("\n")

commitizen/commands/bump.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def __init__(self, config: BaseConfig, arguments: dict):
3333
raise NotAGitProjectError()
3434

3535
self.config: BaseConfig = config
36+
self.encoding = config.settings["encoding"]
3637
self.arguments: dict = arguments
3738
self.bump_settings: dict = {
3839
**config.settings,
@@ -267,6 +268,7 @@ def __call__(self): # noqa: C901
267268
current_version,
268269
str(new_version),
269270
version_files,
271+
self.encoding,
270272
check_consistency=self.check_consistency,
271273
)
272274

commitizen/commands/changelog.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def __init__(self, config: BaseConfig, args):
2424
raise NotAGitProjectError()
2525

2626
self.config: BaseConfig = config
27+
self.encoding = self.config.settings["encoding"]
2728
self.cz = factory.commiter_factory(self.config)
2829

2930
self.start_rev = args.get("start_rev") or self.config.settings.get(
@@ -87,7 +88,7 @@ def write_changelog(
8788
)
8889

8990
changelog_hook: Optional[Callable] = self.cz.changelog_hook
90-
with smart_open(self.file_name, "w") as changelog_file:
91+
with smart_open(self.file_name, "w", encoding=self.encoding) as changelog_file:
9192
partial_changelog: Optional[str] = None
9293
if self.incremental:
9394
new_lines = changelog.incremental_build(
@@ -129,7 +130,7 @@ def __call__(self):
129130
end_rev = ""
130131

131132
if self.incremental:
132-
changelog_meta = changelog.get_metadata(self.file_name)
133+
changelog_meta = changelog.get_metadata(self.file_name, self.encoding)
133134
latest_version = changelog_meta.get("latest_version")
134135
if latest_version:
135136
latest_tag_version: str = bump.normalize_tag(
@@ -170,7 +171,7 @@ def __call__(self):
170171

171172
lines = []
172173
if self.incremental and os.path.isfile(self.file_name):
173-
with open(self.file_name, "r") as changelog_file:
174+
with open(self.file_name, "r", encoding=self.encoding) as changelog_file:
174175
lines = changelog_file.readlines()
175176

176177
self.write_changelog(changelog_out, lines, changelog_meta)

commitizen/commands/check.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def __init__(self, config: BaseConfig, arguments: Dict[str, str], cwd=os.getcwd(
3333
self._valid_command_argument()
3434

3535
self.config: BaseConfig = config
36+
self.encoding = config.settings["encoding"]
3637
self.cz = factory.commiter_factory(self.config)
3738

3839
def _valid_command_argument(self):
@@ -86,7 +87,7 @@ def _get_commits(self):
8687
# Get commit message from file (--commit-msg-file)
8788
if self.commit_msg_file is not None:
8889
# Enter this branch if commit_msg_file is "".
89-
with open(self.commit_msg_file, "r", encoding="utf-8") as commit_file:
90+
with open(self.commit_msg_file, "r", encoding=self.encoding) as commit_file:
9091
msg = commit_file.read()
9192
# Get commit message from command line (--message)
9293
elif self.commit_msg is not None:

commitizen/commands/commit.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def __init__(self, config: BaseConfig, arguments: dict):
2727
raise NotAGitProjectError()
2828

2929
self.config: BaseConfig = config
30+
self.encoding = config.settings["encoding"]
3031
self.cz = factory.commiter_factory(self.config)
3132
self.arguments = arguments
3233
self.temp_file: str = os.path.join(
@@ -40,7 +41,7 @@ def read_backup_message(self) -> str:
4041
raise NoCommitBackupError()
4142

4243
# Read commit message from backup
43-
with open(self.temp_file, "r") as f:
44+
with open(self.temp_file, "r", encoding=self.encoding) as f:
4445
return f.read().strip()
4546

4647
def prompt_commit_questions(self) -> str:
@@ -90,7 +91,7 @@ def __call__(self):
9091
out.error(c.err)
9192

9293
# Create commit backup
93-
with smart_open(self.temp_file, "w") as f:
94+
with smart_open(self.temp_file, "w", encoding=self.encoding) as f:
9495
f.write(m)
9596

9697
raise CommitError()

commitizen/commands/init.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
class Init:
1717
def __init__(self, config: BaseConfig, *args):
1818
self.config: BaseConfig = config
19+
self.encoding = config.settings["encoding"]
1920
self.cz = factory.commiter_factory(self.config)
2021

2122
def __call__(self):
@@ -122,7 +123,9 @@ def _install_pre_commit_hook(self):
122123
# .pre-commit-config does not exist
123124
config_data["repos"] = [cz_hook_config]
124125
else:
125-
with open(pre_commit_config_filename) as config_file:
126+
with open(
127+
pre_commit_config_filename, encoding=self.encoding
128+
) as config_file:
126129
yaml_data = yaml.safe_load(config_file)
127130
if yaml_data:
128131
config_data = yaml_data
@@ -138,7 +141,9 @@ def _install_pre_commit_hook(self):
138141
# .pre-commit-config exists but there's no "repos" key
139142
config_data["repos"] = [cz_hook_config]
140143

141-
with smart_open(pre_commit_config_filename, "w") as config_file:
144+
with smart_open(
145+
pre_commit_config_filename, "w", encoding=self.encoding
146+
) as config_file:
142147
yaml.safe_dump(config_data, stream=config_file)
143148

144149
c = cmd.run("pre-commit install --hook-type commit-msg")

commitizen/config/json_config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
class JsonConfig(BaseConfig):
1111
def __init__(self, *, data: Union[bytes, str], path: Union[Path, str]):
1212
super(JsonConfig, self).__init__()
13+
self.encoding = self.settings["encoding"]
1314
self.is_empty_config = False
1415
self._parse_setting(data)
1516
self.add_path(path)
1617

1718
def init_empty_config_content(self):
18-
with smart_open(self.path, "a") as json_file:
19+
with smart_open(self.path, "a", encoding=self.encoding) as json_file:
1920
json.dump({"commitizen": {}}, json_file)
2021

2122
def set_key(self, key, value):
@@ -28,7 +29,7 @@ def set_key(self, key, value):
2829
parser = json.load(f)
2930

3031
parser["commitizen"][key] = value
31-
with smart_open(self.path, "w") as f:
32+
with smart_open(self.path, "w", encoding=self.encoding) as f:
3233
json.dump(parser, f, indent=2)
3334
return self
3435

commitizen/config/toml_config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
class TomlConfig(BaseConfig):
1111
def __init__(self, *, data: Union[bytes, str], path: Union[Path, str]):
1212
super(TomlConfig, self).__init__()
13+
self.encoding = self.settings["encoding"]
1314
self.is_empty_config = False
1415
self._parse_setting(data)
1516
self.add_path(path)
@@ -25,7 +26,7 @@ def init_empty_config_content(self):
2526
if parser.get("tool") is None:
2627
parser["tool"] = table()
2728
parser["tool"]["commitizen"] = table()
28-
output_toml_file.write(parser.as_string().encode("utf-8"))
29+
output_toml_file.write(parser.as_string().encode(self.encoding))
2930

3031
def set_key(self, key, value):
3132
"""Set or update a key in the conf.
@@ -38,7 +39,7 @@ def set_key(self, key, value):
3839

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

4445
def _parse_setting(self, data: Union[bytes, str]) -> None:

commitizen/config/yaml_config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
class YAMLConfig(BaseConfig):
1212
def __init__(self, *, data: Union[bytes, str], path: Union[Path, str]):
1313
super(YAMLConfig, self).__init__()
14+
self.encoding = self.settings["encoding"]
1415
self.is_empty_config = False
1516
self._parse_setting(data)
1617
self.add_path(path)
1718

1819
def init_empty_config_content(self):
19-
with smart_open(self.path, "a") as json_file:
20+
with smart_open(self.path, "a", encoding=self.encoding) as json_file:
2021
yaml.dump({"commitizen": {}}, json_file)
2122

2223
def _parse_setting(self, data: Union[bytes, str]) -> None:
@@ -43,7 +44,7 @@ def set_key(self, key, value):
4344
parser = yaml.load(yaml_file, Loader=yaml.FullLoader)
4445

4546
parser["commitizen"][key] = value
46-
with smart_open(self.path, "w") as yaml_file:
47+
with smart_open(self.path, "w", encoding=self.encoding) as yaml_file:
4748
yaml.dump(parser, yaml_file)
4849

4950
return self

commitizen/cz/conventional_commits/conventional_commits.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def schema_pattern(self) -> str:
202202
def info(self) -> str:
203203
dir_path = os.path.dirname(os.path.realpath(__file__))
204204
filepath = os.path.join(dir_path, "conventional_commits_info.txt")
205-
with open(filepath, "r") as f:
205+
with open(filepath, "r", encoding=self.config.settings["encoding"]) as f:
206206
content = f.read()
207207
return content
208208

commitizen/cz/customize/customize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def info(self) -> Optional[str]:
7373
info_path = self.custom_settings.get("info_path")
7474
info = self.custom_settings.get("info")
7575
if info_path:
76-
with open(info_path, "r") as f:
76+
with open(info_path, "r", encoding=self.config.settings["encoding"]) as f:
7777
content = f.read()
7878
return content
7979
elif info:

commitizen/cz/jira/jira.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,6 @@ def schema_pattern(self) -> str:
7676
def info(self) -> str:
7777
dir_path = os.path.dirname(os.path.realpath(__file__))
7878
filepath = os.path.join(dir_path, "jira_info.txt")
79-
with open(filepath, "r") as f:
79+
with open(filepath, "r", encoding=self.config.settings["encoding"]) as f:
8080
content = f.read()
8181
return content

commitizen/defaults.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class Settings(TypedDict, total=False):
4040
style: Optional[List[Tuple[str, str]]]
4141
customize: CzSettings
4242
major_version_zero: bool
43+
encoding: str
4344

4445

4546
name: str = "cz_conventional_commits"
@@ -51,6 +52,7 @@ class Settings(TypedDict, total=False):
5152
".cz.yaml",
5253
"cz.yaml",
5354
]
55+
encoding: str = "utf-8"
5456

5557
DEFAULT_SETTINGS: Settings = {
5658
"name": "cz_conventional_commits",
@@ -65,6 +67,7 @@ class Settings(TypedDict, total=False):
6567
"update_changelog_on_bump": False,
6668
"use_shortcuts": False,
6769
"major_version_zero": False,
70+
"encoding": "utf-8",
6871
}
6972

7073
MAJOR = "MAJOR"

0 commit comments

Comments
 (0)