Skip to content

Commit 342209c

Browse files
authored
Merge pull request #14 from yuxiaoli/fix/utf8-encoding
fix: use utf-8 encoding for subprocess to handle non-ascii chars
2 parents 2fa475e + 8056391 commit 342209c

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

git_py_stats/git_operations.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,17 @@ def run_git_command(cmd: List[str]) -> Optional[str]:
2020
print("Error: Command list is empty!")
2121
return None
2222
try:
23+
# We enforce utf-8 encoding and use surrogateescape to handle non-ascii characters
24+
# that may be present in git history (e.g. in author names or commit messages)
25+
# as recommended by PEP 383 for system interfaces.
2326
result = subprocess.run(
24-
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True
27+
cmd,
28+
stdout=subprocess.PIPE,
29+
stderr=subprocess.PIPE,
30+
text=True,
31+
check=True,
32+
encoding="utf-8",
33+
errors="surrogateescape",
2534
)
2635
return result.stdout.strip()
2736
except subprocess.CalledProcessError as e:

git_py_stats/tests/test_git_operations.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ def test_run_git_command_success(self, mock_subprocess_run):
3131
stderr=subprocess.PIPE,
3232
text=True,
3333
check=True,
34+
encoding="utf-8",
35+
errors="surrogateescape",
3436
)
3537

3638
@patch("subprocess.run")
@@ -54,6 +56,8 @@ def test_run_git_command_failure(self, mock_subprocess_run):
5456
stderr=subprocess.PIPE,
5557
text=True,
5658
check=True,
59+
encoding="utf-8",
60+
errors="surrogateescape",
5761
)
5862

5963
@patch("subprocess.run")
@@ -72,7 +76,13 @@ def test_run_git_command_no_output(self, mock_subprocess_run):
7276
self.assertEqual(output, "")
7377

7478
mock_subprocess_run.assert_called_once_with(
75-
["git", "status"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True
79+
["git", "status"],
80+
stdout=subprocess.PIPE,
81+
stderr=subprocess.PIPE,
82+
text=True,
83+
check=True,
84+
encoding="utf-8",
85+
errors="surrogateescape",
7686
)
7787

7888
@patch("subprocess.run")
@@ -87,7 +97,13 @@ def test_run_git_command_exception(self, mock_subprocess_run):
8797
self.assertIsNone(output)
8898

8999
mock_subprocess_run.assert_called_once_with(
90-
["git", "status"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True
100+
["git", "status"],
101+
stdout=subprocess.PIPE,
102+
stderr=subprocess.PIPE,
103+
text=True,
104+
check=True,
105+
encoding="utf-8",
106+
errors="surrogateescape",
91107
)
92108

93109
def test_run_git_command_empty_command(self):

0 commit comments

Comments
 (0)