Skip to content

fix: correct add logic for all vs specific files and add tests (#2373) #2379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
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)
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 sucessfully"

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" in staged_files
assert result == "Files staged sucessfully"