Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/git/src/mcp_server_git/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
from .server import serve


@click.command()
@click.option("--repository", "-r", type=Path, help="Git repository path")
@click.option("-v", "--verbose", count=True)
Expand All @@ -20,5 +21,6 @@ def main(repository: Path | None, verbose: bool) -> None:
logging.basicConfig(level=logging_level, stream=sys.stderr)
asyncio.run(serve(repository))


if __name__ == "__main__":
main()
48 changes: 44 additions & 4 deletions src/git/tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import shutil
import unittest.mock as mock


@pytest.fixture
def test_repository(tmp_path: Path):
repo_path = tmp_path / "temp_test_repo"
Expand All @@ -32,7 +33,17 @@ def test_repository(tmp_path: Path):

yield test_repo

shutil.rmtree(repo_path)
test_repo.close()

def remove_readonly(func, path, excinfo):
import stat
import os

os.chmod(path, stat.S_IWRITE)
func(path)

shutil.rmtree(repo_path, onerror=remove_readonly)


def test_git_checkout_existing_branch(test_repository):
test_repository.git.branch("test-branch")
Expand All @@ -41,31 +52,37 @@ def test_git_checkout_existing_branch(test_repository):
assert "Switched to branch 'test-branch'" in result
assert test_repository.active_branch.name == "test-branch"

def test_git_checkout_nonexistent_branch(test_repository):

def test_git_checkout_nonexistent_branch(test_repository):
with pytest.raises(BadName):
git_checkout(test_repository, "nonexistent-branch")


def test_git_branch_local(test_repository):
test_repository.git.branch("new-branch-local")
result = git_branch(test_repository, "local")
assert "new-branch-local" in result


def test_git_branch_remote(test_repository):
result = git_branch(test_repository, "remote")
assert "" == result.strip() # Should be empty if no remote branches


def test_git_branch_all(test_repository):
test_repository.git.branch("new-branch-all")
result = git_branch(test_repository, "all")
assert "new-branch-all" in result


def test_git_branch_contains(test_repository):
# Get the default branch name (could be "main" or "master")
default_branch = test_repository.active_branch.name
# Create a new branch and commit to it
test_repository.git.checkout("-b", "feature-branch")
Path(test_repository.working_dir / Path("feature.txt")).write_text("feature content")
Path(test_repository.working_dir / Path("feature.txt")).write_text(
"feature content"
)
test_repository.index.add(["feature.txt"])
commit = test_repository.index.commit("feature commit")
test_repository.git.checkout(default_branch)
Expand All @@ -74,12 +91,15 @@ def test_git_branch_contains(test_repository):
assert "feature-branch" in result
assert default_branch not in result


def test_git_branch_not_contains(test_repository):
# Get the default branch name (could be "main" or "master")
default_branch = test_repository.active_branch.name
# Create a new branch and commit to it
test_repository.git.checkout("-b", "another-feature-branch")
Path(test_repository.working_dir / Path("another_feature.txt")).write_text("another feature content")
Path(test_repository.working_dir / Path("another_feature.txt")).write_text(
"another feature content"
)
test_repository.index.add(["another_feature.txt"])
commit = test_repository.index.commit("another feature commit")
test_repository.git.checkout(default_branch)
Expand All @@ -88,6 +108,7 @@ def test_git_branch_not_contains(test_repository):
assert "another-feature-branch" not in result
assert default_branch in result


def test_git_add_all_files(test_repository):
file_path = Path(test_repository.working_dir) / "all_file.txt"
file_path.write_text("adding all")
Expand All @@ -98,6 +119,7 @@ def test_git_add_all_files(test_repository):
assert "all_file.txt" in staged_files
assert result == "Files staged successfully"


def test_git_add_specific_files(test_repository):
file1 = Path(test_repository.working_dir) / "file1.txt"
file2 = Path(test_repository.working_dir) / "file2.txt"
Expand Down Expand Up @@ -143,6 +165,7 @@ def test_git_status(test_repository):
assert result is not None
assert "On branch" in result or "branch" in result.lower()


def test_git_diff_unstaged(test_repository):
file_path = Path(test_repository.working_dir) / "test.txt"
file_path.write_text("modified content")
Expand All @@ -152,11 +175,13 @@ def test_git_diff_unstaged(test_repository):
assert "test.txt" in result
assert "modified content" in result


def test_git_diff_unstaged_empty(test_repository):
result = git_diff_unstaged(test_repository)

assert result == ""


def test_git_diff_staged(test_repository):
file_path = Path(test_repository.working_dir) / "staged_file.txt"
file_path.write_text("staged content")
Expand All @@ -167,11 +192,13 @@ def test_git_diff_staged(test_repository):
assert "staged_file.txt" in result
assert "staged content" in result


def test_git_diff_staged_empty(test_repository):
result = git_diff_staged(test_repository)

assert result == ""


def test_git_diff(test_repository):
# Get the default branch name (could be "main" or "master")
default_branch = test_repository.active_branch.name
Expand All @@ -186,6 +213,7 @@ def test_git_diff(test_repository):
assert "test.txt" in result
assert "feature changes" in result


def test_git_commit(test_repository):
file_path = Path(test_repository.working_dir) / "commit_test.txt"
file_path.write_text("content to commit")
Expand All @@ -198,6 +226,7 @@ def test_git_commit(test_repository):
latest_commit = test_repository.head.commit
assert latest_commit.message.strip() == "test commit message"


def test_git_reset(test_repository):
file_path = Path(test_repository.working_dir) / "reset_test.txt"
file_path.write_text("content to reset")
Expand All @@ -213,6 +242,7 @@ def test_git_reset(test_repository):
staged_after = [item.a_path for item in test_repository.index.diff("HEAD")]
assert "reset_test.txt" not in staged_after


def test_git_log(test_repository):
for i in range(3):
file_path = Path(test_repository.working_dir) / f"log_test_{i}.txt"
Expand All @@ -229,13 +259,15 @@ def test_git_log(test_repository):
assert "Date:" in result[0]
assert "Message:" in result[0]


def test_git_log_default(test_repository):
result = git_log(test_repository)

assert isinstance(result, list)
assert len(result) >= 1
assert "initial commit" in result[0]


def test_git_create_branch(test_repository):
result = git_create_branch(test_repository, "new-feature-branch")

Expand All @@ -244,6 +276,7 @@ def test_git_create_branch(test_repository):
branches = [ref.name for ref in test_repository.references]
assert "new-feature-branch" in branches


def test_git_create_branch_from_base(test_repository):
test_repository.git.checkout("-b", "base-branch")
file_path = Path(test_repository.working_dir) / "base.txt"
Expand All @@ -255,6 +288,7 @@ def test_git_create_branch_from_base(test_repository):

assert "Created branch 'derived-branch' from 'base-branch'" in result


def test_git_show(test_repository):
file_path = Path(test_repository.working_dir) / "show_test.txt"
file_path.write_text("show content")
Expand All @@ -270,6 +304,7 @@ def test_git_show(test_repository):
assert "show test commit" in result
assert "show_test.txt" in result


def test_git_show_initial_commit(test_repository):
initial_commit = list(test_repository.iter_commits())[-1]

Expand All @@ -282,6 +317,7 @@ def test_git_show_initial_commit(test_repository):

# Tests for validate_repo_path (repository scoping security fix)


def test_validate_repo_path_no_restriction():
"""When no repository restriction is configured, any path should be allowed."""
validate_repo_path(Path("/any/path"), None) # Should not raise
Expand Down Expand Up @@ -341,8 +377,11 @@ def test_validate_repo_path_symlink_escape(tmp_path: Path):
with pytest.raises(ValueError) as exc_info:
validate_repo_path(symlink, allowed)
assert "outside the allowed repository" in str(exc_info.value)


# Tests for argument injection protection


def test_git_diff_rejects_flag_injection(test_repository):
"""git_diff should reject flags that could be used for argument injection."""
with pytest.raises(BadName):
Expand Down Expand Up @@ -457,6 +496,7 @@ def test_git_checkout_rejects_malicious_refs(test_repository):
# git_log, and git_branch — matching the existing guards on git_diff and
# git_checkout.


def test_git_show_rejects_flag_injection(test_repository):
"""git_show should reject revisions starting with '-'."""
with pytest.raises(BadName):
Expand Down