Skip to content

Commit 6d4fa8e

Browse files
cliffhallclaude
andcommitted
fix(git): merge only the validated commit; name conflicts as written
git_merge now hands git the sha it resolved, so a name that stands for several heads (FETCH_HEAD) cannot become an octopus merge reported as a fast-forward, with a message naming the source as given. Conflicting file names go through _git_text, so non-ASCII names are not octal-escaped. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 886c2f8 commit 6d4fa8e

2 files changed

Lines changed: 60 additions & 2 deletions

File tree

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,9 @@ def _merge_failure(repo: git.Repo, error: git.GitCommandError, what: str) -> str
368368
if not (Path(repo.git_dir) / "MERGE_HEAD").exists():
369369
# Git refused before changing anything.
370370
return f"{what} failed. {unchanged} Git reported:\n{_git_stderr(error)}"
371-
conflicts = repo.git.diff("--name-only", "--diff-filter=U").splitlines()
371+
# Through _git_text, so a non-ASCII name prints as written, not as
372+
# core.quotepath's octal escapes.
373+
conflicts = _git_text(repo, "diff", "--name-only", "--diff-filter=U").splitlines()
372374
repo.git.merge("--abort")
373375
if conflicts:
374376
return (
@@ -432,6 +434,12 @@ def git_merge(
432434
# opening an editor for the message. --no-overwrite-ignore refuses to
433435
# replace an ignored local file (a `.env`, say) that the source tracks:
434436
# `is_dirty()` cannot see one, and git would overwrite it unrecoverably.
437+
#
438+
# Git is given the commit validated above, not the name: a name can stand
439+
# for more than one commit (`git merge FETCH_HEAD` merges every head
440+
# FETCH_HEAD lists, as an octopus), and only the one checked is merged.
441+
# With a sha, git's default message would name the sha, so the message
442+
# names what the caller asked for.
435443
mode = "--ff-only" if fast_forward_only else "--ff"
436444
try:
437445
repo.git.merge(
@@ -440,7 +448,9 @@ def git_merge(
440448
"--no-squash",
441449
"--no-edit",
442450
"--no-overwrite-ignore",
443-
source_branch,
451+
"-m",
452+
f"Merge '{source_branch}' into '{target}'",
453+
source.hexsha,
444454
)
445455
except git.GitCommandError as e:
446456
what = f"Merge of '{source_branch}' into '{target}'"

‎src/git/tests/test_merge.py‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,54 @@ async def test_conflict_is_aborted_and_reported(repo: git.Repo):
156156
assert (root_of(repo) / "test.txt").read_text() == "main\n"
157157

158158

159+
async def test_conflict_in_non_ascii_file_names_it_as_written(repo: git.Repo):
160+
name = "日本語.txt"
161+
commit_file(repo, name, "base\n")
162+
repo.git.checkout("-b", "feature")
163+
commit_file(repo, name, "feature\n")
164+
repo.git.checkout("main")
165+
commit_file(repo, name, "main\n")
166+
head = repo.head.commit.hexsha
167+
168+
assert await merge(repo, "feature", fast_forward_only=False) == tool_error(
169+
"git_merge",
170+
f"Merge of 'feature' into 'main' stopped on conflicts in: {name}. "
171+
"The merge was aborted and the repository is unchanged. "
172+
"Resolve the conflicts by hand.",
173+
)
174+
assert_untouched(repo, head)
175+
176+
177+
async def test_merge_commit_message_names_the_source(repo: git.Repo):
178+
diverge(repo, conflicting=False)
179+
180+
await merge(repo, "feature", fast_forward_only=False)
181+
182+
assert repo.head.commit.message.strip() == "Merge 'feature' into 'main'"
183+
184+
185+
async def test_fetch_head_merges_only_the_commit_it_resolves_to(repo: git.Repo):
186+
# FETCH_HEAD can list several heads, and `git merge FETCH_HEAD` would
187+
# merge them all as an octopus. The tool merges only the one it resolved.
188+
repo.git.checkout("-b", "first")
189+
first = commit_file(repo, "first.txt", "first\n")
190+
repo.git.checkout("-b", "second", "main")
191+
second = commit_file(repo, "second.txt", "second\n")
192+
repo.git.checkout("main")
193+
(Path(repo.git_dir) / "FETCH_HEAD").write_text(
194+
f"{first.hexsha}\t\tbranch 'first' of origin\n"
195+
f"{second.hexsha}\t\tbranch 'second' of origin\n"
196+
)
197+
198+
result = await merge(repo, "FETCH_HEAD", fast_forward_only=False)
199+
200+
assert result == text_result(
201+
f"Fast-forwarded 'main' to 'FETCH_HEAD' ({first.hexsha[:7]})"
202+
)
203+
assert repo.head.commit == first
204+
assert not (root_of(repo) / "second.txt").exists()
205+
206+
159207
@pytest.mark.skipif(
160208
sys.platform == "win32", reason="git for Windows runs hooks through sh"
161209
)

0 commit comments

Comments
 (0)