Skip to content

Commit f3070ed

Browse files
cliffhallclaude
andcommitted
fix(git): print non-ASCII paths as written and replace undecodable diff bytes
git_status, git_diff_unstaged, git_diff_staged and git_diff now run git through one helper that passes `-c core.quotepath=false` for that command only, so a non-ASCII path prints as written instead of as octal escapes that git_add cannot take back (#5129). Nothing is written to the repository's config. Names git C-quotes for other reasons (control characters, a double quote, a backslash) stay quoted. The helper also takes git's output as bytes and decodes it with U+FFFD for undecodable sequences, as git_show does, so a diff of a non-UTF-8 file no longer carries lone surrogates that fail UTF-8 serialization (#5193). Closes #5129 Closes #5193 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent b8260a0 commit f3070ed

3 files changed

Lines changed: 164 additions & 4 deletions

File tree

‎src/git/src/mcp_server_git/server.py‎

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,47 @@ class GitTools(str, Enum):
7373
BRANCH = "git_branch"
7474

7575

76+
def _git_text(repo: git.Repo, command: str, *args: str) -> str:
77+
"""Run `git <command> <args>` for a tool that hands git's output to the client.
78+
79+
Two things about that output are fixed here, for git_status and the three
80+
diff tools alike:
81+
82+
- Paths (#5129). Under git's default `core.quotepath=true`, every byte of a
83+
path at or above 0x80 prints as an octal escape (`"\\346\\227\\245.txt"`),
84+
a name the client cannot pass back to git_add. `-c core.quotepath=false`
85+
prints such names as written, for this one command only: nothing is
86+
written to the repository's config. Git still C-quotes a name holding a
87+
control character (a tab, say) or a double quote or backslash; the flag
88+
governs only the high-bit bytes.
89+
- Content (#5193). GitPython decodes output with surrogateescape, so a
90+
non-UTF-8 file (Latin-1, say) leaves lone surrogates that cannot be
91+
serialized as UTF-8. Take the bytes and replace what does not decode
92+
with U+FFFD, as git_show does.
93+
94+
The `-c` has to come before the subcommand, so it goes through
95+
`repo.git(c=...)`; `repo.git.status(c=...)` would put it after and fail.
96+
That call sets the option on the shared `repo.git` until its next command
97+
runs, which is the one made here.
98+
"""
99+
output = getattr(repo.git(c="core.quotepath=false"), command)(
100+
*args, stdout_as_string=False
101+
)
102+
return output.decode("utf-8", errors="replace")
103+
104+
76105
def git_status(repo: git.Repo) -> str:
77-
return repo.git.status()
106+
return _git_text(repo, "status")
78107

79108

80109
def git_diff_unstaged(
81110
repo: git.Repo, context_lines: int = DEFAULT_CONTEXT_LINES
82111
) -> str:
83-
return repo.git.diff(f"--unified={context_lines}")
112+
return _git_text(repo, "diff", f"--unified={context_lines}")
84113

85114

86115
def git_diff_staged(repo: git.Repo, context_lines: int = DEFAULT_CONTEXT_LINES) -> str:
87-
return repo.git.diff(f"--unified={context_lines}", "--cached")
116+
return _git_text(repo, "diff", f"--unified={context_lines}", "--cached")
88117

89118

90119
def git_diff(
@@ -95,7 +124,7 @@ def git_diff(
95124
if target.startswith("-"):
96125
raise FlagInjectionError(f"Invalid target: '{target}' - cannot start with '-'")
97126
repo.rev_parse(target) # Validates target is a real git ref, throws BadName if not
98-
return repo.git.diff(f"--unified={context_lines}", target)
127+
return _git_text(repo, "diff", f"--unified={context_lines}", target)
99128

100129

101130
def _has_staged_changes(repo: git.Repo) -> bool:

‎src/git/tests/test_protocol.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,42 @@ async def test_git_diff_rejects_flag_injection(repo: git.Repo):
315315
)
316316

317317

318+
async def test_git_diff_of_non_utf8_content_is_a_normal_result(repo: git.Repo):
319+
"""A Latin-1 file's diff reaches the client with U+FFFD in place of the
320+
undecodable bytes, as git_show's does, and serializes as UTF-8 (#5193)."""
321+
root = root_of(repo)
322+
(root / "latin1.txt").write_bytes(b"caf\xe9\n")
323+
repo.index.add(["latin1.txt"])
324+
repo.index.commit("add Latin-1 file")
325+
(root / "latin1.txt").write_bytes(b"caf\xe9 na\xefve\n")
326+
result = await call(None, "git_diff_unstaged", {"repo_path": str(root)})
327+
assert result["isError"] is False
328+
text = result["content"][0]["text"]
329+
assert text.startswith("Unstaged changes:\ndiff --git a/latin1.txt b/latin1.txt")
330+
assert "-caf�\n+caf� na�ve" in text
331+
json.dumps(result, ensure_ascii=False).encode("utf-8") # no lone surrogates
332+
333+
334+
async def test_git_status_non_ascii_path_round_trips_through_git_add(
335+
repo: git.Repo,
336+
):
337+
"""Under git's default core.quotepath=true the name still comes back as
338+
written, so a client can pass it straight to git_add (#5129)."""
339+
root = root_of(repo)
340+
with repo.config_writer() as config:
341+
config.set_value("core", "quotepath", "true")
342+
(root / "日本語.txt").write_text("new\n", newline="\n")
343+
status = await call(None, "git_status", {"repo_path": str(root)})
344+
assert "\t日本語.txt\n" in status["content"][0]["text"]
345+
added = await call(
346+
None, "git_add", {"repo_path": str(root), "files": ["日本語.txt"]}
347+
)
348+
assert added["isError"] is False
349+
# Read the index directly: `staged_paths` lists through git, which would
350+
# octal-escape the name under the pinned core.quotepath.
351+
assert "日本語.txt" in {path for path, _stage in repo.index.entries}
352+
353+
318354
# --------------------------------------------------------------------------
319355
# git_commit
320356
# --------------------------------------------------------------------------

‎src/git/tests/test_server.py‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,101 @@ def test_git_diff(test_repository):
268268
assert "feature changes" in result
269269

270270

271+
# A non-ASCII name, and the octal escape git prints for its first character
272+
# under its default core.quotepath=true (#5129).
273+
NON_ASCII_FILENAME = "日本語.txt"
274+
NON_ASCII_OCTAL = "\\346\\227\\245"
275+
276+
277+
def pin_quotepath_true(repo: git.Repo) -> None:
278+
"""Pin git's default repo-locally, so a global config cannot hide the bug."""
279+
with repo.config_writer() as config:
280+
config.set_value("core", "quotepath", "true")
281+
282+
283+
def commit_non_ascii_file(repo: git.Repo) -> None:
284+
"""Commit NON_ASCII_FILENAME, then leave an unstaged change to it."""
285+
pin_quotepath_true(repo)
286+
file_path = Path(repo.working_dir) / NON_ASCII_FILENAME
287+
file_path.write_text("original\n")
288+
repo.index.add([NON_ASCII_FILENAME])
289+
repo.index.commit("add non-ASCII file")
290+
file_path.write_text("changed\n")
291+
292+
293+
def test_git_status_prints_non_ascii_path_unquoted(test_repository):
294+
pin_quotepath_true(test_repository)
295+
(Path(test_repository.working_dir) / NON_ASCII_FILENAME).write_text("new\n")
296+
297+
result = git_status(test_repository)
298+
299+
assert f"\t{NON_ASCII_FILENAME}\n" in result
300+
assert NON_ASCII_OCTAL not in result
301+
302+
303+
def test_git_status_non_ascii_path_round_trips_through_git_add(test_repository):
304+
pin_quotepath_true(test_repository)
305+
(Path(test_repository.working_dir) / NON_ASCII_FILENAME).write_text("new\n")
306+
307+
untracked = [
308+
line.strip()
309+
for line in git_status(test_repository).splitlines()
310+
if line.startswith("\t")
311+
]
312+
git_add(test_repository, untracked)
313+
314+
staged = [path for path, _stage in test_repository.index.entries]
315+
assert NON_ASCII_FILENAME in staged
316+
317+
318+
@pytest.mark.parametrize("tool", ["unstaged", "staged", "target"])
319+
def test_git_diff_tools_print_non_ascii_path_unquoted(test_repository, tool):
320+
commit_non_ascii_file(test_repository)
321+
if tool == "unstaged":
322+
result = git_diff_unstaged(test_repository)
323+
elif tool == "staged":
324+
test_repository.index.add([NON_ASCII_FILENAME])
325+
result = git_diff_staged(test_repository)
326+
else:
327+
result = git_diff(test_repository, "HEAD")
328+
329+
assert f"diff --git a/{NON_ASCII_FILENAME} b/{NON_ASCII_FILENAME}" in result
330+
assert NON_ASCII_OCTAL not in result
331+
332+
333+
def test_quotepath_override_is_not_written_to_repo_config(test_repository):
334+
commit_non_ascii_file(test_repository)
335+
config_path = Path(test_repository.git_dir) / "config"
336+
before = config_path.read_bytes()
337+
338+
git_status(test_repository)
339+
git_diff_unstaged(test_repository)
340+
341+
assert config_path.read_bytes() == before
342+
# The override was for that one command: a plain call still escapes.
343+
assert NON_ASCII_OCTAL in test_repository.git.status()
344+
345+
346+
@pytest.mark.parametrize("tool", ["unstaged", "staged", "target"])
347+
def test_git_diff_tools_replace_undecodable_bytes(test_repository, tool):
348+
"""A Latin-1 file's diff comes back with U+FFFD, not lone surrogates (#5193)."""
349+
file_path = Path(test_repository.working_dir) / "latin1.txt"
350+
file_path.write_bytes(b"caf\xe9\n")
351+
test_repository.index.add(["latin1.txt"])
352+
test_repository.index.commit("add Latin-1 file")
353+
file_path.write_bytes(b"caf\xe9 na\xefve\n")
354+
if tool == "unstaged":
355+
result = git_diff_unstaged(test_repository)
356+
elif tool == "staged":
357+
test_repository.index.add(["latin1.txt"])
358+
result = git_diff_staged(test_repository)
359+
else:
360+
result = git_diff(test_repository, "HEAD")
361+
362+
assert "+caf� na�ve" in result
363+
result.encode("utf-8") # raises UnicodeEncodeError on a lone surrogate
364+
365+
271366
def test_git_commit(test_repository):
272367
file_path = Path(test_repository.working_dir) / "commit_test.txt"
273368
file_path.write_text("content to commit")

0 commit comments

Comments
 (0)