Skip to content

Commit 1794cf6

Browse files
committed
fix(git): emit git-compatible diff header (a/ b/ prefixes, diff --git line) in git_show
git_show built the unified-diff header by hand and printed --- <path> / +++ <path> directly, omitting both the diff --git a/X b/Y line and the a/ / b/ path prefixes that git show always emits. As a result the output could not be fed straight into git apply / patch (issue modelcontextprotocol#5107). The header now mirrors git show: - a diff --git a/<name> b/<name> line using the real paths (even when one side is /dev/null, which only appears in the ---/+++ lines), - --- a/<path> / +++ b/<path>, with /dev/null for the missing side. Added regression coverage in src/git/tests/test_git_show_diff_header.py. Fixes modelcontextprotocol#5107
1 parent bbb701e commit 1794cf6

2 files changed

Lines changed: 82 additions & 1 deletion

File tree

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,20 @@ def git_show(repo: git.Repo, revision: str) -> str:
389389
# git prints /dev/null for the missing side of an added or deleted file.
390390
a_path = "/dev/null" if d.new_file or d.a_path is None else d.a_path
391391
b_path = "/dev/null" if d.deleted_file or d.b_path is None else d.b_path
392-
output.append(f"\n--- {a_path}\n+++ {b_path}\n")
392+
# git prefixes the paths with a/ and b/ in the diff header so the
393+
# output can be applied by `git apply` / `patch` (see issue #5107).
394+
# The `diff --git` line always prints the real paths with a/ and b/
395+
# prefixes (even when one side is /dev/null, which only appears in the
396+
# ---/+++ header lines), matching `git show`.
397+
a_header = "/dev/null" if a_path == "/dev/null" else f"a/{a_path}"
398+
b_header = "/dev/null" if b_path == "/dev/null" else f"b/{b_path}"
399+
a_name = a_path if a_path != "/dev/null" else b_path
400+
b_name = b_path if b_path != "/dev/null" else a_path
401+
output.append(
402+
f"\ndiff --git a/{a_name} b/{b_name}\n"
403+
f"--- {a_header}\n"
404+
f"+++ {b_header}\n"
405+
)
393406
if d.diff is None:
394407
continue # pragma: no cover # with create_patch=True GitPython always assigns the patch as bytes
395408
if isinstance(d.diff, bytes):
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# -*- coding: utf-8 -*-
2+
"""Regression tests for the git_show diff header (issue #5107).
3+
4+
git_show must emit a git-compatible unified-diff header - a `diff --git`
5+
line plus `a/` and `b/` prefixed `---`/`+++` paths - so that its output
6+
matches `git show` and can be consumed by `git apply` / `patch`.
7+
"""
8+
from pathlib import Path
9+
10+
import git
11+
12+
from mcp_server_git.server import git_show
13+
14+
15+
def _init_repo(path: Path) -> git.Repo:
16+
repo = git.Repo.init(path)
17+
with repo.config_writer() as cw:
18+
cw.set_value("user", "name", "Test")
19+
cw.set_value("user", "email", "[email protected]")
20+
cw.set_value("core", "autocrlf", "false")
21+
return repo
22+
23+
24+
def _commit(repo: git.Repo, message: str) -> git.Commit:
25+
repo.git.add(A=True)
26+
return repo.index.commit(message)
27+
28+
29+
def test_git_show_modified_file_header(tmp_path: Path):
30+
repo = _init_repo(tmp_path)
31+
(tmp_path / "demo.txt").write_text("line1\nline2\n")
32+
_commit(repo, "initial")
33+
(tmp_path / "demo.txt").write_text("line1\nline2 modified\nline3\n")
34+
second = _commit(repo, "modify demo")
35+
36+
out = git_show(repo, second.hexsha)
37+
38+
assert "diff --git a/demo.txt b/demo.txt" in out
39+
assert "--- a/demo.txt" in out
40+
assert "+++ b/demo.txt" in out
41+
42+
43+
def test_git_show_added_file_header(tmp_path: Path):
44+
repo = _init_repo(tmp_path)
45+
(tmp_path / "new.txt").write_text("fresh\n")
46+
first = _commit(repo, "add new.txt")
47+
48+
out = git_show(repo, first.hexsha)
49+
50+
# The diff --git line always uses the real paths (a/new.txt b/new.txt),
51+
# while the ---/+++ header lines use /dev/null for the missing side.
52+
assert "diff --git a/new.txt b/new.txt" in out
53+
assert "--- /dev/null" in out
54+
assert "+++ b/new.txt" in out
55+
56+
57+
def test_git_show_deleted_file_header(tmp_path: Path):
58+
repo = _init_repo(tmp_path)
59+
(tmp_path / "old.txt").write_text("going away\n")
60+
_commit(repo, "add old.txt")
61+
repo.git.rm("old.txt")
62+
second = _commit(repo, "delete old.txt")
63+
64+
out = git_show(repo, second.hexsha)
65+
66+
assert "diff --git a/old.txt b/old.txt" in out
67+
assert "--- a/old.txt" in out
68+
assert "+++ /dev/null" in out

0 commit comments

Comments
 (0)