@@ -730,29 +730,23 @@ async def test_git_checkout_rejects_flag_injection(repo: git.Repo):
730730# --------------------------------------------------------------------------
731731
732732
733- def without_addresses (text : str ) -> str :
734- """Mask CPython object addresses (`object at 0x...`)."""
735- return re .sub (r" at 0x[0-9a-fA-F]+" , " at 0x?" , text )
736-
737-
738733def show_text (result : dict [str , Any ]) -> str :
739734 assert result ["isError" ] is False
740- return without_addresses ( result ["content" ][0 ]["text" ])
735+ return result ["content" ][0 ]["text" ]
741736
742737
743738def show_header (commit : git .Commit ) -> str :
744- # git_show formats its header with !r, so the reply carries Python reprs:
745- # a quoted sha, `<git.Actor ...>`, and a `datetime.datetime(...)` whose
746- # tzinfo repr includes a memory address. Compared with addresses masked.
747- return without_addresses (
748- f"Commit: { commit .hexsha !r } \n "
749- f "Author: { commit . author !r } \n "
750- f"Date: { commit . authored_datetime !r } \n "
751- f"Message: { commit . message !r } \n "
739+ # git_show's header follows `git show --date=iso`.
740+ date = commit . authored_datetime . strftime ( "%Y-%m-%d %H:%M:%S %z" )
741+ lines = str ( commit . message ). rstrip ( " \n " ). split ( " \n " )
742+ return (
743+ f"commit { commit .hexsha } \n "
744+ "Author: Test User <test@example.com> \n "
745+ f"Date: { date } \n "
746+ " \n " + "" . join ( f" { line } \n " for line in lines )
752747 )
753748
754749
755- # KNOWN BUG #4998: git_show prints Python reprs (quoted sha, <git.Actor>, datetime with a memory address) instead of git's format; the fix changes this assertion.
756750async def test_git_show_commit_with_parent (repo : git .Repo ):
757751 root = root_of (repo )
758752 (root / "test.txt" ).write_text (
@@ -769,29 +763,52 @@ async def test_git_show_commit_with_parent(repo: git.Repo):
769763 + "@@ -1,5 +1,5 @@\n line 1\n line 2\n -line 3\n +SHOWN\n line 4\n line 5\n "
770764 )
771765 text = show_text (result )
772- assert f"Commit: '{ commit .hexsha } '\n " in text
773- assert 'Author: <git.Actor "Test User <test@example.com>">\n ' in text
774- assert "Date: datetime.datetime(" in text
775- assert "Message: 'show me'\n " in text
766+ assert text .startswith (
767+ f"commit { commit .hexsha } \n Author: Test User <test@example.com>\n "
768+ )
769+ assert re .search (
770+ r"\nDate: \d{4}-\d\d-\d\d \d\d:\d\d:\d\d [+-]\d{4}\n\n show me\n" , text
771+ )
772+
773+
774+ async def test_git_show_multiline_message_is_indented (repo : git .Repo ):
775+ root = root_of (repo )
776+ (root / "test.txt" ).write_text ("changed\n " )
777+ repo .index .add (["test.txt" ])
778+ commit = repo .index .commit ("subject\n \n body line\n " )
779+ result = await call (None , "git_show" , {"repo_path" : str (root ), "revision" : "HEAD" })
780+ text = show_text (result )
781+ assert text .startswith (show_header (commit ))
782+ assert "\n \n subject\n \n body line\n \n --- test.txt\n " in text
776783
777784
778- # KNOWN BUG #4998: git_show prints Python reprs (with a memory address) and "--- None" where git prints /dev/null; the fix changes this assertion.
779785async def test_git_show_initial_commit_diffs_against_empty_tree (repo : git .Repo ):
780786 # With no parent the commit is diffed against NULL_TREE. An added file has
781- # no a_path, so the header prints Python's `None` where git prints
782- # `/dev/null`.
787+ # no a_path, so the header prints /dev/null, as git does.
783788 initial = repo .head .commit
784789 result = await call (
785790 None , "git_show" , {"repo_path" : str (root_of (repo )), "revision" : "HEAD" }
786791 )
787792 assert show_text (result ) == (
788793 show_header (initial )
789- + "\n --- None \n +++ test.txt\n "
794+ + "\n --- /dev/null \n +++ test.txt\n "
790795 + "@@ -0,0 +1,5 @@\n +line 1\n +line 2\n +line 3\n +line 4\n +line 5\n "
791796 )
792797
793798
794- # KNOWN BUG #4998: git_show prints Python reprs (quoted sha, <git.Actor>, datetime with a memory address) instead of git's format; the fix changes this assertion.
799+ async def test_git_show_deleted_file_diffs_to_dev_null (repo : git .Repo ):
800+ repo .index .remove (["test.txt" ], working_tree = True )
801+ commit = repo .index .commit ("delete" )
802+ result = await call (
803+ None , "git_show" , {"repo_path" : str (root_of (repo )), "revision" : "HEAD" }
804+ )
805+ assert show_text (result ) == (
806+ show_header (commit )
807+ + "\n --- test.txt\n +++ /dev/null\n "
808+ + "@@ -1,5 +0,0 @@\n -line 1\n -line 2\n -line 3\n -line 4\n -line 5\n "
809+ )
810+
811+
795812async def test_git_show_rename_only_commit_has_header_and_no_patch (repo : git .Repo ):
796813 # A pure rename yields a diff entry whose patch is empty bytes.
797814 repo .index .move (["test.txt" , "renamed.txt" ])
@@ -804,7 +821,6 @@ async def test_git_show_rename_only_commit_has_header_and_no_patch(repo: git.Rep
804821 )
805822
806823
807- # KNOWN BUG #4998: git_show prints Python reprs (quoted sha, <git.Actor>, datetime with a memory address) instead of git's format; the fix changes this assertion.
808824async def test_git_show_binary_commit (repo : git .Repo ):
809825 root = root_of (repo )
810826 (root / "blob.bin" ).write_bytes (bytes (range (256 )))
@@ -813,36 +829,57 @@ async def test_git_show_binary_commit(repo: git.Repo):
813829 result = await call (None , "git_show" , {"repo_path" : str (root ), "revision" : "HEAD" })
814830 assert show_text (result ) == (
815831 show_header (commit )
816- + "\n --- blob.bin \n +++ blob.bin\n "
832+ + "\n --- /dev/null \n +++ blob.bin\n "
817833 + "Binary files /dev/null and b/blob.bin differ\n "
818834 )
819835
820836
821- # KNOWN BUG #4997: git_show decodes the patch as strict UTF-8, so a Latin-1 file fails the whole call; the fix changes this assertion.
822- async def test_git_show_non_utf8_patch_is_decode_error (repo : git .Repo ):
823- # Characterization: the patch bytes are decoded as strict UTF-8, so a
824- # Latin-1 text file makes the whole call fail.
837+ async def test_git_show_non_utf8_patch_is_decoded_with_replacement (repo : git .Repo ):
838+ # A Latin-1 text file's patch is not valid UTF-8; the undecodable byte
839+ # becomes U+FFFD and the rest of the commit is still shown.
825840 root = root_of (repo )
826841 (root / "latin.txt" ).write_bytes ("caf\xe9 \n " .encode ("latin-1" ))
827842 repo .index .add (["latin.txt" ])
828- repo .index .commit ("latin-1" )
843+ commit = repo .index .commit ("latin-1" )
829844 result = await call (None , "git_show" , {"repo_path" : str (root ), "revision" : "HEAD" })
830- assert result ["isError" ] is True
831- assert "codec can't decode byte 0xe9" in result ["content" ][0 ]["text" ]
845+ assert show_text (result ) == (
846+ show_header (commit )
847+ + "\n --- /dev/null\n +++ latin.txt\n "
848+ + "@@ -0,0 +1 @@\n +caf\ufffd \n "
849+ )
832850
833851
834- # KNOWN BUG #1682: pins current (wrong) behavior; the fix changes this assertion.
835- async def test_git_show_revision_path_syntax_is_error (repo : git .Repo ):
836- # Pins #1682's current error: `<rev>:<path>` names a blob, not a commit,
837- # and repo.commit() appends `^0`, so the call fails with this message
838- # rather than crashing the server.
852+ async def test_git_show_revision_path_shows_the_file (repo : git .Repo ):
853+ # `<rev>:<path>` names a blob; git_show returns its content (#1682).
839854 result = await call (
840855 None ,
841856 "git_show" ,
842857 {"repo_path" : str (root_of (repo )), "revision" : "HEAD:test.txt" },
843858 )
859+ # Compared with the raw bytes: the fixture writes text, so CRLF on Windows.
860+ expected = (root_of (repo ) / "test.txt" ).read_bytes ().decode ()
861+ assert result == text_result (expected )
862+
863+
864+ async def test_git_show_revision_path_to_a_directory_lists_it (repo : git .Repo ):
865+ # `<rev>:<dir>` names a tree; git_show lists it, subdirectories with `/`.
866+ root = root_of (repo )
867+ (root / "logic" ).mkdir ()
868+ (root / "logic" / "infos.py" ).write_text ("print('infos')\n " )
869+ repo .index .add (["logic/infos.py" ])
870+ repo .index .commit ("add infos" )
871+ result = await call (None , "git_show" , {"repo_path" : str (root ), "revision" : "HEAD:" })
872+ assert result == text_result ("logic/\n test.txt" )
873+
874+
875+ async def test_git_show_revision_path_not_found_is_error (repo : git .Repo ):
876+ result = await call (
877+ None ,
878+ "git_show" ,
879+ {"repo_path" : str (root_of (repo )), "revision" : "HEAD:nope.txt" },
880+ )
844881 assert result == text_result (
845- "\" Blob or Tree named 'test .txt^0 ' not found\" " , is_error = True
882+ "\" Blob or Tree named 'nope .txt' not found\" " , is_error = True
846883 )
847884
848885
0 commit comments