Skip to content

Commit 3266eac

Browse files
Merge pull request Aider-AI#4150 from muravvv/fix_encoding
Fix issues on repositories with non-Unicode encodings
2 parents 484b8a3 + bfaad12 commit 3266eac

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

aider/io.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ def log_llm_history(self, role, content):
749749
if not self.llm_history_file:
750750
return
751751
timestamp = datetime.now().isoformat(timespec="seconds")
752-
with open(self.llm_history_file, "a", encoding=self.encoding) as log_file:
752+
with open(self.llm_history_file, "a", encoding="utf-8") as log_file:
753753
log_file.write(f"{role.upper()} {timestamp}\n")
754754
log_file.write(content + "\n")
755755

aider/repo.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,14 +391,20 @@ def get_diffs(self, fnames=None):
391391
try:
392392
if current_branch_has_commits:
393393
args = ["HEAD", "--"] + list(fnames)
394-
diffs += self.repo.git.diff(*args)
394+
diffs += self.repo.git.diff(*args, stdout_as_string=False).decode(
395+
self.io.encoding, "replace"
396+
)
395397
return diffs
396398

397399
wd_args = ["--"] + list(fnames)
398400
index_args = ["--cached"] + wd_args
399401

400-
diffs += self.repo.git.diff(*index_args)
401-
diffs += self.repo.git.diff(*wd_args)
402+
diffs += self.repo.git.diff(*index_args, stdout_as_string=False).decode(
403+
self.io.encoding, "replace"
404+
)
405+
diffs += self.repo.git.diff(*wd_args, stdout_as_string=False).decode(
406+
self.io.encoding, "replace"
407+
)
402408

403409
return diffs
404410
except ANY_GIT_ERROR as err:
@@ -412,7 +418,9 @@ def diff_commits(self, pretty, from_commit, to_commit):
412418
args += ["--color=never"]
413419

414420
args += [from_commit, to_commit]
415-
diffs = self.repo.git.diff(*args)
421+
diffs = self.repo.git.diff(*args, stdout_as_string=False).decode(
422+
self.io.encoding, "replace"
423+
)
416424

417425
return diffs
418426

tests/basic/test_repo.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,28 @@ def test_diffs_nonempty_repo(self):
5959
self.assertIn("index", diffs)
6060
self.assertIn("workingdir", diffs)
6161

62+
def test_diffs_with_single_byte_encoding(self):
63+
with GitTemporaryDirectory():
64+
encoding = "cp1251"
65+
66+
repo = git.Repo()
67+
68+
fname = Path("foo.txt")
69+
fname.write_text("index\n", encoding=encoding)
70+
repo.git.add(str(fname))
71+
72+
# Make a change with non-ASCII symbols in the working dir
73+
fname.write_text("АБВ\n", encoding=encoding)
74+
75+
git_repo = GitRepo(InputOutput(encoding=encoding), None, ".")
76+
diffs = git_repo.get_diffs()
77+
78+
# check that all diff output can be converted to utf-8 for sending to model
79+
diffs.encode("utf-8")
80+
81+
self.assertIn("index", diffs)
82+
self.assertIn("АБВ", diffs)
83+
6284
def test_diffs_detached_head(self):
6385
with GitTemporaryDirectory():
6486
repo = git.Repo()

0 commit comments

Comments
 (0)