@@ -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\xef ve\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+
271366def 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