Skip to content

Commit b09395c

Browse files
cliffhallConnorMoss02Sumo-99claude
committed
fix(git): stop reporting staged files when nothing was staged (#4763)
`git add` exits 0 when it stages nothing, so git_add returned "Files staged successfully" for an empty file list and for '.' on a clean tree. An empty list is now rejected (minItems: 1 in the schema, plus a guard in git_add), and the index entries matching the pathspecs are compared before and after the add, so a call that left the index unchanged says "No changes were staged" instead. The two protocol tests that pinned the false success (KNOWN BUG #4763) now assert the rejection and the no-op message; the list_tools snapshot gains the minItems constraint. Ported from #4764 by Connor Moss (empty-list guard, schema minimum) and #4849 by Sumo-99 (before/after index comparison scoped to the pathspecs). Co-authored-by: Connor Moss <Conmoss30@gmail.com> Co-authored-by: Sumo-99 <sumanthssr@gmail.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent cb2cd0d commit b09395c

5 files changed

Lines changed: 65 additions & 14 deletions

File tree

‎src/git/README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ Please note that mcp-server-git is currently in early development. The functiona
5353
- Adds file contents to the staging area
5454
- Inputs:
5555
- `repo_path` (string): Path to Git repository
56-
- `files` (string[]): Array of file paths to stage
57-
- Returns: Confirmation of staged files
56+
- `files` (string[]): Non-empty array of file paths to stage; `["."]` stages everything
57+
- Returns: Confirmation of staged files, or a message saying nothing was staged when the call left the index unchanged
5858

5959
7. `git_reset`
6060
- Unstages all staged changes

‎src/git/src/mcp_server_git/server.py‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class GitCommit(BaseModel):
4444

4545
class GitAdd(BaseModel):
4646
repo_path: str
47-
files: list[str]
47+
files: list[str] = Field(..., min_length=1)
4848

4949

5050
class GitReset(BaseModel):
@@ -167,8 +167,20 @@ def git_commit(repo: git.Repo, message: str) -> str:
167167
return f"Changes committed successfully with hash {commit.hexsha}"
168168

169169

170+
def _index_entries(repo: git.Repo, files: list[str]) -> str:
171+
"""Mode, blob and stage of the index entries matching the pathspecs."""
172+
return repo.git.ls_files("--stage", "--", *files)
173+
174+
170175
def git_add(repo: git.Repo, files: list[str]) -> str:
176+
if not files:
177+
# `git add --` with no pathspec is a no-op that exits 0.
178+
raise ValueError(
179+
"No files provided to stage. Pass one or more paths, "
180+
"or ['.'] to stage everything."
181+
)
171182
if files == ["."]:
183+
before = _index_entries(repo, files)
172184
repo.git.add(".")
173185
else:
174186
# Defense in depth: validate each path resolves within the repository
@@ -184,8 +196,16 @@ def git_add(repo: git.Repo, files: list[str]) -> str:
184196
resolved.relative_to(repo_root)
185197
except ValueError:
186198
raise ValueError(f"Path '{f}' is outside the repository '{repo_root}'")
199+
before = _index_entries(repo, files)
187200
# Use '--' to prevent files starting with '-' from being interpreted as options
188201
repo.git.add("--", *files)
202+
# `git add` exits 0 when it stages nothing (e.g. '.' on a clean tree), so
203+
# read the outcome back from the index rather than the exit status.
204+
if _index_entries(repo, files) == before:
205+
return (
206+
"No changes were staged: the given paths had nothing new to stage. "
207+
"git_status shows what is modified or untracked."
208+
)
189209
return "Files staged successfully"
190210

191211

‎src/git/tests/snapshots/list_tools.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153
"items": {
154154
"type": "string"
155155
},
156+
"minItems": 1,
156157
"title": "Files",
157158
"type": "array"
158159
}

‎src/git/tests/test_protocol.py‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -370,26 +370,27 @@ async def test_git_add_file_starting_with_dash_is_a_path(repo: git.Repo):
370370
assert staged_paths(repo) == {"-n"}
371371

372372

373-
# KNOWN BUG #4763: pins current (wrong) behavior; the fix changes this assertion.
374-
async def test_git_add_empty_list_reports_success_but_stages_nothing(
375-
repo: git.Repo,
376-
):
377-
# Pins #4763: `files: []` runs `git add --`, a no-op, and still reports
378-
# success. Fixing #4763 changes this test.
373+
async def test_git_add_empty_list_is_rejected(repo: git.Repo):
374+
# #4763: `files: []` would run `git add --`, a no-op. The schema's
375+
# minItems makes the SDK reject it before the tool runs.
379376
root = root_of(repo)
380377
(root / "test.txt").write_text("edited\n")
381378
result = await call(None, "git_add", {"repo_path": str(root), "files": []})
382-
assert result == text_result("Files staged successfully")
379+
assert result == text_result(
380+
"Input validation error: [] should be non-empty", is_error=True
381+
)
383382
assert staged_paths(repo) == set()
384383

385384

386-
# KNOWN BUG #4763: pins current (wrong) behavior; the fix changes this assertion.
387-
async def test_git_add_dot_on_clean_tree_reports_success(repo: git.Repo):
388-
# Pins #4763: nothing to stage, success reported anyway.
385+
async def test_git_add_dot_on_clean_tree_reports_nothing_staged(repo: git.Repo):
386+
# #4763: nothing to stage, and the result says so.
389387
result = await call(
390388
None, "git_add", {"repo_path": str(root_of(repo)), "files": ["."]}
391389
)
392-
assert result == text_result("Files staged successfully")
390+
assert result == text_result(
391+
"No changes were staged: the given paths had nothing new to stage. "
392+
"git_status shows what is modified or untracked."
393+
)
393394
assert staged_paths(repo) == set()
394395

395396

‎src/git/tests/test_server.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,35 @@ def test_git_add_specific_files(test_repository):
110110
assert result == "Files staged successfully"
111111

112112

113+
def test_git_add_rejects_an_empty_file_list(test_repository):
114+
# #4763: `git add --` with no pathspec is a no-op that exits 0.
115+
with pytest.raises(ValueError, match="No files provided to stage"):
116+
git_add(test_repository, [])
117+
118+
119+
def test_git_add_clean_tree_reports_nothing_staged(test_repository):
120+
result = git_add(test_repository, ["."])
121+
122+
assert result.startswith("No changes were staged")
123+
assert not test_repository.index.diff(test_repository.head.commit)
124+
125+
126+
def test_git_add_already_staged_file_reports_nothing_staged(test_repository):
127+
# A change staged earlier must not make a no-op call read as a success.
128+
Path(test_repository.working_dir, "staged.txt").write_text("staged")
129+
test_repository.index.add(["staged.txt"])
130+
131+
assert git_add(test_repository, ["staged.txt"]).startswith("No changes were staged")
132+
assert git_add(test_repository, ["."]).startswith("No changes were staged")
133+
134+
135+
def test_git_add_reports_a_staged_deletion(test_repository):
136+
Path(test_repository.working_dir, "test.txt").unlink()
137+
138+
assert git_add(test_repository, ["test.txt"]) == "Files staged successfully"
139+
assert "test.txt" not in [path for path, _stage in test_repository.index.entries]
140+
141+
113142
def test_git_add_rejects_path_traversal(test_repository):
114143
# Security invariant (CVE-2026-27735): a relative path escaping the
115144
# repository must never be staged. Accept rejection from either the

0 commit comments

Comments
 (0)