Skip to content

Commit d1f63f4

Browse files
committed
verify(git): quotepath=false for the four path-printing tools, plus the tests from modelcontextprotocol#4879 ported to v2/main
Evidence only, not a contribution: the repository takes issues, not pull requests. Implements the approach prototyped in modelcontextprotocol#4879 on top of v2/main @ 38569bd and re-runs its four tests to check the claims in the issue.
1 parent 38569bd commit d1f63f4

2 files changed

Lines changed: 76 additions & 4 deletions

File tree

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,18 +152,26 @@ class GitTools(str, Enum):
152152
BRANCH = "git_branch"
153153

154154

155+
def _git_unquoted_paths(repo: git.Repo) -> git.Git:
156+
# git's default core.quotepath=true prints non-ASCII paths as quoted octal
157+
# escapes (e.g. "\346\227\245.txt"), which a client cannot map back to a real
158+
# file, so a follow-up git_add with the printed name fails. -c sets the option
159+
# for this command only; it is not written to the repository's config.
160+
return repo.git(c="core.quotepath=false")
161+
162+
155163
def git_status(repo: git.Repo) -> str:
156-
return repo.git.status()
164+
return _git_unquoted_paths(repo).status()
157165

158166

159167
def git_diff_unstaged(
160168
repo: git.Repo, context_lines: int = DEFAULT_CONTEXT_LINES
161169
) -> str:
162-
return repo.git.diff(f"--unified={context_lines}")
170+
return _git_unquoted_paths(repo).diff(f"--unified={context_lines}")
163171

164172

165173
def git_diff_staged(repo: git.Repo, context_lines: int = DEFAULT_CONTEXT_LINES) -> str:
166-
return repo.git.diff(f"--unified={context_lines}", "--cached")
174+
return _git_unquoted_paths(repo).diff(f"--unified={context_lines}", "--cached")
167175

168176

169177
def git_diff(
@@ -174,7 +182,7 @@ def git_diff(
174182
if target.startswith("-"):
175183
raise FlagInjectionError(f"Invalid target: '{target}' - cannot start with '-'")
176184
repo.rev_parse(target) # Validates target is a real git ref, throws BadName if not
177-
return repo.git.diff(f"--unified={context_lines}", target)
185+
return _git_unquoted_paths(repo).diff(f"--unified={context_lines}", target)
178186

179187

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

‎src/git/tests/test_server.py‎

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

270270

271+
# --- ported verbatim from the fix prototyped in #4879, to check its claims ----
272+
273+
NON_ASCII_FILENAME = "日本語.txt"
274+
ESCAPED_NON_ASCII_FILENAME = r"\346\227\245\346\234\254\350\252\236.txt"
275+
276+
277+
@pytest.fixture
278+
def quotepath_repository(test_repository):
279+
# Pin git's default explicitly so the tests don't depend on the user's global config
280+
with test_repository.config_writer() as config:
281+
config.set_value("core", "quotepath", "true")
282+
return test_repository
283+
284+
285+
def test_git_status_non_ascii_filename(quotepath_repository):
286+
Path(quotepath_repository.working_dir, NON_ASCII_FILENAME).write_text(
287+
"content", encoding="utf-8"
288+
)
289+
290+
result = git_status(quotepath_repository)
291+
292+
assert NON_ASCII_FILENAME in result
293+
assert ESCAPED_NON_ASCII_FILENAME not in result
294+
295+
296+
def test_git_diff_unstaged_non_ascii_filename(quotepath_repository):
297+
file_path = Path(quotepath_repository.working_dir, NON_ASCII_FILENAME)
298+
file_path.write_text("content", encoding="utf-8")
299+
quotepath_repository.index.add([NON_ASCII_FILENAME])
300+
file_path.write_text("modified content", encoding="utf-8")
301+
302+
result = git_diff_unstaged(quotepath_repository)
303+
304+
assert f"a/{NON_ASCII_FILENAME}" in result
305+
assert ESCAPED_NON_ASCII_FILENAME not in result
306+
307+
308+
def test_git_diff_staged_non_ascii_filename(quotepath_repository):
309+
Path(quotepath_repository.working_dir, NON_ASCII_FILENAME).write_text(
310+
"content", encoding="utf-8"
311+
)
312+
quotepath_repository.index.add([NON_ASCII_FILENAME])
313+
314+
result = git_diff_staged(quotepath_repository)
315+
316+
assert f"b/{NON_ASCII_FILENAME}" in result
317+
assert ESCAPED_NON_ASCII_FILENAME not in result
318+
319+
320+
def test_git_diff_non_ascii_filename(quotepath_repository):
321+
default_branch = quotepath_repository.active_branch.name
322+
quotepath_repository.git.checkout("-b", "feature-non-ascii")
323+
Path(quotepath_repository.working_dir, NON_ASCII_FILENAME).write_text(
324+
"content", encoding="utf-8"
325+
)
326+
quotepath_repository.index.add([NON_ASCII_FILENAME])
327+
quotepath_repository.index.commit("add non-ASCII file")
328+
329+
result = git_diff(quotepath_repository, default_branch)
330+
331+
assert f"b/{NON_ASCII_FILENAME}" in result
332+
assert ESCAPED_NON_ASCII_FILENAME not in result
333+
334+
271335
def test_git_commit(test_repository):
272336
file_path = Path(test_repository.working_dir) / "commit_test.txt"
273337
file_path.write_text("content to commit")

0 commit comments

Comments
 (0)