Skip to content

fix(indexer): UTF-8 encoding for git log on non-UTF-8 Windows locales - #143

Open
rajkumarsakthivel wants to merge 7 commits into
mainfrom
fix/git-indexer-encoding-140
Open

fix(indexer): UTF-8 encoding for git log on non-UTF-8 Windows locales#143
rajkumarsakthivel wants to merge 7 commits into
mainfrom
fix/git-indexer-encoding-140

Conversation

@rajkumarsakthivel

Copy link
Copy Markdown
Member

Summary

Fixes #140. Git history indexing fails with UnicodeDecodeError on Windows machines using non-UTF-8 system locales (e.g. Korean cp949, Japanese Shift_JIS, Chinese GBK).

The two subprocess.run calls in index_commits used text=True without an explicit encoding=. Python decodes subprocess output using locale.getpreferredencoding(False), which on Windows returns the system ANSI codepage. Since git log output is always UTF-8, any non-ASCII commit message or author name that isn't valid in the system codepage raises UnicodeDecodeError.

Fix: pass encoding="utf-8", errors="replace" on both calls, matching git's actual output encoding. The errors="replace" fallback handles the rare case where git output contains non-UTF-8 bytes (e.g. legacy repos with Latin-1 commit messages) by substituting U+FFFD instead of crashing.

One-line change per call site, no new dependencies.

#140)

On Windows with non-UTF-8 system locales (e.g. Korean cp949), Python's
subprocess.run with text=True decodes using the system codepage. Git log
output is UTF-8 regardless of locale, so non-ASCII commit messages or
author names cause UnicodeDecodeError, aborting git history indexing.

Pass encoding="utf-8", errors="replace" on both subprocess.run calls in
index_commits to match git's actual output encoding.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes Windows git-history indexing failures caused by decoding git log output using the host locale (non‑UTF‑8 ANSI codepages like cp949/Shift_JIS), by explicitly decoding subprocess output as UTF‑8 with a non-fatal error strategy.

Changes:

  • Add encoding="utf-8" to both subprocess.run(..., text=True) calls in index_commits.
  • Add errors="replace" to prevent crashes on unexpected/non‑UTF‑8 bytes in git output.
Comments suppressed due to low confidence (1)

src/context_engine/indexer/git_indexer.py:47

  • Same as above: if git is configured to output log text in a non-UTF-8 encoding, decoding as UTF-8 will replace characters and lose filenames/metadata context. Force git’s log output encoding to UTF-8 for this call as well to keep producer/decoder consistent.
        ["git", "log", range_arg, "--name-only", "--format=%H"],
        cwd=project_dir, capture_output=True, text=True,
        encoding="utf-8", errors="replace", check=False,

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@@ -32,7 +32,8 @@ 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"],
Comment on lines +35 to +36
cwd=project_dir, capture_output=True, text=True,
encoding="utf-8", errors="replace", check=False,
)

Adds -c i18n.logOutputEncoding=UTF-8 to both git log calls so the
producer and decoder are aligned regardless of the repo's configured
log encoding. Adds a test with Japanese author/message metadata.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (1)

tests/indexer/test_git_indexer.py:117

  • The new non-ASCII test is likely to pass even on the pre-fix implementation because it doesn’t actually exercise the locale-driven decoding path that caused #140; it only asserts the decoded content. To make this a reliable regression test, spy on context_engine.indexer.git_indexer.subprocess.run and assert index_commits() passes encoding='utf-8' and errors='replace' (and avoid leaving nodes/edges unused, which Ruff will flag).
    chunks, nodes, edges = await index_commits(tmp_path, max_commits=10)
    assert len(chunks) == 1
    assert "田中太郎" in chunks[0].metadata["author"]
    assert "機能追加" in chunks[0].content

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Git history indexing fails with UnicodeDecodeError on non-UTF-8 Windows locales (e.g. Korean cp949)

2 participants