Skip to content

Commit 01075ca

Browse files
cliffhallFazeelUsmaniclaude
committed
fix(git): resolve --repository to the repo root; log a missing path
`--repository` now walks up to the enclosing working tree, the way `git rev-parse --show-toplevel` does, so `--repository .` works when a shared config is launched from any subdirectory of the repository (#3029). A resolved path that differs from the one given is logged at startup. Ported from #3997. A `--repository` path that does not exist no longer escapes serve() as NoSuchPathError with a Python traceback: it is logged as a one-line error, like an invalid repository, and the process exits 1 (#4993). The pinning tests in test_entrypoints.py now assert the fixed behavior and their KNOWN BUG markers are gone. Closes #3029 Closes #4993 Co-authored-by: Fazeel Usmani <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 571e2b3 commit 01075ca

3 files changed

Lines changed: 65 additions & 33 deletions

File tree

‎src/git/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ python -m mcp_server_git
123123

124124
## Configuration
125125

126+
`--repository` (`-r`) accepts any path inside a Git working tree. At startup the server walks up to the enclosing repository root, the way `git rev-parse --show-toplevel` does, so `--repository .` works when a shared config is launched from any subdirectory of the repository. A path inside the repository therefore allows tool calls anywhere in that repository, not only under the subdirectory given. A path that does not exist, or is not inside a Git repository, is logged as a one-line error at startup.
127+
126128
### Usage with Claude Desktop
127129

128130
Add this to your `claude_desktop_config.json`:

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,21 @@ async def serve(repository: Path | None) -> None:
331331

332332
if repository is not None:
333333
try:
334-
git.Repo(repository)
335-
logger.info(f"Using repository at {repository}")
334+
# Walk up to the enclosing working tree, like `git rev-parse
335+
# --show-toplevel`, so `--repository .` works from a subdirectory.
336+
root = Path(
337+
git.Repo(repository, search_parent_directories=True).working_dir
338+
)
339+
except git.NoSuchPathError:
340+
logger.error(f"{repository} does not exist")
341+
raise SystemExit(1)
336342
except git.InvalidGitRepositoryError:
337343
logger.error(f"{repository} is not a valid Git repository")
338344
return
345+
if root != repository:
346+
logger.info(f"Resolved --repository {repository} to repository root {root}")
347+
repository = root
348+
logger.info(f"Using repository at {repository}")
339349

340350
server = Server("mcp-git")
341351

‎src/git/tests/test_entrypoints.py‎

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,38 @@ async def test_serve_logs_the_repository_it_uses(
3838
assert f"Using repository at {root}" in caplog.text
3939

4040

41-
# KNOWN BUG #3029: pins current (wrong) behavior; the fix changes this assertion.
42-
async def test_serve_returns_early_for_repository_subdirectory(
41+
async def test_serve_resolves_repository_subdirectory_to_root(
4342
repo: git.Repo, caplog: pytest.LogCaptureFixture
4443
):
45-
# Pins #3029: `--repository` pointing inside a working tree (for example
46-
# `.` from a subdirectory) is not resolved to the repository root. serve()
47-
# logs an error and returns before opening stdio, so the server exits
48-
# without serving. Fixing #3029 changes this test.
44+
# #3029: `--repository` pointing inside a working tree is resolved to the
45+
# repository root, like `git rev-parse --show-toplevel`, and the server
46+
# serves that root.
4947
assert repo.working_dir is not None
50-
sub = Path(repo.working_dir) / "sub"
48+
root = Path(repo.working_dir)
49+
sub = root / "sub" / "deeper"
50+
sub.mkdir(parents=True)
51+
with caplog.at_level(logging.INFO, logger="mcp_server_git.server"):
52+
async with connect(sub) as session:
53+
result = await session.call_tool("git_status", {"repo_path": str(root)})
54+
assert not result.isError
55+
assert f"Resolved --repository {sub} to repository root {root}" in caplog.text
56+
assert f"Using repository at {root}" in caplog.text
57+
58+
59+
async def test_serve_resolves_dot_from_subdirectory(
60+
repo: git.Repo, caplog: pytest.LogCaptureFixture, monkeypatch: pytest.MonkeyPatch
61+
):
62+
# #3029: the shared-config case, `--repository .` launched from a
63+
# subdirectory of the repository.
64+
assert repo.working_dir is not None
65+
root = Path(repo.working_dir)
66+
sub = root / "sub"
5167
sub.mkdir()
52-
stdio = mock.MagicMock()
53-
with (
54-
mock.patch("mcp_server_git.server.stdio_server", stdio),
55-
caplog.at_level(logging.ERROR, logger="mcp_server_git.server"),
56-
):
57-
assert await serve(sub) is None
58-
stdio.assert_not_called()
59-
assert f"{sub} is not a valid Git repository" in caplog.text
68+
monkeypatch.chdir(sub)
69+
with caplog.at_level(logging.INFO, logger="mcp_server_git.server"):
70+
async with connect(Path(".")):
71+
pass
72+
assert f"Using repository at {root}" in caplog.text
6073

6174

6275
async def test_serve_returns_early_for_non_repository(
@@ -72,16 +85,22 @@ async def test_serve_returns_early_for_non_repository(
7285
assert f"{tmp_path} is not a valid Git repository" in caplog.text
7386

7487

75-
# KNOWN BUG #4993: a nonexistent --repository escapes serve() as NoSuchPathError instead of a logged error; the fix changes this assertion.
76-
async def test_serve_raises_for_nonexistent_repository(tmp_path: Path):
77-
# Characterization: serve() catches only InvalidGitRepositoryError, so a
78-
# nonexistent path escapes as NoSuchPathError and kills the server with a
79-
# traceback (see the console-script smoke below).
88+
async def test_serve_exits_for_nonexistent_repository(
89+
tmp_path: Path, caplog: pytest.LogCaptureFixture
90+
):
91+
# #4993: a nonexistent path is logged like an invalid repository, and the
92+
# server exits non-zero without opening stdio.
8093
stdio = mock.MagicMock()
81-
with mock.patch("mcp_server_git.server.stdio_server", stdio):
82-
with pytest.raises(git.NoSuchPathError):
83-
await serve(tmp_path / "missing")
94+
missing = tmp_path / "missing"
95+
with (
96+
mock.patch("mcp_server_git.server.stdio_server", stdio),
97+
caplog.at_level(logging.ERROR, logger="mcp_server_git.server"),
98+
):
99+
with pytest.raises(SystemExit) as exc:
100+
await serve(missing)
101+
assert exc.value.code == 1
84102
stdio.assert_not_called()
103+
assert f"{missing} does not exist" in caplog.text
85104

86105

87106
# --------------------------------------------------------------------------
@@ -167,12 +186,12 @@ def test_python_dash_m_calls_main():
167186
fake_main.assert_called_once_with()
168187

169188

170-
# KNOWN BUG #4993: a nonexistent --repository kills the server with a Python traceback instead of a one-line error; the fix changes this assertion.
171-
def test_console_script_nonexistent_repository_exits_with_traceback(tmp_path: Path):
189+
def test_console_script_nonexistent_repository_exits_with_one_line_error(
190+
tmp_path: Path,
191+
):
172192
# The one subprocess test: the installed entry point, end to end. A
173-
# nonexistent --repository is not caught (see
174-
# test_serve_raises_for_nonexistent_repository), so the process dies with
175-
# a Python traceback instead of a one-line error.
193+
# nonexistent --repository exits non-zero with a one-line logged error
194+
# naming the path, not a Python traceback (#4993).
176195
script = shutil.which("mcp-server-git", path=str(Path(sys.executable).parent))
177196
assert script is not None
178197
missing = tmp_path / "missing"
@@ -184,6 +203,7 @@ def test_console_script_nonexistent_repository_exits_with_traceback(tmp_path: Pa
184203
timeout=60,
185204
)
186205
assert proc.returncode == 1
187-
assert "Traceback (most recent call last)" in proc.stderr
188-
assert "git.exc.NoSuchPathError" in proc.stderr
189-
assert str(missing) in proc.stderr
206+
assert "Traceback" not in proc.stderr
207+
assert proc.stderr.splitlines() == [
208+
f"ERROR:mcp_server_git.server:{missing} does not exist"
209+
]

0 commit comments

Comments
 (0)