From 9a54466b53ad804f727adafb7e315bbdc335c714 Mon Sep 17 00:00:00 2001 From: sama Pyb Date: Tue, 1 Dec 2026 18:34:15 +0800 Subject: [PATCH] git_local, github: decode subprocess output with replacement `_run_git` and `_run_gh` decoded subprocess output as strict UTF-8, so a single non-UTF-8 byte in a commit diff, commit message, or PR diff raised an unhandled `UnicodeDecodeError` and aborted generation instead of letting the caller handle or skip the candidate (unlike `gitlab.py` and `bootstrap/docker.py`). Also pass `-z` to `git show --name-only` in `changed_files` so non-ASCII paths are returned verbatim instead of C-escaped with quotes. --- src/repo2rlenv/git_local.py | 5 +++-- src/repo2rlenv/github.py | 1 + tests/test_git_local.py | 37 +++++++++++++++++++++++++++++++++++++ tests/test_read_retry.py | 15 +++++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/repo2rlenv/git_local.py b/src/repo2rlenv/git_local.py index efe17977..627d0589 100644 --- a/src/repo2rlenv/git_local.py +++ b/src/repo2rlenv/git_local.py @@ -78,6 +78,7 @@ def _run_git(args: list[str], cwd: Path, *, timeout: int = 60) -> str: capture_output=True, text=True, encoding="utf-8", + errors="replace", timeout=timeout, check=False, ) @@ -172,8 +173,8 @@ def show_diff(clone_dir: Path, commit_sha: str) -> str: def changed_files(clone_dir: Path, commit_sha: str) -> list[str]: """Return the list of paths touched by `commit_sha` (relative to repo root).""" raw = _run_git( - ["show", "--no-color", "--format=", "--name-only", commit_sha], + ["show", "--no-color", "--format=", "--name-only", "-z", commit_sha], clone_dir, timeout=30, ) - return [line for line in raw.splitlines() if line.strip()] + return [path for path in raw.split("\0") if path] diff --git a/src/repo2rlenv/github.py b/src/repo2rlenv/github.py index 337b4951..3f0dc3b1 100644 --- a/src/repo2rlenv/github.py +++ b/src/repo2rlenv/github.py @@ -72,6 +72,7 @@ def _run_gh(args: list[str], token: str | None = None) -> str: capture_output=True, text=True, encoding="utf-8", + errors="replace", timeout=60, env=env, check=False, diff --git a/tests/test_git_local.py b/tests/test_git_local.py index 4e20f8d1..fb3139a1 100644 --- a/tests/test_git_local.py +++ b/tests/test_git_local.py @@ -9,6 +9,7 @@ import shutil import subprocess +import unicodedata from pathlib import Path import pytest @@ -231,3 +232,39 @@ def test_commit_info_message_no_body(): body="", ) assert ci.message == "subject only" + + +def test_show_diff_replaces_invalid_utf8_bytes(tmp_path: Path): + """A commit touching a non-UTF-8 fixture must not raise UnicodeDecodeError.""" + repo = tmp_path / "latin1" + repo.mkdir() + _git("init", "-q", "-b", "main", cwd=repo) + (repo / "fixture.txt").write_bytes(b"caf\xe9\n") + _git("add", "fixture.txt", cwd=repo) + _git("commit", "-q", "-m", "add latin1 fixture", cwd=repo) + + sha = list_commits(repo, limit=1)[0].sha + diff = show_diff(repo, sha) + assert "fixture.txt" in diff + assert "+caf\ufffd" in diff + + +def test_changed_files_preserves_unquoted_non_ascii_paths(tmp_path: Path): + """changed_files must return raw UTF-8 paths rather than git's C-escaped quotes.""" + repo = tmp_path / "unicode_paths" + repo.mkdir() + _git("init", "-q", "-b", "main", cwd=repo) + (repo / "caf\u00e9.txt").write_text("v1\n", encoding="utf-8") + _git("add", ".", cwd=repo) + _git("commit", "-q", "-m", "add unicode file", cwd=repo) + + (repo / "caf\u00e9.txt").write_text("v2\n", encoding="utf-8") + _git("add", ".", cwd=repo) + _git("commit", "-q", "-m", "update unicode file", cwd=repo) + + commits = list_commits(repo, limit=2) + for commit in commits: + files = changed_files(repo, commit.sha) + assert len(files) == 1 + assert not files[0].startswith('"') + assert unicodedata.normalize("NFC", files[0]) == "caf\u00e9.txt" diff --git a/tests/test_read_retry.py b/tests/test_read_retry.py index 952f8e86..7560bcbe 100644 --- a/tests/test_read_retry.py +++ b/tests/test_read_retry.py @@ -1,4 +1,5 @@ import subprocess +import sys from unittest.mock import Mock import pytest @@ -90,3 +91,17 @@ def test_github_read_recovers_after_server_error(monkeypatch): monkeypatch.setattr("repo2rlenv.github.subprocess.run", run) assert _run_gh(["pr", "list", "--repo", "a/b"]) == "[]" assert run.call_count == 2 + + +def test_github_run_gh_replaces_invalid_utf8_bytes(monkeypatch): + monkeypatch.setattr("repo2rlenv.github.shutil.which", lambda _: "/usr/bin/gh") + real_run = subprocess.run + script = ( + "import sys; sys.stdout.buffer.write(b'+caf\\xe9\\n'); sys.stderr.buffer.write(b'\\xff')" + ) + + def run_python(cmd, **kwargs): + return real_run([sys.executable, "-c", script], **kwargs) + + monkeypatch.setattr("repo2rlenv.github.subprocess.run", run_python) + assert _run_gh(["pr", "diff", "1", "--repo", "a/b"]) == "+caf\ufffd\n"