Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/context_engine/indexer/git_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ async def index_commits(

meta_result = await asyncio.to_thread(
subprocess.run,
["git", "log", range_arg, "--format=%H%n%an%n%ai%n%s%n%b%x00"],
cwd=project_dir, capture_output=True, text=True, check=False,
["git", "-c", "i18n.logOutputEncoding=UTF-8",
"log", range_arg, "--format=%H%n%an%n%ai%n%s%n%b%x00"],
cwd=project_dir, capture_output=True, text=True,
encoding="utf-8", errors="replace", check=False,
Comment thread
rajkumarsakthivel marked this conversation as resolved.
)

if meta_result.returncode != 0:
Expand All @@ -41,8 +43,10 @@ async def index_commits(

files_result = await asyncio.to_thread(
subprocess.run,
["git", "log", range_arg, "--name-only", "--format=%H"],
cwd=project_dir, capture_output=True, text=True, check=False,
["git", "-c", "i18n.logOutputEncoding=UTF-8",
"log", range_arg, "--name-only", "--format=%H"],
cwd=project_dir, capture_output=True, text=True,
encoding="utf-8", errors="replace", check=False,
)

changed_files_by_hash: dict[str, list[str]] = {}
Expand Down
43 changes: 43 additions & 0 deletions tests/indexer/test_git_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import subprocess
from datetime import datetime
from unittest.mock import patch

import pytest
from context_engine.indexer.git_indexer import index_commits
Expand Down Expand Up @@ -89,3 +90,45 @@ async def test_incremental_since_sha(git_repo):
first_sha = chunks_all[-1].metadata["hash"] # oldest commit
chunks_new, _, _ = await index_commits(git_repo, since_sha=first_sha)
assert len(chunks_new) < len(chunks_all)


@pytest.mark.asyncio
async def test_non_ascii_commit_metadata(tmp_path):
"""Non-ASCII author names and commit messages must not crash indexing,
even when the system locale prefers a non-UTF-8 encoding (#140)."""
subprocess.run(["git", "init"], cwd=tmp_path, capture_output=True, check=True)
subprocess.run(
["git", "config", "user.name", "田中太郎"],
cwd=tmp_path, capture_output=True, check=True,
)
subprocess.run(
["git", "config", "user.email", "tanaka@example.com"],
cwd=tmp_path, capture_output=True, check=True,
)
(tmp_path / "hello.py").write_text("print('こんにちは')\n", encoding="utf-8")
subprocess.run(["git", "add", "."], cwd=tmp_path, capture_output=True, check=True)
subprocess.run(
["git", "-c", "commit.gpgsign=false", "commit", "-m", "機能追加: 挨拶モジュール"],
cwd=tmp_path, capture_output=True, check=True,
)

calls: list[dict] = []
real_run = subprocess.run

def spy_run(*args, **kwargs):
calls.append(kwargs)
return real_run(*args, **kwargs)

with patch("subprocess.run", side_effect=spy_run):
chunks, _, _ = await index_commits(tmp_path, max_commits=10)

assert len(chunks) == 1
assert "田中太郎" in chunks[0].metadata["author"]
assert "機能追加" in chunks[0].content

# index_commits must pass encoding/errors so non-ASCII never crashes
git_log_calls = [c for c in calls if c.get("encoding") is not None]
assert git_log_calls, "index_commits should pass encoding kwarg to subprocess.run"
for c in git_log_calls:
assert c["encoding"] == "utf-8"
assert c["errors"] == "replace"
Loading