Skip to content

Commit 886c2f8

Browse files
cliffhallclaude
andcommitted
fix(git): pin git_merge's outcome against config and ignored files
Pass --commit and --no-squash so branch.<name>.mergeOptions cannot leave a merge pending while the tool reports a merge commit, and --no-overwrite-ignore so a merge cannot silently replace an ignored local file the source tracks. Regression tests for each. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 98845dc commit 886c2f8

3 files changed

Lines changed: 59 additions & 5 deletions

File tree

‎src/git/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ Please note that mcp-server-git is currently in early development. The functiona
9191
- `source_branch` (string): Branch, tag or commit to merge into the current branch (check out the branch to merge into first with `git_checkout`)
9292
- `fast_forward_only` (boolean, optional): Only fast-forward, failing if the branches have diverged (default `true`). `false` lets Git create a merge commit when they have
9393
- Returns: `Fast-forwarded`, `Merged ... with merge commit <short sha>`, or `Already up to date`
94-
- Refuses a detached HEAD, a working tree with uncommitted changes, and a merge already in progress. A merge that stops on conflicts is aborted (`git merge --abort`) and the error names the conflicting files, so the repository is left as it was
94+
- Refuses a detached HEAD, a working tree with uncommitted changes, a merge already in progress, and a merge that would overwrite an ignored local file. A merge that stops on conflicts is aborted (`git merge --abort`) and the error names the conflicting files, so the repository is left as it was
9595
12. `git_show`
9696
- Shows the contents of a commit, or of a file or directory at a revision
9797
- Inputs:

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -425,12 +425,23 @@ def git_merge(
425425
"have diverged. Nothing was changed. Pass fast_forward_only=false "
426426
"to create a merge commit."
427427
)
428-
# --ff overrides a merge.ff setting in the user's config, so the outcome
429-
# is the one this tool describes; --no-edit keeps git from opening an
430-
# editor for the merge commit message.
428+
# The flags pin the outcome this tool describes, whatever the user's
429+
# config says (merge.ff, branch.<name>.mergeOptions): --ff/--ff-only
430+
# choose fast-forwarding, --commit and --no-squash make a merge that
431+
# succeeds a completed one that moves HEAD, and --no-edit keeps git from
432+
# opening an editor for the message. --no-overwrite-ignore refuses to
433+
# replace an ignored local file (a `.env`, say) that the source tracks:
434+
# `is_dirty()` cannot see one, and git would overwrite it unrecoverably.
431435
mode = "--ff-only" if fast_forward_only else "--ff"
432436
try:
433-
repo.git.merge(mode, "--no-edit", source_branch)
437+
repo.git.merge(
438+
mode,
439+
"--commit",
440+
"--no-squash",
441+
"--no-edit",
442+
"--no-overwrite-ignore",
443+
source_branch,
444+
)
434445
except git.GitCommandError as e:
435446
what = f"Merge of '{source_branch}' into '{target}'"
436447
raise ValueError(_merge_failure(repo, e, what)) from e

‎src/git/tests/test_merge.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,49 @@ async def test_git_refusal_before_merging_is_reported(repo: git.Repo):
203203
assert (root_of(repo) / "new.txt").read_text() == "untracked\n"
204204

205205

206+
async def test_ignored_file_the_source_tracks_is_not_overwritten(repo: git.Repo):
207+
# is_dirty() does not see an ignored file, and git overwrites one by
208+
# default; the tool passes --no-overwrite-ignore so git refuses instead.
209+
commit_file(repo, ".gitignore", ".env\n")
210+
repo.git.checkout("-b", "feature")
211+
(root_of(repo) / ".env").write_text("tracked\n")
212+
repo.git.add("-f", ".env")
213+
repo.index.commit("track .env")
214+
repo.git.checkout("main")
215+
head = repo.head.commit.hexsha
216+
(root_of(repo) / ".env").write_text("SECRET=local\n")
217+
218+
result = await merge(repo, "feature")
219+
220+
assert result["isError"] is True
221+
assert "would be overwritten by merge" in result["content"][0]["text"]
222+
assert repo.head.commit.hexsha == head
223+
assert (root_of(repo) / ".env").read_text() == "SECRET=local\n"
224+
225+
226+
@pytest.mark.parametrize("options", ["--no-commit", "--squash"])
227+
async def test_configured_merge_options_still_complete_the_merge(
228+
repo: git.Repo, options: str
229+
):
230+
# branch.<name>.mergeOptions could make git stop short of a commit (or
231+
# squash); the tool overrides them, so a reported merge commit is real.
232+
diverge(repo, conflicting=False)
233+
with repo.config_writer() as config:
234+
config.set_value('branch "main"', "mergeOptions", options)
235+
main_tip = repo.head.commit
236+
feature_tip = repo.commit("feature")
237+
238+
result = await merge(repo, "feature", fast_forward_only=False)
239+
240+
merged = repo.head.commit
241+
assert result == text_result(
242+
f"Merged 'feature' into 'main' with merge commit {merged.hexsha[:7]}"
243+
)
244+
assert merged.parents == (main_tip, feature_tip)
245+
assert not (Path(repo.git_dir) / "MERGE_HEAD").exists()
246+
assert not repo.is_dirty(untracked_files=True)
247+
248+
206249
async def test_dirty_working_tree_is_refused(repo: git.Repo):
207250
repo.git.checkout("-b", "feature")
208251
commit_file(repo, "feature.txt", "feature\n")

0 commit comments

Comments
 (0)