Skip to content
Merged
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
5 changes: 4 additions & 1 deletion src/git/src/mcp_server_git/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ def git_commit(repo: git.Repo, message: str) -> str:
return f"Changes committed successfully with hash {commit.hexsha}"

def git_add(repo: git.Repo, files: list[str]) -> str:
repo.index.add(files)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forgive my ignorance here, but is there a reason we don't just use repo.git.add always? e.g. could we get away with not making . a special case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forgive my ignorance here, but is there a reason we don't just use repo.git.add always? e.g. could we get away with not making . a special case?

The issue was caused by using repo.index.add(["."]), which doesn't behave like git add . and ends up including .git/ files, corrupting the repo. I added a check to use repo.git.add(".") when ["."] is passed, which correctly mimics Git’s behavior. This keeps things safe and avoids staging internal Git files.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant why not just repo.git.add everything (i.e. never use repo.index.add)?

Or @claude if you're around are you able to chime in?

if files == ["."]:
repo.git.add(".")
else:
repo.index.add(files)
return "Files staged successfully"

def git_reset(repo: git.Repo) -> str:
Expand Down
25 changes: 24 additions & 1 deletion src/git/tests/test_server.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from pathlib import Path
import git
from mcp_server_git.server import git_checkout, git_branch
from mcp_server_git.server import git_checkout, git_branch, git_add
import shutil

@pytest.fixture
Expand Down Expand Up @@ -68,3 +68,26 @@ def test_git_branch_not_contains(test_repository):
result = git_branch(test_repository, "local", not_contains=commit.hexsha)
assert "another-feature-branch" not in result
assert "master" 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")

result = git_add(test_repository, ["."])

staged_files = [item.a_path for item in test_repository.index.diff("HEAD")]
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"
file1.write_text("file 1 content")
file2.write_text("file 2 content")

result = git_add(test_repository, ["file1.txt"])

staged_files = [item.a_path for item in test_repository.index.diff("HEAD")]
assert "file1.txt" in staged_files
assert "file2.txt" not in staged_files
assert result == "Files staged successfully"