Skip to content
Open
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
6 changes: 3 additions & 3 deletions src/git/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ classifiers = [
]
dependencies = [
"click>=8.1.7",
"gitpython>=3.1.43",
"gitpython>=3.1.45",
"mcp>=1.0.0",
"pydantic>=2.0.0",
]
Expand All @@ -29,8 +29,8 @@ mcp-server-git = "mcp_server_git:main"
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.uv]
dev-dependencies = ["pyright>=1.1.389", "ruff>=0.7.3", "pytest>=8.0.0"]
[dependency-groups]
dev = ["pyright>=1.1.407", "ruff>=0.7.3", "pytest>=8.0.0"]

[tool.pytest.ini_options]
testpaths = ["tests"]
Expand Down
19 changes: 12 additions & 7 deletions src/git/src/mcp_server_git/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ def git_log(repo: git.Repo, max_count: int = 10, start_timestamp: Optional[str]
if end_timestamp:
args.extend(['--until', end_timestamp])
args.extend(['--format=%H%n%an%n%ad%n%s%n'])

log_output = repo.git.log(*args).split('\n')

log = []
# Process commits in groups of 4 (hash, author, date, message)
for i in range(0, len(log_output), 4):
Expand Down Expand Up @@ -199,7 +199,12 @@ def git_show(repo: git.Repo, revision: str) -> str:
diff = commit.diff(git.NULL_TREE, create_patch=True)
for d in diff:
output.append(f"\n--- {d.a_path}\n+++ {d.b_path}\n")
output.append(d.diff.decode('utf-8'))
if d.diff is None:
continue
if isinstance(d.diff, bytes):
output.append(d.diff.decode('utf-8'))
else:
output.append(d.diff)
return "".join(output)

def git_branch(repo: git.Repo, branch_type: str, contains: str | None = None, not_contains: str | None = None) -> str:
Expand Down Expand Up @@ -343,7 +348,7 @@ def by_commandline() -> Sequence[str]:
@server.call_tool()
async def call_tool(name: str, arguments: dict) -> list[TextContent]:
repo_path = Path(arguments["repo_path"])

# For all commands, we need an existing repo
repo = git.Repo(repo_path)

Expand Down Expand Up @@ -400,7 +405,7 @@ async def call_tool(name: str, arguments: dict) -> list[TextContent]:
# Update the LOG case:
case GitTools.LOG:
log = git_log(
repo,
repo,
arguments.get("max_count", 10),
arguments.get("start_timestamp"),
arguments.get("end_timestamp")
Expand All @@ -409,7 +414,7 @@ async def call_tool(name: str, arguments: dict) -> list[TextContent]:
type="text",
text="Commit history:\n" + "\n".join(log)
)]

case GitTools.CREATE_BRANCH:
result = git_create_branch(
repo,
Expand Down Expand Up @@ -446,7 +451,7 @@ async def call_tool(name: str, arguments: dict) -> list[TextContent]:
type="text",
text=result
)]

case _:
raise ValueError(f"Unknown tool: {name}")

Expand Down
12 changes: 8 additions & 4 deletions src/git/tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,32 @@ def test_git_branch_all(test_repository):
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")
test_repository.index.add(["feature.txt"])
commit = test_repository.index.commit("feature commit")
test_repository.git.checkout("master")
test_repository.git.checkout(default_branch)

result = git_branch(test_repository, "local", contains=commit.hexsha)
assert "feature-branch" in result
assert "master" not 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")
test_repository.index.add(["another_feature.txt"])
commit = test_repository.index.commit("another feature commit")
test_repository.git.checkout("master")
test_repository.git.checkout(default_branch)

result = git_branch(test_repository, "local", not_contains=commit.hexsha)
assert "another-feature-branch" not in result
assert "master" 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"
Expand Down
Loading