Skip to content
Open
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
5 changes: 3 additions & 2 deletions src/repo2rlenv/git_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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]
1 change: 1 addition & 0 deletions src/repo2rlenv/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
37 changes: 37 additions & 0 deletions tests/test_git_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import shutil
import subprocess
import unicodedata
from pathlib import Path

import pytest
Expand Down Expand Up @@ -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"
15 changes: 15 additions & 0 deletions tests/test_read_retry.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import subprocess
import sys
from unittest.mock import Mock

import pytest
Expand Down Expand Up @@ -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"