Skip to content

Commit d3b2af9

Browse files
cliffhallclaude
andcommitted
fix(git): conclude a merge in git_commit with both parents (#5012)
While MERGE_HEAD is present, git_commit now records HEAD plus every MERGE_HEAD commit as parents and clears MERGE_HEAD, MERGE_MSG, MERGE_MODE and AUTO_MERGE, as `git commit` does. Before, it wrote a single-parent commit and left the repository mid-merge. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent 5fef755 commit d3b2af9

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
@@ -163,7 +163,20 @@ def git_commit(repo: git.Repo, message: str) -> str:
163163
"No changes staged for commit. Use git_add to stage changes first; "
164164
"git_status shows what is currently staged."
165165
)
166-
commit = repo.index.commit(message)
166+
git_dir = Path(repo.git_dir)
167+
merge_head = git_dir / "MERGE_HEAD"
168+
if merge_head.exists():
169+
# Conclude the merge as `git commit` does: HEAD plus every MERGE_HEAD
170+
# commit as parents, then clear the merge state. index.commit() alone
171+
# records only HEAD and leaves the repository mid-merge.
172+
parents = [repo.head.commit] + [
173+
repo.commit(sha) for sha in merge_head.read_text().split()
174+
]
175+
commit = repo.index.commit(message, parent_commits=parents)
176+
for name in ("MERGE_HEAD", "MERGE_MSG", "MERGE_MODE", "AUTO_MERGE"):
177+
(git_dir / name).unlink(missing_ok=True)
178+
else:
179+
commit = repo.index.commit(message)
167180
return f"Changes committed successfully with hash {commit.hexsha}"
168181

169182

‎src/git/tests/test_server.py‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,10 @@ def test_git_commit_allows_an_empty_merge_commit(repo):
299299
repo.git.checkout("-b", "side")
300300
Path(repo.working_dir, "side.txt").write_text("side only")
301301
repo.git.add("side.txt")
302-
repo.index.commit("side change")
302+
side_sha = repo.index.commit("side change").hexsha
303303

304304
repo.git.checkout(starting_branch)
305+
head_before = repo.head.commit.hexsha
305306
repo.git.merge("side", "--no-commit", "--no-ff")
306307
assert (Path(repo.git_dir) / "MERGE_HEAD").exists()
307308

@@ -314,6 +315,33 @@ def test_git_commit_allows_an_empty_merge_commit(repo):
314315
result = git_commit(repo, "merge side")
315316

316317
assert "Changes committed successfully with hash" in result
318+
assert [p.hexsha for p in repo.head.commit.parents] == [head_before, side_sha]
319+
assert not (Path(repo.git_dir) / "MERGE_HEAD").exists()
320+
321+
322+
def test_git_commit_concludes_a_merge_with_both_parents(repo):
323+
# #5012: a merge commit records HEAD and MERGE_HEAD as parents and clears
324+
# the merge state, as `git commit` does.
325+
starting_branch = repo.active_branch.name
326+
repo.git.checkout("-b", "side")
327+
Path(repo.working_dir, "side.txt").write_text("side only")
328+
repo.git.add("side.txt")
329+
side_sha = repo.index.commit("side change").hexsha
330+
331+
repo.git.checkout(starting_branch)
332+
head_before = repo.head.commit.hexsha
333+
repo.git.merge("side", "--no-commit", "--no-ff")
334+
335+
result = git_commit(repo, "merge side")
336+
337+
commit = repo.head.commit
338+
assert commit.hexsha in result
339+
assert [p.hexsha for p in commit.parents] == [head_before, side_sha]
340+
assert "side.txt" in commit.tree
341+
git_dir = Path(repo.git_dir)
342+
for name in ("MERGE_HEAD", "MERGE_MSG", "MERGE_MODE"):
343+
assert not (git_dir / name).exists()
344+
assert repo.active_branch.name == starting_branch
317345

318346

319347
def test_git_reset(test_repository):

0 commit comments

Comments
 (0)