Skip to content

Commit 1bbfb79

Browse files
committed
Merge remote-tracking branch 'origin/v2/fix/4762-git-commit-add-nothing-staged' into v2/chore/5004-wave6-git-rollup
2 parents e847cfb + d3b2af9 commit 1bbfb79

3 files changed

Lines changed: 44 additions & 3 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; an error if nothing is staged (as `git commit` without `--allow-empty`), except while a merge is in progress (`MERGE_HEAD` present), where git also allows an empty commit
50+
- Returns: Confirmation with new commit hash; an error if nothing is staged (as `git commit` without `--allow-empty`), except while a merge is in progress (`MERGE_HEAD` present), where git also allows an empty commit. While a merge is in progress the commit concludes it: its parents are `HEAD` and every `MERGE_HEAD` commit, and the merge state is cleared
5151

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

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,20 @@ def git_commit(repo: git.Repo, message: str) -> str:
181181
"No changes staged for commit. Use git_add to stage changes first; "
182182
"git_status shows what is currently staged."
183183
)
184-
commit = repo.index.commit(message)
184+
git_dir = Path(repo.git_dir)
185+
merge_head = git_dir / "MERGE_HEAD"
186+
if merge_head.exists():
187+
# Conclude the merge as `git commit` does: HEAD plus every MERGE_HEAD
188+
# commit as parents, then clear the merge state. index.commit() alone
189+
# records only HEAD and leaves the repository mid-merge.
190+
parents = [repo.head.commit] + [
191+
repo.commit(sha) for sha in merge_head.read_text().split()
192+
]
193+
commit = repo.index.commit(message, parent_commits=parents)
194+
for name in ("MERGE_HEAD", "MERGE_MSG", "MERGE_MODE", "AUTO_MERGE"):
195+
(git_dir / name).unlink(missing_ok=True)
196+
else:
197+
commit = repo.index.commit(message)
185198
return f"Changes committed successfully with hash {commit.hexsha}"
186199

187200

‎src/git/tests/test_server.py‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,10 @@ def test_git_commit_allows_an_empty_merge_commit(repo):
344344
repo.git.checkout("-b", "side")
345345
Path(repo.working_dir, "side.txt").write_text("side only")
346346
repo.git.add("side.txt")
347-
repo.index.commit("side change")
347+
side_sha = repo.index.commit("side change").hexsha
348348

349349
repo.git.checkout(starting_branch)
350+
head_before = repo.head.commit.hexsha
350351
repo.git.merge("side", "--no-commit", "--no-ff")
351352
assert (Path(repo.git_dir) / "MERGE_HEAD").exists()
352353

@@ -359,6 +360,33 @@ def test_git_commit_allows_an_empty_merge_commit(repo):
359360
result = git_commit(repo, "merge side")
360361

361362
assert "Changes committed successfully with hash" in result
363+
assert [p.hexsha for p in repo.head.commit.parents] == [head_before, side_sha]
364+
assert not (Path(repo.git_dir) / "MERGE_HEAD").exists()
365+
366+
367+
def test_git_commit_concludes_a_merge_with_both_parents(repo):
368+
# #5012: a merge commit records HEAD and MERGE_HEAD as parents and clears
369+
# the merge state, as `git commit` does.
370+
starting_branch = repo.active_branch.name
371+
repo.git.checkout("-b", "side")
372+
Path(repo.working_dir, "side.txt").write_text("side only")
373+
repo.git.add("side.txt")
374+
side_sha = repo.index.commit("side change").hexsha
375+
376+
repo.git.checkout(starting_branch)
377+
head_before = repo.head.commit.hexsha
378+
repo.git.merge("side", "--no-commit", "--no-ff")
379+
380+
result = git_commit(repo, "merge side")
381+
382+
commit = repo.head.commit
383+
assert commit.hexsha in result
384+
assert [p.hexsha for p in commit.parents] == [head_before, side_sha]
385+
assert "side.txt" in commit.tree
386+
git_dir = Path(repo.git_dir)
387+
for name in ("MERGE_HEAD", "MERGE_MSG", "MERGE_MODE"):
388+
assert not (git_dir / name).exists()
389+
assert repo.active_branch.name == starting_branch
362390

363391

364392
def test_git_reset(test_repository):

0 commit comments

Comments
 (0)