Skip to content

Commit 6c8ff9b

Browse files
committed
Fix pyright failures
1 parent 1e8312a commit 6c8ff9b

2 files changed

Lines changed: 28 additions & 41 deletions

File tree

src/git/src/mcp_server_git/server.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@ def get_repo(self, repo_path: str) -> git.Repo:
219219
"""
220220
if not self.base_path:
221221
raise ValueError("No base repository path configured")
222+
223+
# Initialize resolved_path to ensure it's always bound for exception handlers
224+
resolved_path = Path(repo_path)
225+
222226
try:
223227
resolved_path = self.resolve_repo_path(repo_path)
224228
validated_path = self.path_validator.validate_path(resolved_path)
@@ -229,9 +233,9 @@ def get_repo(self, repo_path: str) -> git.Repo:
229233
raise ValueError(f"Repository root {repo_root} not in allowed scope")
230234
return repo
231235
except git.InvalidGitRepositoryError:
232-
raise ValueError(f"{validated_path} is not a Git repository")
236+
raise ValueError(f"{resolved_path} is not a Git repository")
233237
except git.NoSuchPathError:
234-
raise ValueError(f"Path {validated_path} does not exist")
238+
raise ValueError(f"Path {resolved_path} does not exist")
235239
except ValueError as e:
236240
# Re-raise ValueError with the original message
237241
raise ValueError(str(e))
@@ -295,17 +299,20 @@ def git_create_branch(self, repo: git.Repo, branch_name: str, base_branch: str |
295299
try:
296300
# Try to get the ref (branch)
297301
base = repo.refs[base_branch]
302+
new_branch = repo.create_head(branch_name, base)
303+
return f"Created branch '{branch_name}' from '{base.name}'"
298304
except (IndexError, AttributeError):
299305
# If it's not a ref, try to get the commit
300306
try:
301-
base = repo.commit(base_branch)
307+
commit = repo.commit(base_branch)
308+
new_branch = repo.create_head(branch_name, commit.hexsha)
309+
return f"Created branch '{branch_name}' from '{commit.hexsha}'"
302310
except (git.BadName, ValueError):
303311
raise ValueError(f"Invalid base branch or commit: {base_branch}")
304312
else:
305313
base = repo.active_branch
306-
307-
new_branch = repo.create_head(branch_name, base)
308-
return f"Created branch '{branch_name}' from '{base.name if hasattr(base, 'name') else base.hexsha}'"
314+
new_branch = repo.create_head(branch_name, base)
315+
return f"Created branch '{branch_name}' from '{base.name}'"
309316
except git.GitCommandError as e:
310317
raise ValueError(f"Failed to create branch: {str(e)}")
311318

@@ -437,7 +444,7 @@ async def list_tools() -> list[Tool]:
437444
if repository:
438445
# In single-repo mode with a parent directory,
439446
# we still need repo_path but restrict it to subdirectories
440-
if not git.repo.fun.is_git_dir(repository / ".git"):
447+
if not (repository / ".git").exists():
441448
schema["properties"]["repo_path"]["description"] = "Path to Git repository (must be under the configured base directory)"
442449
else:
443450
# If repository itself is a Git repo, remove repo_path as before
@@ -483,7 +490,7 @@ def by_commandline() -> Sequence[str]:
483490
async def call_tool(name: str, arguments: dict) -> list[TextContent]:
484491
try:
485492
# Auto-use configured repository only if it's actually a Git repository
486-
if repository and git.repo.fun.is_git_dir(repository / ".git"):
493+
if repository and (repository / ".git").exists():
487494
arguments = arguments.copy()
488495
arguments["repo_path"] = str(repository)
489496
elif repository:

src/git/tests/test_server.py

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -50,47 +50,27 @@ def test_path_validator_accepts_inside_path(git_server_with_repo, test_repositor
5050

5151
@pytest.mark.asyncio
5252
async def test_call_tool_uses_configured_repo(test_repository):
53-
# Create a server with configured repository
54-
server = Server("test-git")
53+
# Create a git_server with configured repository
5554
git_server = GitServer(test_repository.working_dir)
5655

5756
# Create a change in the working directory
5857
test_file = Path(test_repository.working_dir) / "test.txt"
5958
test_file.write_text("modified content")
6059

61-
# Mock the call_tool method to use our git_server
62-
async def mock_call_tool(name: str, arguments: dict) -> list[TextContent]:
63-
if name == "git_status":
64-
try:
65-
status = git_server.git_status(git_server.get_repo(arguments["repo_path"]))
66-
return [TextContent(
67-
type="text",
68-
text=f"Repository status:\n{status}"
69-
)]
70-
except ValueError as e:
71-
# We expect this error when trying to use a different path
72-
if "not in allowed scope" in str(e):
73-
# Use the configured repository instead
74-
status = git_server.git_status(git_server.get_repo(test_repository.working_dir))
75-
return [TextContent(
76-
type="text",
77-
text=f"Repository status:\n{status}"
78-
)]
79-
raise
80-
return []
60+
# Test that the git_server correctly handles the configured repository
61+
try:
62+
# This should fail because the path is outside the allowed scope
63+
git_server.get_repo("/some/other/path")
64+
assert False, "Expected ValueError for out-of-scope path"
65+
except ValueError as e:
66+
assert "not in allowed scope" in str(e)
8167

82-
server.call_tool = mock_call_tool
68+
# This should work because it's the configured repository
69+
repo = git_server.get_repo(test_repository.working_dir)
70+
status = git_server.git_status(repo)
8371

84-
# Try to use a different repo_path in arguments
85-
different_path = "/some/other/path"
86-
result = await server.call_tool("git_status", {"repo_path": different_path})
87-
88-
# Verify the configured repository was used instead
89-
assert isinstance(result, list)
90-
assert len(result) == 1
91-
assert isinstance(result[0], TextContent)
92-
assert "Repository status" in result[0].text
93-
assert "test.txt" in result[0].text # Our test file from the fixture
72+
# Verify the configured repository was used
73+
assert "test.txt" in status # Our test file from the fixture
9474

9575
def test_git_server_requires_base_path_for_operations(git_server_without_repo):
9676
with pytest.raises(ValueError, match="No base repository path configured"):

0 commit comments

Comments
 (0)