Skip to content

Commit 9436617

Browse files
committed
fix(cmd): improve character encoding detection for sub-commands
1 parent 2ff9f15 commit 9436617

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

commitizen/cmd.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ class Command(NamedTuple):
1212
return_code: int
1313

1414

15+
def _try_decode(bytes_: bytes) -> str:
16+
try:
17+
return bytes_.decode("utf-8")
18+
except UnicodeDecodeError:
19+
result = chardet.detect(bytes_)
20+
return bytes_.decode(result["encoding"] or "utf-8")
21+
22+
1523
def run(cmd: str) -> Command:
1624
process = subprocess.Popen(
1725
cmd,
@@ -23,8 +31,8 @@ def run(cmd: str) -> Command:
2331
stdout, stderr = process.communicate()
2432
return_code = process.returncode
2533
return Command(
26-
stdout.decode(chardet.detect(stdout)["encoding"] or "utf-8"),
27-
stderr.decode(chardet.detect(stderr)["encoding"] or "utf-8"),
34+
_try_decode(stdout),
35+
_try_decode(stderr),
2836
stdout,
2937
stderr,
3038
return_code,

0 commit comments

Comments
 (0)