Skip to content

Commit 3de22b0

Browse files
authored
Merge pull request #5145 from modelcontextprotocol/v2/fix/5107-git-show-diff-prefixes
fix(git): prefix git_show diff headers with a/ and b/
2 parents 0584bee + 5902152 commit 3de22b0

2 files changed

Lines changed: 36 additions & 10 deletions

File tree

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,9 +405,11 @@ def git_show(repo: git.Repo, revision: str) -> str:
405405
else:
406406
diff = commit.diff(git.NULL_TREE, create_patch=True)
407407
for d in diff:
408-
# git prints /dev/null for the missing side of an added or deleted file.
409-
a_path = "/dev/null" if d.new_file or d.a_path is None else d.a_path
410-
b_path = "/dev/null" if d.deleted_file or d.b_path is None else d.b_path
408+
# git prints /dev/null for the missing side of an added or deleted file,
409+
# and prefixes an existing side with a/ or b/, which `git apply` and
410+
# `patch -p1` strip as the first path component.
411+
a_path = "/dev/null" if d.new_file or d.a_path is None else f"a/{d.a_path}"
412+
b_path = "/dev/null" if d.deleted_file or d.b_path is None else f"b/{d.b_path}"
411413
output.append(f"\n--- {a_path}\n+++ {b_path}\n")
412414
if d.diff is None:
413415
continue # pragma: no cover # with create_patch=True GitPython always assigns the patch as bytes

‎src/git/tests/test_protocol.py‎

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ async def test_git_show_commit_with_parent(repo: git.Repo):
759759
)
760760
assert show_text(result) == (
761761
show_header(commit)
762-
+ "\n--- test.txt\n+++ test.txt\n"
762+
+ "\n--- a/test.txt\n+++ b/test.txt\n"
763763
+ "@@ -1,5 +1,5 @@\n line 1\n line 2\n-line 3\n+SHOWN\n line 4\n line 5\n"
764764
)
765765
text = show_text(result)
@@ -779,7 +779,7 @@ async def test_git_show_multiline_message_is_indented(repo: git.Repo):
779779
result = await call(None, "git_show", {"repo_path": str(root), "revision": "HEAD"})
780780
text = show_text(result)
781781
assert text.startswith(show_header(commit))
782-
assert "\n\n subject\n \n body line\n\n--- test.txt\n" in text
782+
assert "\n\n subject\n \n body line\n\n--- a/test.txt\n" in text
783783

784784

785785
async def test_git_show_initial_commit_diffs_against_empty_tree(repo: git.Repo):
@@ -791,7 +791,7 @@ async def test_git_show_initial_commit_diffs_against_empty_tree(repo: git.Repo):
791791
)
792792
assert show_text(result) == (
793793
show_header(initial)
794-
+ "\n--- /dev/null\n+++ test.txt\n"
794+
+ "\n--- /dev/null\n+++ b/test.txt\n"
795795
+ "@@ -0,0 +1,5 @@\n+line 1\n+line 2\n+line 3\n+line 4\n+line 5\n"
796796
)
797797

@@ -804,11 +804,35 @@ async def test_git_show_deleted_file_diffs_to_dev_null(repo: git.Repo):
804804
)
805805
assert show_text(result) == (
806806
show_header(commit)
807-
+ "\n--- test.txt\n+++ /dev/null\n"
807+
+ "\n--- a/test.txt\n+++ /dev/null\n"
808808
+ "@@ -1,5 +0,0 @@\n-line 1\n-line 2\n-line 3\n-line 4\n-line 5\n"
809809
)
810810

811811

812+
async def test_git_show_patch_applies_with_git_apply(repo: git.Repo):
813+
# The a/ and b/ prefixes make the output a patch `git apply` accepts as
814+
# is: without them its default -p1 strips `logic/` from a nested path
815+
# (#5107). Reverse-applying the commit's own patch must check clean.
816+
root = root_of(repo)
817+
(root / "logic").mkdir()
818+
(root / "logic" / "a.py").write_bytes(b"x = 1\n")
819+
repo.index.add(["logic/a.py"])
820+
repo.index.commit("add logic/a.py")
821+
(root / "logic" / "a.py").write_bytes(b"x = 2\n")
822+
repo.index.add(["logic/a.py"])
823+
commit = repo.index.commit("change logic/a.py")
824+
result = await call(None, "git_show", {"repo_path": str(root), "revision": "HEAD"})
825+
text = show_text(result)
826+
assert text == (
827+
show_header(commit)
828+
+ "\n--- a/logic/a.py\n+++ b/logic/a.py\n"
829+
+ "@@ -1 +1 @@\n-x = 1\n+x = 2\n"
830+
)
831+
patch_file = root.parent / "show.patch"
832+
patch_file.write_bytes(text.encode())
833+
repo.git.apply("-R", "--check", str(patch_file))
834+
835+
812836
async def test_git_show_rename_only_commit_has_header_and_no_patch(repo: git.Repo):
813837
# A pure rename yields a diff entry whose patch is empty bytes.
814838
repo.index.move(["test.txt", "renamed.txt"])
@@ -817,7 +841,7 @@ async def test_git_show_rename_only_commit_has_header_and_no_patch(repo: git.Rep
817841
None, "git_show", {"repo_path": str(root_of(repo)), "revision": "HEAD"}
818842
)
819843
assert show_text(result) == (
820-
show_header(commit) + "\n--- test.txt\n+++ renamed.txt\n"
844+
show_header(commit) + "\n--- a/test.txt\n+++ b/renamed.txt\n"
821845
)
822846

823847

@@ -829,7 +853,7 @@ async def test_git_show_binary_commit(repo: git.Repo):
829853
result = await call(None, "git_show", {"repo_path": str(root), "revision": "HEAD"})
830854
assert show_text(result) == (
831855
show_header(commit)
832-
+ "\n--- /dev/null\n+++ blob.bin\n"
856+
+ "\n--- /dev/null\n+++ b/blob.bin\n"
833857
+ "Binary files /dev/null and b/blob.bin differ\n"
834858
)
835859

@@ -844,7 +868,7 @@ async def test_git_show_non_utf8_patch_is_decoded_with_replacement(repo: git.Rep
844868
result = await call(None, "git_show", {"repo_path": str(root), "revision": "HEAD"})
845869
assert show_text(result) == (
846870
show_header(commit)
847-
+ "\n--- /dev/null\n+++ latin.txt\n"
871+
+ "\n--- /dev/null\n+++ b/latin.txt\n"
848872
+ "@@ -0,0 +1 @@\n+caf\ufffd\n"
849873
)
850874

0 commit comments

Comments
 (0)