Skip to content

Commit 35becbd

Browse files
cliffhallclaude
andcommitted
fix(git): reapply a merge autostash when git_commit concludes a merge
git_commit concluded a `git merge --autostash` merge without touching MERGE_AUTOSTASH, so the pre-merge edits stayed stashed. Reapply the stash the way `git commit` does: `git stash apply`, or `git stash store` when that conflicts, then remove MERGE_AUTOSTASH. The reply says which happened. Closes modelcontextprotocol#5050 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent b0d865d commit 35becbd

2 files changed

Lines changed: 80 additions & 1 deletion

File tree

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,35 @@ def git_commit(repo: git.Repo, message: str) -> str:
193193
commit = repo.index.commit(message, parent_commits=parents)
194194
for name in ("MERGE_HEAD", "MERGE_MSG", "MERGE_MODE", "AUTO_MERGE"):
195195
(git_dir / name).unlink(missing_ok=True)
196+
autostash_note = _apply_merge_autostash(repo)
196197
else:
197198
commit = repo.index.commit(message)
198-
return f"Changes committed successfully with hash {commit.hexsha}"
199+
autostash_note = ""
200+
return f"Changes committed successfully with hash {commit.hexsha}{autostash_note}"
201+
202+
203+
def _apply_merge_autostash(repo: git.Repo) -> str:
204+
"""Reapply a `git merge --autostash` stash, as `git commit` does.
205+
206+
Mirrors git's sequencer: apply the stash, store it in the stash list if
207+
that conflicts, then remove MERGE_AUTOSTASH. Returns a note for the reply,
208+
empty when the merge had no autostash.
209+
"""
210+
autostash = Path(repo.git_dir) / "MERGE_AUTOSTASH"
211+
if not autostash.exists():
212+
return ""
213+
stash_oid = autostash.read_text().strip()
214+
try:
215+
repo.git.stash("apply", stash_oid)
216+
note = "\nApplied autostash."
217+
except git.GitCommandError:
218+
repo.git.stash("store", "-m", "autostash", "-q", stash_oid)
219+
note = (
220+
"\nApplying autostash resulted in conflicts. Your changes are safe "
221+
'in the stash; run "git stash pop" or "git stash drop" at any time.'
222+
)
223+
autostash.unlink()
224+
return note
199225

200226

201227
def _index_entries(repo: git.Repo, files: list[str]) -> str:

‎src/git/tests/test_server.py‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,59 @@ def test_git_commit_concludes_a_merge_with_both_parents(repo):
389389
assert repo.active_branch.name == starting_branch
390390

391391

392+
def _start_autostash_merge(repo: git.Repo, dirty: str) -> str:
393+
"""Begin `git merge --no-commit --autostash side` over a dirty test.txt.
394+
395+
Returns the autostash's oid, read from MERGE_AUTOSTASH.
396+
"""
397+
starting_branch = repo.active_branch.name
398+
repo.git.checkout("-b", "side")
399+
Path(repo.working_dir, "side.txt").write_text("side only")
400+
repo.git.add("side.txt")
401+
repo.index.commit("side change")
402+
403+
repo.git.checkout(starting_branch)
404+
Path(repo.working_dir, "test.txt").write_text(dirty, newline="\n")
405+
repo.git.merge("side", "--no-commit", "--no-ff", "--autostash")
406+
autostash = Path(repo.git_dir) / "MERGE_AUTOSTASH"
407+
assert autostash.exists()
408+
# The merge stashed the edit away, so the tree is back at HEAD.
409+
assert Path(repo.working_dir, "test.txt").read_text() != dirty
410+
return autostash.read_text().strip()
411+
412+
413+
def test_git_commit_reapplies_a_merge_autostash(repo):
414+
# #5050: concluding a `--autostash` merge restores the pre-merge edits and
415+
# removes MERGE_AUTOSTASH, as `git commit` does.
416+
dirty = "line 1\nline 2\nline 3\nline 4\nline 5\ndirty\n"
417+
_start_autostash_merge(repo, dirty)
418+
419+
result = git_commit(repo, "merge side")
420+
421+
assert "Applied autostash." in result
422+
assert len(repo.head.commit.parents) == 2
423+
assert Path(repo.working_dir, "test.txt").read_text() == dirty
424+
assert not (Path(repo.git_dir) / "MERGE_AUTOSTASH").exists()
425+
assert repo.git.stash("list") == ""
426+
427+
428+
def test_git_commit_stores_a_conflicting_merge_autostash(repo):
429+
# #5050: when reapplying the autostash conflicts, `git commit` keeps it in
430+
# the stash list instead, so the edits are never lost.
431+
stash_oid = _start_autostash_merge(repo, "dirty\n")
432+
# Stage a different edit to the same line into the merge commit, so the
433+
# stash no longer applies cleanly on top of it.
434+
Path(repo.working_dir, "test.txt").write_text("merged\n", newline="\n")
435+
repo.git.add("test.txt")
436+
437+
result = git_commit(repo, "merge side")
438+
439+
assert "Applying autostash resulted in conflicts" in result
440+
assert len(repo.head.commit.parents) == 2
441+
assert not (Path(repo.git_dir) / "MERGE_AUTOSTASH").exists()
442+
assert repo.git.rev_parse("stash@{0}") == stash_oid
443+
444+
392445
def test_git_reset(test_repository):
393446
file_path = Path(test_repository.working_dir) / "reset_test.txt"
394447
file_path.write_text("content to reset")

0 commit comments

Comments
 (0)