fix: mcp_server_git: correct add logic for ["."] (#2379)

This commit is contained in:
Taj Baba
2025-08-11 16:39:26 +05:30
committed by GitHub
parent c9f7d0275c
commit a67e3b0a92
2 changed files with 28 additions and 2 deletions

View File

@@ -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:

View File

@@ -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
@@ -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"