Skip to content

Commit cb2cd0d

Browse files
cliffhallConnorMoss02claude
committed
fix(git): refuse to commit when nothing is staged (#4762)
repo.index.commit() writes a tree unconditionally, so git_commit with nothing staged created an empty commit and reported its hash as a success. It now refuses with an error, as `git commit` does without --allow-empty, while still allowing the first commit on an unborn branch and an empty merge commit while MERGE_HEAD is present. The protocol test that pinned the empty commit (KNOWN BUG #4762) now asserts the refusal. Ported from #4761 by Connor Moss. Co-authored-by: Connor Moss <Conmoss30@gmail.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent 571e2b3 commit cb2cd0d

4 files changed

Lines changed: 111 additions & 10 deletions

File tree

‎src/git/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Please note that mcp-server-git is currently in early development. The functiona
4747
- Inputs:
4848
- `repo_path` (string): Path to Git repository
4949
- `message` (string): Commit message
50-
- Returns: Confirmation with new commit hash
50+
- Returns: Confirmation with new commit hash; an error if nothing is staged (as `git commit` without `--allow-empty`)
5151

5252
6. `git_add`
5353
- Adds file contents to the staging area

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,29 @@ def git_diff(
140140
return repo.git.diff(f"--unified={context_lines}", target)
141141

142142

143+
def _has_staged_changes(repo: git.Repo) -> bool:
144+
"""Whether the index holds anything git would record as a commit.
145+
146+
Mirrors `git commit`, which refuses to create an empty commit unless
147+
--allow-empty is given, but permits one while a merge is in progress.
148+
"""
149+
if (Path(repo.git_dir) / "MERGE_HEAD").exists():
150+
return True
151+
if not repo.head.is_valid():
152+
# Unborn branch: the first commit, so anything in the index counts.
153+
return bool(repo.index.entries)
154+
return bool(repo.index.diff(repo.head.commit))
155+
156+
143157
def git_commit(repo: git.Repo, message: str) -> str:
158+
# repo.index.commit() writes a tree unconditionally, so without this check
159+
# a caller that forgot to stage gets a hash back for an empty commit and no
160+
# way to tell it apart from a real one.
161+
if not _has_staged_changes(repo):
162+
raise ValueError(
163+
"No changes staged for commit. Use git_add to stage changes first; "
164+
"git_status shows what is currently staged."
165+
)
144166
commit = repo.index.commit(message)
145167
return f"Changes committed successfully with hash {commit.hexsha}"
146168

‎src/git/tests/test_protocol.py‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -299,23 +299,21 @@ async def test_git_commit_records_staged_changes(repo: git.Repo):
299299
assert "new.txt" in head.stats.files
300300

301301

302-
# KNOWN BUG #4762: pins current (wrong) behavior; the fix changes this assertion.
303-
async def test_git_commit_with_nothing_staged_creates_empty_commit(repo: git.Repo):
304-
# Pins #4762: with nothing staged, git_commit still writes a commit (whose
305-
# tree equals its parent's) and reports success. `git commit` would refuse
306-
# with "no changes added to commit". Fixing #4762 changes this test.
302+
async def test_git_commit_with_nothing_staged_is_refused(repo: git.Repo):
303+
# #4762: with nothing staged, git_commit refuses, as `git commit` does
304+
# without --allow-empty, instead of writing an empty commit.
307305
root = root_of(repo)
308306
(root / "test.txt").write_text("edited but not staged\n")
309307
before = repo.head.commit
310308
result = await call(
311309
None, "git_commit", {"repo_path": str(root), "message": "claims a fix"}
312310
)
313-
after = repo.head.commit
314311
assert result == text_result(
315-
f"Changes committed successfully with hash {after.hexsha}"
312+
"No changes staged for commit. Use git_add to stage changes first; "
313+
"git_status shows what is currently staged.",
314+
is_error=True,
316315
)
317-
assert after.parents == (before,)
318-
assert after.tree.hexsha == before.tree.hexsha
316+
assert repo.head.commit == before
319317
assert repo.is_dirty()
320318

321319

‎src/git/tests/test_server.py‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,87 @@ def test_git_commit(test_repository):
206206
assert latest_commit.message.strip() == "test commit message"
207207

208208

209+
def test_git_commit_refuses_when_nothing_is_staged(test_repository):
210+
# #4762: repo.index.commit() writes a tree unconditionally, so each of
211+
# these used to come back as a hash for an empty commit.
212+
head_before = test_repository.head.commit.hexsha
213+
working_file = Path(test_repository.working_dir) / "test.txt"
214+
215+
# A clean tree.
216+
with pytest.raises(ValueError, match="No changes staged for commit"):
217+
git_commit(test_repository, "nothing to record")
218+
219+
# An untracked file, which git_add was never called for.
220+
Path(test_repository.working_dir, "untracked.txt").write_text("never added")
221+
with pytest.raises(ValueError, match="No changes staged for commit"):
222+
git_commit(test_repository, "nothing to record")
223+
224+
# A tracked file edited but not staged.
225+
working_file.write_text("edited but never staged")
226+
with pytest.raises(ValueError, match="No changes staged for commit"):
227+
git_commit(test_repository, "nothing to record")
228+
229+
assert test_repository.head.commit.hexsha == head_before
230+
assert working_file.read_text() == "edited but never staged"
231+
232+
233+
def test_git_commit_records_a_staged_deletion(test_repository):
234+
# A deletion leaves no file behind, so it must not read as an empty index.
235+
test_repository.git.rm("test.txt")
236+
237+
result = git_commit(test_repository, "remove test.txt")
238+
239+
assert "Changes committed successfully with hash" in result
240+
assert "test.txt" not in test_repository.head.commit.tree
241+
242+
243+
def test_git_commit_allows_the_first_commit_on_an_unborn_branch(tmp_path: Path):
244+
repo = git.Repo.init(tmp_path / "unborn")
245+
Path(repo.working_dir, "first.txt").write_text("first")
246+
repo.index.add(["first.txt"])
247+
248+
result = git_commit(repo, "initial commit")
249+
250+
assert "Changes committed successfully with hash" in result
251+
assert repo.head.commit.message.strip() == "initial commit"
252+
repo.close()
253+
254+
255+
def test_git_commit_refuses_an_empty_unborn_branch(tmp_path: Path):
256+
repo = git.Repo.init(tmp_path / "unborn")
257+
258+
with pytest.raises(ValueError, match="No changes staged for commit"):
259+
git_commit(repo, "initial commit")
260+
261+
assert not repo.head.is_valid()
262+
repo.close()
263+
264+
265+
def test_git_commit_allows_an_empty_merge_commit(repo):
266+
# git permits an empty commit while a merge is in progress, so a merge
267+
# whose result matches HEAD must still be committable. --no-commit sets
268+
# MERGE_HEAD deterministically, without provoking a conflict.
269+
starting_branch = repo.active_branch.name
270+
repo.git.checkout("-b", "side")
271+
Path(repo.working_dir, "side.txt").write_text("side only")
272+
repo.git.add("side.txt")
273+
repo.index.commit("side change")
274+
275+
repo.git.checkout(starting_branch)
276+
repo.git.merge("side", "--no-commit", "--no-ff")
277+
assert (Path(repo.git_dir) / "MERGE_HEAD").exists()
278+
279+
# Roll the index back to HEAD's own content: the pending merge commit
280+
# records no change at all, which git allows.
281+
repo.git.rm("side.txt", "--cached")
282+
Path(repo.working_dir, "side.txt").unlink()
283+
assert not repo.index.diff(repo.head.commit)
284+
285+
result = git_commit(repo, "merge side")
286+
287+
assert "Changes committed successfully with hash" in result
288+
289+
209290
def test_git_reset(test_repository):
210291
file_path = Path(test_repository.working_dir) / "reset_test.txt"
211292
file_path.write_text("content to reset")

0 commit comments

Comments
 (0)